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

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

Introduction

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

Prototype

public void setParameter(String paramString1, String paramString2) 

Source Link

Usage

From source file:com.denimgroup.threadfix.importer.impl.remoteprovider.utils.DefaultRequestConfigurer.java

@Override
public void configure(HttpMethodBase method) {
    if (username != null && password != null) {
        String login = username + ":" + password;
        String encodedLogin = DatatypeConverter.printBase64Binary(login.getBytes());

        method.setRequestHeader("Authorization", "Basic " + encodedLogin);
    }/* ww w.  j  a v a 2s .c o m*/

    method.setRequestHeader("Content-type", contentType);

    if (headerNames != null && headerVals != null && headerNames.length == headerVals.length) {
        for (int i = 0; i < headerNames.length; i++) {
            method.setRequestHeader(headerNames[i], headerVals[i]);
        }
    }

    if (method instanceof PostMethod) {
        PostMethod postVersion = (PostMethod) method;
        if (parameterNames != null && parameterVals != null && parameterNames.length == parameterVals.length) {
            for (int i = 0; i < parameterNames.length; i++) {
                postVersion.setParameter(parameterNames[i], parameterVals[i]);
            }
        }

        if (requestBody != null) {
            postVersion.setRequestEntity(getRequestEntity());
        }
    } // if it's a get then the parameters should be in the URL
}

From source file:com.owncloud.android.lib.resources.users.SendCSROperation.java

/**
 * @param client Client object//from   ww  w.j  a va  2 s .  c o  m
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    PostMethod postMethod = null;
    RemoteOperationResult result;

    try {
        // remote request
        postMethod = new PostMethod(client.getBaseUri() + PUBLIC_KEY_URL + JSON_FORMAT);
        postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
        postMethod.setParameter(CSR, csr);

        int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

        if (status == HttpStatus.SC_OK) {
            String response = postMethod.getResponseBodyAsString();

            // Parse the response
            JSONObject respJSON = new JSONObject(response);
            String key = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA)
                    .get(NODE_PUBLIC_KEY);

            result = new RemoteOperationResult(true, postMethod);
            ArrayList<Object> keys = new ArrayList<>();
            keys.add(key);
            result.setData(keys);
        } else {
            result = new RemoteOperationResult(false, postMethod);
            client.exhaustResponse(postMethod.getResponseBodyAsStream());
        }

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Fetching of signing CSR failed: " + result.getLogMessage(), result.getException());
    } finally {
        if (postMethod != null)
            postMethod.releaseConnection();
    }
    return result;
}

From source file:com.owncloud.android.lib.resources.users.StorePrivateKeyOperation.java

/**
 * @param client Client object/*from w  ww  . j  a  v  a2s  .c om*/
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    PostMethod postMethod = null;
    RemoteOperationResult result;

    try {
        // remote request
        postMethod = new PostMethod(client.getBaseUri() + PRIVATE_KEY_URL + JSON_FORMAT);
        postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
        postMethod.setParameter(PRIVATE_KEY, privateKey);

        int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

        if (status == HttpStatus.SC_OK) {
            String response = postMethod.getResponseBodyAsString();

            // Parse the response
            JSONObject respJSON = new JSONObject(response);
            String key = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA)
                    .get(NODE_PRIVATE_KEY);

            result = new RemoteOperationResult(true, postMethod);
            ArrayList<Object> keys = new ArrayList<>();
            keys.add(key);
            result.setData(keys);
        } else {
            result = new RemoteOperationResult(false, postMethod);
            client.exhaustResponse(postMethod.getResponseBodyAsStream());
        }

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Storing private key failed: " + result.getLogMessage(), result.getException());
    } finally {
        if (postMethod != null)
            postMethod.releaseConnection();
    }
    return result;
}

From source file:com.owncloud.android.lib.resources.files.StoreMetadataOperation.java

/**
 * @param client Client object// ww w. j  a v  a2  s.  co  m
 */
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    PostMethod postMethod = null;
    RemoteOperationResult result;

    try {
        // remote request
        postMethod = new PostMethod(client.getBaseUri() + METADATA_URL + fileId + JSON_FORMAT);
        postMethod.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
        postMethod.setParameter(METADATA, encryptedMetadataJson);

        int status = client.executeMethod(postMethod, SYNC_READ_TIMEOUT, SYNC_CONNECTION_TIMEOUT);

        if (status == HttpStatus.SC_OK) {
            String response = postMethod.getResponseBodyAsString();

            // Parse the response
            JSONObject respJSON = new JSONObject(response);
            String metadata = (String) respJSON.getJSONObject(NODE_OCS).getJSONObject(NODE_DATA)
                    .get(NODE_META_DATA);

            result = new RemoteOperationResult(true, postMethod);
            ArrayList<Object> keys = new ArrayList<>();
            keys.add(metadata);
            result.setData(keys);
        } else {
            result = new RemoteOperationResult(false, postMethod);
            client.exhaustResponse(postMethod.getResponseBodyAsStream());
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Storing of metadata for folder " + fileId + " failed: " + result.getLogMessage(),
                result.getException());
    } finally {
        if (postMethod != null)
            postMethod.releaseConnection();
    }
    return result;
}

From source file:fr.aliasource.webmail.server.invitation.GetInvitationInfoProxyImpl.java

@SuppressWarnings("unchecked")
@Override/*from  w  w w.  jav a2s  .c o m*/
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    IAccount ac = (IAccount) req.getSession().getAttribute("account");

    if (ac == null) {
        GWT.log("Account not found in session", null);
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    PostMethod pm = new PostMethod(backendUrl);
    if (req.getQueryString() != null) {
        pm.setQueryString(req.getQueryString());
    }
    Map<String, String[]> params = req.getParameterMap();
    for (String p : params.keySet()) {
        String[] val = params.get(p);
        pm.setParameter(p, val[0]);
    }

    synchronized (hc) {
        try {
            int ret = hc.executeMethod(pm);
            if (ret != HttpStatus.SC_OK) {
                log("method failed:\n" + pm.getStatusLine() + "\n" + pm.getResponseBodyAsString());
                resp.setStatus(ret);
            } else {
                InputStream is = pm.getResponseBodyAsStream();
                transfer(is, resp.getOutputStream(), false);
            }

        } catch (Exception e) {
            log("error occured on call proxyfication", e);
        } finally {
            pm.releaseConnection();
        }
    }
}

From source file:eu.earthobservatory.org.StrabonEndpoint.capabilities.AutoDiscoveryCapabilities.java

@Override
public RequestCapabilities getQueryCapabilities() {
    RequestCapabilities request = new RequestCapabilitiesImpl();

    String query = "SELECT * WHERE {?s ?p ?o. FILTER(regex(str(?p), \"geometry\"))} LIMIT 1";

    String[] queryParams = { "SPARQLQuery", "query" };

    String[] formatValues = { "XML", "KML", "KMZ", "GeoJSON", "HTML", "TSV" };

    String[] acceptValues = { "application/sparql-results+xml", "text/tab-separated-values",
            "application/vnd.google-earth.kml+xml", "application/vnd.google-earth.kmz", "text/html",
            "application/json" };

    // check query parameter and format parameter
    for (int q = 0; q < queryParams.length; q++) {
        for (int v = 0; v < formatValues.length; v++) {
            HttpClient hc = new HttpClient();

            // create a post method to execute
            PostMethod method = new PostMethod(getConnectionURL() + "/Query");

            // set the query parameter
            method.setParameter(queryParams[q], query);

            // set the format parameter
            method.setParameter("format", formatValues[v]);

            try {
                // execute the method
                int statusCode = hc.executeMethod(method);

                if (statusCode == 301 || statusCode == 200) {
                    //System.out.println(queryParams[q] + ", " + formatValues[v]);
                    request.getParametersObject().addParameter(new Parameter(queryParams[q], null));
                    request.getParametersObject().addParameter(new Parameter("format", null));
                    request.getParametersObject().getParameter("format").addAcceptedValue(formatValues[v]);
                }/* w w  w . j  a v a 2 s .c o m*/

            } catch (IOException e) {
                e.printStackTrace();

            } finally {
                // release the connection.
                method.releaseConnection();
            }
        }
    }

    // check query parameter and accept header
    for (int q = 0; q < queryParams.length; q++) {
        for (int a = 0; a < acceptValues.length; a++) {
            HttpClient hc = new HttpClient();

            // create a post method to execute
            PostMethod method = new PostMethod(getConnectionURL() + "/Query");

            // set the query parameter
            method.setParameter(queryParams[q], query);

            // check for accept value as well 
            // set the accept format
            method.addRequestHeader("Accept", acceptValues[a]);

            try {
                // execute the method
                int statusCode = hc.executeMethod(method);

                if (statusCode == 301 || statusCode == 200) {
                    //System.out.println(queryParams[q] + ", " + acceptValues[a]);
                    request.getParametersObject().addParameter(new Parameter(queryParams[q], null));
                    request.getParametersObject().addParameter(new Parameter("Accept", null));
                    request.getParametersObject().getParameter("Accept").addAcceptedValue(acceptValues[a]);
                }

            } catch (IOException e) {
                e.printStackTrace();

            } finally {
                // release the connection.
                method.releaseConnection();
            }
        }
    }

    return request;
}

From source file:fr.aliasource.webmail.server.calendar.CalendarProxyImpl.java

@SuppressWarnings("unchecked")
@Override/* ww w . j a va 2 s. c  om*/
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    IAccount ac = (IAccount) req.getSession().getAttribute("account");

    if (ac == null) {
        GWT.log("Account not found in session", null);
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    PostMethod pm = new PostMethod(backendUrl);
    if (req.getQueryString() != null) {
        pm.setQueryString(req.getQueryString());
    }
    Map<String, String[]> params = req.getParameterMap();
    for (String p : params.keySet()) {
        String[] val = params.get(p);
        pm.setParameter(p, val[0]);
    }

    synchronized (hc) {
        try {
            int ret = hc.executeMethod(pm);
            if (ret != HttpStatus.SC_OK) {
                log("method failed:\n" + pm.getStatusLine() + "\n" + pm.getResponseBodyAsString());
                resp.setStatus(ret);
            } else {
                InputStream is = pm.getResponseBodyAsStream();
                transfer(is, resp.getOutputStream(), false);
            }
        } catch (Exception e) {
            log("error occured on call proxyfication", e);
        } finally {
            pm.releaseConnection();
        }
    }
}

From source file:fr.aliasource.webmail.server.invitation.GoingInvitationProxyImpl.java

@SuppressWarnings("unchecked")
@Override//from  w  w w.ja va2  s .  co  m
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    IAccount ac = (IAccount) req.getSession().getAttribute("account");

    if (ac == null) {
        GWT.log("Account not found in session", null);
        resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    PostMethod pm = new PostMethod(backendUrl);
    if (req.getQueryString() != null) {
        pm.setQueryString(req.getQueryString());
    }
    Map<String, String[]> params = req.getParameterMap();
    for (String p : params.keySet()) {
        String[] val = params.get(p);
        pm.setParameter(p, val[0]);
    }

    synchronized (hc) {
        try {
            int ret = hc.executeMethod(pm);
            if (ret != HttpStatus.SC_OK) {
                log("method failed:\n" + pm.getStatusLine() + "\n" + pm.getResponseBodyAsString());
                resp.setStatus(ret);
            } else {
                InputStream is = pm.getResponseBodyAsStream();
                transfer(is, resp.getOutputStream(), false);
            }
        } catch (Exception e) {
            log("error occured on call proxyfication", e);
        } finally {
            pm.releaseConnection();
        }
    }
}

From source file:com.atlassian.jira.web.action.setup.SetupProductBundleHelper.java

public void enableWebSudo() {
    final HttpClient httpClient = prepareClient();
    final PostMethod method = new PostMethod(getWebSudoUrl());

    final String token = DigestUtils
            .shaHex(String.valueOf(System.currentTimeMillis() + String.valueOf(Math.random())));
    sharedVariables.setWebSudoToken(token);

    try {/* w  w w .j a v a2 s. c o m*/
        method.setParameter("webSudoToken", token);

        final int status = httpClient.executeMethod(method);
        if (status == 200) {
            saveCookies(httpClient.getState().getCookies());
        } else {
            log.warn("Problem when enabling websudo during product bundle license installation, status code: "
                    + status);
        }
    } catch (final IOException e) {
        log.warn("Problem when enabling websudo during product bundle license installation", e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.twinsoft.convertigo.eclipse.wizards.setup.SetupWizard.java

public void postRegisterState(final String page) {
    if (!page.equals(previousPageName)) {
        previousPageName = page;//www  . j  a  va 2s.  c  o  m
        Thread th = new Thread(new Runnable() {

            public void run() {
                synchronized (SetupWizard.this) {

                    try {
                        String[] url = { "http://www.google-analytics.com/collect" };
                        HttpClient client = prepareHttpClient(url);
                        PostMethod method = new PostMethod(url[0]);
                        HeaderName.ContentType.setRequestHeader(method, MimeType.WwwForm.value());

                        // set parameters for POST method
                        method.setParameter("v", "1");
                        method.setParameter("tid", "UA-660091-6");
                        method.setParameter("cid", getUniqueID());
                        method.setParameter("t", "pageview");
                        method.setParameter("dh", "http://www.convertigo.com");
                        method.setParameter("dp", "/StudioRegistrationWizard_" + page + ".html");
                        method.setParameter("dt", page + "_" + ProductVersion.productVersion);

                        // execute HTTP post with parameters
                        if (client != null) {
                            client.executeMethod(method);
                        }
                    } catch (Exception e) {
                        // ConvertigoPlugin.logWarning(e,
                        // "Error while trying to send registration");
                    }
                }
            }
        });
        th.setDaemon(true);
        th.setName("SetupWizard.register_steps");
        th.start();
    } else {
        previousPageName = page;
    }
}