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

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

Introduction

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

Prototype

public void addParameters(NameValuePair[] paramArrayOfNameValuePair) 

Source Link

Usage

From source file:org.olat.restapi.CatalogTest.java

@Test
public void testUpdateCatalogEntryForm() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry2.getKey().toString())
            .build();/*  w  w w.  j a  va2 s.  com*/
    final PostMethod method = createPost(uri, MediaType.APPLICATION_JSON, true);
    method.addParameters(new NameValuePair[] { new NameValuePair("name", "Entry-2-c"),
            new NameValuePair("description", "Entry-description-2-c"),
            new NameValuePair("type", String.valueOf(CatalogEntry.TYPE_NODE)) });

    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final CatalogEntryVO vo = parse(body, CatalogEntryVO.class);
    assertNotNull(vo);

    final CatalogManager catalogManager = CatalogManager.getInstance();
    final CatalogEntry updatedEntry = catalogManager.loadCatalogEntry(entry2);
    assertEquals("Entry-2-c", updatedEntry.getName());
    assertEquals("Entry-description-2-c", updatedEntry.getDescription());
}

From source file:org.openmicroscopy.shoola.svc.proxy.MessengerRequest.java

/**
 * Prepares the <code>method</code> to post.
 * @see Request#marshal()/*from   www.  j a v a2s .c om*/
 */
public HttpMethod marshal() throws TransportException {
    //Create request.
    PostMethod request = new PostMethod();
    //Marshal.
    if (email != null)
        request.addParameter(EMAIL, email);
    if (comment != null)
        request.addParameter(COMMENT, comment);
    if (error != null)
        request.addParameter(ERROR, error);
    if (extra != null)
        request.addParameter(EXTRA, extra);
    if (applicationNumber != null)
        request.addParameter(ProxyUtil.APP_NAME, applicationNumber);
    if (invoker != null)
        request.addParameter(INVOKER, invoker);
    if (applicationVersion != null)
        request.addParameter(ProxyUtil.APP_VERSION, applicationVersion);
    Map<String, String> info = ProxyUtil.collectInfo();
    Entry entry;
    Iterator k = info.entrySet().iterator();
    while (k.hasNext()) {
        entry = (Entry) k.next();
        request.addParameter((String) entry.getKey(), (String) entry.getValue());
    }

    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    if (mainFile != null) {
        pairs.add(new NameValuePair(MAIN_FILE_NAME, mainFile.getName()));
        pairs.add(new NameValuePair(MAIN_FILE_PATH, mainFile.getAbsolutePath()));
    }
    if (associatedFiles != null) {
        Iterator<File> i = associatedFiles.iterator();
        File f;
        while (i.hasNext()) {
            f = i.next();
            pairs.add(new NameValuePair(ADDITIONAL_FILE_NAME, f.getName()));
            if (f.getParent() != null)
                pairs.add(new NameValuePair(ADDITIONAL_FILE_PATH, f.getParent()));
            pairs.add(new NameValuePair(ADDITIONAL_FILE_SIZE, ((Long) f.length()).toString()));
        }
    }
    if (pairs.size() > 0) {
        Iterator<NameValuePair> j = pairs.iterator();
        NameValuePair[] values = new NameValuePair[pairs.size()];
        int index = 0;
        while (j.hasNext()) {
            values[index] = j.next();
            index++;
        }
        request.addParameters(values);
    }

    return request;
}

From source file:org.oscelot.blackboard.lti.DashboardFeed.java

public String readUrlAsString(String urlString) {

    String fileContent = "";
    LtiMessage message = new DashboardMessage(this.b2Context, this.tool, this.module);
    message.signParameters(urlString, message.tool.getLaunchGUID(), message.tool.getLaunchSecret(),
            tool.getLaunchSignatureMethod());

    int timeout;/*w w  w  .  ja v a2  s . c  om*/
    try {
        timeout = Integer.parseInt(this.b2Context.getSetting(Constants.TIMEOUT_PARAMETER) + "000");
    } catch (NumberFormatException e) {
        timeout = Constants.TIMEOUT;
    }
    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
    PostMethod httpPost = new PostMethod(urlString);
    httpPost.addParameters(message.getHTTPParams());
    try {
        httpPost.setURI(new URI(urlString, false));
        int resp = client.executeMethod(httpPost);
        if (resp < 300) {
            fileContent = httpPost.getResponseBodyAsString();
        } else if (resp < 400) {
            if (httpPost.getResponseHeader("Location") != null) {
                String url = httpPost.getResponseHeader("Location").getValue();
                if (!url.startsWith("http://") && !url.startsWith("https://")) {
                    String host = httpPost.getURI().getHost();
                    if (httpPost.getResponseHeader("Host") != null) {
                        host = httpPost.getResponseHeader("Host").getValue();
                    }
                    url = httpPost.getURI().getScheme() + "://" + host + url;
                }
                Header[] cookies = httpPost.getResponseHeaders("Set-Cookie");
                String[] cookie;
                Map<String, String> headers = new HashMap<String, String>();
                for (int i = 0; i < cookies.length; i++) {
                    cookie = cookies[i].getValue().split(";", 2);
                    headers.put("Cookie", cookie[0].trim());
                }
                fileContent = Utils.readUrlAsString(this.b2Context, url, headers);
            }
        }
    } catch (IOException e) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, e);
        fileContent = "";
    }
    httpPost.releaseConnection();

    return fileContent;

}

From source file:org.svenk.redmine.core.client.AbstractRedmineClient.java

public int createTicket(String project, Map<String, String> postValues, IProgressMonitor monitor)
        throws RedmineException {
    PostMethod method = new PostMethod(String.format(REDMINE_URL_TICKET_NEW, project));

    List<NameValuePair> values = this.ticket2HttpData(postValues);
    method.addParameters(values.toArray(new NameValuePair[values.size()]));

    String errorMsg = executeMethod(method, submitErrorParser, monitor, HttpStatus.SC_OK,
            HttpStatus.SC_MOVED_TEMPORARILY);
    if (errorMsg == null) {
        //TODO PRFEN !!!
        Header respHeader = method.getResponseHeader(HEADER_REDIRECT);
        if (respHeader != null) {
            String location = respHeader.getValue();

            Matcher m = Pattern.compile("(\\d+)$").matcher(location); //$NON-NLS-1$
            if (m.find()) {
                try {
                    return Integer.parseInt(m.group(1));
                } catch (NumberFormatException e) {
                    throw new RedmineException(Messages.AbstractRedmineClient_INVALID_TASK_ID);
                }/*  w w w  .j av a2s .  com*/
            } else {
                throw new RedmineException(Messages.AbstractRedmineClient_MISSING_TASK_ID_IN_RESPONSE);
            }
        }
    } else {
        throw new RedmineStatusException(IStatus.INFO, errorMsg);
    }

    throw new RedmineException(Messages.AbstractRedmineClient_UNHANDLED_SUBMIT_ERROR);
}

From source file:org.svenk.redmine.core.client.AbstractRedmineClient.java

public void updateTicket(int ticketId, Map<String, String> postValues, String comment, IProgressMonitor monitor)
        throws RedmineException {
    PostMethod method = new PostMethod(REDMINE_URL_TICKET_EDIT + ticketId);

    List<NameValuePair> values = this.ticket2HttpData(postValues);
    values.add(new NameValuePair(CLIENT_FIELD_NOTES, comment));

    method.addParameters(values.toArray(new NameValuePair[values.size()]));

    String errorMsg = executeMethod(method, submitErrorParser, monitor, HttpStatus.SC_OK,
            HttpStatus.SC_MOVED_TEMPORARILY);
    if (errorMsg != null) {
        throw new RedmineStatusException(IStatus.INFO, errorMsg);
    }//  w w  w .java 2 s  .  c  o m
}

From source file:org.talend.dataprofiler.ecos.service.EcosystemService.java

public static String sendPostRequest(String urlAddress, Map<String, String> parameters) throws Exception {
    HttpClient httpclient = new HttpClient();

    // adapt proxy to http client.
    EcosystemProxyAdapter.adapt(httpclient, urlAddress);

    PostMethod postMethod = new PostMethod(urlAddress);
    postMethod.getParams().setSoTimeout(TIMEOUT);
    if (parameters != null) {
        NameValuePair[] postData = new NameValuePair[parameters.size()];
        int i = 0;
        for (String key : parameters.keySet()) {
            String value = parameters.get(key);
            postData[i++] = new NameValuePair(key, value);
        }/*from  www  .  ja va 2s . c om*/
        postMethod.addParameters(postData);
    }

    httpclient.executeMethod(postMethod);
    String response = postMethod.getResponseBodyAsString();
    postMethod.releaseConnection();
    return response;
}

From source file:org.talend.designer.components.exchange.util.ExchangeUtils.java

public static String sendPostRequest(String urlAddress, Map<String, String> parameters) throws Exception {
    HttpClient httpclient = new HttpClient();
    PostMethod postMethod = new PostMethod(urlAddress);
    if (parameters != null) {
        NameValuePair[] postData = new NameValuePair[parameters.size()];
        int i = 0;
        for (String key : parameters.keySet()) {
            String value = parameters.get(key);
            postData[i++] = new NameValuePair(key, value);
        }/*  w  w w.j  av a  2 s. c o m*/
        postMethod.addParameters(postData);
    }

    httpclient.executeMethod(postMethod);
    String response = postMethod.getResponseBodyAsString();
    postMethod.releaseConnection();
    return response;
}

From source file:org.tsaap.lti.tp.ResourceLink.java

/**
 * Performs an HTTP POST request.//from w  w  w .ja  v  a 2  s .c  o  m
 *
 * @param url    URL to send request to
 * @param params map of parameter values to be passed
 * @param header values to include in the request header
 * @return response returned from request, null if an error occurred
 */
private String doPostRequest(String url, NameValuePair[] params, Map<String, String> header,
        StringRequestEntity entity) {

    String fileContent = null;

    HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT);
    PostMethod httpPost = new PostMethod(url);
    httpPost.setFollowRedirects(false);
    httpPost.addParameters(params);
    try {
        if (entity != null) {
            httpPost.setRequestEntity(entity);
        }
        if (header != null) {
            String name;
            for (Iterator<String> iter = header.keySet().iterator(); iter.hasNext();) {
                name = iter.next();
                httpPost.addRequestHeader(name, header.get(name));
            }
        }
        httpPost.setURI(new URI(url, false));
        int resp = client.executeMethod(httpPost);
        if (resp < 400) {
            fileContent = httpPost.getResponseBodyAsString();
        }
    } catch (IOException e) {
        fileContent = null;
    }
    httpPost.releaseConnection();

    return fileContent;

}

From source file:org.wso2.appfactory.dynamicslave.JenkinsClient.java

private PostMethod createPost(String urlFragment, NameValuePair[] queryParameters, RequestEntity requestEntity,
        NameValuePair[] postParameters) {

    PostMethod post = new PostMethod(this.url + urlFragment);

    post.setDoAuthentication(true);/*from  w  ww . j a  va2 s.  c o m*/

    if (queryParameters != null) {
        post.setQueryString(queryParameters);
    }

    if (requestEntity != null) {
        post.setRequestEntity(requestEntity);
    }

    if (postParameters != null) {
        post.addParameters(postParameters);
    }

    return post;
}