use of org.jboss.resteasy.core.interception.jaxrs.ServerReaderInterceptorContext in project resteasy by resteasy.
the class MessageBodyParameterInjector method inject.
@Override
public Object inject(HttpRequest request, HttpResponse response, boolean unwrapAsync) {
Object o = getBody();
if (o != null) {
return o;
}
MediaType mediaType = request.getHttpHeaders().getMediaType();
if (mediaType == null) {
mediaType = MediaType.WILDCARD_TYPE;
// throw new BadRequestException("content-type was null and expecting to extract a body into " + this.target);
}
InputStream is = null;
if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.equals(mediaType)) {
if (request.formParametersRead()) {
MultivaluedMap<String, String> map = request.getDecodedFormParameters();
if (map != null) {
StringBuilder sb = new StringBuilder();
for (Entry<String, List<String>> entry : map.entrySet()) {
String key = entry.getKey();
sb.append(key);
List<String> values = entry.getValue();
for (String value : values) {
if (!("".equals(value))) {
sb.append("=").append(value);
}
sb.append("&");
}
}
if (sb.length() > 0 && '&' == sb.charAt(sb.length() - 1)) {
sb.deleteCharAt(sb.length() - 1);
}
String charset = "UTF-8";
if (mediaType.getParameters().get("charset") != null) {
charset = mediaType.getParameters().get("charset");
}
try {
is = new ByteArrayInputStream(sb.toString().getBytes(charset));
} catch (Exception e) {
LogMessages.LOGGER.charsetUnavailable(charset);
}
}
}
}
try {
if (is == null) {
is = request.getInputStream();
}
if (isMarshalledEntity) {
is = new InputStreamToByteArray(is);
}
AbstractReaderInterceptorContext messageBodyReaderContext = new ServerReaderInterceptorContext(getReaderInterceptors(), factory, type, genericType, annotations, mediaType, request.getMutableHeaders(), is, request);
RESTEasyTracingLogger tracingLogger = RESTEasyTracingLogger.getInstance(request);
final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
final Object obj;
try {
obj = messageBodyReaderContext.proceed();
} finally {
tracingLogger.logDuration("RI_SUMMARY", timestamp, messageBodyReaderContext.getProcessedInterceptorCount());
}
if (isMarshalledEntity) {
InputStreamToByteArray isba = (InputStreamToByteArray) is;
final byte[] bytes = isba.toByteArray();
return new MarshalledEntity() {
@Override
public byte[] getMarshalledBytes() {
return bytes;
}
@Override
public Object getEntity() {
return obj;
}
};
} else {
return obj;
}
} catch (Exception e) {
if (e instanceof ReaderException) {
throw (ReaderException) e;
} else {
throw new ReaderException(e);
}
}
}
use of org.jboss.resteasy.core.interception.jaxrs.ServerReaderInterceptorContext in project resteasy by resteasy.
the class GZIPDecodingInterceptor method aroundReadFrom.
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
LogMessages.LOGGER.debugf("Interceptor : %s, Method : aroundReadFrom", getClass().getName());
Object encoding = context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING);
if (encoding != null && encoding.toString().equalsIgnoreCase("gzip")) {
InputStream old = context.getInputStream();
FinishableGZIPInputStream is = new FinishableGZIPInputStream(old, context instanceof ServerReaderInterceptorContext, getMaxSize());
context.setInputStream(is);
try {
return context.proceed();
} finally {
// see RESTEASY-554 for more details
if (!context.getType().equals(InputStream.class))
is.finish();
context.setInputStream(old);
}
} else {
return context.proceed();
}
}
Aggregations