-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add JAXRS3Decoder to allow using jakarta.ws.rs.core.Response as respo… #2361
Open
chrste90
wants to merge
1
commit into
OpenFeign:master
Choose a base branch
from
chrste90:add-jaxrs-response-decoder
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package feign.jaxrs3; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import feign.Response; | ||
import feign.codec.Decoder; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
public class JAXRS3Decoder implements Decoder { | ||
|
||
private final Decoder delegate; | ||
private final ObjectMapper objectMapper; | ||
|
||
public JAXRS3Decoder(Decoder delegate, ObjectMapper objectMapper) { | ||
this.delegate = delegate; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public Object decode(Response response, Type type) throws IOException { | ||
|
||
if (jakarta.ws.rs.core.Response.class.equals(type)) { | ||
var bodyString = new String(response.body().asInputStream().readAllBytes(), StandardCharsets.UTF_8); | ||
jakarta.ws.rs.core.Response.ResponseBuilder responseBuilder = jakarta.ws.rs.core.Response | ||
.status(response.status()) | ||
.entity(bodyString); | ||
for (Map.Entry<String, Collection<String>> entry : response.headers().entrySet()) { | ||
responseBuilder.header(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
return new JAXRS3Response(responseBuilder.build(), objectMapper); | ||
} | ||
return delegate.decode(response, type); | ||
} | ||
} |
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,171 @@ | ||
package feign.jaxrs3; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
import jakarta.ws.rs.core.*; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.net.URI; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Wrapper class around an existing response. Using jersey with | ||
* jakarta.ws.rs.core.Response | ||
* .entity() | ||
* .build() | ||
* will create an OutboundJaxrsResponse which doesn't implement readEntity-methods. | ||
* Only readEntity-Methods are implemented. Everything else will call the original response methods. | ||
*/ | ||
public class JAXRS3Response extends Response { | ||
|
||
private final Response originalResponse; | ||
private final ObjectMapper objectMapper; | ||
|
||
public JAXRS3Response(Response originalResponse, ObjectMapper objectMapper) { | ||
this.originalResponse = originalResponse; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public int getStatus() { | ||
return originalResponse.getStatus(); | ||
} | ||
|
||
@Override | ||
public StatusType getStatusInfo() { | ||
return originalResponse.getStatusInfo(); | ||
} | ||
|
||
@Override | ||
public Object getEntity() { | ||
return originalResponse.getEntity(); | ||
} | ||
|
||
@Override | ||
public <T> T readEntity(Class<T> entityType) { | ||
return readEntity(TypeFactory.defaultInstance().constructType(entityType)); | ||
} | ||
|
||
@Override | ||
public <T> T readEntity(GenericType<T> entityType) { | ||
return readEntity(objectMapper.constructType(entityType.getType())); | ||
} | ||
|
||
@Override | ||
public <T> T readEntity(Class<T> entityType, Annotation[] annotations) { | ||
return readEntity(entityType); | ||
} | ||
|
||
@Override | ||
public <T> T readEntity(GenericType<T> entityType, Annotation[] annotations) { | ||
return readEntity(entityType); | ||
} | ||
|
||
@Override | ||
public boolean hasEntity() { | ||
return originalResponse.hasEntity(); | ||
} | ||
|
||
@Override | ||
public boolean bufferEntity() { | ||
return originalResponse.bufferEntity(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
originalResponse.close(); | ||
} | ||
|
||
@Override | ||
public MediaType getMediaType() { | ||
return originalResponse.getMediaType(); | ||
} | ||
|
||
@Override | ||
public Locale getLanguage() { | ||
return originalResponse.getLanguage(); | ||
} | ||
|
||
@Override | ||
public int getLength() { | ||
return originalResponse.getLength(); | ||
} | ||
|
||
@Override | ||
public Set<String> getAllowedMethods() { | ||
return originalResponse.getAllowedMethods(); | ||
} | ||
|
||
@Override | ||
public Map<String, NewCookie> getCookies() { | ||
return originalResponse.getCookies(); | ||
} | ||
|
||
@Override | ||
public EntityTag getEntityTag() { | ||
return originalResponse.getEntityTag(); | ||
} | ||
|
||
@Override | ||
public Date getDate() { | ||
return originalResponse.getDate(); | ||
} | ||
|
||
@Override | ||
public Date getLastModified() { | ||
return originalResponse.getLastModified(); | ||
} | ||
|
||
@Override | ||
public URI getLocation() { | ||
return originalResponse.getLocation(); | ||
} | ||
|
||
@Override | ||
public Set<Link> getLinks() { | ||
return originalResponse.getLinks(); | ||
} | ||
|
||
@Override | ||
public boolean hasLink(String s) { | ||
return originalResponse.hasLink(s); | ||
} | ||
|
||
@Override | ||
public Link getLink(String s) { | ||
return originalResponse.getLink(s); | ||
} | ||
|
||
@Override | ||
public Link.Builder getLinkBuilder(String s) { | ||
return originalResponse.getLinkBuilder(s); | ||
} | ||
|
||
@Override | ||
public MultivaluedMap<String, Object> getMetadata() { | ||
return originalResponse.getMetadata(); | ||
} | ||
|
||
@Override | ||
public MultivaluedMap<String, String> getStringHeaders() { | ||
return originalResponse.getStringHeaders(); | ||
} | ||
|
||
@Override | ||
public String getHeaderString(String s) { | ||
return originalResponse.getHeaderString(s); | ||
} | ||
|
||
private <T> T readEntity(JavaType type) { | ||
try { | ||
return objectMapper.readValue((String) getEntity(), type); | ||
} catch (JsonProcessingException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jax-rs don't have a way to parse this without we manually parsing it using jackson?