Search in sources :

Example 66 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project RoboZombie by sahan.

the class Entities method resolve.

/**
 * <p>Discovers which implementation of {@link HttpEntity} is suitable for wrapping the given object.
 * This discovery proceeds in the following order by checking the runtime-type of the object:</p>
 *
 * <ol>
 * 	<li>org.apache.http.{@link HttpEntity} --&gt; returned as-is.</li>
 * 	<li>{@code byte[]}, {@link Byte}[] --&gt; {@link ByteArrayEntity}</li>
 *  	<li>java.io.{@link File} --&gt; {@link FileEntity}</li>
 * 	<li>java.io.{@link InputStream} --&gt; {@link BufferedHttpEntity}</li>
 * 	<li>{@link CharSequence} --&gt; {@link StringEntity}</li>
 * 	<li>java.io.{@link Serializable} --&gt; {@link SerializableEntity} (with an internal buffer)</li>
 * </ol>
 *
 * @param genericEntity
 * 			a generic reference to an object whose concrete {@link HttpEntity} is to be resolved
 * <br><br>
 * @return the resolved concrete {@link HttpEntity} implementation
 * <br><br>
 * @throws NullPointerException
 * 			if the supplied generic type was {@code null}
 * <br><br>
 * @throws EntityResolutionFailedException
 * 			if the given generic instance failed to be translated to an {@link HttpEntity}
 * <br><br>
 * @since 1.3.0
 */
public static final HttpEntity resolve(Object genericEntity) {
    assertNotNull(genericEntity);
    try {
        if (genericEntity instanceof HttpEntity) {
            return (HttpEntity) genericEntity;
        } else if (byte[].class.isAssignableFrom(genericEntity.getClass())) {
            return new ByteArrayEntity((byte[]) genericEntity);
        } else if (Byte[].class.isAssignableFrom(genericEntity.getClass())) {
            Byte[] wrapperBytes = (Byte[]) genericEntity;
            byte[] primitiveBytes = new byte[wrapperBytes.length];
            for (int i = 0; i < wrapperBytes.length; i++) {
                primitiveBytes[i] = wrapperBytes[i].byteValue();
            }
            return new ByteArrayEntity(primitiveBytes);
        } else if (genericEntity instanceof File) {
            return new FileEntity((File) genericEntity, null);
        } else if (genericEntity instanceof InputStream) {
            BasicHttpEntity basicHttpEntity = new BasicHttpEntity();
            basicHttpEntity.setContent((InputStream) genericEntity);
            return new BufferedHttpEntity(basicHttpEntity);
        } else if (genericEntity instanceof CharSequence) {
            return new StringEntity(((CharSequence) genericEntity).toString());
        } else if (genericEntity instanceof Serializable) {
            return new SerializableEntity((Serializable) genericEntity, true);
        } else {
            throw new EntityResolutionFailedException(genericEntity);
        }
    } catch (Exception e) {
        throw (e instanceof EntityResolutionFailedException) ? (EntityResolutionFailedException) e : new EntityResolutionFailedException(genericEntity, e);
    }
}
Also used : FileEntity(org.apache.http.entity.FileEntity) Serializable(java.io.Serializable) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) InputStream(java.io.InputStream) SerializableEntity(org.apache.http.entity.SerializableEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) StringEntity(org.apache.http.entity.StringEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) File(java.io.File)

Example 67 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project cosmic by MissionCriticalCloud.

the class ClusterServiceServletHttpHandler method writeResponse.

private void writeResponse(final HttpResponse response, final int statusCode, String content) {
    if (content == null) {
        content = "";
    }
    response.setStatusCode(statusCode);
    final BasicHttpEntity body = new BasicHttpEntity();
    body.setContentType("text/html; charset=UTF-8");
    final byte[] bodyData = content.getBytes();
    body.setContent(new ByteArrayInputStream(bodyData));
    body.setContentLength(bodyData.length);
    response.setEntity(body);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 68 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project cosmic by MissionCriticalCloud.

the class ApiServer method writeResponse.

// FIXME: rather than isError, we might was to pass in the status code to give more flexibility
private void writeResponse(final HttpResponse resp, final String responseText, final int statusCode, final String responseType, final String reasonPhrase) {
    try {
        resp.setStatusCode(statusCode);
        resp.setReasonPhrase(reasonPhrase);
        final BasicHttpEntity body = new BasicHttpEntity();
        if (HttpUtils.RESPONSE_TYPE_JSON.equalsIgnoreCase(responseType)) {
            // JSON response
            body.setContentType(getJSONContentType());
            if (responseText == null) {
                body.setContent(new ByteArrayInputStream("{ \"error\" : { \"description\" : \"Internal Server Error\" } }".getBytes(HttpUtils.UTF_8)));
            }
        } else {
            body.setContentType("text/xml");
            if (responseText == null) {
                body.setContent(new ByteArrayInputStream("<error>Internal Server Error</error>".getBytes(HttpUtils.UTF_8)));
            }
        }
        if (responseText != null) {
            body.setContent(new ByteArrayInputStream(responseText.getBytes(HttpUtils.UTF_8)));
        }
        resp.setEntity(body);
    } catch (final Exception ex) {
        s_logger.error("error!", ex);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) AccountLimitException(com.cloud.exception.AccountLimitException) EventBusException(com.cloud.framework.events.EventBusException) HttpException(org.apache.http.HttpException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InterruptedIOException(java.io.InterruptedIOException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudAuthenticationException(com.cloud.exception.CloudAuthenticationException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RequestLimitException(com.cloud.exception.RequestLimitException) URISyntaxException(java.net.URISyntaxException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ParseException(java.text.ParseException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConnectionClosedException(org.apache.http.ConnectionClosedException)

Example 69 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project XobotOS by xamarin.

the class AndroidHttpClientConnection method receiveResponseEntity.

/**
     * Return the next response entity.
     * @param headers contains values for parsing entity
     * @see HttpClientConnection#receiveResponseEntity(HttpResponse response)
     */
public HttpEntity receiveResponseEntity(final Headers headers) {
    assertOpen();
    BasicHttpEntity entity = new BasicHttpEntity();
    long len = determineLength(headers);
    if (len == ContentLengthStrategy.CHUNKED) {
        entity.setChunked(true);
        entity.setContentLength(-1);
        entity.setContent(new ChunkedInputStream(inbuffer));
    } else if (len == ContentLengthStrategy.IDENTITY) {
        entity.setChunked(false);
        entity.setContentLength(-1);
        entity.setContent(new IdentityInputStream(inbuffer));
    } else {
        entity.setChunked(false);
        entity.setContentLength(len);
        entity.setContent(new ContentLengthInputStream(inbuffer, len));
    }
    String contentTypeHeader = headers.getContentType();
    if (contentTypeHeader != null) {
        entity.setContentType(contentTypeHeader);
    }
    String contentEncodingHeader = headers.getContentEncoding();
    if (contentEncodingHeader != null) {
        entity.setContentEncoding(contentEncodingHeader);
    }
    return entity;
}
Also used : ContentLengthInputStream(org.apache.http.impl.io.ContentLengthInputStream) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) IdentityInputStream(org.apache.http.impl.io.IdentityInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 70 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project XobotOS by xamarin.

the class EntityDeserializer method doDeserialize.

protected BasicHttpEntity doDeserialize(final SessionInputBuffer inbuffer, final HttpMessage message) throws HttpException, IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    long len = this.lenStrategy.determineLength(message);
    if (len == ContentLengthStrategy.CHUNKED) {
        entity.setChunked(true);
        entity.setContentLength(-1);
        entity.setContent(new ChunkedInputStream(inbuffer));
    } else if (len == ContentLengthStrategy.IDENTITY) {
        entity.setChunked(false);
        entity.setContentLength(-1);
        entity.setContent(new IdentityInputStream(inbuffer));
    } else {
        entity.setChunked(false);
        entity.setContentLength(len);
        entity.setContent(new ContentLengthInputStream(inbuffer, len));
    }
    Header contentTypeHeader = message.getFirstHeader(HTTP.CONTENT_TYPE);
    if (contentTypeHeader != null) {
        entity.setContentType(contentTypeHeader);
    }
    Header contentEncodingHeader = message.getFirstHeader(HTTP.CONTENT_ENCODING);
    if (contentEncodingHeader != null) {
        entity.setContentEncoding(contentEncodingHeader);
    }
    return entity;
}
Also used : Header(org.apache.http.Header) ContentLengthInputStream(org.apache.http.impl.io.ContentLengthInputStream) ChunkedInputStream(org.apache.http.impl.io.ChunkedInputStream) IdentityInputStream(org.apache.http.impl.io.IdentityInputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Aggregations

BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)139 ByteArrayInputStream (java.io.ByteArrayInputStream)104 Test (org.junit.Test)89 InputStream (java.io.InputStream)60 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)15 StatusLine (org.apache.http.StatusLine)12 BasicStatusLine (org.apache.http.message.BasicStatusLine)11 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)9 HttpException (org.apache.http.HttpException)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpContext (org.apache.http.protocol.HttpContext)8 URISyntaxException (java.net.URISyntaxException)7 Map (java.util.Map)7 URI (java.net.URI)6 Header (org.apache.http.Header)6 ProtocolVersion (org.apache.http.ProtocolVersion)6 HttpGet (org.apache.http.client.methods.HttpGet)6 ChunkedInputStream (org.apache.http.impl.io.ChunkedInputStream)6 ContentLengthInputStream (org.apache.http.impl.io.ContentLengthInputStream)6