Java Code Examples for com.linecorp.armeria.common.AggregatedHttpRequest#contentType()

The following examples show how to use com.linecorp.armeria.common.AggregatedHttpRequest#contentType() . 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: SamlService.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a {@link SamlParameters} instance with the specified {@link AggregatedHttpRequest}.
 */
SamlParameters(AggregatedHttpRequest req) {
    requireNonNull(req, "req");
    final MediaType contentType = req.contentType();

    if (contentType != null && contentType.belongsTo(MediaType.FORM_DATA)) {
        final String query = req.content(contentType.charset(StandardCharsets.UTF_8));
        params = QueryParams.fromQueryString(query);
    } else {
        final String path = req.path();
        final int queryStartIdx = path.indexOf('?');
        if (queryStartIdx < 0) {
            params = QueryParams.of();
        } else {
            params = QueryParams.fromQueryString(path.substring(queryStartIdx + 1));
        }
    }
}
 
Example 2
Source File: StringRequestConverterFunction.java    From armeria with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the specified {@link AggregatedHttpRequest} to a {@link String}.
 */
@Override
public Object convertRequest(
        ServiceRequestContext ctx, AggregatedHttpRequest request, Class<?> expectedResultType,
        @Nullable ParameterizedType expectedParameterizedResultType) throws Exception {

    if (expectedResultType == String.class ||
        expectedResultType == CharSequence.class) {
        final Charset charset;
        final MediaType contentType = request.contentType();
        if (contentType != null) {
            charset = contentType.charset(ArmeriaHttpUtil.HTTP_DEFAULT_CONTENT_CHARSET);
        } else {
            charset = ArmeriaHttpUtil.HTTP_DEFAULT_CONTENT_CHARSET;
        }
        return request.content(charset);
    }
    return RequestConverterFunction.fallthrough();
}
 
Example 3
Source File: MessageConverterService.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public Object convertRequest(
        ServiceRequestContext ctx, AggregatedHttpRequest request, Class<?> expectedResultType,
        @Nullable ParameterizedType expectedParameterizedResultType) throws Exception {

    final MediaType mediaType = request.contentType();
    if (mediaType != null && mediaType.is(MediaType.PLAIN_TEXT_UTF_8)) {
        return new Request(request.contentUtf8());
    }
    return RequestConverterFunction.fallthrough();
}
 
Example 4
Source File: DefaultHealthCheckUpdateHandler.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static JsonNode toJsonNode(AggregatedHttpRequest req) {
    final MediaType contentType = req.contentType();
    if (contentType != null && !contentType.is(MediaType.JSON)) {
        throw HttpStatusException.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
    }

    final Charset charset = contentType == null ? StandardCharsets.UTF_8
                                                : contentType.charset(StandardCharsets.UTF_8);
    try {
        return StandardCharsets.UTF_8.equals(charset) ? mapper.readTree(req.content().array())
                                                      : mapper.readTree(req.content(charset));
    } catch (IOException e) {
        throw HttpStatusException.of(HttpStatus.BAD_REQUEST);
    }
}