Example usage for org.apache.http.entity InputStreamEntity setContentEncoding

List of usage examples for org.apache.http.entity InputStreamEntity setContentEncoding

Introduction

In this page you can find the example usage for org.apache.http.entity InputStreamEntity setContentEncoding.

Prototype

public void setContentEncoding(Header header) 

Source Link

Usage

From source file:com.meplato.store2.MockResponse.java

/**
 * Parse a response from String contents.
 *
 * @param contents//from   ww  w.  j  av a  2  s  . co  m
 * @return String contents
 * @throws IOException
 * @throws HttpException
 * @throws ServiceException
 */
public static Response fromContents(String contents) throws IOException, HttpException, ServiceException {
    // If this code works, it was written by Georg Wall.
    SessionInputBufferImpl sib = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 65535);
    sib.bind(new ByteArrayInputStream(contents.getBytes(Consts.UTF_8)));
    DefaultHttpResponseParser parser = new DefaultHttpResponseParser(sib);
    HttpResponse httpResponse = parser.parse();
    int endOfHeader = contents.indexOf("\r\n\r\n");
    if (endOfHeader >= 0) {
        endOfHeader += 4; // for \r\n\r\n
        byte[] bytes = contents.getBytes(Consts.UTF_8);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes, endOfHeader, bytes.length - endOfHeader);
        InputStreamEntity entity = new InputStreamEntity(bais);
        entity.setContentType(httpResponse.getFirstHeader("Content-Type"));
        entity.setContentEncoding(httpResponse.getFirstHeader("Content-Encoding"));
        httpResponse.setEntity(entity);
    }
    return new ApacheHttpResponse(httpResponse);
}

From source file:org.openjena.riot.web.HttpOp.java

/** POST with response body.
 * The input stream is assumed to be UTF-8.
 *//*w  ww . ja v  a 2s .c o  m*/
public static void execHttpPost(String url, String contentType, InputStream input, int length,
        String acceptType, Map<String, HttpResponseHandler> handlers) {

    InputStreamEntity e = new InputStreamEntity(input, length);
    e.setContentType(contentType);
    e.setContentEncoding("UTF-8");
    execHttpPost(url, e, acceptType, handlers);
}

From source file:cn.mrdear.pay.util.WebUtils.java

/**
 * POST// w w  w. j av  a2  s  .c  o m
 *
 * @param url
 *            URL
 * @param inputStreamEntity
 *            
 * @return 
 */
public static String post(String url, InputStreamEntity inputStreamEntity) {

    String result = null;
    try {
        HttpPost httpPost = new HttpPost(url);
        inputStreamEntity.setContentEncoding("UTF-8");
        httpPost.setEntity(inputStreamEntity);
        CloseableHttpResponse httpResponse = HTTP_CLIENT.execute(httpPost);
        result = consumeResponse(httpResponse);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (ClientProtocolException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (ParseException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    return result;
}

From source file:org.apache.camel.component.http4.HttpEntityConverter.java

private static HttpEntity asHttpEntity(InputStream in, Exchange exchange) throws IOException {
    InputStreamEntity entity;
    if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        InputStream stream = GZIPHelper.compressGzip(contentEncoding, in);
        entity = new InputStreamEntity(stream,
                stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1
                        : -1);/*from  w  w  w . j av a 2s.c om*/
    } else {
        entity = new InputStreamEntity(in, -1);
    }
    if (exchange != null) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        String contentType = ExchangeHelper.getContentType(exchange);
        entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
    }
    return entity;
}

From source file:org.apache.camel.component.http4.HttpEntityConverter.java

private static HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception {
    InputStreamEntity entity;
    if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        InputStream stream = GZIPHelper.compressGzip(contentEncoding, data);
        entity = new InputStreamEntity(stream,
                stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1
                        : -1);/*from  www.jav a  2  s . com*/
    } else {
        entity = new InputStreamEntity(new ByteArrayInputStream(data), data.length);
    }
    if (exchange != null) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        String contentType = ExchangeHelper.getContentType(exchange);
        entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
    }
    return entity;
}

From source file:cn.mrdear.pay.util.WebUtils.java

/**
 * ?/*from  w w  w  .j a  v  a2 s.  co  m*/
 * @param certPath ?
 * @param passwd  ??
 * @param uri ?
 * @param entity xml
 * @return 
 */
public static String post(String certPath, String passwd, String uri, InputStreamEntity entity)
        throws Exception {
    String result = null;
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    FileInputStream instream = new FileInputStream(new File(certPath));
    try {
        keyStore.load(instream, passwd.toCharArray());
    } finally {
        instream.close();
    }
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, passwd.toCharArray()).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {
        HttpPost httpPost = new HttpPost(uri);
        entity.setContentEncoding("UTF-8");
        httpPost.setEntity(entity);
        CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
        result = consumeResponse(httpResponse);
    } finally {
        httpclient.close();
    }
    return result;
}

From source file:com.sun.identity.proxy.client.EntityRequest.java

/**
 * Creates a new entity enclosing request for the specified incoming proxy
 * request.//from  w  w  w  .j av  a2s  . com
 *
 * @param request the incoming proxy request.
 */
public EntityRequest(Request request) {
    this.method = request.method;
    String contentEncoding = request.headers.first("Content-Encoding");
    int contentLength = IntegerUtil.parseInt(request.headers.first("Content-Length"), -1);
    String contentType = request.headers.first("Content-Type");
    InputStreamEntity entity = new InputStreamEntity(request.entity, contentLength);
    if (contentEncoding != null) {
        entity.setContentEncoding(contentEncoding);
    }
    if (contentType != null) {
        entity.setContentType(contentType);
    }
    setEntity(entity);
}

From source file:org.graphity.core.util.jena.HttpOp.java

/**
 * Executes a HTTP PUT operation//  w  w w  . jav a2 s .  c  o  m
 * 
 * @param url
 *            URL
 * @param contentType
 *            Content Type for the PUT
 * @param input
 *            Input Stream to read PUT content from
 * @param length
 *            Amount of content to PUT
 * @param httpClient
 *            HTTP Client
 * @param httpContext
 *            HTTP Context
 * @param authenticator
 *            HTTP Authenticator
 */
public static void execHttpPut(String url, String contentType, InputStream input, long length,
        HttpClient httpClient, HttpContext httpContext, HttpAuthenticator authenticator) {
    InputStreamEntity e = new InputStreamEntity(input, length);
    e.setContentType(contentType);
    e.setContentEncoding("UTF-8");
    try {
        execHttpPut(url, e, httpClient, httpContext, authenticator);
    } finally {
        closeEntity(e);
    }
}

From source file:org.graphity.core.util.jena.HttpOp.java

/**
 * Executes a HTTP POST with request body from an input stream and response
 * handling./* w  ww.j av  a 2 s.  c o  m*/
 * <p>
 * The input stream is assumed to be UTF-8.
 * </p>
 * 
 * @param url
 *            URL
 * @param contentType
 *            Content Type to POST
 * @param input
 *            Input Stream to POST content from
 * @param length
 *            Length of content to POST
 * @param acceptType
 *            Accept Type
 * @param handler
 *            Response handler called to process the response
 * @param httpClient
 *            HTTP Client
 * @param httpContext
 *            HTTP Context
 * @param authenticator
 *            HTTP Authenticator
 */
public static void execHttpPost(String url, String contentType, InputStream input, long length,
        String acceptType, HttpResponseHandler handler, HttpClient httpClient, HttpContext httpContext,
        HttpAuthenticator authenticator) {
    InputStreamEntity e = new InputStreamEntity(input, length);
    e.setContentType(contentType);
    e.setContentEncoding("UTF-8");
    try {
        execHttpPost(url, e, acceptType, handler, httpClient, httpContext, authenticator);
    } finally {
        closeEntity(e);
    }
}

From source file:org.graphity.core.util.jena.DatasetGraphAccessorHTTP.java

private HttpEntity graphToHttpEntity(final Graph graph) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Model model = ModelFactory.createModelForGraph(graph);
    model.write(out, getSendLang().getName()); //model.write(out, WebContent.langNTriples) ;
    byte[] bytes = out.toByteArray();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    InputStreamEntity reqEntity = new InputStreamEntity(in, bytes.length);
    reqEntity.setContentType(getSendLang().getContentType().getContentType());
    reqEntity.setContentEncoding("UTF-8");
    HttpEntity entity = reqEntity;//w  w  w .  ja v  a  2 s  .c  o m
    return entity;
    //((HttpEntityEnclosingRequestBase)httpRequest).setEntity(entity) ;

    /*
    ContentProducer producer = new ContentProducer() {
    @Override
    public void writeTo(OutputStream out) throws IOException {
        RDFDataMgr.write(out, graph, sendLang) ;
    }
    } ;
            
    EntityTemplate entity = new EntityTemplate(producer) ;
    String ct = sendLang.getLang().getContentType().getContentType() ;
    entity.setContentType(ct) ;
    return entity ;
    */
}