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 */
016
017package org.fcrepo.client;
018
019import java.net.URI;
020
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    MOVE(HttpMove.class),
047    COPY(HttpCopy.class);
048
049    final Class<? extends HttpRequestBase> clazz;
050
051    final boolean entity;
052
053    HttpMethods(final Class<? extends HttpRequestBase> clazz) {
054        this.clazz = clazz;
055        entity = HttpEntityEnclosingRequestBase.class.isAssignableFrom(clazz);
056    }
057
058    /**
059     * Instantiate a new HttpRequst object from the method type
060     *
061     * @param url the URI that is part of the request
062     * @return an instance of the corresponding request class
063     */
064    public HttpRequestBase createRequest(final URI url) {
065        try {
066            return clazz.getDeclaredConstructor(URI.class).newInstance(url);
067        } catch (ReflectiveOperationException ex) {
068            throw new RuntimeException(ex);
069        }
070    }
071
072    /**
073     * HTTP MOVE method.
074     * 
075     * @author bbpennel
076     */
077    public static class HttpMove extends HttpRequestBase {
078
079        public final static String METHOD_NAME = "MOVE";
080
081        /**
082         * Instantiate MOVE request base
083         * 
084         * @param uri uri for the request
085         */
086        public HttpMove(final URI uri) {
087            super();
088            setURI(uri);
089        }
090
091        @Override
092        public String getMethod() {
093            return METHOD_NAME;
094        }
095    }
096
097    /**
098     * HTTP COPY method.
099     * 
100     * @author bbpennel
101     */
102    public static class HttpCopy extends HttpRequestBase {
103
104        public final static String METHOD_NAME = "COPY";
105
106        /**
107         * Instantiate COPY request base
108         * 
109         * @param uri uri for the request
110         */
111        public HttpCopy(final URI uri) {
112            super();
113            setURI(uri);
114        }
115
116        @Override
117        public String getMethod() {
118            return METHOD_NAME;
119        }
120    }
121}