Example usage for org.apache.commons.httpclient.methods PostMethod setFollowRedirects

List of usage examples for org.apache.commons.httpclient.methods PostMethod setFollowRedirects

Introduction

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

Prototype

public void setFollowRedirects(boolean paramBoolean) 

Source Link

Usage

From source file:org.apache.sling.commons.testing.integration.SlingIntegrationTestClient.java

/** Upload to an file node structure, see SLING-168 */
public void uploadToFileNode(String url, File localFile, String fieldName, String typeHint) throws IOException {

    final Part[] parts = new Part[typeHint == null ? 1 : 2];
    parts[0] = new FilePart(fieldName, localFile);
    if (typeHint != null) {
        parts[1] = new StringPart(fieldName + "@TypeHint", typeHint);
    }//w  ww.j  a v  a2s  .  c  o m
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

    final int status = httpClient.executeMethod(post);
    final int expected = 200;
    if (status != expected) {
        throw new HttpStatusCodeException(expected, status, "POST",
                HttpTestBase.getResponseBodyAsStream(post, 0));
    }
}

From source file:org.apache.sling.commons.testing.integration.SlingIntegrationTestClient.java

/** Upload multiple files to file node structures */
public void uploadToFileNodes(String url, File[] localFiles, String[] fieldNames, String[] typeHints)
        throws IOException {

    List<Part> partsList = new ArrayList<Part>();
    for (int i = 0; i < localFiles.length; i++) {
        Part filePart = new FilePart(fieldNames[i], localFiles[i]);
        partsList.add(filePart);//w  w  w .j a  va2  s  .c om
        if (typeHints != null) {
            Part typeHintPart = new StringPart(fieldNames[i] + "@TypeHint", typeHints[i]);
            partsList.add(typeHintPart);
        }
    }

    final Part[] parts = partsList.toArray(new Part[partsList.size()]);
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

    final int expected = 200;
    final int status = httpClient.executeMethod(post);
    if (status != expected) {
        throw new HttpStatusCodeException(expected, status, "POST",
                HttpTestBase.getResponseBodyAsStream(post, 0));
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.accessManager.AbstractAccessManagerTest.java

/** Execute a POST request and check status */
protected void assertAuthenticatedPostStatus(Credentials creds, String url, int expectedStatusCode,
        List<NameValuePair> postParams, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);

    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);//from  w w w.  j a va  2  s. c om
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);

        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }

        final int status = httpClient.executeMethod(post);
        if (assertMessage == null) {
            assertEquals(expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.auth.AuthenticationResponseCodeTest.java

protected HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams,
        List<Header> headers, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);

    if (headers != null) {
        for (Header header : headers) {
            post.addRequestHeader(header);
        }// www  . java  2 s . c o  m
    }

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    final int status = H.getHttpClient().executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.AuthenticatedTestUtil.java

/** Execute a POST request and check status */
public void assertAuthenticatedPostStatus(Credentials creds, String url, int expectedStatusCode,
        List<NameValuePair> postParams, String assertMessage) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);

    URL baseUrl = new URL(HTTP_BASE_URL);
    AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
    post.setDoAuthentication(true);//from ww  w.j  a v  a2  s.  c  o  m
    Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
    try {
        httpClient.getState().setCredentials(authScope, creds);

        if (postParams != null) {
            final NameValuePair[] nvp = {};
            post.setRequestBody(postParams.toArray(nvp));
        }

        final int status = httpClient.executeMethod(post);
        if (assertMessage == null) {
            assertEquals(expectedStatusCode, status);
        } else {
            assertEquals(assertMessage, expectedStatusCode, status);
        }
    } finally {
        httpClient.getState().setCredentials(authScope, oldCredentials);
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.issues.SLING2085Test.java

public void testRecentRequestsEscape() throws Exception {
    final String basePath = "/" + getClass().getSimpleName() + "/" + Math.random();
    final String path = basePath + ".html/%22%3e%3cscript%3ealert(29679)%3c/script%3e";

    // POST to create node 
    {/*from  w  w  w  .j ava 2s  .  co m*/
        final PostMethod post = new PostMethod(HTTP_BASE_URL + path);
        post.setFollowRedirects(false);
        final int status = httpClient.executeMethod(post);
        assertEquals(201, status);
    }

    // And check that recent requests output does not contain <script>
    {
        final String content = getContent(HTTP_BASE_URL + "/system/console/requests?index=1",
                CONTENT_TYPE_HTML);
        final String scriptTag = "<script>";
        assertFalse("Content should not contain '" + scriptTag + "'", content.contains(scriptTag));
    }
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.jcrinstall.JcrinstallTestBase.java

protected void enableJcrinstallService(boolean enable) throws IOException {
    if (enable != getJcrinstallServiceEnabled()) {
        final PostMethod post = new PostMethod(HTTP_BASE_URL + JCRINSTALL_STATUS_PATH);
        post.setFollowRedirects(false);
        post.addParameter("enabled", String.valueOf(enable));
        final int status = httpClient.executeMethod(post);
        assertEquals("Expected status 200 in enableJcrinstallService", 200, status);
        assertEquals("Expected jcrinstall.enabled to be " + enable, enable, getJcrinstallServiceEnabled());
    }//from   w w  w. j  ava 2s . c  o m
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.login.RedirectOnLoginErrorTest.java

/** Execute a POST request and check status
  * @return the HttpMethod executed/*from   www. ja va  2  s . c o m*/
  * @throws IOException */
private HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams,
        String assertMessage, String referer) throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setDoAuthentication(false);

    //set the referer to indicate where we came from
    post.setRequestHeader("Referer", referer);

    //set Accept header to trick sling into treating the request as from a browser
    post.setRequestHeader("User-Agent", "Mozilla/5.0 Sling Integration Test");

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    final int status = httpClient.executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.login.SelectorRedirectOnLoginErrorTest.java

/** Execute a POST request and check status
  * @return the HttpMethod executed/*from w ww.  j a  v a2 s  .co m*/
  * @throws IOException */
private HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams,
        String assertMessage, String referer) throws IOException {
    // TODO - method copied from org.apache.sling.launchpad.webapp.integrationtest.login.RedirectOnLoginErrorTest
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setDoAuthentication(false);

    //set the referer to indicate where we came from
    post.setRequestHeader("Referer", referer);

    //set Accept header to trick sling into treating the request as from a browser
    post.setRequestHeader("User-Agent", "Mozilla/5.0 Sling Integration Test");

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    if (postParams != null) {
        final NameValuePair[] nvp = {};
        post.setRequestBody(postParams.toArray(nvp));
    }

    final int status = httpClient.executeMethod(post);
    if (assertMessage == null) {
        assertEquals(expectedStatusCode, status);
    } else {
        assertEquals(assertMessage, expectedStatusCode, status);
    }
    return post;
}

From source file:org.apache.sling.launchpad.webapp.integrationtest.servlets.post.PostResponseCreatorTest.java

public void testCustomPostResponseCreator() throws Exception {
    final PostMethod post = new PostMethod(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX);
    post.addParameter(":responseType", "custom");

    post.setFollowRedirects(false);

    final int status = httpClient.executeMethod(post);
    assertEquals("Unexpected status response", 201, status);

    assertEquals("Thanks!", post.getResponseBodyAsString());

    post.releaseConnection();/*from  w w  w  .ja  va  2s .  co m*/
}