Example usage for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET

List of usage examples for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET

Introduction

In this page you can find the example usage for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET.

Prototype

String DEFAULT_CONTENT_CHARSET

To view the source code for org.apache.http.protocol HTTP DEFAULT_CONTENT_CHARSET.

Click Source Link

Usage

From source file:com.navercorp.pinpoint.plugin.httpclient4.interceptor.HttpRequestExecutorExecuteMethodInterceptor.java

/**
 * copy: EntityUtils Get the entity content as a String, using the provided default character set if none is found in the entity. If defaultCharset is null, the default "ISO-8859-1" is used.
 *
 * @param entity//from ww w.j  ava  2  s .  c  om
 *            must not be null
 * @param defaultCharset
 *            character set to be applied if none found in the entity
 * @return the entity content as a String. May be null if {@link HttpEntity#getContent()} is null.
 * @throws ParseException
 *             if header elements cannot be parsed
 * @throws IllegalArgumentException
 *             if entity is null or if content length > Integer.MAX_VALUE
 * @throws IOException
 *             if an error occurs reading the input stream
 */
@SuppressWarnings("deprecation")
public static String entityUtilsToString(final HttpEntity entity, final String defaultCharset, int maxLength)
        throws Exception {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        return "HTTP entity is too large to be buffered in memory length:" + entity.getContentLength();
    }
    if (entity.getContentType().getValue().startsWith("multipart/form-data")) {
        return "content type is multipart/form-data. content length:" + entity.getContentLength();
    }

    String charset = getContentCharSet(entity);

    if (charset == null) {
        charset = defaultCharset;
    }
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }

    FixedByteArrayOutputStream outStream = new FixedByteArrayOutputStream(maxLength);
    entity.writeTo(outStream);

    String entityValue = outStream.toString(charset);

    if (entity.getContentLength() > maxLength) {
        StringBuilder sb = new StringBuilder();
        sb.append(entityValue);
        sb.append(" (HTTP entity is large. length: ");
        sb.append(entity.getContentLength());
        sb.append(" )");
        return sb.toString();
    }

    return entityValue;
}

From source file:com.pc.dailymile.DailyMileClient.java

private void initHttpClient() {
    HttpParams parameters = new BasicHttpParams();
    HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
    // set the User-Agent to a common User-Agent because currently
    // the default httpclient User-Agent doesn't work with dailymile
    HttpProtocolParams.setUserAgent(parameters, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
    HttpProtocolParams.setUseExpectContinue(parameters, false);
    HttpConnectionParams.setTcpNoDelay(parameters, true);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager tsccm = new ThreadSafeClientConnManager(parameters, schReg);

    DefaultHttpClient defaultHttpClient = new DefaultHttpClient(tsccm, parameters);
    defaultHttpClient.addRequestInterceptor(new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            if (!request.containsHeader("Accept-Encoding")) {
                request.addHeader("Accept-Encoding", "gzip");
            }/*  w  w  w . j  a v  a2 s  .co m*/
        }
    });

    defaultHttpClient.addResponseInterceptor(new HttpResponseInterceptor() {
        public void process(final HttpResponse response, final HttpContext context)
                throws HttpException, IOException {
            HttpEntity entity = response.getEntity();
            Header ceheader = entity.getContentEncoding();
            if (ceheader != null) {
                HeaderElement[] codecs = ceheader.getElements();
                for (int i = 0; i < codecs.length; i++) {
                    if (codecs[i].getName().equalsIgnoreCase("gzip")) {
                        response.setEntity(new GzipDecompressingEntity(response.getEntity()));
                        return;
                    }
                }
            }
        }
    });
    httpClient = defaultHttpClient;
}

From source file:org.jets3t.service.utils.RestUtils.java

/**
 * Default Http parameters got from the DefaultHttpClient implementation.
 *
 * @return/*from w  ww  . jav  a 2s .  com*/
 * Default HTTP connection parameters
 */
public static HttpParams createDefaultHttpParams() {
    HttpParams params = new SyncBasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSocketBufferSize(params, 8192);

    // determine the release version from packaged version info
    final VersionInfo vi = VersionInfo.loadVersionInfo("org.apache.http.client",
            HttpClient.class.getClassLoader());
    final String release = (vi != null) ? vi.getRelease() : VersionInfo.UNAVAILABLE;
    HttpProtocolParams.setUserAgent(params, "Apache-HttpClient/" + release + " (java 1.5)");

    return params;
}

From source file:com.pk.wallpapermanager.PkWallpaperManager.java

/**
 * Initializes our handy little client for cloud calls.
 * Don't modify this unless you know what you're doing.
 *//* ww  w .j a  va  2 s  .  co m*/
private void initHttpClient() {
    // Basic HTTP parameters & manager set up
    HttpParams parameters = new BasicHttpParams();
    HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(parameters, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(parameters, false);
    HttpConnectionParams.setTcpNoDelay(parameters, true);
    HttpConnectionParams.setSocketBufferSize(parameters, 8192);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    ClientConnectionManager tsccm = new ThreadSafeClientConnManager(parameters, schReg);

    // Finally, initialize our client with these parameters and manager
    this.httpClient = new DefaultHttpClient(tsccm, parameters);
}

From source file:com.googlecode.sardine.impl.SardineImpl.java

/**
 * Creates default params setting the user agent.
 * //  ww w . j  a va 2  s  . com
 * @return Basic HTTP parameters with a custom user agent
 */
protected HttpParams createDefaultHttpParams() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    String version = Version.getSpecification();
    if (version == null) {
        version = VersionInfo.UNAVAILABLE;
    }
    HttpProtocolParams.setUserAgent(params, "Sardine/" + version);
    // Only selectively enable this for PUT but not all entity enclosing
    // methods
    HttpProtocolParams.setUseExpectContinue(params, false);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);

    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    return params;
}

From source file:com.mhise.util.MHISEUtil.java

public static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }/*from  w  ww .  j  a v a2s  .co  m*/

    InputStream instream = entity.getContent();

    if (instream == null) {
        return "";
    }

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

                "HTTP entity too large to be buffered in memory");
    }

    String charset = getContentCharSet(entity);

    if (charset == null) {

        charset = HTTP.DEFAULT_CONTENT_CHARSET;

    }

    Reader reader = new InputStreamReader(instream, charset);

    StringBuilder buffer = new StringBuilder();

    try {

        char[] tmp = new char[1024];

        int l;

        while ((l = reader.read(tmp)) != -1) {

            buffer.append(tmp, 0, l);

        }

    } finally {

        reader.close();

    }

    return buffer.toString();

}

From source file:org.wso2.mobile.idp.proxy.utils.ServerUtilities.java

/**
 *
 * @param entity//from  ww w  .  j  a v a 2s  . com
 * @return
 * @throws IOException
 * @throws ParseException
 */
public static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }
    InputStream instream = entity.getContent();
    if (instream == null) {
        return "";
    }
    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

                "HTTP entity too large to be buffered in memory");
    }
    String charset = getContentCharSet(entity);
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    Reader reader = new InputStreamReader(instream, charset);
    StringBuilder buffer = new StringBuilder();
    try {
        char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
    } finally {
        reader.close();
    }
    return buffer.toString();
}