Skip to content

Commit

Permalink
Remove erroneous state tracking from client readers/writers
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Jan 11, 2025
1 parent 8da6801 commit cae6630
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,40 @@
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.ClientWebApplicationException;
import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.client.spi.ClientMessageBodyReader;
import org.jboss.resteasy.reactive.common.providers.serialisers.AbstractJsonMessageBodyReader;
import org.jboss.resteasy.reactive.common.util.EmptyInputStream;
import org.jboss.resteasy.reactive.server.jackson.JacksonBasicMessageBodyReader;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

public class ClientJacksonMessageBodyReader extends JacksonBasicMessageBodyReader implements ClientRestHandler {
public class ClientJacksonMessageBodyReader extends AbstractJsonMessageBodyReader implements ClientMessageBodyReader<Object> {

private static final Logger log = Logger.getLogger(ClientJacksonMessageBodyReader.class);

private final ConcurrentMap<ObjectMapper, ObjectReader> objectReaderMap = new ConcurrentHashMap<>();
private RestClientRequestContext context;
private final ObjectReader defaultReader;

@Inject
public ClientJacksonMessageBodyReader(ObjectMapper mapper) {
super(mapper);
this.defaultReader = mapper.reader();
}

@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
return doRead(type, genericType, mediaType, entityStream, null);
}

private Object doRead(Class<Object> type, Type genericType, MediaType mediaType, InputStream entityStream,
RestClientRequestContext context)
throws IOException {
try {
if (entityStream instanceof EmptyInputStream) {
return null;
}
ObjectReader reader = getEffectiveReader(mediaType);
ObjectReader reader = getEffectiveReader(mediaType, context);
return reader.forType(reader.getTypeFactory().constructType(genericType != null ? genericType : type))
.readValue(entityStream);

Expand All @@ -57,14 +63,18 @@ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annota
}

@Override
public void handle(RestClientRequestContext requestContext) {
this.context = requestContext;
public Object readFrom(Class<Object> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream,
RestClientRequestContext context) throws java.io.IOException, jakarta.ws.rs.WebApplicationException {
return doRead(type, genericType, mediaType, entityStream, context);
}

private ObjectReader getEffectiveReader(MediaType responseMediaType) {
private ObjectReader getEffectiveReader(MediaType responseMediaType, RestClientRequestContext context) {
ObjectMapper effectiveMapper = getObjectMapperFromContext(responseMediaType, context);
if (effectiveMapper == null) {
return getEffectiveReader();
return defaultReader;
}

return objectReaderMap.computeIfAbsent(effectiveMapper, new Function<>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;

import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.client.spi.ClientMessageBodyWriter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

public class ClientJacksonMessageBodyWriter implements MessageBodyWriter<Object>, ClientRestHandler {
public class ClientJacksonMessageBodyWriter implements ClientMessageBodyWriter<Object> {

protected final ObjectMapper originalMapper;
protected final ObjectWriter defaultWriter;
private final ObjectWriter defaultWriter;
private final ConcurrentMap<ObjectMapper, ObjectWriter> objectWriterMap = new ConcurrentHashMap<>();
private RestClientRequestContext context;

@Inject
public ClientJacksonMessageBodyWriter(ObjectMapper mapper) {
this.originalMapper = mapper;
this.defaultWriter = createDefaultWriter(mapper);
}

Expand All @@ -45,15 +41,17 @@ public boolean isWriteable(Class type, Type genericType, Annotation[] annotation
@Override
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
doLegacyWrite(o, annotations, httpHeaders, entityStream, getEffectiveWriter(mediaType));
doLegacyWrite(o, annotations, httpHeaders, entityStream, getEffectiveWriter(mediaType, null));
}

@Override
public void handle(RestClientRequestContext requestContext) throws Exception {
this.context = requestContext;
public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream,
RestClientRequestContext context) throws IOException, WebApplicationException {
doLegacyWrite(o, annotations, httpHeaders, entityStream, getEffectiveWriter(mediaType, context));
}

protected ObjectWriter getEffectiveWriter(MediaType responseMediaType) {
protected ObjectWriter getEffectiveWriter(MediaType responseMediaType, RestClientRequestContext context) {
ObjectMapper objectMapper = getObjectMapperFromContext(responseMediaType, context);
if (objectMapper == null) {
return defaultWriter;
Expand All @@ -66,5 +64,4 @@ public ObjectWriter apply(ObjectMapper objectMapper) {
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import jakarta.ws.rs.ext.ReaderInterceptor;
import jakarta.ws.rs.ext.ReaderInterceptorContext;

import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.client.spi.ClientMessageBodyReader;
import org.jboss.resteasy.reactive.client.spi.MissingMessageBodyReaderErrorMessageContextualizer;
import org.jboss.resteasy.reactive.common.core.Serialisers;
import org.jboss.resteasy.reactive.common.jaxrs.ConfigurationImpl;
Expand Down Expand Up @@ -76,15 +76,16 @@ public Object proceed() throws IOException, WebApplicationException {
for (MessageBodyReader<?> reader : readers) {
if (reader.isReadable(entityClass, entityType, annotations, mediaType)) {
try {
if (reader instanceof ClientRestHandler) {
try {
((ClientRestHandler) reader).handle(clientRequestContext);
} catch (Exception e) {
throw new WebApplicationException("Can't inject the client request context", e);
}
if (reader instanceof ClientMessageBodyReader) {
return ((ClientMessageBodyReader) reader).readFrom(entityClass, entityType, annotations, mediaType,
headers,
inputStream, clientRequestContext);
} else {
return ((MessageBodyReader) reader).readFrom(entityClass, entityType, annotations, mediaType,
headers,
inputStream);
}
return ((MessageBodyReader) reader).readFrom(entityClass, entityType, annotations, mediaType, headers,
inputStream);

} catch (IOException e) {
throw new ProcessingException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.client.providers.serialisers.ClientDefaultTextPlainBodyHandler;
import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.client.spi.ClientMessageBodyWriter;
import org.jboss.resteasy.reactive.common.core.Serialisers;
import org.jboss.resteasy.reactive.common.core.UnmanagedBeanFactory;
import org.jboss.resteasy.reactive.common.jaxrs.ConfigurationImpl;
Expand Down Expand Up @@ -115,16 +115,13 @@ public static Buffer invokeClientWriter(Entity<?> entity, Object entityObject, C
if (writer.isWriteable(entityClass, entityType, entity.getAnnotations(), entity.getMediaType())) {
if ((writerInterceptors == null) || writerInterceptors.length == 0) {
VertxBufferOutputStream out = new VertxBufferOutputStream();
if (writer instanceof ClientRestHandler) {
try {
((ClientRestHandler) writer).handle(clientRequestContext);
} catch (Exception e) {
throw new WebApplicationException("Can't inject the client request context", e);
}
if (writer instanceof ClientMessageBodyWriter cw) {
cw.writeTo(entityObject, entityClass, entityType, entity.getAnnotations(),
entity.getMediaType(), headerMap, out, clientRequestContext);
} else {
writer.writeTo(entityObject, entityClass, entityType, entity.getAnnotations(),
entity.getMediaType(), headerMap, out);
}

writer.writeTo(entityObject, entityClass, entityType, entity.getAnnotations(),
entity.getMediaType(), headerMap, out);
return out.getBuffer();
} else {
return runClientWriterInterceptors(entityObject, entityClass, entityType, entity.getAnnotations(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import jakarta.ws.rs.ext.WriterInterceptor;
import jakarta.ws.rs.ext.WriterInterceptorContext;

import org.jboss.resteasy.reactive.client.spi.ClientRestHandler;
import org.jboss.resteasy.reactive.client.spi.ClientMessageBodyWriter;
import org.jboss.resteasy.reactive.common.core.Serialisers;
import org.jboss.resteasy.reactive.common.jaxrs.ConfigurationImpl;

Expand Down Expand Up @@ -70,16 +70,14 @@ public void proceed() throws IOException, WebApplicationException {
effectiveWriter = newWriters.get(0);
}

if (effectiveWriter instanceof ClientRestHandler) {
try {
((ClientRestHandler) effectiveWriter).handle(clientRequestContext);
} catch (Exception e) {
throw new WebApplicationException("Can't inject the client request context", e);
}
if (effectiveWriter instanceof ClientMessageBodyWriter cw) {
cw.writeTo(entity, entityClass, entityType,
annotations, mediaType, headers, outputStream, clientRequestContext);
} else {
effectiveWriter.writeTo(entity, entityClass, entityType,
annotations, mediaType, headers, outputStream);
}

effectiveWriter.writeTo(entity, entityClass, entityType,
annotations, mediaType, headers, outputStream);
outputStream.close();
result = Buffer.buffer(baos.toByteArray());
done = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jboss.resteasy.reactive.client.spi;

import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyReader;

import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;

public interface ClientMessageBodyReader<T> extends MessageBodyReader<T> {

T readFrom(Class<T> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream,
RestClientRequestContext context) throws java.io.IOException, jakarta.ws.rs.WebApplicationException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.resteasy.reactive.client.spi;

import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;

import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext;

public interface ClientMessageBodyWriter<T> extends MessageBodyWriter<T> {

void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream,
RestClientRequestContext context)
throws java.io.IOException, jakarta.ws.rs.WebApplicationException;

}

0 comments on commit cae6630

Please sign in to comment.