Package org.apache.commons.httpclient

Examples of org.apache.commons.httpclient.HttpMethod


    @Test
    public void testValidatingCorrectHttpBasicCredentials() throws Exception {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new NameValuePair("j_validate", "true"));
        HttpMethod post = H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_OK,
                params, null);
        assertTrue(post.getResponseBodyAsString().length() == 0);

        HttpMethod get = H.assertHttpStatus(HttpTest.HTTP_BASE_URL + "/?j_validate=true", HttpServletResponse.SC_OK);
        assertTrue(get.getResponseBodyAsString().length() == 0);
    }
View Full Code Here


    public void testValidatingIncorrectCredentials() throws Exception {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new NameValuePair("j_username", "garbage"));
        params.add(new NameValuePair("j_password", "garbage"));
        params.add(new NameValuePair("j_validate", "true"));
        HttpMethod post = H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check",
                HttpServletResponse.SC_FORBIDDEN, params, null);
        assertNotNull(post.getResponseHeader("X-Reason"));
    }
View Full Code Here

        params.add(new NameValuePair("j_validate", "true"));

        List<Header> headers = new ArrayList<Header>();
        headers.add(new Header("Cookie", "sling.formauth=garbage"));

        HttpMethod post = assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check",
                HttpServletResponse.SC_FORBIDDEN, params, headers, null);
        assertXReason(post);
    }
View Full Code Here

        H.getHttpClient().getState()
                .setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), defaultcreds);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new NameValuePair("j_validate", "true"));
        HttpMethod post = H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check",
                HttpServletResponse.SC_FORBIDDEN, params, null);
        assertXReason(post);

        HttpMethod get = H.assertHttpStatus(HttpTest.HTTP_BASE_URL + "/?j_validate=true",
                HttpServletResponse.SC_FORBIDDEN);
        assertXReason(get);
    }
View Full Code Here

        Credentials defaultcreds = new UsernamePasswordCredentials("garbage", "garbage");
        H.getHttpClient().getState()
                .setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), defaultcreds);

        final String requestUrl = HttpTest.HTTP_BASE_URL + "/junk?param1=1";
        HttpMethod get = new GetMethod(requestUrl);
        get.setRequestHeader("Referer", requestUrl);
        get.setRequestHeader("User-Agent", "Mozilla/5.0 Sling Integration Test");
        int status = H.getHttpClient().executeMethod(get);
        assertEquals(HttpServletResponse.SC_UNAUTHORIZED, status);
    }
View Full Code Here

        final String requestUrl = HttpTest.HTTP_BASE_URL + "/j_security_check";
        List<Header> headers = new ArrayList<Header>();
        headers.add(new Header("Referer", requestUrl));
        headers.add(new Header("User-Agent", "Mozilla/5.0 Sling Integration Test"));

        HttpMethod post = assertPostStatus(requestUrl, HttpServletResponse.SC_FORBIDDEN, params, headers, null);
        assertNotNull(post.getResponseHeader("X-Reason"));
        assertEquals("Username and Password do not match", post.getResponseHeader("X-Reason").getValue());
    }
View Full Code Here

        // end dance
    }

    @Override
    public Response execute(Request request) throws IOException {
        HttpMethod http = null;

        switch (request.method()) {
        case DELETE:
            http = new DeleteMethod();
            break;
        case HEAD:
            http = new HeadMethod();
            break;
        case GET:
            http = new GetMethod();
            break;
        case POST:
            http = new PostMethod();
            break;
        case PUT:
            http = new PutMethod();
            break;

        default:
            throw new EsHadoopTransportException("Unknown request method " + request.method());
        }

        CharSequence uri = request.uri();
        if (StringUtils.hasText(uri)) {
            http.setURI(new URI(escapeUri(uri.toString(), settings.getNetworkSSLEnabled()), false));
        }
        // NB: initialize the path _after_ the URI otherwise the path gets reset to /
        http.setPath(prefixPath(request.path().toString()));

        try {
            // validate new URI
            uri = http.getURI().toString();
        } catch (URIException uriex) {
            throw new EsHadoopTransportException("Invalid target URI " + request, uriex);
        }

        CharSequence params = request.params();
        if (StringUtils.hasText(params)) {
            http.setQueryString(params.toString());
        }

        ByteSequence ba = request.body();
        if (ba != null && ba.length() > 0) {
            EntityEnclosingMethod entityMethod = (EntityEnclosingMethod) http;
            entityMethod.setRequestEntity(new BytesArrayRequestEntity(ba));
            entityMethod.setContentChunked(false);
        }

        // when tracing, log everything
        if (log.isTraceEnabled()) {
            log.trace(String.format("Tx %s[%s]@[%s][%s] w/ payload [%s]", proxyInfo, request.method().name(), httpInfo, request.path(), request.body()));
        }

        long start = System.currentTimeMillis();
        try {
            client.executeMethod(http);
        } finally {
            stats.netTotalTime += (System.currentTimeMillis() - start);
        }

        if (log.isTraceEnabled()) {
            Socket sk = ReflectionUtils.invoke(GET_SOCKET, conn, (Object[]) null);
            String addr = sk.getLocalAddress().getHostAddress();
            log.trace(String.format("Rx %s@[%s] [%s-%s] [%s]", proxyInfo, addr, http.getStatusCode(), HttpStatus.getStatusText(http.getStatusCode()), http.getResponseBodyAsString()));
        }

        return new SimpleResponse(http.getStatusCode(), new ResponseInputStream(http), request.uri());
    }
View Full Code Here

    //login before the user is disabled, so login should work
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new NameValuePair("j_username", testUserId));
        params.add(new NameValuePair("j_password", "testPwd"));
        params.add(new NameValuePair("j_validate", "true"));
        HttpMethod post = H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_OK, params, null);
        assertNull(post.getResponseHeader("X-Reason"));
    H.getHttpClient().getState().clearCredentials();
    H.getHttpClient().getState().clearCookies();

        //update the user to disable it
        String postUrl = HttpTest.HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".update.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":disabled", "true"));
    postParams.add(new NameValuePair(":disabledReason", "Just Testing"));
    H.assertAuthenticatedAdminPostStatus(postUrl, HttpServletResponse.SC_OK, postParams, null);
   
    //the user is now disabled, so login should fail
        post = H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_FORBIDDEN, params, null);
        assertNotNull(post.getResponseHeader("X-Reason"));
    H.getHttpClient().getState().clearCredentials();
    H.getHttpClient().getState().clearCookies();

    //enable the user again
    postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair(":disabled", "false"));
    H.assertAuthenticatedAdminPostStatus(postUrl, HttpServletResponse.SC_OK, postParams, null);

    //login after the user is enabled, so login should work
        post = H.assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_OK, params, null);
        assertNull(post.getResponseHeader("X-Reason"));
    H.getHttpClient().getState().clearCredentials();
    H.getHttpClient().getState().clearCookies();
  }
View Full Code Here

    public HttpProducer(HttpEndpoint endpoint) {
        super(endpoint);
    }

    public void process(Exchange exchange) throws Exception {
        HttpMethod method = createMethod(exchange);
        int responseCode = httpClient.executeMethod(method);

        // lets store the result in the output message.
        InputStream in = method.getResponseBodyAsStream();
        Message out = exchange.getOut(true);
        out.setBody(in);

        // lets set the headers
        Header[] headers = method.getResponseHeaders();
        for (Header header : headers) {
            String name = header.getName();
            String value = header.getValue();
            out.setHeader(name, value);
        }
View Full Code Here

     * @throws java.io.IOException if the request body couldn't be read
     */
    public static HttpMethod clone(HttpMethod m)
      throws java.io.IOException
    {
        HttpMethod copy = null;

        // copy the HttpMethod
        try {
            copy = (HttpMethod) m.getClass().newInstance();
        } catch (InstantiationException iEx) {
        } catch (IllegalAccessException iaEx) {
        }
        if ( copy == null ) {
            return null;
        }
        copy.setDoAuthentication(m.getDoAuthentication());
        copy.setFollowRedirects(m.getFollowRedirects());
        copy.setPath( m.getPath() );
        copy.setQueryString(m.getQueryString());

        // clone the headers
        Header[] h = m.getRequestHeaders();
        int size = (h == null) ? 0 : h.length;

        for (int i = 0; i < size; i++) {
            copy.setRequestHeader(
              new Header(h[i].getName(), h[i].getValue()));
        }
        copy.setStrictMode(m.isStrictMode());
        if (m instanceof HttpMethodBase) {
            copyHttpMethodBase(
              (HttpMethodBase)m,
              (HttpMethodBase)copy);
        }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.HttpMethod

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.