001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.client;
017
018import java.net.URI;
019
020import org.apache.http.client.methods.HttpDelete;
021import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
022import org.apache.http.client.methods.HttpGet;
023import org.apache.http.client.methods.HttpHead;
024import org.apache.http.client.methods.HttpOptions;
025import org.apache.http.client.methods.HttpPatch;
026import org.apache.http.client.methods.HttpPost;
027import org.apache.http.client.methods.HttpPut;
028import org.apache.http.client.methods.HttpRequestBase;
029
030/**
031 * Represents an HTTP method to pass to the underlying client
032 *
033 * @author Aaron Coburn
034 * @since January 8, 2015
035 */
036public enum HttpMethods {
037
038    GET(HttpGet.class),
039    PATCH(HttpPatch.class),
040    POST(HttpPost.class),
041    PUT(HttpPut.class),
042    DELETE(HttpDelete.class),
043    HEAD(HttpHead.class),
044    OPTIONS(HttpOptions.class);
045
046    final Class<? extends HttpRequestBase> clazz;
047    final boolean entity;
048
049    HttpMethods(final Class<? extends HttpRequestBase> clazz) {
050        this.clazz = clazz;
051        entity = HttpEntityEnclosingRequestBase.class.isAssignableFrom(clazz);
052    }
053
054    /**
055     * Instantiate a new HttpRequst object from the method type
056     *
057     * @param url the URI that is part of the request
058     * @return an instance of the corresponding request class
059     */
060    public HttpRequestBase createRequest(final URI url) {
061        try {
062            return clazz.getDeclaredConstructor(URI.class).newInstance(url);
063        } catch (ReflectiveOperationException ex) {
064            throw new RuntimeException(ex);
065        }
066    }
067}