001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.client;
007
008import java.net.URI;
009
010import org.apache.http.client.methods.HttpDelete;
011import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
012import org.apache.http.client.methods.HttpGet;
013import org.apache.http.client.methods.HttpHead;
014import org.apache.http.client.methods.HttpOptions;
015import org.apache.http.client.methods.HttpPatch;
016import org.apache.http.client.methods.HttpPost;
017import org.apache.http.client.methods.HttpPut;
018import org.apache.http.client.methods.HttpRequestBase;
019
020/**
021 * Represents an HTTP method to pass to the underlying client
022 *
023 * @author Aaron Coburn
024 * @since January 8, 2015
025 */
026public enum HttpMethods {
027
028    GET(HttpGet.class),
029    PATCH(HttpPatch.class),
030    POST(HttpPost.class),
031    PUT(HttpPut.class),
032    DELETE(HttpDelete.class),
033    HEAD(HttpHead.class),
034    OPTIONS(HttpOptions.class),
035    MOVE(HttpMove.class),
036    COPY(HttpCopy.class);
037
038    final Class<? extends HttpRequestBase> clazz;
039
040    final boolean entity;
041
042    HttpMethods(final Class<? extends HttpRequestBase> clazz) {
043        this.clazz = clazz;
044        entity = HttpEntityEnclosingRequestBase.class.isAssignableFrom(clazz);
045    }
046
047    /**
048     * Instantiate a new HttpRequst object from the method type
049     *
050     * @param url the URI that is part of the request
051     * @return an instance of the corresponding request class
052     */
053    public HttpRequestBase createRequest(final URI url) {
054        try {
055            return clazz.getDeclaredConstructor(URI.class).newInstance(url);
056        } catch (ReflectiveOperationException ex) {
057            throw new RuntimeException(ex);
058        }
059    }
060
061    /**
062     * HTTP MOVE method.
063     * 
064     * @author bbpennel
065     */
066    public static class HttpMove extends HttpRequestBase {
067
068        public final static String METHOD_NAME = "MOVE";
069
070        /**
071         * Instantiate MOVE request base
072         * 
073         * @param uri uri for the request
074         */
075        public HttpMove(final URI uri) {
076            super();
077            setURI(uri);
078        }
079
080        @Override
081        public String getMethod() {
082            return METHOD_NAME;
083        }
084    }
085
086    /**
087     * HTTP COPY method.
088     * 
089     * @author bbpennel
090     */
091    public static class HttpCopy extends HttpRequestBase {
092
093        public final static String METHOD_NAME = "COPY";
094
095        /**
096         * Instantiate COPY request base
097         * 
098         * @param uri uri for the request
099         */
100        public HttpCopy(final URI uri) {
101            super();
102            setURI(uri);
103        }
104
105        @Override
106        public String getMethod() {
107            return METHOD_NAME;
108        }
109    }
110}