001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.client;
019
020import java.net.URI;
021
022import org.apache.http.client.methods.HttpDelete;
023import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
024import org.apache.http.client.methods.HttpGet;
025import org.apache.http.client.methods.HttpHead;
026import org.apache.http.client.methods.HttpOptions;
027import org.apache.http.client.methods.HttpPatch;
028import org.apache.http.client.methods.HttpPost;
029import org.apache.http.client.methods.HttpPut;
030import org.apache.http.client.methods.HttpRequestBase;
031
032/**
033 * Represents an HTTP method to pass to the underlying client
034 *
035 * @author Aaron Coburn
036 * @since January 8, 2015
037 */
038public enum HttpMethods {
039
040    GET(HttpGet.class),
041    PATCH(HttpPatch.class),
042    POST(HttpPost.class),
043    PUT(HttpPut.class),
044    DELETE(HttpDelete.class),
045    HEAD(HttpHead.class),
046    OPTIONS(HttpOptions.class),
047    MOVE(HttpMove.class),
048    COPY(HttpCopy.class);
049
050    final Class<? extends HttpRequestBase> clazz;
051
052    final boolean entity;
053
054    HttpMethods(final Class<? extends HttpRequestBase> clazz) {
055        this.clazz = clazz;
056        entity = HttpEntityEnclosingRequestBase.class.isAssignableFrom(clazz);
057    }
058
059    /**
060     * Instantiate a new HttpRequst object from the method type
061     *
062     * @param url the URI that is part of the request
063     * @return an instance of the corresponding request class
064     */
065    public HttpRequestBase createRequest(final URI url) {
066        try {
067            return clazz.getDeclaredConstructor(URI.class).newInstance(url);
068        } catch (ReflectiveOperationException ex) {
069            throw new RuntimeException(ex);
070        }
071    }
072
073    /**
074     * HTTP MOVE method.
075     * 
076     * @author bbpennel
077     */
078    public static class HttpMove extends HttpRequestBase {
079
080        public final static String METHOD_NAME = "MOVE";
081
082        /**
083         * Instantiate MOVE request base
084         * 
085         * @param uri uri for the request
086         */
087        public HttpMove(final URI uri) {
088            super();
089            setURI(uri);
090        }
091
092        @Override
093        public String getMethod() {
094            return METHOD_NAME;
095        }
096    }
097
098    /**
099     * HTTP COPY method.
100     * 
101     * @author bbpennel
102     */
103    public static class HttpCopy extends HttpRequestBase {
104
105        public final static String METHOD_NAME = "COPY";
106
107        /**
108         * Instantiate COPY request base
109         * 
110         * @param uri uri for the request
111         */
112        public HttpCopy(final URI uri) {
113            super();
114            setURI(uri);
115        }
116
117        @Override
118        public String getMethod() {
119            return METHOD_NAME;
120        }
121    }
122}