use of jakarta.ws.rs.ext.WriterInterceptorContext in project resteasy by resteasy.
the class PriorityTest method testWriterPriorityOverride.
@Test
public void testWriterPriorityOverride() {
Client client = ClientBuilder.newClient();
try {
fakeHttpServer.start();
WebTarget webTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
StringBuilder result = new StringBuilder();
webTarget.register(new WriterInterceptor() {
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
result.append("K");
context.proceed();
}
}, 1);
webTarget.register(new WriterInterceptor() {
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
result.append("O");
context.proceed();
}
}, 0);
webTarget.request().post(Entity.text("Hello")).close();
Assert.assertEquals("OK", result.toString());
} finally {
client.close();
}
}
use of jakarta.ws.rs.ext.WriterInterceptorContext in project tomee by apache.
the class JAXRSUtils method writeMessageBody.
// CHECKSTYLE:OFF
public static void writeMessageBody(List<WriterInterceptor> writers, Object entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Message message) throws WebApplicationException, IOException {
OutputStream entityStream = message.getContent(OutputStream.class);
if ("org.apache.cxf.jaxrs.reactivestreams.server.StreamingAsyncSubscriber$StreamingResponseImpl".equals(entity.getClass().getName())) {
// cache the OutputStream when it's reactive response
entityStream = new CacheAndWriteOutputStream(entityStream);
}
if (writers.size() > 1) {
WriterInterceptor first = writers.remove(0);
WriterInterceptorContext context = new WriterInterceptorContextImpl(entity, type, genericType, annotations, entityStream, message, writers);
first.aroundWriteTo(context);
} else {
MessageBodyWriter<Object> writer = ((WriterInterceptorMBW) writers.get(0)).getMBW();
if (type == byte[].class) {
long size = writer.getSize(entity, type, genericType, annotations, mediaType);
if (size != -1) {
httpHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size));
}
}
HttpUtils.convertHeaderValuesToString(httpHeaders, true);
writer.writeTo(entity, type, genericType, annotations, mediaType, httpHeaders, entityStream);
}
}
Aggregations