Example usage for org.apache.commons.httpclient NameValuePair NameValuePair

List of usage examples for org.apache.commons.httpclient NameValuePair NameValuePair

Introduction

In this page you can find the example usage for org.apache.commons.httpclient NameValuePair NameValuePair.

Prototype

public NameValuePair(String name, String value) 

Source Link

Document

Constructor.

Usage

From source file:com.mengka.diamond.sdkapi.impl.DiamondSDKManagerImpl.java

public BatchContextResult<ConfigInfoEx> batchAddOrUpdate(String serverId, String groupName,
        Map<String, String> dataId2ContentMap) {
    // // w  w  w .ja v a2 s .c om
    BatchContextResult<ConfigInfoEx> response = new BatchContextResult<ConfigInfoEx>();

    // map?null
    if (dataId2ContentMap == null) {
        log.error("dataId2ContentMap cannot be null, serverId=" + serverId + " ,group=" + groupName);
        response.setSuccess(false);
        response.setStatusMsg("dataId2ContentMap cannot be null");
        return response;
    }

    // dataIdcontentmap????
    StringBuilder allDataIdAndContentBuilder = new StringBuilder();
    for (String dataId : dataId2ContentMap.keySet()) {
        String content = dataId2ContentMap.get(dataId);
        allDataIdAndContentBuilder.append(dataId + Constants.WORD_SEPARATOR + content)
                .append(Constants.LINE_SEPARATOR);
    }
    String allDataIdAndContent = allDataIdAndContentBuilder.toString();

    // 
    if (!login(serverId)) {
        response.setSuccess(false);
        response.setStatusMsg("login fail, serverId=" + serverId);
        return response;
    }

    // HTTP method
    PostMethod post = new PostMethod("/diamond-server/admin.do?method=batchAddOrUpdate");
    // 
    post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
    try {
        // ?
        NameValuePair dataId_value = new NameValuePair("allDataIdAndContent", allDataIdAndContent);
        NameValuePair group_value = new NameValuePair("group", groupName);

        post.setRequestBody(new NameValuePair[] { dataId_value, group_value });

        // http??
        int status = client.executeMethod(post);
        response.setStatusCode(status);
        String responseMsg = post.getResponseBodyAsString();
        response.setResponseMsg(responseMsg);

        if (status == HttpStatus.SC_OK) {
            String json = null;
            try {
                json = responseMsg;

                // ???json, ??BatchContextResult
                List<ConfigInfoEx> configInfoExList = new LinkedList<ConfigInfoEx>();
                Object resultObj = JSONUtils.deserializeObject(json, new TypeReference<List<ConfigInfoEx>>() {
                });
                if (!(resultObj instanceof List<?>)) {
                    throw new RuntimeException("batch write deserialize type error, not list, json=" + json);
                }
                List<ConfigInfoEx> resultList = (List<ConfigInfoEx>) resultObj;
                for (ConfigInfoEx configInfoEx : resultList) {
                    configInfoExList.add(configInfoEx);
                }
                response.getResult().addAll(configInfoExList);
                // ????, ???
                response.setStatusMsg("batch write success");
                log.info("batch write success,serverId=" + serverId + ",allDataIdAndContent="
                        + allDataIdAndContent + ",group=" + groupName + ",json=" + json);
            } catch (Exception e) {
                response.setSuccess(false);
                response.setStatusMsg("batch write deserialize error");
                log.error("batch write deserialize error, serverId=" + serverId + ",allDataIdAndContent="
                        + allDataIdAndContent + ",group=" + groupName + ",json=" + json, e);
            }
        } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
            response.setSuccess(false);
            response.setStatusMsg("batch write timeout, socket timeout(ms):" + require_timeout);
            log.error("batch write timeout, socket timeout(ms):" + require_timeout + ", serverId=" + serverId
                    + ",allDataIdAndContent=" + allDataIdAndContent + ",group=" + groupName);
        } else {
            response.setSuccess(false);
            response.setStatusMsg("batch write fail, status:" + status);
            log.error("batch write fail, status:" + status + ", response:" + responseMsg + ",serverId="
                    + serverId + ",allDataIdAndContent=" + allDataIdAndContent + ",group=" + groupName);
        }
    } catch (HttpException e) {
        response.setSuccess(false);
        response.setStatusMsg("batch write http exception" + e.getMessage());
        log.error("batch write http exception, serverId=" + serverId + ",allDataIdAndContent="
                + allDataIdAndContent + ",group=" + groupName, e);
    } catch (IOException e) {
        response.setSuccess(false);
        response.setStatusMsg("batch write io exception" + e.getMessage());
        log.error("batch write io exception, serverId=" + serverId + ",allDataIdAndContent="
                + allDataIdAndContent + ",group=" + groupName, e);
    } finally {
        // ?
        post.releaseConnection();
    }

    return response;
}

From source file:cn.leancloud.diamond.sdkapi.impl.DiamondSDKManagerImpl.java

@Override
public BatchContextResult<ConfigInfoEx> batchAddOrUpdate(String serverId, String groupName,
        Map<String, String> dataId2ContentMap) {
    // /*from w  w  w  .  j  a  v  a2 s.  co m*/
    BatchContextResult<ConfigInfoEx> response = new BatchContextResult<ConfigInfoEx>();

    // map?null
    if (dataId2ContentMap == null) {
        log.error("dataId2ContentMap cannot be null, serverId=" + serverId + " ,group=" + groupName);
        response.setSuccess(false);
        response.setStatusMsg("dataId2ContentMap cannot be null");
        return response;
    }

    // dataIdcontentmap????
    StringBuilder allDataIdAndContentBuilder = new StringBuilder();
    for (String dataId : dataId2ContentMap.keySet()) {
        String content = dataId2ContentMap.get(dataId);
        allDataIdAndContentBuilder.append(dataId + Constants.WORD_SEPARATOR + content)
                .append(Constants.LINE_SEPARATOR);
    }
    String allDataIdAndContent = allDataIdAndContentBuilder.toString();

    // 
    if (!login(serverId)) {
        response.setSuccess(false);
        response.setStatusMsg("login fail, serverId=" + serverId);
        return response;
    }

    // HTTP method
    PostMethod post = new PostMethod("/diamond-server/admin.do?method=batchAddOrUpdate");
    // 
    post.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, require_timeout);
    try {
        // ?
        NameValuePair dataId_value = new NameValuePair("allDataIdAndContent", allDataIdAndContent);
        NameValuePair group_value = new NameValuePair("group", groupName);

        post.setRequestBody(new NameValuePair[] { dataId_value, group_value });

        // http??
        int status = client.executeMethod(post);
        response.setStatusCode(status);
        String responseMsg = post.getResponseBodyAsString();
        response.setResponseMsg(responseMsg);

        if (status == HttpStatus.SC_OK) {
            String json = null;
            try {
                json = responseMsg;

                // ???json, ??BatchContextResult
                List<ConfigInfoEx> configInfoExList = new LinkedList<ConfigInfoEx>();
                Object resultObj = JSONUtils.deserializeObject(json, new TypeReference<List<ConfigInfoEx>>() {
                });
                if (!(resultObj instanceof List<?>)) {
                    throw new RuntimeException("batch write deserialize type error, not list, json=" + json);
                }
                List<ConfigInfoEx> resultList = (List<ConfigInfoEx>) resultObj;
                for (ConfigInfoEx configInfoEx : resultList) {
                    configInfoExList.add(configInfoEx);
                }
                response.getResult().addAll(configInfoExList);
                // ????, ???
                response.setStatusMsg("batch write success");
                log.info("batch write success,serverId=" + serverId + ",allDataIdAndContent="
                        + allDataIdAndContent + ",group=" + groupName + ",json=" + json);
            } catch (Exception e) {
                response.setSuccess(false);
                response.setStatusMsg("batch write deserialize error");
                log.error("batch write deserialize error, serverId=" + serverId + ",allDataIdAndContent="
                        + allDataIdAndContent + ",group=" + groupName + ",json=" + json, e);
            }
        } else if (status == HttpStatus.SC_REQUEST_TIMEOUT) {
            response.setSuccess(false);
            response.setStatusMsg("batch write timeout, socket timeout(ms):" + require_timeout);
            log.error("batch write timeout, socket timeout(ms):" + require_timeout + ", serverId=" + serverId
                    + ",allDataIdAndContent=" + allDataIdAndContent + ",group=" + groupName);
        } else {
            response.setSuccess(false);
            response.setStatusMsg("batch write fail, status:" + status);
            log.error("batch write fail, status:" + status + ", response:" + responseMsg + ",serverId="
                    + serverId + ",allDataIdAndContent=" + allDataIdAndContent + ",group=" + groupName);
        }
    } catch (HttpException e) {
        response.setSuccess(false);
        response.setStatusMsg("batch write http exception" + e.getMessage());
        log.error("batch write http exception, serverId=" + serverId + ",allDataIdAndContent="
                + allDataIdAndContent + ",group=" + groupName, e);
    } catch (IOException e) {
        response.setSuccess(false);
        response.setStatusMsg("batch write io exception" + e.getMessage());
        log.error("batch write io exception, serverId=" + serverId + ",allDataIdAndContent="
                + allDataIdAndContent + ",group=" + groupName, e);
    } finally {
        // ?
        post.releaseConnection();
    }

    return response;
}

From source file:com.ixora.rms.ui.RMSFrame.java

/**
 * Overridden to change the HTTP request parameters.
 * @see com.ixora.common.ui.AppFrame#handleSendFeedback()
 *//*from   w  w  w.  java  2  s  .co m*/
@SuppressWarnings("serial")
protected void handleSendFeedback() {
    try {
        FeedbackDialog dlg = new FeedbackDialog(this, fFeedbackURL) {
            protected void postData(URL url, String email, String comment) throws HttpException, IOException {
                NetUtils.postHttpForm(url, new NameValuePair[] { new NameValuePair("entry.3.group", "General"),
                        new NameValuePair("entry.3.group.other_option_", "IxoraRMS feedback"),
                        new NameValuePair("entry.4.single", email),
                        new NameValuePair("entry.2.single", comment), new NameValuePair("pageNumber", "0"),
                        new NameValuePair("backupCache", ""), new NameValuePair("submit", "Submit") },
                        new NameValuePair[] { new NameValuePair("key", "tx9wMAl2ak8WS1EKazPYspg"),
                                new NameValuePair("ifq", "") });
            }

        };
        UIUtils.centerDialogAndShow(this, dlg);
    } catch (Exception e) {
        UIExceptionMgr.userException(e);
    }
}

From source file:edu.ku.brc.specify.tools.l10nios.StrLocalizerAppForiOS.java

/**
 * Creates an array of POST method parameters to send with the version checking / usage tracking connection.
 * //from  w  w w  .j a  va  2 s .  co m
 * @param doSendSecondaryStats if true, the POST parameters include usage stats
 * @return an array of POST parameters
 */
protected NameValuePair[] createPostParameters(final String inputStr, final String src) {
    Vector<NameValuePair> postParams = new Vector<NameValuePair>();
    try {
        // get the install ID
        // get the OS name and version
        postParams.add(new NameValuePair("doit", "done")); //$NON-NLS-1$
        postParams.add(new NameValuePair("ei", "UTF-8")); //$NON-NLS-1$
        postParams.add(new NameValuePair("lp", src)); //$NON-NLS-1$
        postParams.add(new NameValuePair("fr", "bf-home")); //$NON-NLS-1$

        postParams.add(new NameValuePair("intl", "1")); //$NON-NLS-1$
        postParams.add(new NameValuePair("tt", "urltext")); //$NON-NLS-1$
        postParams.add(new NameValuePair("trtext", inputStr)); //$NON-NLS-1$
        //postParams.add(new NameValuePair("wl_trglang",   dst)); //$NON-NLS-1$

        // create an array from the params
        NameValuePair[] paramArray = new NameValuePair[postParams.size()];
        for (int i = 0; i < paramArray.length; ++i) {
            paramArray[i] = postParams.get(i);
        }

        return paramArray;

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.sonyericsson.hudson.plugins.multislaveconfigplugin.UIHudsonTest.java

/**
 * Sets up a common {@link NodeProperty} for all slaves.
 * @throws Exception if something goes wrong
 *///  www  .j a  v  a  2  s.  co  m
private void setUpNodeProperties() throws Exception {
    //Takes the web client to "search for slaves"-page.
    clickLinkOnCurrentPage(CONFIGURE);

    searchForAndSelectAllSlaves();

    // Instead of requesting the page directly we create a WebRequestSettings object
    WebRequestSettings requestSettings = new WebRequestSettings(
            webClient.createCrumbedUrl(NodeManageLink.URL + "/apply"), HttpMethod.POST);

    // Then we set the request parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("json",
            "{\"addOrChangeProperties\": {\"env\": { \"key\": \"FOODPREF\",\"value\": "
                    + "\"BURGERS\"},\"stapler-class\": \"hudson.slaves.EnvironmentVariablesNodeProperty\",\"kind\": "
                    + "\"hudson.slaves.EnvironmentVariablesNodeProperty\"}}"));
    params.add(new NameValuePair("Submit", "Save"));

    requestSettings.setRequestParameters(params);

    webClient.getPage(requestSettings);
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public void pauseJob(String jobName, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    HttpMethod method = null;/* w w w  .  ja  v a2 s .  c  om*/
    try {
        Job j = this.getPersistedJob(jobName);
        if (this.getJobStatus(j.getName()).equals(CrawlStatus.RUNNING.toString())) {
            method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
            ((PostMethod) method).addParameter(new NameValuePair("action", "pause"));
            // TODO check status
            int status = this.httpClient.executeMethod(method);
            method.releaseConnection();
            j.setJobStage(CrawlStatus.PAUSED.toString());
            this.entityManager.merge(j);
            this.updateJobsList(currentUser);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (DocumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public void resumeJob(String jobName, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    HttpMethod method = null;//from   w w  w.  j a  v a 2 s  . c o m
    try {
        Job j = this.getPersistedJob(jobName);
        if (this.getJobStatus(j.getName()).equals(CrawlStatus.PAUSED.toString())) {
            method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
            ((PostMethod) method).addParameter(new NameValuePair("action", "unpause"));
            // TODO check status
            int status = this.httpClient.executeMethod(method);
            method.releaseConnection();
            j.setJobStage(CrawlStatus.RUNNING.toString());
            this.entityManager.merge(j);
            this.updateJobsList(currentUser);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (DocumentException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public void startJob(String jobName, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    HttpMethod method = null;/*w w  w.  jav a2 s  .  com*/
    try {
        Matcher m = CrawlerManager.childJobPattern.matcher(jobName);
        method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
        ((PostMethod) method).addParameter(new NameValuePair("action", "build"));
        // TODO check status
        int status = this.httpClient.executeMethod(method);
        Thread.sleep(3000);
        method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
        method.releaseConnection();
        ((PostMethod) method).addParameter(new NameValuePair("action", "launch"));
        // TODO check status
        status = this.httpClient.executeMethod(method);
        method.releaseConnection();
        this.updateJobsList(currentUser);
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Admin')}")
public String stopCrawlerEngine() throws HeritrixException {
    HttpMethod method = null;//from  w w  w. jav a 2s .c  o m
    try {
        String job = this.getJobsArray()[0];
        method = new PostMethod(this.engineUri + "job/" + job + "/script");
        // method.setFollowRedirects(true);
        ((PostMethod) method).addParameter(new NameValuePair("engine", "groovy"));
        ((PostMethod) method).addParameter(new NameValuePair("script", "System.exit(0);"));
        int status = this.httpClient.executeMethod(method);
        method.releaseConnection();
    } catch (HttpException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (XPathExpressionException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } catch (SAXException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return "OK";
}

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
public void stopJob(String jobName, User currentUser) throws HeritrixException {
    this.updateJobsList(currentUser);
    HttpMethod method = null;//from  ww  w  . ja v  a  2  s  .  c om
    try {
        method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(jobName, "UTF-8"));
        ((PostMethod) method).addParameter(new NameValuePair("action", "terminate"));
        // TODO check status
        int status = this.httpClient.executeMethod(method);
        method.releaseConnection();
        this.updateJobsList(currentUser);
    } catch (IOException e) {
        e.printStackTrace();
        throw new HeritrixException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}