Example usage for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity

List of usage examples for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity

Introduction

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

Prototype

public StringRequestEntity(String paramString1, String paramString2, String paramString3)
  throws UnsupportedEncodingException

Source Link

Usage

From source file:org.alfresco.rest.api.tests.client.AuthenticatedHttp.java

/**
 * Adds the JSON as request-body the the method and sets the correct
 * content-type./*from  ww w .ja  va  2 s .  c  o  m*/
 * @param method EntityEnclosingMethod
 * @param object JSONObject
 */
private void populateRequestBody(EntityEnclosingMethod method, JSONObject object) {
    try {
        method.setRequestEntity(new StringRequestEntity(object.toJSONString(), MIME_TYPE_JSON, "UTF-8"));
    } catch (UnsupportedEncodingException error) {
        // This will never happen!
        throw new RuntimeException("All hell broke loose, a JVM that doesn't have UTF-8 encoding...");
    }
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse post(final Class<?> c, final RequestContext rq, final Object entityId,
        final Object relationshipEntityId, final String body) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, null);
    String url = endpoint.getUrl();

    PostMethod req = new PostMethod(url.toString());
    if (body != null) {
        StringRequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        req.setRequestEntity(requestEntity);
    }//from ww w .  ja  va 2s . c om
    return submitRequest(req, rq);
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse post(final RequestContext rq, final String urlSuffix, String body) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), urlSuffix, null);
    String url = endpoint.getUrl();

    PostMethod req = new PostMethod(url.toString());
    if (body != null) {
        StringRequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        req.setRequestEntity(requestEntity);
    }/*from  www  .j  a  v a 2 s .com*/
    return submitRequest(req, rq);
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse post(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation,
        final String body) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation,
            null);//from   w  w  w  . ja  va 2s . c o  m
    String url = endpoint.getUrl();

    PostMethod req = new PostMethod(url.toString());
    if (body != null) {
        StringRequestEntity requestEntity = null;
        if (cmisBinding.equals(Binding.atom)) {
            requestEntity = new StringRequestEntity(body, "text/xml", "UTF-8");
        } else if (cmisBinding.equals(Binding.browser)) {
            requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        }
        req.setRequestEntity(requestEntity);
    }
    return submitRequest(req, rq);
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse put(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation,
        final String body) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation,
            null);//from  www  .  j  a v  a 2  s  .c  o  m
    String url = endpoint.getUrl();

    PutMethod req = new PutMethod(url.toString());
    if (body != null) {
        StringRequestEntity requestEntity = null;
        if (cmisBinding.equals(Binding.atom)) {
            requestEntity = new StringRequestEntity(body, "text/xml", "UTF-8");
        } else if (cmisBinding.equals(Binding.browser)) {
            requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        }
        req.setRequestEntity(requestEntity);
    }
    return submitRequest(req, rq);
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse post(final RequestContext rq, final String scope, final int version,
        final String entityCollectionName, final Object entityId, final String relationCollectionName,
        final Object relationshipEntityId, final String body, String contentType) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName,
            entityId, relationCollectionName, relationshipEntityId, null);
    String url = endpoint.getUrl();

    PostMethod req = new PostMethod(url.toString());
    if (body != null) {
        if (contentType == null || contentType.isEmpty()) {
            contentType = "application/json";
        }/*from  w  w w  . j  a v  a2 s  .co m*/
        StringRequestEntity requestEntity = new StringRequestEntity(body, contentType, "UTF-8");
        req.setRequestEntity(requestEntity);
    }
    return submitRequest(req, rq);
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse put(final Class<?> c, final RequestContext rq, final Object entityId,
        final Object relationshipEntityId, final String body) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, null);
    String url = endpoint.getUrl();

    PutMethod req = new PutMethod(url);
    if (body != null) {
        StringRequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        req.setRequestEntity(requestEntity);
    }//from  ww  w .  ja  v a 2s  .co  m
    return submitRequest(req, rq);
}

From source file:org.alfresco.rest.api.tests.client.PublicApiHttpClient.java

public HttpResponse put(final RequestContext rq, final String scope, final int version,
        final String entityCollectionName, final Object entityId, final String relationCollectionName,
        final Object relationshipEntityId, final String body, Map<String, String> params) throws IOException {
    RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName,
            entityId, relationCollectionName, relationshipEntityId, params);
    String url = endpoint.getUrl();

    PutMethod req = new PutMethod(url);
    if (body != null) {
        StringRequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
        req.setRequestEntity(requestEntity);
    }/*from   w  w w.  j  ava 2  s  . co m*/
    return submitRequest(req, rq);
}

From source file:org.ambraproject.service.article.AIArticleClassifier.java

/**
 * Queries the MAI server for taxonomic terms for a given article, and returns a list
 * of the raw results.// w w  w  . ja v a  2 s. c  o m
 *
 * @param articleXml DOM of the article to categorize
 * @return List of results from the server.  This will consist of raw XML fragments, and
 *     include things like counts that we don't currently store in mysql.
 * @throws Exception
 */
private List<String> getRawTerms(Document articleXml) throws Exception {
    String toCategorize = getCategorizationContent(articleXml);
    String aiMessage = String.format(MESSAGE_BEGIN, thesaurus) + toCategorize + MESSAGE_END;
    PostMethod post = new PostMethod(serviceUrl);
    post.setRequestEntity(new StringRequestEntity(aiMessage, "application/xml", "UTF-8"));
    httpClient.executeMethod(post);
    Document response = DocumentBuilderFactoryCreator.createFactory().newDocumentBuilder()
            .parse(post.getResponseBodyAsStream());

    //parse result
    NodeList vectorElements = response.getElementsByTagName("VectorElement");
    List<String> results = new ArrayList<String>(vectorElements.getLength());
    //The first and last elements of the vector response are just MAITERMS
    for (int i = 1; i < vectorElements.getLength() - 1; i++) {
        results.add(vectorElements.item(i).getTextContent());
    }
    return results;
}

From source file:org.apache.abdera.ext.gdata.GoogleLoginAuthScheme.java

protected String getAuth(String id, String pwd, String service) {
    try {/*from  w  ww.j  a v a  2 s  . com*/
        AbderaClient abderaClient = new AbderaClient();
        Formatter f = new Formatter();
        f.format("Email=%s&Passwd=%s&service=%s&source=%s", URLEncoder.encode(id, "utf-8"),
                URLEncoder.encode(pwd, "utf-8"), (service != null) ? URLEncoder.encode(service, "utf-8") : "",
                URLEncoder.encode(Version.APP_NAME, "utf-8"));
        StringRequestEntity stringreq = new StringRequestEntity(f.toString(),
                "application/x-www-form-urlencoded", "utf-8");
        String uri = "https://www.google.com/accounts/ClientLogin";
        RequestOptions options = abderaClient.getDefaultRequestOptions();
        options.setContentType("application/x-www-form-urlencoded");
        ClientResponse response = abderaClient.post(uri, stringreq, options);
        InputStream in = response.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int n = -1;
        while ((n = in.read()) != -1) {
            out.write(n);
        }
        out.flush();
        response.release();
        String auth = new String(out.toByteArray());
        return auth.split("\n")[2].replaceAll("Auth=", "auth=");
    } catch (Exception e) {
    }
    return null;
}