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

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

Introduction

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

Prototype

public void addParameter(String paramString1, String paramString2) throws IllegalArgumentException 

Source Link

Usage

From source file:org.jboss.test.security.test.WebJASPIFormUnitTestCase.java

public void testFormAuthSuccess() throws Exception {
    GetMethod indexGet = new GetMethod(baseURLNoAuth + "jaspi-web-form/");
    int responseCode = httpConn.executeMethod(indexGet);
    String body = indexGet.getResponseBodyAsString();
    assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
    assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);

    HttpState state = httpConn.getState();
    Cookie[] cookies = state.getCookies();
    String sessionID = null;//  w ww  .j  a va 2 s  .com
    for (int c = 0; c < cookies.length; c++) {
        Cookie k = cookies[c];
        if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
    }
    getLog().debug("Saw JSESSIONID=" + sessionID);

    // Submit the login form
    PostMethod formPost = new PostMethod(baseURLNoAuth + "jaspi-web-form/j_security_check");
    formPost.addRequestHeader("Referer", baseURLNoAuth + "jaspi-web-form/login.jsp");
    formPost.addParameter("j_username", username);
    formPost.addParameter("j_password", password);
    responseCode = httpConn.executeMethod(formPost);
    String response = formPost.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);

    //  Follow the redirect to the SecureServlet
    Header location = formPost.getResponseHeader("Location");
    String indexURI = location.getValue();
    GetMethod war1Index = new GetMethod(indexURI);
    responseCode = httpConn.executeMethod(war1Index);
    response = war1Index.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    body = war1Index.getResponseBodyAsString();
    if (body.indexOf("j_security_check") > 0)
        fail("get of " + indexURI + " redirected to login page");
    if (body.indexOf("Hi Anil") < 0)
        fail("index.html not seen");
}

From source file:org.jboss.test.security.test.WebJASPIFormUnitTestCase.java

public void testFormAuthFailure() throws Exception {
    log.info("+++ testFormAuthFailure");
    GetMethod indexGet = new GetMethod(baseURLNoAuth + "jaspi-web-form/");
    int responseCode = httpConn.executeMethod(indexGet);
    String body = indexGet.getResponseBodyAsString();
    assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
    assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);

    HttpState state = httpConn.getState();
    Cookie[] cookies = state.getCookies();
    String sessionID = null;/*from   w  w w.ja  va 2 s  .c  om*/
    for (int c = 0; c < cookies.length; c++) {
        Cookie k = cookies[c];
        if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
    }
    getLog().debug("Saw JSESSIONID=" + sessionID);

    // Submit the login form
    PostMethod formPost = new PostMethod(baseURLNoAuth + "jaspi-web-form/j_security_check");
    formPost.addRequestHeader("Referer", baseURLNoAuth + "jaspi-web-form/login.jsp");
    formPost.addParameter("j_username", "baduser");
    formPost.addParameter("j_password", "badpass");
    responseCode = httpConn.executeMethod(formPost);
    String response = formPost.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    String responseBody = formPost.getResponseBodyAsString();
    if (responseBody.indexOf("Errored") < 0)
        fail("Error page not seen");
}

From source file:org.jboss.test.web.security.CustomHeaderAuthTestCase.java

private PostMethod doSecureGetWithLogin(String path, String username, String password) throws Exception {
    GetMethod indexGet = new GetMethod(baseURLNoAuth + path);
    int responseCode = httpConn.executeMethod(indexGet);
    String body = indexGet.getResponseBodyAsString();
    assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
    assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);

    HttpState state = httpConn.getState();
    Cookie[] cookies = state.getCookies();
    String sessionID = null;//from www  .  j a  v  a2s. c  o  m
    for (int c = 0; c < cookies.length; c++) {
        Cookie k = cookies[c];
        if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
    }
    getLog().debug("Saw JSESSIONID=" + sessionID);

    // Submit the login form
    PostMethod formPost = new PostMethod(baseURLNoAuth + "header-form-auth/j_security_check");
    formPost.addRequestHeader("Referer", baseURLNoAuth + "header-form-auth/restricted/login.html");
    formPost.addParameter("j_username", username);
    formPost.addParameter("j_password", password);
    responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state);
    String response = formPost.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);

    //  Follow the redirect to the SecureServlet
    Header location = formPost.getResponseHeader("Location");
    String indexURI = location.getValue();
    GetMethod war1Index = new GetMethod(indexURI);
    responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), war1Index, state);
    response = war1Index.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    body = war1Index.getResponseBodyAsString();
    if (body.indexOf("j_security_check") > 0)
        fail("get of " + indexURI + " redirected to login page");
    return formPost;
}

From source file:org.jboss.test.web.security.GenericHeaderAuthUnitTestCase.java

/**
 * <p>//from   w  ww. java2 s .  c  o  m
 * Check that, in the absence of headers, regular form authentication takes place.
 * </p>
 * 
 * @throws Exception if an error occurs when running the test.
 */
public void testRegularFormAuth() throws Exception {
    GetMethod getMethod = new GetMethod(this.testAppBaseURL + this.securedServletPath);
    // execute a plain request to the SecureServlet
    try {
        int responseCode = this.httpClient.executeMethod(getMethod);
        String body = getMethod.getResponseBodyAsString();
        // check the response code and assert the redirection to the login page
        assertTrue("Unexpected response code received: " + responseCode,
                responseCode == HttpURLConnection.HTTP_OK);
        assertTrue("Failed to redirect the request to the login page", body.indexOf("j_security_check") > 0);
    } finally {
        getMethod.releaseConnection();
    }

    HttpState state = this.httpClient.getState();
    // fill in the login form and submit it
    PostMethod postMethod = new PostMethod(this.testAppBaseURL + "j_security_check");
    postMethod.addRequestHeader("Referer", this.testAppBaseURL + "restricted/login.html");
    postMethod.addParameter("j_username", "jduke");
    postMethod.addParameter("j_password", "theduke");
    Header location = null;
    try {
        int responseCode = this.httpClient.executeMethod(postMethod.getHostConfiguration(), postMethod, state);
        log.debug("responseCode=" + responseCode + ", response=" + postMethod.getStatusText());
        // check the response code received and the presence of a location header in the response
        assertTrue("Unexpected response code received: " + responseCode,
                responseCode == HttpURLConnection.HTTP_MOVED_TEMP);
        location = postMethod.getResponseHeader("Location");
        assertNotNull("Location header not found in response", location);
    } finally {
        postMethod.releaseConnection();
    }

    // follow the redirect as defined by the location header
    String indexURI = location.getValue();
    getMethod = new GetMethod(indexURI);
    try {
        int responseCode = this.httpClient.executeMethod(getMethod.getHostConfiguration(), getMethod, state);
        log.debug("responseCode=" + responseCode + ", response=" + getMethod.getStatusText());
        // check the reponse code received
        assertTrue("Unexpected response code received: " + responseCode,
                responseCode == HttpURLConnection.HTTP_OK);
        String body = getMethod.getResponseBodyAsString();
        // assert the redirection of to the SecureServlet
        assertTrue("Redirect to SecureServlet has failed", body.indexOf("SecureServlet") > 0);
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.jboss.test.web.test.FormAuthUnitTestCase.java

/**
 * Test that a bad login is redirected to the errors.jsp and that the
 * session j_exception is not null.//from w w  w  .j  a  v a2 s.  c  o m
 * 
 * @throws Exception
 */
public void testFormAuthException() throws Exception {
    log.info("+++ testFormAuthException");
    GetMethod indexGet = new GetMethod(baseURLNoAuth + "form-auth/restricted/SecuredServlet");
    int responseCode = httpConn.executeMethod(indexGet);
    String body = indexGet.getResponseBodyAsString();
    assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
    assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);

    HttpState state = httpConn.getState();
    Cookie[] cookies = state.getCookies();
    String sessionID = null;
    for (int c = 0; c < cookies.length; c++) {
        Cookie k = cookies[c];
        if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
    }
    getLog().debug("Saw JSESSIONID=" + sessionID);

    // Submit the login form
    PostMethod formPost = new PostMethod(baseURLNoAuth + "form-auth/j_security_check");
    formPost.addRequestHeader("Referer", baseURLNoAuth + "form-auth/restricted/login.html");
    formPost.addParameter("j_username", "baduser");
    formPost.addParameter("j_password", "badpass");
    responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state);
    String response = formPost.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    Header jex = formPost.getResponseHeader("X-JException");
    log.debug("Saw X-JException, " + jex);
    assertNotNull("X-JException != null", jex);
}

From source file:org.jboss.test.web.test.FormAuthUnitTestCase.java

/** Test form authentication of a secured servlet and validate that there is
 * a SecurityAssociation setting Subject. 
 * /*from w  w w. j  a  v a  2  s .c om*/
 * @throws Exception
 */
public void testFormAuthSubject() throws Exception {
    log.info("+++ testFormAuthSubject");
    // Start by accessing the secured index.html of war1
    HttpClient httpConn = new HttpClient();
    GetMethod indexGet = new GetMethod(baseURLNoAuth + "form-auth/restricted/SecuredServlet");
    indexGet.setQueryString("validateSubject=true");
    int responseCode = httpConn.executeMethod(indexGet);
    String body = indexGet.getResponseBodyAsString();
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);

    HttpState state = httpConn.getState();
    Cookie[] cookies = state.getCookies();
    String sessionID = null;
    for (int c = 0; c < cookies.length; c++) {
        Cookie k = cookies[c];
        if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
    }
    getLog().debug("Saw JSESSIONID=" + sessionID);

    // Submit the login form
    PostMethod formPost = new PostMethod(baseURLNoAuth + "form-auth/j_security_check");
    formPost.addRequestHeader("Referer", baseURLNoAuth + "form-auth/restricted/login.html");
    formPost.addParameter("j_username", "jduke");
    formPost.addParameter("j_password", "theduke");
    responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state);
    String response = formPost.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);

    //  Follow the redirect to the SecureServlet
    Header location = formPost.getResponseHeader("Location");
    String indexURI = location.getValue();
    GetMethod war1Index = new GetMethod(indexURI);
    responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), war1Index, state);
    response = war1Index.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    body = war1Index.getResponseBodyAsString();
    if (body.indexOf("j_security_check") > 0)
        fail("get of " + indexURI + " redirected to login page");
}

From source file:org.jboss.test.web.test.FormAuthUnitTestCase.java

/** Test that a post from an unsecured form to a secured servlet does not
 * loose its data during the redirct to the form login.
 * /* w  w  w . j ava2  s  . com*/
 * @throws Exception
 */
public void testPostDataFormAuth() throws Exception {
    log.info("+++ testPostDataFormAuth");
    // Start by accessing the secured index.html of war1
    HttpClient httpConn = new HttpClient();
    GetMethod indexGet = new GetMethod(baseURLNoAuth + "form-auth/unsecure_form.html");
    int responseCode = httpConn.executeMethod(indexGet);
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    // Submit the form to /restricted/SecuredPostServlet
    PostMethod servletPost = new PostMethod(baseURLNoAuth + "form-auth/restricted/SecuredPostServlet");
    servletPost.addParameter("checkParam", "123456");
    responseCode = httpConn.executeMethod(servletPost);

    String body = servletPost.getResponseBodyAsString();
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);

    HttpState state = httpConn.getState();
    Cookie[] cookies = state.getCookies();
    String sessionID = null;
    for (int c = 0; c < cookies.length; c++) {
        Cookie k = cookies[c];
        if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
    }
    getLog().debug("Saw JSESSIONID=" + sessionID);
    // Submit the login form
    PostMethod formPost = new PostMethod(baseURLNoAuth + "form-auth/j_security_check");
    formPost.addRequestHeader("Referer", baseURLNoAuth + "form-auth/unsecure_form.html");
    formPost.addParameter("j_username", "jduke");
    formPost.addParameter("j_password", "theduke");
    responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state);
    String response = formPost.getStatusText();
    getLog().debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);

    //  Follow the redirect to the SecureServlet
    Header location = formPost.getResponseHeader("Location");
    String indexURI = location.getValue();
    GetMethod war1Index = new GetMethod(indexURI);
    responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), war1Index, state);
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    body = war1Index.getResponseBodyAsString();
    if (body.indexOf("j_security_check") > 0)
        fail("get of " + indexURI + " redirected to login page");
}

From source file:org.jboss.test.web.test.FormAuthUnitTestCase.java

public PostMethod doSecureGetWithLogin(String path, String username, String password) throws Exception {
    GetMethod indexGet = new GetMethod(baseURLNoAuth + path);
    int responseCode = httpConn.executeMethod(indexGet);
    String body = indexGet.getResponseBodyAsString();
    assertTrue("Get OK(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_OK);
    assertTrue("Redirected to login page", body.indexOf("j_security_check") > 0);

    HttpState state = httpConn.getState();
    Cookie[] cookies = state.getCookies();
    String sessionID = null;//ww w  . j ava  2  s .c o  m
    for (int c = 0; c < cookies.length; c++) {
        Cookie k = cookies[c];
        if (k.getName().equalsIgnoreCase("JSESSIONID"))
            sessionID = k.getValue();
    }
    getLog().debug("Saw JSESSIONID=" + sessionID);

    // Submit the login form
    PostMethod formPost = new PostMethod(baseURLNoAuth + "form-auth/j_security_check");
    formPost.addRequestHeader("Referer", baseURLNoAuth + "form-auth/restricted/login.html");
    formPost.addParameter("j_username", username);
    formPost.addParameter("j_password", password);
    responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, state);
    String response = formPost.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Saw HTTP_MOVED_TEMP", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);

    //  Follow the redirect to the SecureServlet
    Header location = formPost.getResponseHeader("Location");
    String indexURI = location.getValue();
    GetMethod war1Index = new GetMethod(indexURI);
    responseCode = httpConn.executeMethod(war1Index.getHostConfiguration(), war1Index, state);
    response = war1Index.getStatusText();
    log.debug("responseCode=" + responseCode + ", response=" + response);
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    body = war1Index.getResponseBodyAsString();
    if (body.indexOf("j_security_check") > 0)
        fail("get of " + indexURI + " redirected to login page");
    return formPost;
}

From source file:org.jboss.test.web.test.SSOBaseCase.java

public static void executeFormLogin(HttpClient httpConn, String warURL) throws IOException, HttpException {
    PostMethod formPost = new PostMethod(warURL + "j_security_check");
    formPost.addRequestHeader("Referer", warURL + "login.html");
    formPost.addParameter("j_username", "jduke");
    formPost.addParameter("j_password", "theduke");
    int responseCode = httpConn.executeMethod(formPost.getHostConfiguration(), formPost, httpConn.getState());
    assertTrue("Saw HTTP_MOVED_TEMP(" + responseCode + ")", responseCode == HttpURLConnection.HTTP_MOVED_TEMP);

    //  Follow the redirect to the index.html page
    Header location = formPost.getResponseHeader("Location");
    String indexURI = location.getValue();
    GetMethod warIndex = new GetMethod(indexURI);
    responseCode = httpConn.executeMethod(warIndex.getHostConfiguration(), warIndex, httpConn.getState());
    assertTrue("Get OK", responseCode == HttpURLConnection.HTTP_OK);
    String body = warIndex.getResponseBodyAsString();
    if (body.indexOf("j_security_check") > 0)
        fail("get of " + indexURI + " redirected to login page");
}

From source file:org.jboss.tools.common.reporting.Submit.java

private int submitReport(String reportText) throws IOException {
    HttpClient httpClient = HttpUtil.createHttpClient(URL);
    PostMethod httpPost = new PostMethod(URL);
    httpPost.addParameter(REPORT_DESRIPTION_PARAMETER_NAME, reportText);
    int responseCode = httpClient.executeMethod(httpPost);

    return responseCode;
}