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

The following examples show how to use org.apache.http.protocol.HTTP#DEF_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: ReadLogString.java    From docker-java-api with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Try to get charset from content type.
 * If charset not set, try get default charset by mime type
 * If not set return ISO-8859-1
 *
 * @param contentType Content type.
 * @return Charset.
 */
private Charset getCharset(final ContentType contentType) {
    Charset charset = null;
    if (contentType != null) {
        charset = contentType.getCharset();
        if (charset == null) {
            ContentType defaultContentType =
                    ContentType.getByMimeType(contentType.getMimeType());
            if (defaultContentType != null) {
                charset = defaultContentType.getCharset();
            } else {
                charset = null;
            }
        }
    }

    if (charset == null) {
        charset = HTTP.DEF_CONTENT_CHARSET;
    }
    return charset;
}
 
Example 2
Source File: LocalRestRequest.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void setContent(final String source, final ContentType contentType) throws UnsupportedCharsetException {
    Args.notNull(source, "Source string");
    Charset charset = contentType != null?contentType.getCharset():null;
    if(charset == null) {
        charset = HTTP.DEF_CONTENT_CHARSET;
    }

    try {
        this.content = new BytesArray(source.getBytes(charset.name()));
    } catch (UnsupportedEncodingException var) {
        throw new UnsupportedCharsetException(charset.name());
    }

    if(contentType != null) {
        addHeader("Content-Type", contentType.toString());
    }
}
 
Example 3
Source File: HubicAuthenticationResponseHandler.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AuthenticationResponse handleResponse(final HttpResponse response) throws IOException {
    if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        Charset charset = HTTP.DEF_CONTENT_CHARSET;
        ContentType contentType = ContentType.get(response.getEntity());
        if(contentType != null) {
            if(contentType.getCharset() != null) {
                charset = contentType.getCharset();
            }
        }
        try {
            final JsonObject json = JsonParser.parseReader(new InputStreamReader(response.getEntity().getContent(), charset)).getAsJsonObject();
            final String token = json.getAsJsonPrimitive("token").getAsString();
            final String endpoint = json.getAsJsonPrimitive("endpoint").getAsString();
            return new AuthenticationResponse(response, token,
                    Collections.singleton(new Region(null, URI.create(endpoint), null, true)));
        }
        catch(JsonParseException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
    else if(response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
            || response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN) {
        throw new AuthorizationException(new Response(response));
    }
    throw new GenericException(new Response(response));
}
 
Example 4
Source File: AnnotatedServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
static HttpPost form(String path, @Nullable Charset charset, String... kv) {
    final HttpPost req = (HttpPost) request(HttpMethod.POST, path, MediaType.FORM_DATA.toString());

    final List<NameValuePair> params = new ArrayList<>();
    for (int i = 0; i < kv.length; i += 2) {
        params.add(new BasicNameValuePair(kv[i], kv[i + 1]));
    }
    // HTTP.DEF_CONTENT_CHARSET = ISO-8859-1
    final Charset encoding = charset == null ? HTTP.DEF_CONTENT_CHARSET : charset;
    final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, encoding);
    req.setEntity(entity);
    return req;
}