-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Ensure that Jackson writer doesn't negatively affect endpoint score
- Loading branch information
Showing
7 changed files
with
142 additions
and
111 deletions.
There are no files selected for viewing
91 changes: 0 additions & 91 deletions
91
.../quarkus/resteasy/reactive/jackson/runtime/serialisers/JacksonBasicMessageBodyWriter.java
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
...o/quarkus/resteasy/reactive/jackson/runtime/serialisers/JacksonMessageBodyWriterUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.quarkus.resteasy.reactive.jackson.runtime.serialisers; | ||
|
||
import static org.jboss.resteasy.reactive.common.providers.serialisers.JsonMessageBodyWriterUtil.setContentTypeIfNecessary; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
|
||
import javax.ws.rs.core.MultivaluedMap; | ||
|
||
import com.fasterxml.jackson.annotation.JsonView; | ||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
|
||
public final class JacksonMessageBodyWriterUtil { | ||
|
||
private JacksonMessageBodyWriterUtil() { | ||
} | ||
|
||
public static ObjectWriter createDefaultWriter(ObjectMapper mapper) { | ||
// we don't want the ObjectWriter to close the stream automatically, as we want to handle closing manually at the proper points | ||
JsonFactory jsonFactory = mapper.getFactory(); | ||
if (JacksonMessageBodyWriterUtil.needsNewFactory(jsonFactory)) { | ||
jsonFactory = jsonFactory.copy(); | ||
JacksonMessageBodyWriterUtil.setNecessaryJsonFactoryConfig(jsonFactory); | ||
return mapper.writer().with(jsonFactory); | ||
} else { | ||
return mapper.writer(); | ||
} | ||
} | ||
|
||
private static boolean needsNewFactory(JsonFactory jsonFactory) { | ||
return jsonFactory.isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET) | ||
|| jsonFactory.isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM); | ||
} | ||
|
||
public static void setNecessaryJsonFactoryConfig(JsonFactory jsonFactory) { | ||
jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); | ||
jsonFactory.configure(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM, false); | ||
} | ||
|
||
public static void doLegacyWrite(Object o, Annotation[] annotations, MultivaluedMap<String, Object> httpHeaders, | ||
OutputStream entityStream, ObjectWriter defaultWriter) throws IOException { | ||
setContentTypeIfNecessary(httpHeaders); | ||
if (o instanceof String) { // YUK: done in order to avoid adding extra quotes... | ||
entityStream.write(((String) o).getBytes()); | ||
} else { | ||
if (annotations != null) { | ||
for (Annotation annotation : annotations) { | ||
if (JsonView.class.equals(annotation.annotationType())) { | ||
if (handleJsonView(((JsonView) annotation), o, entityStream, defaultWriter)) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
entityStream.write(defaultWriter.writeValueAsBytes(o)); | ||
} | ||
} | ||
|
||
private static boolean handleJsonView(JsonView jsonView, Object o, OutputStream stream, ObjectWriter defaultWriter) | ||
throws IOException { | ||
if ((jsonView != null) && (jsonView.value().length > 0)) { | ||
defaultWriter.withView(jsonView.value()[0]).writeValue(stream, o); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...rkus/rest/client/reactive/jackson/runtime/serialisers/ClientJacksonMessageBodyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.quarkus.rest.client.reactive.jackson.runtime.serialisers; | ||
|
||
import static io.quarkus.resteasy.reactive.jackson.runtime.serialisers.JacksonMessageBodyWriterUtil.createDefaultWriter; | ||
import static io.quarkus.resteasy.reactive.jackson.runtime.serialisers.JacksonMessageBodyWriterUtil.doLegacyWrite; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyWriter; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
|
||
public class ClientJacksonMessageBodyWriter implements MessageBodyWriter<Object> { | ||
|
||
protected final ObjectMapper originalMapper; | ||
protected final ObjectWriter defaultWriter; | ||
|
||
@Inject | ||
public ClientJacksonMessageBodyWriter(ObjectMapper mapper) { | ||
this.originalMapper = mapper; | ||
this.defaultWriter = createDefaultWriter(mapper); | ||
} | ||
|
||
@Override | ||
public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return true; | ||
} | ||
|
||
@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, defaultWriter); | ||
} | ||
} |