Skip to content

Commit f9dd572

Browse files
jansupolsenivam
authored andcommitted
Allow HTTP headers to be modified by a writer on aborted response
Signed-off-by: jansupol <[email protected]>
1 parent 8f38b23 commit f9dd572

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

core-client/src/main/java/org/glassfish/jersey/client/ClientResponse.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ public class ClientResponse extends InboundMessageContext implements ClientRespo
6363
* @param response JAX-RS response to be used to initialize the response context.
6464
*/
6565
public ClientResponse(final ClientRequest requestContext, final Response response) {
66-
this(response.getStatusInfo(), requestContext);
67-
this.headers(OutboundJaxrsResponse.from(response, requestContext.getConfiguration()).getContext().getStringHeaders());
66+
super(requestContext.getConfiguration(),
67+
OutboundJaxrsResponse.from(response, requestContext.getConfiguration()).getContext().getStringHeaders(),
68+
false
69+
);
70+
this.requestContext = requestContext;
71+
init(response.getStatusInfo(), requestContext, requestContext.getUri());
6872

6973
final Object entity = response.getEntity();
7074
if (entity != null) {
@@ -122,10 +126,13 @@ public ClientResponse(Response.StatusType status, ClientRequest requestContext)
122126
*/
123127
public ClientResponse(Response.StatusType status, ClientRequest requestContext, URI resolvedRequestUri) {
124128
super(requestContext.getConfiguration());
125-
this.status = status;
126-
this.resolvedUri = resolvedRequestUri;
127129
this.requestContext = requestContext;
130+
init(status, requestContext, resolvedRequestUri);
131+
}
128132

133+
private void init(Response.StatusType status, ClientRequest requestContext, URI resolvedRequestUri) {
134+
this.status = status;
135+
this.resolvedUri = resolvedRequestUri;
129136
setWorkers(requestContext.getWorkers());
130137
}
131138

core-client/src/test/java/org/glassfish/jersey/client/AbortTest.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package org.glassfish.jersey.client;
1818

19+
import org.junit.jupiter.api.Assertions;
1920
import org.junit.jupiter.api.Test;
2021

22+
import javax.ws.rs.Priorities;
2123
import javax.ws.rs.Produces;
2224
import javax.ws.rs.WebApplicationException;
2325
import javax.ws.rs.client.Client;
@@ -44,6 +46,7 @@
4446

4547
public class AbortTest {
4648
private static final String TEXT_CSV = "text/csv";
49+
private static final String TEXT_HEADER = "text/header";
4750
private static final String EXPECTED_CSV = "hello;goodbye\nsalutations;farewell";
4851
private static final List<List<String>> CSV_LIST = Arrays.asList(
4952
Arrays.asList("hello", "goodbye"),
@@ -77,7 +80,6 @@ public static class CsvWriter implements MessageBodyWriter<List<List<String>>> {
7780

7881
@Override
7982
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
80-
System.out.println(genericType.getTypeName());
8183
return List.class.isAssignableFrom(type) && genericType instanceof ParameterizedType
8284
&& ((ParameterizedType) genericType).getActualTypeArguments()[0] instanceof ParameterizedType
8385
&& String.class.equals(((ParameterizedType) ((ParameterizedType) genericType).getActualTypeArguments()[0])
@@ -99,4 +101,33 @@ public void writeTo(List<List<String>> csvList, Class<?> type, Type genericType,
99101
}
100102
}
101103

104+
@Test
105+
void testAbortWithMBWWritingHeaders() {
106+
final String entity = "HI";
107+
final String header = "CUSTOM_HEADER";
108+
try (Response response = ClientBuilder.newClient().register(new ClientRequestFilter() {
109+
@Override
110+
public void filter(ClientRequestContext requestContext) throws IOException {
111+
requestContext.abortWith(Response.ok(entity, TEXT_HEADER).build());
112+
}
113+
}).register(new MessageBodyWriter<String>() {
114+
115+
@Override
116+
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
117+
return mediaType.toString().equals(TEXT_HEADER);
118+
}
119+
120+
@Override
121+
public void writeTo(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
122+
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
123+
WebApplicationException {
124+
httpHeaders.add(header, entity);
125+
entityStream.write(s.getBytes());
126+
}
127+
}, Priorities.USER - 1).target("http://localhost:8080").request().get()) {
128+
Assertions.assertEquals(entity, response.readEntity(String.class));
129+
Assertions.assertEquals(entity, response.getHeaderString(header));
130+
}
131+
}
132+
102133
}

core-common/src/main/java/org/glassfish/jersey/message/internal/InboundMessageContext.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,22 @@ public InboundMessageContext(Configuration configuration) {
157157
* as required by JAX-RS specification on the server side.
158158
*/
159159
public InboundMessageContext(Configuration configuration, boolean translateNce) {
160+
this(configuration, HeaderUtils.createInbound(), translateNce);
161+
}
162+
163+
/**
164+
* Create new inbound message context.
165+
*
166+
* @param configuration the related client/server side {@link Configuration}. If {@code null},
167+
* the default behaviour is expected.
168+
* @param httpHeaders the http headers map.
169+
* @param translateNce if {@code true}, the {@link javax.ws.rs.core.NoContentException} thrown by a
170+
* selected message body reader will be translated into a {@link javax.ws.rs.BadRequestException}
171+
* as required by JAX-RS specification on the server side.
172+
*/
173+
public InboundMessageContext(Configuration configuration, MultivaluedMap<String, String> httpHeaders, boolean translateNce) {
160174
super(configuration);
161-
this.headers = new GuardianStringKeyMultivaluedMap<>(HeaderUtils.createInbound());
175+
this.headers = new GuardianStringKeyMultivaluedMap<>(httpHeaders);
162176
this.entityContent = new EntityContent();
163177
this.translateNce = translateNce;
164178
this.configuration = configuration;

0 commit comments

Comments
 (0)