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() 

Source Link

Usage

From source file:net.sf.j2ep.test.AllowHeaderTest.java

public void testSetAllowed() {
    String allow = "OPTIONS,PROPFIND,OP,PUT";
    AllowedMethodHandler.setAllowedMethods(allow);
    assertEquals("Checking that allow is set", allow, AllowedMethodHandler.getAllowHeader());

    assertTrue("Checking that OPTIONS is allowed", AllowedMethodHandler.methodAllowed("OPTIONS"));
    assertTrue("Checking that PROPFIND is allowed", AllowedMethodHandler.methodAllowed("PROPFIND"));
    assertTrue("Checking that OP is allowed", AllowedMethodHandler.methodAllowed("OP"));
    assertTrue("Checking that PUT is allowed", AllowedMethodHandler.methodAllowed("PUT"));
    assertFalse("Checking that PROP isn't allowed", AllowedMethodHandler.methodAllowed("PROP"));

    assertTrue("Checking OPTIONS method", AllowedMethodHandler.methodAllowed(new OptionsMethod()));
    assertFalse("Checking GET method", AllowedMethodHandler.methodAllowed(new GetMethod()));
}

From source file:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java

/**
 * @see it.greenvulcano.gvesb.virtual.CallOperation#perform(it.greenvulcano.gvesb.buffer.GVBuffer)
 *//*from  w w  w .ja va2  s .  c om*/
@Override
public GVBuffer perform(GVBuffer gvBuffer) throws ConnectionException, CallException, InvalidDataException {
    logger.debug("BEGIN perform(GVBuffer gvBuffer)");
    HttpMethod method = null;
    try {
        String currMethodURI = null;
        Map<String, Object> params = GVBufferPropertiesHelper.getPropertiesMapSO(gvBuffer, true);

        String currHost = PropertiesHandler.expand(host, params, gvBuffer);
        String currPort = PropertiesHandler.expand(port, params, gvBuffer);
        logger.debug("Server Host: " + currHost + " - Port: " + currPort);
        httpClient.getHostConfiguration().setHost(currHost, Integer.parseInt(currPort), protocol);

        auth.setAuthentication(httpClient, host, Integer.parseInt(currPort), gvBuffer, params);
        proxy.setProxy(httpClient, gvBuffer, params);

        currMethodURI = PropertiesHandler.expand(contextPath + methodURI, params, gvBuffer);
        logger.debug("MethodURI[escaped:" + uriEscaped + "]=[" + currMethodURI + "]");
        switch (methodName) {
        case OPTIONS:
            method = new OptionsMethod();
            break;
        case GET:
            method = new GetMethod();
            break;
        case HEAD:
            method = new HeadMethod();
            break;
        case POST:
            method = new PostMethod();
            break;
        case PUT:
            method = new PutMethod();
            break;
        case DELETE:
            method = new DeleteMethod();
            break;
        default:
            throw new CallException("GV_CALL_SERVICE_ERROR",
                    new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() },
                            { "id", gvBuffer.getId().toString() },
                            { "message", "Unknown method = " + methodName } });
        }
        method.setURI(new URI(currMethodURI, uriEscaped));

        if ((refDP != null) && (refDP.length() > 0)) {
            logger.debug("Calling configured Data Provider: " + refDP);
            DataProviderManager dataProviderManager = DataProviderManager.instance();
            IDataProvider dataProvider = dataProviderManager.getDataProvider(refDP);
            try {
                dataProvider.setContext(method);
                dataProvider.setObject(gvBuffer);
                method = (HttpMethod) dataProvider.getResult();
            } finally {
                dataProviderManager.releaseDataProvider(refDP, dataProvider);
            }
        }

        int status = httpClient.executeMethod(method);
        gvBuffer.setProperty(RESPONSE_STATUS, String.valueOf(status));
        String statusTxt = method.getStatusText();
        gvBuffer.setProperty(RESPONSE_MESSAGE, (statusTxt != null ? statusTxt : "NULL"));
        Header[] responseHeaders = method.getResponseHeaders();
        for (Header header : responseHeaders) {
            String headerName = RESPONSE_HEADER_PREFIX + header.getName();
            String value = header.getValue();
            if (value == null) {
                value = "";
            }
            gvBuffer.setProperty(headerName, value);
        }
        String cType = "text/html";
        Header cTypeHeader = method.getResponseHeader("Content-Type");
        if (cTypeHeader != null) {
            String cTypeValue = cTypeHeader.getValue();
            if (cTypeValue != null) {
                cType = cTypeValue;
            }
        }
        logger.debug("Response content-type: " + cType);
        ContentType contentType = new ContentType(cType);
        byte[] responseBody = method.getResponseBody();
        Object object = responseBody;
        if (contentType.getPrimaryType().equals("multipart")) {
            object = handleMultipart(responseBody, cType);
        }
        gvBuffer.setObject(object);
    } catch (CallException exc) {
        throw exc;
    } catch (Exception exc) {
        logger.error("ERROR perform(GVBuffer gvBuffer)", exc);
        throw new CallException("GV_CALL_SERVICE_ERROR",
                new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() },
                        { "id", gvBuffer.getId().toString() }, { "message", exc.getMessage() } },
                exc);
    } finally {
        try {
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception exc) {
            logger.warn("Error while releasing connection", exc);
        }
        logger.debug("END perform(GVBuffer gvBuffer)");
    }
    return gvBuffer;
}

From source file:org.apache.wink.itest.headers.HeadersTest.java

public void testAllowHeaders() throws Exception {
    try {//w ww .  j  a v a2s .  c o m
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/headersallow1/allow1", false));
        httpClient = new HttpClient();
        try {
            int result = httpClient.executeMethod(httpMethod);
            assertEquals(204, result);
            assertNotNull(httpMethod.getResponseHeader("Allow"));
            List<String> allowedMethods = Arrays
                    .asList(httpMethod.getResponseHeader("Allow").getValue().split(", "));
            System.out.println(allowedMethods);
            assertEquals(3, allowedMethods.size());
            assertTrue(allowedMethods.contains("HEAD"));
            assertTrue(allowedMethods.contains("OPTIONS"));
            assertTrue(allowedMethods.contains("GET"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/headersallow2", false));
        httpClient = new HttpClient();
        try {
            int result = httpClient.executeMethod(httpMethod);
            assertEquals(204, result);
            assertNotNull(httpMethod.getResponseHeader("Allow"));
            List<String> allowedMethods = Arrays
                    .asList(httpMethod.getResponseHeader("Allow").getValue().split(", "));
            System.out.println(allowedMethods);
            assertEquals(6, allowedMethods.size());
            assertTrue(allowedMethods.contains("HEAD"));
            assertTrue(allowedMethods.contains("OPTIONS"));
            assertTrue(allowedMethods.contains("GET"));
            assertTrue(allowedMethods.contains("PUT"));
            assertTrue(allowedMethods.contains("POST"));
            assertTrue(allowedMethods.contains("DELETE"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/headersallow3/sublocator", false));
        httpClient = new HttpClient();
        try {
            int result = httpClient.executeMethod(httpMethod);
            assertEquals(204, result);
            assertNotNull(httpMethod.getResponseHeader("Allow"));
            List<String> allowedMethods = Arrays
                    .asList(httpMethod.getResponseHeader("Allow").getValue().split(", "));
            System.out.println(allowedMethods);
            assertEquals(3, allowedMethods.size());
            assertTrue(allowedMethods.contains("HEAD"));
            assertTrue(allowedMethods.contains("OPTIONS"));
            assertTrue(allowedMethods.contains("GET"));
            assertEquals(null, httpMethod.getResponseBodyAsString());
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

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./* www  .  ja  v a2  s . co 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());
    }
}

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

/**
 * Tests that a OPTIONS request can be sent to resource annotated with a
 * custom OPTIONS annotation.//from  w  w  w.  j av  a  2 s.  c om
 */
public void testCustomOPTIONSRequest() {
    try {
        OptionsMethod httpMethod = new OptionsMethod();
        httpMethod.setURI(new URI(ALT_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(200, result);
            assertEquals("", responseBody);
            Header header = httpMethod.getResponseHeader("Allow");
            assertNotNull(header);
            String value = header.getValue();
            assertTrue(value.contains("HEAD"));
            assertTrue(value.contains("OPTIONS"));
            assertTrue(value.contains("GET"));
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.easyj.http.RESTHttpClient.java

/**
 * Executes a OPTIONS HTTP request and returns the body response as {@code String}
 *
 * @param uri URI of the resource to be requested
 * @return The body response of the request as {@code String}
 *//*from w w w  .  ja  v a2 s  .co  m*/
public RESTHttpClient options(String uri) {
    method = new OptionsMethod();
    return execute(uri);
}