All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.comcast.drivethru.utils.Method Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
/**
 * Copyright 2013 Comcast Cable Communications Management, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.comcast.drivethru.utils;

import java.lang.reflect.Constructor;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;

import com.comcast.drivethru.exception.HttpException;

/**
 * An enum representing the full set of supported HTTP methods.
 *
 * @author Clark Malmgren
 */
public enum Method {

    /** HTTP GET Method */
    GET(HttpGet.class),

    /** HTTP POST Method */
    POST(HttpPost.class),

    /** HTTP PUT Method */
    PUT(HttpPut.class),

    /** HTTP DELETE Method */
    DELETE(HttpDelete.class);

    private Class type;

    /**
     * Internal constructor that maps this Method to the Apache HTTP class that represents the given
     * request type.
     *
     * @param type
     *            the Apache HTTP request class for this method
     */
    private Method(Class type) {
        this.type = type;
    }

    /**
     * Get a new instance of the appropriate Apache HTTP request object for this HTTP method and
     * pass it the given URL.
     *
     * @param url
     *            the URL to pass into the constructor of the request object
     *
     * @return a new instance of the appropriate Apache HTTP request object for this HTTP method
     *
     * @throws HttpException
     *             if the request object could not be created
     */
    public HttpRequestBase getRequest(String url) throws HttpException {
        try {
            Constructor constructor = type.getConstructor(String.class);
            return constructor.newInstance(url);
        } catch (Exception ex) {
            throw new HttpException("Failed to create an RestRequest", ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy