Example usage for org.apache.commons.httpclient.methods OptionsMethod OptionsMethod

List of usage examples for org.apache.commons.httpclient.methods OptionsMethod OptionsMethod

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods OptionsMethod OptionsMethod.

Prototype

public OptionsMethod(String paramString) 

Source Link

Usage

From source file:OptionsMethodExample.java

public static void main(String args[]) {

    HttpClient client = new HttpClient();
    client.getParams().setParameter("http.useragent", "Test Client");

    OptionsMethod method = new OptionsMethod("http://www.google.com");

    try {//from   w  w  w .j av a  2s.co m
        int returnCode = client.executeMethod(method);

        Enumeration list = method.getAllowedMethods();

        while (list.hasMoreElements()) {
            System.err.println(list.nextElement());
        }

    } catch (Exception e) {
        System.err.println(e);
    } finally {
        method.releaseConnection();
    }

}

From source file:net.sf.j2ep.requesthandlers.MaxForwardRequestHandler.java

/**
 * Sets the headers and does some checking for if this request
 * is meant for the server or for the proxy. This check is done
 * by looking at the Max-Forwards header.
 * /*  w w w .j  a v  a 2  s.c  o m*/
 * @see net.sf.j2ep.model.RequestHandler#process(javax.servlet.http.HttpServletRequest, java.lang.String)
 */
public HttpMethod process(HttpServletRequest request, String url) throws IOException {
    HttpMethodBase method = null;

    if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
        method = new OptionsMethod(url);
    } else if (request.getMethod().equalsIgnoreCase("TRACE")) {
        method = new TraceMethod(url);
    } else {
        return null;
    }

    try {
        int max = request.getIntHeader("Max-Forwards");
        if (max == 0 || request.getRequestURI().equals("*")) {
            setAllHeaders(method, request);
            method.abort();
        } else if (max != -1) {
            setHeaders(method, request);
            method.setRequestHeader("Max-Forwards", "" + max--);
        } else {
            setHeaders(method, request);
        }
    } catch (NumberFormatException e) {
    }

    return method;
}

From source file:com.moss.bdbadmin.client.service.BdbClient.java

public Category map(IdProof assertion) throws ServiceException {
    try {/*from w  w w . j a v  a 2  s. c  o m*/
        OptionsMethod method = new OptionsMethod(baseUrl);
        method.setRequestHeader(AuthenticationHeader.HEADER_NAME, AuthenticationHeader.encode(assertion));

        int result = httpClient.executeMethod(method);
        if (result != 200) {
            throw new ServiceException(result);
        } else {
            Unmarshaller u = context.createUnmarshaller();
            return (Category) u.unmarshal(method.getResponseBodyAsStream());
        }
    } catch (Exception ex) {
        throw new ServiceFailure(ex);
    }
}

From source file:com.arjuna.qa.junit.HttpUtils.java

public static HttpMethodBase createMethod(URL url, int type) {
    HttpMethodBase request = null;/*  w  w w . jav  a  2  s .c  om*/
    switch (type) {
    case GET:
        request = new GetMethod(url.toString());
        break;
    case POST:
        request = new PostMethod(url.toString());
        break;
    case HEAD:
        request = new HeadMethod(url.toString());
        break;
    case OPTIONS:
        request = new OptionsMethod(url.toString());
        break;
    case PUT:
        request = new PutMethod(url.toString());
        break;
    case DELETE:
        request = new DeleteMethod(url.toString());
        break;
    case TRACE:
        request = new TraceMethod(url.toString());
        break;
    }
    return request;
}

From source file:jails.http.client.CommonsClientHttpRequestFactory.java

/**
 * Create a Commons HttpMethodBase object for the given HTTP method
 * and URI specification.//from w w  w .  ja v  a2s .  c om
 * @param httpMethod the HTTP method
 * @param uri the URI
 * @return the Commons HttpMethodBase object
 */
protected HttpMethodBase createCommonsHttpMethod(HttpMethod httpMethod, String uri) {
    switch (httpMethod) {
    case GET:
        return new GetMethod(uri);
    case DELETE:
        return new DeleteMethod(uri);
    case HEAD:
        return new HeadMethod(uri);
    case OPTIONS:
        return new OptionsMethod(uri);
    case POST:
        return new PostMethod(uri);
    case PUT:
        return new PutMethod(uri);
    case TRACE:
        return new TraceMethod(uri);
    default:
        throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod);
    }
}

From source file:com.noelios.restlet.ext.httpclient.HttpMethodCall.java

/**
 * Constructor.// w  w  w  .  j a  v  a  2  s  . c  o  m
 * 
 * @param helper
 *            The parent HTTP client helper.
 * @param method
 *            The method name.
 * @param requestUri
 *            The request URI.
 * @param hasEntity
 *            Indicates if the call will have an entity to send to the
 *            server.
 * @throws IOException
 */
public HttpMethodCall(HttpClientHelper helper, final String method, String requestUri, boolean hasEntity)
        throws IOException {
    super(helper, method, requestUri);
    this.clientHelper = helper;

    if (requestUri.startsWith("http")) {
        if (method.equalsIgnoreCase(Method.GET.getName())) {
            this.httpMethod = new GetMethod(requestUri);
        } else if (method.equalsIgnoreCase(Method.POST.getName())) {
            this.httpMethod = new PostMethod(requestUri);
        } else if (method.equalsIgnoreCase(Method.PUT.getName())) {
            this.httpMethod = new PutMethod(requestUri);
        } else if (method.equalsIgnoreCase(Method.HEAD.getName())) {
            this.httpMethod = new HeadMethod(requestUri);
        } else if (method.equalsIgnoreCase(Method.DELETE.getName())) {
            this.httpMethod = new DeleteMethod(requestUri);
        } else if (method.equalsIgnoreCase(Method.CONNECT.getName())) {
            final HostConfiguration host = new HostConfiguration();
            host.setHost(new URI(requestUri, false));
            this.httpMethod = new ConnectMethod(host);
        } else if (method.equalsIgnoreCase(Method.OPTIONS.getName())) {
            this.httpMethod = new OptionsMethod(requestUri);
        } else if (method.equalsIgnoreCase(Method.TRACE.getName())) {
            this.httpMethod = new TraceMethod(requestUri);
        } else {
            this.httpMethod = new EntityEnclosingMethod(requestUri) {
                @Override
                public String getName() {
                    return method;
                }
            };
        }

        this.httpMethod.setFollowRedirects(this.clientHelper.isFollowRedirects());
        this.httpMethod.setDoAuthentication(false);

        if (this.clientHelper.getRetryHandler() != null) {
            try {
                this.httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                        Engine.loadClass(this.clientHelper.getRetryHandler()).newInstance());
            } catch (Exception e) {
                this.clientHelper.getLogger().log(Level.WARNING,
                        "An error occurred during the instantiation of the retry handler.", e);
            }
        }

        this.responseHeadersAdded = false;
        setConfidential(this.httpMethod.getURI().getScheme().equalsIgnoreCase(Protocol.HTTPS.getSchemeName()));
    } else {
        throw new IllegalArgumentException("Only HTTP or HTTPS resource URIs are allowed here");
    }
}

From source file:com.moss.bdbadmin.client.service.BdbClient.java

public EntryInfo entryInfo(IdProof assertion, String path) throws ServiceException {
    try {/* w w  w  .  j a va 2s .com*/
        OptionsMethod method = new OptionsMethod(baseUrl + "/" + path);
        method.setRequestHeader(AuthenticationHeader.HEADER_NAME, AuthenticationHeader.encode(assertion));

        int result = httpClient.executeMethod(method);
        if (result != 200) {
            throw new ServiceException(result);
        } else {
            Unmarshaller u = context.createUnmarshaller();
            return (EntryInfo) u.unmarshal(method.getResponseBodyAsStream());
        }
    } catch (Exception ex) {
        throw new ServiceFailure(ex);
    }
}

From source file:flex.messaging.services.http.proxy.RequestFilter.java

/**
 * Setup the request./*from w  w  w  . ja va2 s. co m*/
 *
 * @param context the context
 */
protected void setupRequest(ProxyContext context) {
    // set the proxy to send requests through
    ExternalProxySettings externalProxy = context.getExternalProxySettings();
    if (externalProxy != null) {
        String proxyServer = externalProxy.getProxyServer();

        if (proxyServer != null) {
            context.getTarget().getHostConfig().setProxy(proxyServer, externalProxy.getProxyPort());
            if (context.getProxyCredentials() != null) {
                context.getHttpClient().getState().setProxyCredentials(ProxyUtil.getDefaultAuthScope(),
                        context.getProxyCredentials());
            }
        }
    }

    String method = context.getMethod();
    String encodedPath = context.getTarget().getEncodedPath();
    if (MessageIOConstants.METHOD_POST.equals(method)) {
        FlexPostMethod postMethod = new FlexPostMethod(encodedPath);
        context.setHttpMethod(postMethod);
        if (context.hasAuthorization()) {
            postMethod.setConnectionForced(true);
        }
    } else if (ProxyConstants.METHOD_GET.equals(method)) {
        FlexGetMethod getMethod = new FlexGetMethod(context.getTarget().getEncodedPath());
        context.setHttpMethod(getMethod);
        if (context.hasAuthorization()) {
            getMethod.setConnectionForced(true);
        }
    } else if (ProxyConstants.METHOD_HEAD.equals(method)) {
        HeadMethod headMethod = new HeadMethod(encodedPath);
        context.setHttpMethod(headMethod);
    } else if (ProxyConstants.METHOD_PUT.equals(method)) {
        PutMethod putMethod = new PutMethod(encodedPath);
        context.setHttpMethod(putMethod);
    } else if (ProxyConstants.METHOD_OPTIONS.equals(method)) {
        OptionsMethod optionsMethod = new OptionsMethod(encodedPath);
        context.setHttpMethod(optionsMethod);
    } else if (ProxyConstants.METHOD_DELETE.equals(method)) {
        DeleteMethod deleteMethod = new DeleteMethod(encodedPath);
        context.setHttpMethod(deleteMethod);
    } else if (ProxyConstants.METHOD_TRACE.equals(method)) {
        TraceMethod traceMethod = new TraceMethod(encodedPath);
        context.setHttpMethod(traceMethod);
    } else {
        ProxyException pe = new ProxyException(INVALID_METHOD);
        pe.setDetails(INVALID_METHOD, "1", new Object[] { method });
        throw pe;
    }

    HttpMethodBase httpMethod = context.getHttpMethod();
    if (httpMethod instanceof EntityEnclosingMethod) {
        ((EntityEnclosingMethod) httpMethod).setContentChunked(context.getContentChunked());
    }
}

From source file:com.smartitengineering.util.rest.client.jersey.cache.CustomApacheHttpClientResponseResolver.java

/**
 * Determines the HttpClient's request method from the HTTPMethod enum.
 *
 * @param method     the HTTPCache enum that determines
 * @param requestURI the request URI./*from  ww w . ja v a 2  s  .  c  o  m*/
 * @return a new HttpMethod subclass.
 */
protected HttpMethod getMethod(HTTPMethod method, URI requestURI) {
    if (CONNECT.equals(method)) {
        HostConfiguration config = new HostConfiguration();
        config.setHost(requestURI.getHost(), requestURI.getPort(), requestURI.getScheme());
        return new ConnectMethod(config);
    } else if (DELETE.equals(method)) {
        return new CustomHttpMethod(HTTPMethod.DELETE.name(), requestURI.toString());
    } else if (GET.equals(method)) {
        return new GetMethod(requestURI.toString());
    } else if (HEAD.equals(method)) {
        return new HeadMethod(requestURI.toString());
    } else if (OPTIONS.equals(method)) {
        return new OptionsMethod(requestURI.toString());
    } else if (POST.equals(method)) {
        return new PostMethod(requestURI.toString());
    } else if (PUT.equals(method)) {
        return new PutMethod(requestURI.toString());
    } else if (TRACE.equals(method)) {
        return new TraceMethod(requestURI.toString());
    } else {
        return new CustomHttpMethod(method.name(), requestURI.toString());
    }
}

From source file:com.sun.jersey.client.apache.ApacheHttpClientHandler.java

private HttpMethod getHttpMethod(ClientRequest cr) {
    final String strMethod = cr.getMethod();
    final String uri = cr.getURI().toString();

    if (strMethod.equals("GET")) {
        return new GetMethod(uri);
    } else if (strMethod.equals("POST")) {
        return new PostMethod(uri);
    } else if (strMethod.equals("PUT")) {
        return new PutMethod(uri);
    } else if (strMethod.equals("DELETE")) {
        return new CustomMethod("DELETE", uri);
    } else if (strMethod.equals("HEAD")) {
        return new HeadMethod(uri);
    } else if (strMethod.equals("OPTIONS")) {
        return new OptionsMethod(uri);
    } else {//from w w  w . ja  va2  s  .c  o  m
        return new CustomMethod(strMethod, uri);
    }
}