use of jakarta.ws.rs.ext.ReaderInterceptor in project resteasy by resteasy.
the class ClientResponse method readFrom.
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
@Override
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
Type useGeneric = genericType == null ? type : genericType;
Class<?> useType = type;
media = media == null ? MediaType.WILDCARD_TYPE : media;
annotations = annotations == null ? this.annotations : annotations;
boolean isMarshalledEntity = false;
if (type.equals(MarshalledEntity.class)) {
isMarshalledEntity = true;
ParameterizedType param = (ParameterizedType) useGeneric;
useGeneric = param.getActualTypeArguments()[0];
useType = Types.getRawType(useGeneric);
}
Providers current = ResteasyContext.getContextData(Providers.class);
ResteasyContext.pushContext(Providers.class, configuration);
Object obj = null;
try {
InputStream is = getEntityStream();
if (is == null) {
throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
}
if (isMarshalledEntity) {
is = new InputStreamToByteArray(is);
}
ReaderInterceptor[] readerInterceptors = configuration.getReaderInterceptors(null, null);
final Object finalObj;
final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, configuration.getProviderFactory(), useType, useGeneric, annotations, media, getStringHeaders(), is, properties, tracingLogger);
try {
finalObj = context.proceed();
obj = finalObj;
} finally {
tracingLogger.logDuration("RI_SUMMARY", timestamp, context.getProcessedInterceptorCount());
}
if (isMarshalledEntity) {
InputStreamToByteArray isba = (InputStreamToByteArray) is;
final byte[] bytes = isba.toByteArray();
return new MarshalledEntity<Object>() {
@Override
public byte[] getMarshalledBytes() {
return bytes;
}
@Override
public Object getEntity() {
return finalObj;
}
};
} else {
return finalObj;
}
} catch (ProcessingException pe) {
throw pe;
} catch (Exception ex) {
throw new ProcessingException(ex);
} finally {
if (!Publisher.class.isAssignableFrom(type) && !EventInput.class.isAssignableFrom(type)) {
ResteasyContext.popContextData(Providers.class);
if (current != null)
ResteasyContext.pushContext(Providers.class, current);
if (obj instanceof ProvidersContextRetainer)
((ProvidersContextRetainer) obj).setProviders(configuration);
}
}
}
use of jakarta.ws.rs.ext.ReaderInterceptor in project resteasy by resteasy.
the class PriorityTest method testReaderInterceptorPriorityOverride.
@Test
public void testReaderInterceptorPriorityOverride() {
Client client = ClientBuilder.newClient();
try {
fakeHttpServer.start();
WebTarget webTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
webTarget.register((ClientResponseFilter) (containerRequestContext, containerResponseContext) -> {
containerResponseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
containerResponseContext.setEntityStream(new ByteArrayInputStream("hello".getBytes()));
});
StringBuilder result = new StringBuilder();
webTarget.register(new ReaderInterceptor() {
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
result.append("K");
return context.proceed();
}
}, 1);
webTarget.register(new ReaderInterceptor() {
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
result.append("O");
return context.proceed();
}
}, 0);
webTarget.request().get().readEntity(String.class);
Assert.assertEquals("OK", result.toString());
} finally {
client.close();
}
}
use of jakarta.ws.rs.ext.ReaderInterceptor in project resteasy by resteasy.
the class BuiltResponse method readFrom.
// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
Type useGeneric = genericType == null ? type : genericType;
Class<?> useType = type;
media = media == null ? MediaType.WILDCARD_TYPE : media;
annotations = annotations == null ? this.annotations : annotations;
boolean isMarshalledEntity = false;
if (type.equals(MarshalledEntity.class)) {
isMarshalledEntity = true;
ParameterizedType param = (ParameterizedType) useGeneric;
useGeneric = param.getActualTypeArguments()[0];
useType = Types.getRawType(useGeneric);
}
try {
InputStream is = getEntityStream();
if (is == null) {
throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
}
if (isMarshalledEntity) {
is = new InputStreamToByteArray(is);
}
ResteasyProviderFactory providerFactory = ResteasyProviderFactory.getInstance();
ReaderInterceptor[] readerInterceptors = providerFactory.getServerReaderInterceptorRegistry().postMatch(null, null);
final Object finalObj;
AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, providerFactory, useType, useGeneric, annotations, media, getStringHeaders(), is, new HashMap<String, Object>(), null);
finalObj = context.proceed();
if (isMarshalledEntity) {
InputStreamToByteArray isba = (InputStreamToByteArray) is;
final byte[] bytes = isba.toByteArray();
return new MarshalledEntity<Object>() {
@Override
public byte[] getMarshalledBytes() {
return bytes;
}
@Override
public Object getEntity() {
return finalObj;
}
};
} else {
return finalObj;
}
} catch (ProcessingException pe) {
throw pe;
} catch (Exception ex) {
throw new ProcessingException(ex);
}
}
use of jakarta.ws.rs.ext.ReaderInterceptor in project tomee by apache.
the class ProviderFactory method createMessageBodyReaderInterceptor.
public <T> List<ReaderInterceptor> createMessageBodyReaderInterceptor(Class<T> bodyType, Type parameterType, Annotation[] parameterAnnotations, MediaType mediaType, Message m, boolean checkMbrNow, Set<String> names) {
MessageBodyReader<T> mr = !checkMbrNow ? null : createMessageBodyReader(bodyType, parameterType, parameterAnnotations, mediaType, m);
int size = readerInterceptors.size();
if (mr != null || size > 0) {
ReaderInterceptor mbrReader = new ReaderInterceptorMBR(mr, getResponseMessage(m));
final List<ReaderInterceptor> interceptors;
if (size > 0) {
interceptors = new ArrayList<>(size + 1);
List<ProviderInfo<ReaderInterceptor>> readers = getBoundFilters(readerInterceptors, names);
for (ProviderInfo<ReaderInterceptor> p : readers) {
injectContextValues(p, m);
interceptors.add(p.getProvider());
}
interceptors.add(mbrReader);
} else {
interceptors = Collections.singletonList(mbrReader);
}
return interceptors;
}
return null;
}
use of jakarta.ws.rs.ext.ReaderInterceptor in project tomee by apache.
the class JAXRSUtils method readFromMessageBody.
private static Object readFromMessageBody(Class<?> targetTypeClass, Type parameterType, Annotation[] parameterAnnotations, InputStream is, MediaType contentType, OperationResourceInfo ori, Message m) throws IOException, WebApplicationException {
List<MediaType> types = JAXRSUtils.intersectMimeTypes(ori.getConsumeTypes(), contentType);
final ProviderFactory pf = ServerProviderFactory.getInstance(m);
for (MediaType type : types) {
List<ReaderInterceptor> readers = pf.createMessageBodyReaderInterceptor(targetTypeClass, parameterType, parameterAnnotations, type, m, true, ori.getNameBindings());
if (readers != null) {
try {
return readFromMessageBodyReader(readers, targetTypeClass, parameterType, parameterAnnotations, is, type, m);
} catch (IOException e) {
if (e.getClass().getName().equals(NO_CONTENT_EXCEPTION)) {
throw ExceptionUtils.toBadRequestException(e, null);
}
throw e;
} catch (WebApplicationException ex) {
throw ex;
} catch (Exception ex) {
throw new Fault(ex);
}
}
}
logMessageHandlerProblem("NO_MSG_READER", targetTypeClass, contentType);
throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
}
Aggregations