Java Code Examples for org.apache.http.entity.EntityTemplate#setContentType()

The following examples show how to use org.apache.http.entity.EntityTemplate#setContentType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleHttpClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP DELETE request with entity body to the specified URL
 *
 * @param url     Target endpoint URL
 * @param headers Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers, final String payload,
                                        String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 2
Source File: RESTClient.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a HTTP DELETE request with entity body to the specified URL.
 *
 * @param url     Target endpoint URL
 * @param headers Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
                                        final String payload, String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    HTTPUtils.setHTTPHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return httpclient.execute(request);
}
 
Example 3
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP POST request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doPost(String url, final Map<String, String> headers,
                           final String payload, String contentType) throws IOException {
    HttpUriRequest request = new HttpPost(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes());
            out.flush();
            out.close();
        }
    });
    ent.setContentType(contentType);
    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 4
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP PATCH request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doPatch(String url, final Map<String, String> headers,
                            final String payload, String contentType) throws IOException {
    HttpUriRequest request = new HttpPatch(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes());
            out.flush();
            out.close();
        }
    });
    ent.setContentType(contentType);
    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 5
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP DELETE request with entity body to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
        final String payload, String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 6
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Send a HTTP PUT request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doPut(String url, final Map<String, String> headers,
                          final String payload, String contentType) throws IOException {
    HttpUriRequest request = new HttpPut(url);
    setHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes());
            out.flush();
            out.close();
        }
    });
    ent.setContentType(contentType);
    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return client.execute(request);
}
 
Example 7
Source File: RESTClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Sends a HTTP DELETE request with entity body to the specified URL.
 *
 * @param url     Target endpoint URL
 * @param headers Any HTTP headers that should be added to the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doDeleteWithPayload(String url, final Map<String, String> headers,
                                        final String payload, String contentType) throws IOException {

    boolean zip = false;
    HttpUriRequest request = new HttpDeleteWithEntity(url);
    HTTPUtils.setHTTPHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;

    //check if content encoding required
    if (headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING))) {
        zip = true;
    }

    EntityTemplate ent = new EntityTemplate(new EntityContentProducer(payload, zip));
    ent.setContentType(contentType);

    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
    return httpclient.execute(request);
}
 
Example 8
Source File: RESTClient.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Prepares a HttpUriRequest to be sent.
 *
 * @param headers HTTP headers to be set as a MAP <Header name, Header value>
 * @param payload Final payload to be sent
 * @param contentType Content-type (i.e application/json)
 * @param request HttpUriRequest request to be prepared
 */
private void prepareRequest(Map<String, String> headers, final String payload, String contentType,
                            HttpUriRequest request) {
    HTTPUtils.setHTTPHeaders(headers, request);
    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes());
            out.flush();
            out.close();
        }
    });
    ent.setContentType(contentType);
    if (zip) {
        ent.setContentEncoding("gzip");
    }
    entityEncReq.setEntity(ent);
}
 
Example 9
Source File: ModInternationalization.java    From spydroid-ipcamera with GNU General Public License v3.0 6 votes vote down vote up
public void handle(
		final HttpRequest request, 
		final HttpResponse response,
		final HttpContext context) throws HttpException, IOException {

	final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
	if (!method.equals("GET") && !method.equals("HEAD")) {
		throw new MethodNotSupportedException(method + " method not supported"); 
	}

	final EntityTemplate body = new EntityTemplate(new ContentProducer() {
		public void writeTo(final OutputStream outstream) throws IOException {
			OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); 
			writer.write(mJSON);
			writer.flush();
		}
	});

	response.setStatusCode(HttpStatus.SC_OK);
	body.setContentType("text/json; charset=UTF-8");
	response.setEntity(body);

}
 
Example 10
Source File: CustomHttpServer.java    From spydroid-ipcamera with GNU General Public License v3.0 6 votes vote down vote up
public void handle(HttpRequest request, HttpResponse response, HttpContext arg2) throws HttpException, IOException {

			if (request.getRequestLine().getMethod().equals("POST")) {

				// Retrieve the POST content
				HttpEntityEnclosingRequest post = (HttpEntityEnclosingRequest) request;
				byte[] entityContent = EntityUtils.toByteArray(post.getEntity());
				String content = new String(entityContent, Charset.forName("UTF-8"));

				// Execute the request
				final String json = RequestHandler.handle(content);

				// Return the response
				EntityTemplate body = new EntityTemplate(new ContentProducer() {
					public void writeTo(final OutputStream outstream) throws IOException {
						OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
						writer.write(json);
						writer.flush();
					}
				});
				response.setStatusCode(HttpStatus.SC_OK);
				body.setContentType("application/json; charset=UTF-8");
				response.setEntity(body);
			}

		}
 
Example 11
Source File: SimpleHttpClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Send a HTTP OPTIONS request to the specified URL
 *
 * @param url         Target endpoint URL
 * @param headers     Any HTTP headers that should be added to the request
 * @param payload     Content payload that should be sent
 * @param contentType Content-type of the request
 * @return Returned HTTP response
 * @throws IOException If an error occurs while making the invocation
 */
public HttpResponse doOptions(String url, final Map<String, String> headers,
                              final String payload, String contentType) throws IOException {
    HttpUriRequest request = new HttpOptions(url);
    setHeaders(headers, request);
    if(payload != null) {
        HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
        final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

        EntityTemplate ent = new EntityTemplate(new ContentProducer() {
            public void writeTo(OutputStream outputStream) throws IOException {
                OutputStream out = outputStream;
                if (zip) {
                    out = new GZIPOutputStream(outputStream);
                }
                out.write(payload.getBytes());
                out.flush();
                out.close();
            }
        });
        ent.setContentType(contentType);
        if (zip) {
            ent.setContentEncoding("gzip");
        }
        entityEncReq.setEntity(ent);
    }
    return client.execute(request);
}
 
Example 12
Source File: SOAPClient.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Sends a simple SOAP message and returns the response.
 *
 * @param url     HTTP service url to send message
 * @param payload final payload as a String to be sent. Message should adhere to SOAP principles
 * @param action  SOAP Action to set. Specify with "urn:"
 * @param headers HTTP headers to be set as a MAP <Header name, Header value>
 * @return HTTP response after invocation
 * @throws IOException in case of an transport error invoking service
 */
public HttpResponse sendSimpleSOAPMessage(String url, final String payload, String action,
                                          final Map<String, String> headers) throws IOException {
    HttpUriRequest request = new HttpPost(url);
    HTTPUtils.setHTTPHeader(SOAP_ACTION, action, request);

    if(headers != null) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
            HTTPUtils.setHTTPHeader(header.getKey(),header.getValue(), request);
        }
    }

    final boolean zip = headers != null && "gzip".equals(headers.get(HttpHeaders.CONTENT_ENCODING));

    HttpEntityEnclosingRequest entityEncReq = (HttpEntityEnclosingRequest) request;
    EntityTemplate ent = new EntityTemplate(new ContentProducer() {
        public void writeTo(OutputStream outputStream) throws IOException {
            OutputStream out = outputStream;
            if (zip) {
                out = new GZIPOutputStream(outputStream);
            }
            out.write(payload.getBytes());
            out.flush();
            out.close();
        }
    });
    ent.setContentType(HttpConstants.MEDIA_TYPE_TEXT_XML);
    if (zip) {
        ent.setContentEncoding("gzip");
    }

    entityEncReq.setEntity(ent);
    return this.httpclient.execute(request);
}