Java Code Examples for org.apache.http.protocol.HTTP#DEFAULT_CONTENT_CHARSET

The following examples show how to use org.apache.http.protocol.HTTP#DEFAULT_CONTENT_CHARSET . 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: HttpHeaderParser.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 2
Source File: Response.java    From WeGit with Apache License 2.0 6 votes vote down vote up
private Response parseCharset() {
    if (hasParseHeader == false) {
        throw new IllegalStateException("You have not parse headers");
    }
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    this.charset = HTTP.DEFAULT_CONTENT_CHARSET;
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    this.charset = pair[1];
                    return this;
                }
            }
        }
    }
    return this;
}
 
Example 3
Source File: HttpHeaderParser.java    From android-common-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 4
Source File: HttpHeaderParser.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 5
Source File: StringPart.java    From volley with Apache License 2.0 6 votes vote down vote up
/**
 * @param name String - name of parameter (may not be <code>null</code>).
 * @param value String - value of parameter (may not be <code>null</code>).
 * @param charset String, if null is passed then default "ISO-8859-1" charset is used.
 * 
 * @throws IllegalArgumentException if either <code>value</code> 
 *         or <code>name</code> is <code>null</code>.
 * @throws RuntimeException if <code>charset</code> is unsupported by OS.
 */
public StringPart(String name, String value, String charset) {
    if (name == null) {
        throw new IllegalArgumentException("Name may not be null");     //$NON-NLS-1$
    }
    if (value == null) {
        throw new IllegalArgumentException("Value may not be null");    //$NON-NLS-1$
    }
    
    final String partName = UrlEncodingHelper.encode(name, HTTP.DEFAULT_PROTOCOL_CHARSET);
    
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    final String partCharset = charset;
    
    try {
        this.valueBytes = value.getBytes(partCharset);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    
    headersProvider = new IHeadersProvider() {
        public String getContentDisposition() {
            return "Content-Disposition: form-data; name=\"" + partName + '"';                  //$NON-NLS-1$
        }
        public String getContentType() {
            return "Content-Type: " + HTTP.PLAIN_TEXT_TYPE + HTTP.CHARSET_PARAM + partCharset;  //$NON-NLS-1$
        }
        public String getContentTransferEncoding() {
            return "Content-Transfer-Encoding: 8bit";                                           //$NON-NLS-1$
        }
    };
}
 
Example 6
Source File: HttpHeaderParser.java    From okulus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 7
Source File: HttpHeaderParser.java    From FeedListViewDemo with MIT License 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 8
Source File: HttpHeaderParser.java    From android_tv_metro with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 9
Source File: HttpHeaderParser.java    From barterli_android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 10
Source File: StringPart.java    From barterli_android with Apache License 2.0 6 votes vote down vote up
/**
 * @param name String - name of parameter (may not be <code>null</code>).
 * @param value String - value of parameter (may not be <code>null</code>).
 * @param charset String, if null is passed then default "ISO-8859-1" charset is used.
 * 
 * @throws IllegalArgumentException if either <code>value</code> 
 *         or <code>name</code> is <code>null</code>.
 * @throws RuntimeException if <code>charset</code> is unsupported by OS.
 */
public StringPart(String name, String value, String charset) {
    if (name == null) {
        throw new IllegalArgumentException("Name may not be null");     //$NON-NLS-1$
    }
    if (value == null) {
        throw new IllegalArgumentException("Value may not be null");    //$NON-NLS-1$
    }
    
    final String partName = UrlEncodingHelper.encode(name, HTTP.DEFAULT_PROTOCOL_CHARSET);
    
    if (charset == null) {
        charset = HTTP.DEFAULT_CONTENT_CHARSET;
    }
    final String partCharset = charset;
    
    try {
        this.valueBytes = value.getBytes(partCharset);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    
    headersProvider = new IHeadersProvider() {
        public String getContentDisposition() {
            return "Content-Disposition: form-data; name=\"" + partName + '"';                  //$NON-NLS-1$
        }
        public String getContentType() {
            return "Content-Type: " + HTTP.PLAIN_TEXT_TYPE + HTTP.CHARSET_PARAM + partCharset;  //$NON-NLS-1$
        }
        public String getContentTransferEncoding() {
            return "Content-Transfer-Encoding: 8bit";                                           //$NON-NLS-1$
        }
    };
}
 
Example 11
Source File: HttpHeaderParser.java    From WayHoo with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the charset specified in the Content-Type of this header,
 * or the HTTP default (ISO-8859-1) if none can be found.
 */
public static String parseCharset(Map<String, String> headers) {
    String contentType = headers.get(HTTP.CONTENT_TYPE);
    if (contentType != null) {
        String[] params = contentType.split(";");
        for (int i = 1; i < params.length; i++) {
            String[] pair = params[i].trim().split("=");
            if (pair.length == 2) {
                if (pair[0].equals("charset")) {
                    return pair[1];
                }
            }
        }
    }

    return HTTP.DEFAULT_CONTENT_CHARSET;
}
 
Example 12
Source File: ServerUtilities.java    From product-emm with Apache License 2.0 5 votes vote down vote up
public static String getResponseBodyContent(final HttpEntity entity) throws IOException,
                                                                            ParseException {

	InputStream instream = entity.getContent();

	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[] bufferSize = new char[1024];
		int length;

		while ((length = reader.read(bufferSize)) != -1) {
			buffer.append(bufferSize, 0, length);
		}
	} finally {
		reader.close();
	}

	return buffer.toString();

}
 
Example 13
Source File: ServerUtilities.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param entity
 * @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();
}
 
Example 14
Source File: HttpClient4EntityExtractor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
/**
 * 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         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")
private static String entityUtilsToString(final HttpEntity entity, final String defaultCharset, final int maxLength) throws Exception {
    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity must 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;
    }

    final FixedByteArrayOutputStream outStream = new FixedByteArrayOutputStream(maxLength);
    entity.writeTo(outStream);
    final String entityValue = outStream.toString(charset);
    if (entity.getContentLength() > maxLength) {
        final StringBuilder sb = new StringBuilder();
        sb.append(entityValue);
        sb.append("HTTP entity large length: ");
        sb.append(entity.getContentLength());
        sb.append(" )");
        return sb.toString();
    }

    return entityValue;
}
 
Example 15
Source File: UrlUtil.java    From YiBo with Apache License 2.0 5 votes vote down vote up
public static <T> String appendQueryParameters(String url, final Map<String, T> parameters,
	final String encoding) throws UnsupportedEncodingException {
	final StringBuilder result = new StringBuilder();
	if (url == null || parameters == null || parameters.size() == 0) {
		return result.toString();
	}

	String tempEncoding = (encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
	for (Map.Entry<String, T> entry : parameters.entrySet()) {
		if (result.length() > 0) {
			result.append("&");
		}

		result.append(URLEncoder.encode(entry.getKey(), tempEncoding));
		result.append("=");
		result.append(URLEncoder.encode(String.valueOf(entry.getValue()), tempEncoding));
	}

	if (url.indexOf("?") < 0) {
		result.insert(0, "?");
	} else {
		result.insert(0, "&");
	}

	result.insert(0, url);
	return result.toString();
}