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.camel;
017
018import java.net.URI;
019
020import org.apache.camel.RuntimeCamelException;
021import org.apache.http.client.methods.HttpDelete;
022import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
023import org.apache.http.client.methods.HttpGet;
024import org.apache.http.client.methods.HttpHead;
025import org.apache.http.client.methods.HttpOptions;
026import org.apache.http.client.methods.HttpPatch;
027import org.apache.http.client.methods.HttpPost;
028import org.apache.http.client.methods.HttpPut;
029import org.apache.http.client.methods.HttpRequestBase;
030
031/**
032 * Represents an HTTP method to pass to the underlying client
033 *
034 * @author Aaron Coburn
035 * @since January 8, 2015
036 */
037public enum HttpMethods {
038
039    GET(HttpGet.class),
040    PATCH(HttpPatch.class),
041    POST(HttpPost.class),
042    PUT(HttpPut.class),
043    DELETE(HttpDelete.class),
044    HEAD(HttpHead.class),
045    OPTIONS(HttpOptions.class);
046
047    final Class<? extends HttpRequestBase> clazz;
048    final boolean entity;
049
050    HttpMethods(final Class<? extends HttpRequestBase> clazz) {
051        this.clazz = clazz;
052        entity = HttpEntityEnclosingRequestBase.class.isAssignableFrom(clazz);
053    }
054
055    /**
056     * Instantiate a new HttpRequst object from the method type
057     *
058     * @param url the URI that is part of the request
059     * @return an instance of the corresponding request class
060     */
061    public HttpRequestBase createRequest(final URI url) {
062        try {
063            return clazz.getDeclaredConstructor(URI.class).newInstance(url);
064        } catch (ReflectiveOperationException ex) {
065            throw new RuntimeCamelException(ex);
066        }
067    }
068}