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.craftercms.cstudio.impl.service.deployment.PublishingManagerImpl.java

@Override
public long setTargetVersion(PublishingTargetItem target, long newVersion, String site) {
    long resoponseVersion = -1;
    if (target.getVersionUrl() != null && !target.getVersionUrl().isEmpty()) {
        LOGGER.debug("Set deployment agent version for target {0}", target.getName());
        URL versionUrl = null;//from w w w.  jav  a 2 s  .c  om
        try {
            versionUrl = new URL(target.getVersionUrl());
        } catch (MalformedURLException e) {
            LOGGER.error("Invalid set version URL for target [%s]", target.getName());
            return resoponseVersion;
        }
        PostMethod postMethod = null;
        HttpClient client = null;
        try {
            postMethod = new PostMethod(target.getVersionUrl());
            postMethod.addParameter(TARGET_REQUEST_PARAMETER, target.getTarget());
            postMethod.addParameter(VERSION_REQUEST_PARAMETER, String.valueOf(newVersion));
            String siteId = target.getSiteId();
            if (StringUtils.isEmpty(siteId)) {
                siteId = site;
            }
            postMethod.addParameter(SITE_REQUEST_PARAMETER, site);
            client = new HttpClient();
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                String responseText = postMethod.getResponseBodyAsString();
                if (responseText != null && !responseText.isEmpty()) {
                    resoponseVersion = Long.parseLong(responseText);
                } else {
                    resoponseVersion = 0;
                }
            }

        } catch (Exception e) {
            LOGGER.error(
                    "Target {0} responded with error while setting target version. Set version failed for url {1}",
                    target.getName(), target.getVersionUrl());

        } finally {
            if (client != null) {
                HttpConnectionManager mgr = client.getHttpConnectionManager();
                if (mgr instanceof SimpleHttpConnectionManager) {
                    ((SimpleHttpConnectionManager) mgr).shutdown();
                }
            }
            if (postMethod != null) {
                postMethod.releaseConnection();
            }
            postMethod = null;
            client = null;

        }
    }
    return resoponseVersion;
}

From source file:org.craftercms.studio.impl.v1.service.deployment.PublishingManagerImpl.java

@Override
public long setTargetVersion(DeploymentEndpointConfigTO target, long newVersion, String site) {
    long resoponseVersion = -1;
    if (target.getVersionUrl() != null && !target.getVersionUrl().isEmpty()) {
        LOGGER.debug("Set deployment agent version for target {0}", target.getName());
        URL versionUrl = null;//from  w w  w.  j  a v  a2 s  . c  o  m
        try {
            versionUrl = new URL(target.getVersionUrl());
        } catch (MalformedURLException e) {
            LOGGER.error("Invalid set version URL for target [%s]", target.getName());
            return resoponseVersion;
        }
        PostMethod postMethod = null;
        HttpClient client = null;
        try {
            postMethod = new PostMethod(target.getVersionUrl());
            postMethod.addParameter(TARGET_REQUEST_PARAMETER, target.getTarget());
            postMethod.addParameter(VERSION_REQUEST_PARAMETER, String.valueOf(newVersion));
            String siteId = target.getSiteId();
            if (StringUtils.isEmpty(siteId)) {
                siteId = site;
            }
            postMethod.addParameter(SITE_REQUEST_PARAMETER, site);
            client = new HttpClient();
            int status = client.executeMethod(postMethod);
            if (status == HttpStatus.SC_OK) {
                InputStream responseStream = postMethod.getResponseBodyAsStream();
                String responseText = IOUtils.toString(responseStream);
                if (responseText != null && !responseText.isEmpty()) {
                    resoponseVersion = Long.parseLong(responseText);
                } else {
                    resoponseVersion = 0;
                }
            }

        } catch (Exception e) {
            LOGGER.error(
                    "Target {0} responded with error while setting target version. Set version failed for url {1}",
                    target.getName(), target.getVersionUrl());

        } finally {
            if (client != null) {
                HttpConnectionManager mgr = client.getHttpConnectionManager();
                if (mgr instanceof SimpleHttpConnectionManager) {
                    ((SimpleHttpConnectionManager) mgr).shutdown();
                }
            }
            if (postMethod != null) {
                postMethod.releaseConnection();
            }
            postMethod = null;
            client = null;

        }
    }
    return resoponseVersion;
}

From source file:org.dbpedia.spotlight.web.client.RestApiTests.java

private static String http_client(String text, String url, String disambiguator) {
    System.out.println("*** HTTP Client");
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
    method.setRequestHeader("Accept", "application/json");
    method.addParameter("disambiguator", disambiguator);
    method.addParameter("confidence", "-1");
    method.addParameter("support", "-1");
    method.addParameter("text", text);

    // Send POST request
    StringBuffer jsonString = new StringBuffer();
    int statusCode;
    try {/*from  w  w  w  . j ava 2 s .c o  m*/
        statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        InputStream rstream = null;
        rstream = method.getResponseBodyAsStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
        String line;

        while ((line = br.readLine()) != null) {
            jsonString.append(line);
            jsonString.append("\n");
        }
        System.out.println(jsonString);
        br.close();
    } catch (HttpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonString.toString();
}

From source file:org.deri.pipes.utils.HttpResponseCache.java

private static HttpResponseData getDataFromRequest(HttpClient client, String location,
        Map<String, String> requestHeaders) throws IOException, HttpException {
    HttpMethodBase method = new GetMethod(location);
    method.setFollowRedirects(true);/*from  w  w  w.  ja  v a  2 s  .  c  om*/
    try {
        if (location.length() > 2000 && location.indexOf('?') >= 0) {
            logger.info("Using post method because request location is very long");
            PostMethod postMethod = new PostMethod(location.substring(0, location.indexOf('?')));
            String urlDecoded = URLDecoder.decode(location.substring(location.indexOf('?') + 1), "UTF-8");
            String[] parts = urlDecoded.split("\\&");
            for (String part : parts) {
                String[] keyval = part.split("=", 2);
                if (keyval.length == 2) {
                    postMethod.addParameter(keyval[0], keyval[1]);
                } else {
                    postMethod.addParameter(keyval[0], "");
                }
            }
            method = postMethod;
        }
        addRequestHeaders(method, requestHeaders);
        int response = client.executeMethod(method);
        HttpResponseData data = new HttpResponseData();
        setExpires(data, method);
        data.setResponse(response);
        data.setCharSet(method.getResponseCharSet());
        Header lastModifiedHeader = method.getResponseHeader(HEADER_LAST_MODIFIED);
        if (lastModifiedHeader != null) {
            data.setLastModified(lastModifiedHeader.getValue());
        }
        Header contentTypeHeader = method.getResponseHeader(HEADER_CONTENT_TYPE);
        if (contentTypeHeader != null) {
            data.setContentType(contentTypeHeader.getValue());
        }
        data.setBody(method.getResponseBody(MAX_CONTENT_SIZE));

        return data;
    } finally {
        method.releaseConnection();
    }
}

From source file:org.easyj.http.RESTHttpClient.java

/**
 * Sets parameters map on actual method implementation for execution.
 * For GET, TRACE, HEAD, OPTIONS it appends the parameters to que query string.
 *///  w  ww.  j  a  va2s  .c  o m
protected void setMethodParameters() {
    if (method instanceof PostMethod) {
        PostMethod post = (PostMethod) method;
        Object param;
        for (String headerName : parameters.keySet()) {
            param = parameters.get(headerName);
            if (param != null) {
                post.addParameter(headerName, param.toString());
            }
        }
    } else if (method instanceof PutMethod) {
        PutMethod put = (PutMethod) method;
        try {
            put.setRequestEntity(new StringRequestEntity(toQueryString(parameters), null, null));
        } catch (UnsupportedEncodingException ex) {
        }
    } else {
        setQueryString(toQueryString(parameters), true);
        setMethodQueryString();
    }
}

From source file:org.eclipse.mylyn.internal.jira.core.service.web.JiraWebClient.java

public void addCommentToIssue(final JiraIssue issue, final String comment, IProgressMonitor monitor)
        throws JiraException {
    doInSession(monitor, new JiraWebSessionCallback() {
        @Override/*from w w  w.  j  a va2 s .co  m*/
        public void run(JiraClient client, String baseUrl, IProgressMonitor monitor) throws JiraException {
            StringBuilder rssUrlBuffer = new StringBuilder(baseUrl);
            rssUrlBuffer.append("/secure/AddComment.jspa"); //$NON-NLS-1$

            PostMethod post = new PostMethod(rssUrlBuffer.toString());
            post.setRequestHeader("Content-Type", getContentType(monitor)); //$NON-NLS-1$
            post.addParameter("comment", comment); //$NON-NLS-1$
            post.addParameter("commentLevel", ""); //$NON-NLS-1$ //$NON-NLS-2$
            post.addParameter("id", issue.getId()); //$NON-NLS-1$

            try {
                execute(post);
                if (!expectRedirect(post, issue)) {
                    handleErrorMessage(post);
                }
            } finally {
                post.releaseConnection();
            }
        }

    });
}

From source file:org.eclipse.mylyn.internal.jira.core.service.web.JiraWebClient.java

public void updateIssue(final JiraIssue issue, final String comment, IProgressMonitor monitor)
        throws JiraException {
    doInSession(monitor, new JiraWebSessionCallback() {
        @Override/*  w w  w.  jav a 2s.  c o m*/
        public void run(JiraClient client, String baseUrl, IProgressMonitor monitor) throws JiraException {
            StringBuilder rssUrlBuffer = new StringBuilder(baseUrl);
            rssUrlBuffer.append("/secure/EditIssue.jspa"); //$NON-NLS-1$

            PostMethod post = new PostMethod(rssUrlBuffer.toString());
            post.setRequestHeader("Content-Type", getContentType(monitor)); //$NON-NLS-1$
            post.addParameter("summary", issue.getSummary()); //$NON-NLS-1$
            post.addParameter("issuetype", issue.getType().getId()); //$NON-NLS-1$
            if (issue.getPriority() != null) {
                post.addParameter("priority", issue.getPriority().getId()); //$NON-NLS-1$
            }
            addFields(client, issue, post, new String[] { "duedate" }); //$NON-NLS-1$
            post.addParameter("timetracking", Long.toString(issue.getEstimate() / 60) + "m"); //$NON-NLS-1$ //$NON-NLS-2$

            Component[] components = issue.getComponents();
            if (components != null) {
                if (components.length == 0) {
                    post.addParameter("components", "-1"); //$NON-NLS-1$ //$NON-NLS-2$
                } else {
                    for (Component component : components) {
                        post.addParameter("components", component.getId()); //$NON-NLS-1$
                    }
                }
            }

            Version[] versions = issue.getReportedVersions();
            if (versions != null) {
                if (versions.length == 0) {
                    post.addParameter("versions", "-1"); //$NON-NLS-1$ //$NON-NLS-2$
                } else {
                    for (Version version : versions) {
                        post.addParameter("versions", version.getId()); //$NON-NLS-1$
                    }
                }
            }

            Version[] fixVersions = issue.getFixVersions();
            if (fixVersions != null) {
                if (fixVersions.length == 0) {
                    post.addParameter("fixVersions", "-1"); //$NON-NLS-1$ //$NON-NLS-2$
                } else {
                    for (Version fixVersion : fixVersions) {
                        post.addParameter("fixVersions", fixVersion.getId()); //$NON-NLS-1$
                    }
                }
            }

            // TODO need to be able to choose unassigned and automatic
            if (issue.getAssignee() != null) {
                post.addParameter("assignee", issue.getAssignee()); //$NON-NLS-1$
            } else {
                post.addParameter("assignee", "-1"); //$NON-NLS-1$ //$NON-NLS-2$
            }
            if (issue.getReporter() != null) {
                post.addParameter("reporter", issue.getReporter()); //$NON-NLS-1$
            }
            if (issue.getEnvironment() != null) {
                post.addParameter("environment", issue.getEnvironment()); //$NON-NLS-1$
            }
            post.addParameter("description", issue.getDescription()); //$NON-NLS-1$

            if (comment != null) {
                post.addParameter("comment", comment); //$NON-NLS-1$
            }
            post.addParameter("commentLevel", ""); //$NON-NLS-1$ //$NON-NLS-2$
            post.addParameter("id", issue.getId()); //$NON-NLS-1$

            if (issue.getSecurityLevel() != null) {
                post.addParameter("security", issue.getSecurityLevel().getId()); //$NON-NLS-1$
            }

            addCustomFields(client, issue, post);

            try {
                execute(post);
                if (!expectRedirect(post, issue)) {
                    handleErrorMessage(post);
                }
            } finally {
                post.releaseConnection();
            }
        }

    });
}

From source file:org.eclipse.mylyn.internal.jira.core.service.web.JiraWebClient.java

public void assignIssueTo(final JiraIssue issue, final int assigneeType, final String user,
        final String comment, IProgressMonitor monitor) throws JiraException {
    doInSession(monitor, new JiraWebSessionCallback() {
        @Override//  ww  w. j  a  v  a  2s.  com
        public void run(JiraClient server, String baseUrl, IProgressMonitor monitor) throws JiraException {
            StringBuilder rssUrlBuffer = new StringBuilder(baseUrl);
            rssUrlBuffer.append("/secure/AssignIssue.jspa"); //$NON-NLS-1$

            PostMethod post = new PostMethod(rssUrlBuffer.toString());
            post.setRequestHeader("Content-Type", getContentType(monitor)); //$NON-NLS-1$

            post.addParameter("assignee", getAssigneeParam(server, issue, assigneeType, user)); //$NON-NLS-1$

            if (comment != null) {
                post.addParameter("comment", comment); //$NON-NLS-1$
            }
            post.addParameter("commentLevel", ""); //$NON-NLS-1$ //$NON-NLS-2$
            post.addParameter("id", issue.getId()); //$NON-NLS-1$

            try {
                execute(post);
                if (!expectRedirect(post, issue)) {
                    handleErrorMessage(post);
                }
            } finally {
                post.releaseConnection();
            }
        }

    });
}

From source file:org.eclipse.mylyn.internal.jira.core.service.web.JiraWebClient.java

public void advanceIssueWorkflow(final JiraIssue issue, final String actionKey, final String comment,
        final String[] fields, IProgressMonitor monitor) throws JiraException {
    doInSession(monitor, new JiraWebSessionCallback() {
        @Override//from   w w w  . j a v  a2 s  .c  o  m
        public void run(JiraClient server, String baseUrl, IProgressMonitor monitor) throws JiraException {
            PostMethod post = new PostMethod(baseUrl + "/secure/CommentAssignIssue.jspa"); //$NON-NLS-1$
            post.setRequestHeader("Content-Type", getContentType(monitor)); //$NON-NLS-1$

            post.addParameter("id", issue.getId()); //$NON-NLS-1$
            post.addParameter("action", actionKey); //$NON-NLS-1$
            // method.addParameter("assignee", issue.getAssignee());

            if (comment != null) {
                post.addParameter("comment", comment); //$NON-NLS-1$
            }
            post.addParameter("commentLevel", ""); //$NON-NLS-1$ //$NON-NLS-2$

            addFields(server, issue, post, fields);

            try {
                execute(post);
                if (!expectRedirect(post, issue)) {
                    handleErrorMessage(post);
                }
            } finally {
                post.releaseConnection();
            }
        }
    });
}

From source file:org.eclipse.mylyn.internal.jira.core.service.web.JiraWebClient.java

private String createIssue(final String url, final JiraIssue issue, IProgressMonitor monitor)
        throws JiraException {
    final String[] issueKey = new String[1];
    doInSession(monitor, new JiraWebSessionCallback() {
        @Override/*ww  w .ja  va  2  s . c om*/
        public void run(JiraClient client, String baseUrl, IProgressMonitor monitor) throws JiraException {
            StringBuilder attachFileURLBuffer = new StringBuilder(baseUrl);
            attachFileURLBuffer.append(url);

            PostMethod post = new PostMethod(attachFileURLBuffer.toString());
            post.setRequestHeader("Content-Type", getContentType(monitor)); //$NON-NLS-1$

            post.addParameter("pid", issue.getProject().getId()); //$NON-NLS-1$
            post.addParameter("issuetype", issue.getType().getId()); //$NON-NLS-1$
            post.addParameter("summary", issue.getSummary()); //$NON-NLS-1$
            if (issue.getPriority() != null) {
                post.addParameter("priority", issue.getPriority().getId()); //$NON-NLS-1$
            }
            addFields(client, issue, post, new String[] { "duedate" }); //$NON-NLS-1$
            post.addParameter("timetracking", Long.toString(issue.getEstimate() / 60) + "m"); //$NON-NLS-1$ //$NON-NLS-2$

            if (issue.getComponents() != null) {
                for (int i = 0; i < issue.getComponents().length; i++) {
                    post.addParameter("components", issue.getComponents()[i].getId()); //$NON-NLS-1$
                }
            } else {
                post.addParameter("components", "-1"); //$NON-NLS-1$ //$NON-NLS-2$
            }

            if (issue.getReportedVersions() != null) {
                for (int i = 0; i < issue.getReportedVersions().length; i++) {
                    post.addParameter("versions", issue.getReportedVersions()[i].getId()); //$NON-NLS-1$
                }
            } else {
                post.addParameter("versions", "-1"); //$NON-NLS-1$ //$NON-NLS-2$
            }

            if (issue.getFixVersions() != null) {
                for (int i = 0; i < issue.getFixVersions().length; i++) {
                    post.addParameter("fixVersions", issue.getFixVersions()[i].getId()); //$NON-NLS-1$
                }
            } else {
                post.addParameter("fixVersions", "-1"); //$NON-NLS-1$ //$NON-NLS-2$
            }

            if (issue.getAssignee() == null) {
                post.addParameter("assignee", "-1"); // Default assignee //$NON-NLS-1$ //$NON-NLS-2$
            } else if (issue.getAssignee().length() == 0) {
                post.addParameter("assignee", ""); // nobody //$NON-NLS-1$ //$NON-NLS-2$
            } else {
                post.addParameter("assignee", issue.getAssignee()); //$NON-NLS-1$
            }

            post.addParameter("reporter", client.getUserName()); //$NON-NLS-1$

            post.addParameter("environment", issue.getEnvironment() != null ? issue.getEnvironment() : ""); //$NON-NLS-1$ //$NON-NLS-2$
            post.addParameter("description", issue.getDescription() != null ? issue.getDescription() : ""); //$NON-NLS-1$ //$NON-NLS-2$

            if (issue.getParentId() != null) {
                post.addParameter("parentIssueId", issue.getParentId()); //$NON-NLS-1$
            }

            if (issue.getSecurityLevel() != null) {
                post.addParameter("security", issue.getSecurityLevel().getId()); //$NON-NLS-1$
            }

            addCustomFields(client, issue, post);

            try {
                execute(post);
                if (!expectRedirect(post, "/browse/", false)) { //$NON-NLS-1$
                    handleErrorMessage(post);
                } else {
                    final Header locationHeader = post.getResponseHeader("location"); //$NON-NLS-1$
                    // parse issue key from issue URL 
                    String location = locationHeader.getValue();
                    int i = location.lastIndexOf("/"); //$NON-NLS-1$
                    if (i != -1) {
                        issueKey[0] = location.substring(i + 1);
                    } else {
                        throw new JiraException(
                                "The server redirected to an unexpected location while creating an issue: " //$NON-NLS-1$
                                        + location);
                    }
                }
            } finally {
                post.releaseConnection();
            }
        }
    });
    assert issueKey[0] != null;
    return issueKey[0];
}