Example usage for org.apache.commons.httpclient URI URI

List of usage examples for org.apache.commons.httpclient URI URI

Introduction

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

Prototype

public URI(URI base, URI relative) throws URIException 

Source Link

Document

Construct a general URI with the given relative URI.

Usage

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

public void testAllowHeaders() throws Exception {
    try {//ww w  . ja  v a2 s . 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.MatrixParamTest.java

protected String sendGoodRequestAndGetResponse(String aPartialRequestURL, Class<? extends HttpMethod> aClass) {
    try {/*w  w w.  j  av a 2 s .  c om*/
        HttpMethod httpMethod = aClass.newInstance();
        httpMethod.setURI(new URI(BASE_URI + aPartialRequestURL, 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(result, 200);
            return responseBody;
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (InstantiationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    return null;
}

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

/**
 * Tests that it can find a custom GET HttpMethod annotation.
 *//*from   w  w w . j ava 2  s. c o m*/
public void testUserDefinedGETAnnotation() {
    try {
        GetMethod httpMethod = new GetMethod();
        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(200, result);
            assertEquals("You found my GET method!", responseBody);
        } 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./*from w ww .j  a va  2s .  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());
    }
}

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

/**
 * Tests that a HEAD request can be sent to resource containing only a GET
 * method./*from  w w w .java2 s  . c o  m*/
 */
public void testHEADRequest() {
    try {
        HeadMethod httpMethod = new HeadMethod();
        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(200, result);
            assertEquals(null, responseBody);
            Header[] headers = httpMethod.getResponseHeaders();
            assertNotNull(headers);
            assertTrue("Response for HEAD request contained no headers", headers.length > 0);
        } 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 HEAD request can be sent to resource annotated with a custom
 * HEAD annotation./*from w  w  w. ja  v  a2 s . c o m*/
 */
public void testCustomHEADRequest() {
    try {
        HeadMethod httpMethod = new HeadMethod();
        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(null, responseBody);
            Header header = httpMethod.getResponseHeader("HEAD");
            assertNotNull(header);
            assertEquals("TRUE", header.getValue());
        } 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  .ja v  a2 s . co  m
 */
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.apache.wink.itest.methodannotations.HttpMethodWarningsTest.java

/**
 * Tests that two or more <code>@HttpMethod</code> annotated annotations on
 * a method generates an error. Vague on specification but it seems to be an
 * error if two or more annotations (which each have a HttpMethod annotation
 * on them) are on the same resource method. Based on error, it is probably
 * expected that the resource is unavailable. TODO: So this test could be
 * that two custom annotations which are annotated each with
 * <code>@HttpMethod</code> are annotated on the same method.
 *///  ww  w. j av  a2 s .  c  om
public void testTwoOrMoreAnnotationsOnMethodError() {
    try {
        PostMethod httpMethod = new PostMethod();
        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(404, result);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        PutMethod putMethod = new PutMethod();
        System.out.println(BASE_URI);
        putMethod.setURI(new URI(BASE_URI, true));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(putMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = putMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            /*
             * if a filter is used, then the 404 will fall through to the
             * container
             */
            assertTrue("The result is " + result,
                    (result == 403 && "tomcat".equals(ServerEnvironmentInfo.getContainerName()))
                            || result == 405 || result == 404);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            putMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

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

/**
 * Tests that non-public HttpMethod annotations generate a warning.
 *//*from  w  w  w  .  j av a 2s .co m*/
public void testNonPublicMethodsWarning() {
    try {
        GetMethod httpMethod = new GetMethod();
        httpMethod.setURI(new URI(BASE_URI + "/abcd", 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(404, result);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        DeleteMethod httpMethod = new DeleteMethod();
        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);
            /*
             * if a filter is used, then the 404 will fall through to the
             * container
             */
            assertTrue("The result is " + result,
                    (result == 403 && "tomcat".equals(ServerEnvironmentInfo.getContainerName()))
                            || result == 405 || result == 404);
        } 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.pathmethods.PathMethodTest.java

public void testNonPublicMethodPathWarning() {
    try {//from w  ww  . j a v a 2s.  co m
        GetMethod httpMethod = new GetMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/private", 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(404, result);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        GetMethod httpMethod = new GetMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/protected", 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(404, result);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    try {
        GetMethod httpMethod = new GetMethod();
        httpMethod.setURI(new URI(getBaseURI() + "/package", 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(404, result);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}