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:test.integ.be.fedict.eid.idp.OpenIDAssociationsTest.java

@Test
public void testEstablishAssociation() throws Exception {
    // setup//w  w  w  . jav  a 2s  . c o m
    AssociationSessionType associationSessionType = AssociationSessionType.NO_ENCRYPTION_SHA1MAC;
    String opEndpoint = "https://www.e-contract.be/eid-idp/protocol/openid/auth";

    // operate
    DiffieHellmanSession dhSession;
    if (null != associationSessionType.getHAlgorithm()) {
        // Diffie-Hellman
        DHParameterSpec dhParameterSpec = DiffieHellmanSession.getDefaultParameter();
        dhSession = DiffieHellmanSession.create(associationSessionType, dhParameterSpec);

    } else {
        dhSession = null;
    }
    AssociationRequest associationRequest = AssociationRequest.createAssociationRequest(associationSessionType,
            dhSession);
    LOG.debug("association type: " + associationRequest.getType().getAssociationType());
    LOG.debug("session type: " + associationRequest.getType().getSessionType());

    Map<String, String> parameters = associationRequest.getParameterMap();

    HttpClient httpClient = new HttpClient();
    httpClient.getHostConfiguration().setProxy("proxy.yourict.net", 8080);
    PostMethod postMethod = new PostMethod(opEndpoint);
    for (Map.Entry<String, String> parameter : parameters.entrySet()) {
        postMethod.addParameter(parameter.getKey(), parameter.getValue());
    }

    int statusCode = httpClient.executeMethod(postMethod);
    LOG.debug("status code: " + statusCode);
    assertEquals(HttpURLConnection.HTTP_OK, statusCode);

    postMethod.getResponseBody();

    ParameterList responseParameterList = ParameterList
            .createFromKeyValueForm(postMethod.getResponseBodyAsString());
    AssociationResponse associationResponse = AssociationResponse
            .createAssociationResponse(responseParameterList);

    Association association = associationResponse.getAssociation(dhSession);
    LOG.debug("association type: " + association.getType());
    LOG.debug("association handle: " + association.getHandle());
    LOG.debug("association expiry: " + association.getExpiry());
    SecretKey secretKey = association.getMacKey();
    LOG.debug("association MAC key algo: " + secretKey.getAlgorithm());
}

From source file:test.integ.be.fedict.eid.idp.OpenIDAssociationsTest.java

/**
 * http://code.google.com/p/openid4java/issues/detail?id=192
 * //from  w w  w  .j  a v a  2 s .co m
 * @throws Exception
 */
@Test
public void testEstablishAssociationSteam() throws Exception {
    // setup
    AssociationSessionType associationSessionType = AssociationSessionType.NO_ENCRYPTION_SHA1MAC;
    String opEndpoint = "https://steamcommunity.com/openid/login";

    // operate
    DiffieHellmanSession dhSession;
    if (null != associationSessionType.getHAlgorithm()) {
        // Diffie-Hellman
        DHParameterSpec dhParameterSpec = DiffieHellmanSession.getDefaultParameter();
        dhSession = DiffieHellmanSession.create(associationSessionType, dhParameterSpec);

    } else {
        dhSession = null;
    }
    AssociationRequest associationRequest = AssociationRequest.createAssociationRequest(associationSessionType,
            dhSession);
    LOG.debug("association type: " + associationRequest.getType().getAssociationType());
    LOG.debug("session type: " + associationRequest.getType().getSessionType());

    Map<String, String> parameters = associationRequest.getParameterMap();

    HttpClient httpClient = new HttpClient();
    httpClient.getHostConfiguration().setProxy("proxy.yourict.net", 8080);
    PostMethod postMethod = new PostMethod(opEndpoint);
    for (Map.Entry<String, String> parameter : parameters.entrySet()) {
        postMethod.addParameter(parameter.getKey(), parameter.getValue());
    }

    int statusCode = httpClient.executeMethod(postMethod);
    LOG.debug("status code: " + statusCode);
    assertEquals(HttpURLConnection.HTTP_OK, statusCode);

    postMethod.getResponseBody();

    ParameterList responseParameterList = ParameterList
            .createFromKeyValueForm(postMethod.getResponseBodyAsString());
    AssociationResponse associationResponse = AssociationResponse
            .createAssociationResponse(responseParameterList);

    Association association = associationResponse.getAssociation(dhSession);
    LOG.debug("association type: " + association.getType());
    LOG.debug("association handle: " + association.getHandle());
    LOG.debug("association expiry: " + association.getExpiry());
    SecretKey secretKey = association.getMacKey();
    LOG.debug("association MAC key algo: " + secretKey.getAlgorithm());
}

From source file:test.pubman.TestBase.java

/**
 * Logs in the given user with the given password.
 * /*from   ww w  .j a v a2s.c o m*/
 * @param userid The id of the user to log in.
 * @param password The password of the user to log in.
 * @return The handle for the logged in user.
 * @throws HttpException
 * @throws IOException
 * @throws ServiceException
 * @throws URISyntaxException 
 */
protected static String loginUser(String userid, String password)
        throws HttpException, IOException, ServiceException, URISyntaxException {
    String frameworkUrl = ServiceLocator.getFrameworkUrl();
    int delim1 = frameworkUrl.indexOf("//");
    int delim2 = frameworkUrl.indexOf(":", delim1);

    String host;
    int port;

    if (delim2 > 0) {
        host = frameworkUrl.substring(delim1 + 2, delim2);
        port = Integer.parseInt(frameworkUrl.substring(delim2 + 1));
    } else {
        host = frameworkUrl.substring(delim1 + 2);
        port = 80;
    }
    HttpClient client = new HttpClient();

    ProxyHelper.setProxy(client, frameworkUrl);

    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);

    PostMethod login = new PostMethod(frameworkUrl + "/aa/j_spring_security_check");
    login.addParameter("j_username", userid);
    login.addParameter("j_password", password);

    client.executeMethod(login);

    login.releaseConnection();
    CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
    Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies());

    Cookie sessionCookie = logoncookies[0];

    PostMethod postMethod = new PostMethod(frameworkUrl + "/aa/login");
    postMethod.addParameter("target", frameworkUrl);
    client.getState().addCookie(sessionCookie);
    client.executeMethod(postMethod);

    if (HttpServletResponse.SC_SEE_OTHER != postMethod.getStatusCode()) {
        throw new HttpException("Wrong status code: " + login.getStatusCode());
    }

    String userHandle = null;
    Header headers[] = postMethod.getResponseHeaders();
    for (int i = 0; i < headers.length; ++i) {
        if ("Location".equals(headers[i].getName())) {
            String location = headers[i].getValue();
            int index = location.indexOf('=');
            userHandle = new String(Base64.decode(location.substring(index + 1, location.length())));
            //System.out.println("location: "+location);
            //System.out.println("handle: "+userHandle);
        }
    }

    if (userHandle == null) {
        throw new ServiceException("User not logged in.");
    }
    return userHandle;
}

From source file:vle.domain.webservice.http.impl.HttpRestTransportImpl.java

/**
 * @see net.sf.sail.webapp.domain.webservice.http.HttpRestTransport#post(net.sf.sail.webapp.domain.webservice.http.HttpPostRequest)
 *///from  w w w. j a v a2s. co m
public PostMethod post(final HttpPostRequest httpPostRequestData) throws HttpStatusCodeException {
    final PostMethod method = new PostMethod(this.baseUrl + httpPostRequestData.getRelativeUrl());

    this.setHeaders(httpPostRequestData, method);

    // set body data
    final String bodyData = httpPostRequestData.getBodyData();
    //if (StringUtils.hasText(bodyData)) {
    //   method.setRequestEntity(new StringRequestEntity(bodyData));
    //}

    // set parameters
    final Map<String, String> requestParameters = httpPostRequestData.getRequestParameters();
    if (requestParameters != null && !requestParameters.isEmpty()) {
        final Set<String> keys = requestParameters.keySet();
        for (Iterator<String> i = keys.iterator(); i.hasNext();) {
            String key = i.next();
            method.addParameter(key, requestParameters.get(key));
        }
    }

    final Map<String, String> responseHeaders = new HashMap<String, String>();
    try {
        // Execute the method.
        logRequest(method, bodyData);
        method.addParameter("status", "abc");
        NameValuePair[] arr = new NameValuePair[] { new NameValuePair("status", "good") };
        method.setRequestBody(arr);
        method.setRequestBody(bodyData);
        final int statusCode = this.client.executeMethod(method);
        httpPostRequestData.isValidResponseStatus(method, statusCode);
        final Header[] headers = method.getResponseHeaders();
        for (int i = 0; i < headers.length; i++) {
            responseHeaders.put(headers[i].getName(), headers[i].getValue());
        }
    } catch (HttpException e) {
        logAndThrowRuntimeException(e);
    } catch (IOException e) {
        logAndThrowRuntimeException(e);
    } finally {
        method.releaseConnection();
    }

    return method;
}