Search in sources :

Example 16 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project ThinkAndroid by white-cat.

the class AsyncHttpClient method post.

/**
	 * Perform a HTTP POST request and track the Android Context which initiated
	 * the request. Set headers only for this request
	 * 
	 * @param context
	 *            the Android Context which initiated the request.
	 * @param url
	 *            the URL to send the request to.
	 * @param headers
	 *            set headers only for this request
	 * @param params
	 *            additional POST parameters to send with the request.
	 * @param contentType
	 *            the content type of the payload you are sending, for example
	 *            application/json if sending a json payload.
	 * @param responseHandler
	 *            the response handler instance that should handle the response.
	 */
public void post(Context context, String url, Header[] headers, RequestParams params, String contentType, AsyncHttpResponseHandler responseHandler) {
    HttpEntityEnclosingRequestBase request = new HttpPost(url);
    if (params != null)
        request.setEntity(paramsToEntity(params));
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Example 17 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project sling by apache.

the class RequestDocumentor method documentRequest.

protected void documentRequest(PrintWriter pw, RequestExecutor executor, String[] metadataArray) throws IOException {
    // Convert metadata to more convenient Map 
    final Map<String, String> m = new HashMap<String, String>();
    if (metadataArray.length % 2 != 0) {
        throw new IllegalArgumentException("Metadata array must be of even size, got " + metadataArray.length);
    }
    for (int i = 0; i < metadataArray.length; i += 2) {
        m.put(metadataArray[i], metadataArray[i + 1]);
    }
    // TODO use velocity or other templates? Just a rough prototype for now
    // Also need to filter overly long input/output, binary etc.
    pw.println();
    pw.println("====================================================================================");
    pw.print("=== ");
    pw.print(m.get("title"));
    pw.println(" ===");
    pw.println(m.get("description"));
    pw.print("\n=== ");
    pw.print("REQUEST");
    pw.println(" ===");
    pw.print("Method: ");
    pw.println(executor.getRequest().getMethod());
    pw.print("URI: ");
    pw.println(executor.getRequest().getURI());
    final Header[] allHeaders = executor.getRequest().getAllHeaders();
    if (allHeaders != null && allHeaders.length > 0) {
        pw.println("Headers:");
        for (Header h : allHeaders) {
            pw.print(h.getName());
            pw.print(":");
            pw.println(h.getValue());
        }
    }
    if (executor.getRequest() instanceof HttpEntityEnclosingRequestBase) {
        final HttpEntityEnclosingRequestBase heb = (HttpEntityEnclosingRequestBase) executor.getRequest();
        if (heb.getEntity() != null) {
            pw.print("Content-Type:");
            pw.println(heb.getEntity().getContentType().getValue());
            pw.println("Content:");
            final InputStream is = heb.getEntity().getContent();
            final byte[] buffer = new byte[16384];
            int count = 0;
            while ((count = is.read(buffer, 0, buffer.length)) > 0) {
                // TODO encoding??
                pw.write(new String(buffer, 0, count));
            }
            pw.println();
        }
    }
    pw.print("\n=== ");
    pw.print("RESPONSE");
    pw.println(" ===");
    pw.print("Content-Type:");
    pw.println(executor.getResponse().getEntity().getContentType().getValue());
    pw.println("Content:");
    pw.println(executor.getContent());
    pw.println("====================================================================================");
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) Header(org.apache.http.Header) HashMap(java.util.HashMap) InputStream(java.io.InputStream)

Example 18 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project cdap by caskdata.

the class AppFabricTestBase method deploy.

protected HttpResponse deploy(Id.Application appId, AppRequest<? extends Config> appRequest) throws Exception {
    String deployPath = getVersionedAPIPath("apps/" + appId.getId(), appId.getNamespaceId());
    HttpEntityEnclosingRequestBase request = getPut(deployPath);
    return executeDeploy(request, appRequest);
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Example 19 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project cdap by caskdata.

the class AppFabricTestBase method deploy.

protected HttpResponse deploy(ApplicationId appId, AppRequest<? extends Config> appRequest) throws Exception {
    String deployPath = getVersionedAPIPath(String.format("apps/%s/versions/%s/create", appId.getApplication(), appId.getVersion()), appId.getNamespace());
    HttpEntityEnclosingRequestBase request = getPost(deployPath);
    return executeDeploy(request, appRequest);
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase)

Example 20 with HttpEntityEnclosingRequestBase

use of org.apache.http.client.methods.HttpEntityEnclosingRequestBase in project cdap by caskdata.

the class AppFabricTestBase method addArtifactProperties.

// add artifact properties and return the response code
protected HttpResponse addArtifactProperties(Id.Artifact artifactId, Map<String, String> properties) throws Exception {
    String nonNamespacePath = String.format("artifacts/%s/versions/%s/properties", artifactId.getName(), artifactId.getVersion());
    String path = getVersionedAPIPath(nonNamespacePath, artifactId.getNamespace().getId());
    HttpEntityEnclosingRequestBase request = getPut(path);
    request.setEntity(new ByteArrayEntity(properties.toString().getBytes()));
    return execute(request);
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity)

Aggregations

HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)38 HttpPost (org.apache.http.client.methods.HttpPost)17 IOException (java.io.IOException)9 HttpPut (org.apache.http.client.methods.HttpPut)9 HttpEntity (org.apache.http.HttpEntity)7 HttpResponse (org.apache.http.HttpResponse)6 InputStream (java.io.InputStream)5 Header (org.apache.http.Header)5 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)5 NameValuePair (org.apache.http.NameValuePair)4 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)4 StringEntity (org.apache.http.entity.StringEntity)4 File (java.io.File)3 HashMap (java.util.HashMap)3 HttpDelete (org.apache.http.client.methods.HttpDelete)3 HttpGet (org.apache.http.client.methods.HttpGet)3 FileEntity (org.apache.http.entity.FileEntity)3 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 LinkedList (java.util.LinkedList)2