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

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

Introduction

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

Prototype

public Enumeration getAllowedMethods() 

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  ava 2  s. c  om*/
        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:flex.messaging.services.http.proxy.ResponseFilter.java

protected void setupResponse(ProxyContext context) {
    String method = context.getMethod();
    HttpMethodBase httpMethod = context.getHttpMethod();
    if (MessageIOConstants.METHOD_POST.equals(method)) {
        writeResponse(context);//from  w  ww.  j ava 2s. co m
    } else if (ProxyConstants.METHOD_GET.equals(method)) {
        writeResponse(context);
    } else if (ProxyConstants.METHOD_OPTIONS.equals(method)) {
        OptionsMethod optionsMethod = (OptionsMethod) httpMethod;
        Enumeration options = optionsMethod.getAllowedMethods();
        if (options != null) {
            List ops = new ArrayList();
            while (options.hasMoreElements()) {
                Object option = options.nextElement();
                ops.add(option);
            }
            Object[] o = ops.toArray();
            context.setResponse(o);
        }
    } else if (ProxyConstants.METHOD_TRACE.equals(method)) {
        writeResponse(context);
    } else if (ProxyConstants.METHOD_DELETE.equals(method)) {
        writeResponse(context);
    } else if (ProxyConstants.METHOD_HEAD.equals(method)) {
        context.setResponse(context.getResponseHeaders());
    } else if (ProxyConstants.METHOD_PUT.equals(method)) {
        writeResponse(context);
    }
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute OPTIONS method for the given path.
 *
 * @param path the server relative path of the resource to request
 * @return true if the method is succeeded.
 * @exception HttpException/*from w  w w .ja  v  a2 s  . c  o m*/
 * @exception IOException
 * @see #getAllowedMethods()
 */
public boolean optionsMethod(String path) throws HttpException, IOException {

    setClient();
    OptionsMethod method;
    if (path.trim().equals("*"))
        method = new OptionsMethod("*");
    else
        method = new OptionsMethod(URIUtil.encodePath(path));

    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);
    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    int statusCode = client.executeMethod(method);

    setStatusCode(statusCode);

    if (statusCode >= 200 && statusCode < 300) {
        // check if the specific method is possbile
        allowedMethods = method.getAllowedMethods();
        // check WebDAV capabilities.
        davCapabilities = method.getDavCapabilities();
        return true;
    }

    return false;
}

From source file:org.apache.webdav.lib.WebdavResource.java

/**
 * Execute OPTIONS method for the given http URL.
 *
 * @param httpURL the http URL./*w  w  w .ja  v a 2 s  .  c o  m*/
 * @return the allowed methods and capabilities.
 * @exception HttpException
 * @exception IOException
 */
public Enumeration optionsMethod(HttpURL httpURL) throws HttpException, IOException {

    HttpClient client = getSessionInstance(httpURL, true);

    OptionsMethod method = new OptionsMethod(httpURL.getEscapedPath());
    method.setDebug(debug);
    method.setFollowRedirects(this.followRedirects);

    generateTransactionHeader(method);
    generateAdditionalHeaders(method);
    client.executeMethod(method);

    Vector options = new Vector();
    int statusCode = method.getStatusLine().getStatusCode();
    if (statusCode >= 200 && statusCode < 300) {
        // check if the specific method is possbile
        Enumeration allowedMethods = method.getAllowedMethods();
        while (allowedMethods.hasMoreElements()) {
            options.addElement(allowedMethods.nextElement());
        }
        // check WebDAV capabilities.
        Enumeration davCapabilities = method.getDavCapabilities();
        while (davCapabilities.hasMoreElements()) {
            options.addElement(davCapabilities.nextElement());
        }
        Enumeration responses = method.getResponses();
        if (responses.hasMoreElements()) {
            ResponseEntity response = (ResponseEntity) responses.nextElement();
            Enumeration workspaces = response.getWorkspaces();
            String sResult = "";
            while (workspaces.hasMoreElements()) {
                sResult += workspaces.nextElement().toString();
            }
            Enumeration histories = response.getHistories();
            while (histories.hasMoreElements()) {
                sResult += histories.nextElement().toString();
            }
            // Set status code for this resource.
            if ((thisResource == true) && (response.getStatusCode() > 0))
                setStatusCode(response.getStatusCode());
            thisResource = false;
            options.addElement(sResult);
        }
    }

    return options.elements();
}

From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java

/**
 * Tests that an OPTIONS request can be sent to resource containing only a
 * GET method.//  w  w  w .j  a  va2  s.c  o  m
 */
public void testOPTIONSRequest() {
    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(BASE_URI, false));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(204, result);
            Enumeration<?> allowedMethods = httpMethod.getAllowedMethods();
            assertNotNull(allowedMethods);
            assertTrue(allowedMethods.hasMoreElements());
            List<String> methods = new ArrayList<String>();
            while (allowedMethods.hasMoreElements()) {
                methods.add((String) allowedMethods.nextElement());
            }
            assertTrue(methods.contains("HEAD"));
            assertTrue(methods.contains("GET"));
            assertTrue(methods.contains("OPTIONS"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}