Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ public class ClientResponse extends InboundMessageContext implements ClientRespo
* @param response JAX-RS response to be used to initialize the response context.
*/
public ClientResponse(final ClientRequest requestContext, final Response response) {
this(response.getStatusInfo(), requestContext);
this.headers(OutboundJaxrsResponse.from(response, requestContext.getConfiguration()).getContext().getStringHeaders());
super(requestContext.getConfiguration(),
OutboundJaxrsResponse.from(response, requestContext.getConfiguration()).getContext().getStringHeaders(),
false
);
this.requestContext = requestContext;
init(response.getStatusInfo(), requestContext, requestContext.getUri());

final Object entity = response.getEntity();
if (entity != null) {
Expand Down Expand Up @@ -122,10 +126,13 @@ public ClientResponse(Response.StatusType status, ClientRequest requestContext)
*/
public ClientResponse(Response.StatusType status, ClientRequest requestContext, URI resolvedRequestUri) {
super(requestContext.getConfiguration());
this.status = status;
this.resolvedUri = resolvedRequestUri;
this.requestContext = requestContext;
init(status, requestContext, resolvedRequestUri);
}

private void init(Response.StatusType status, ClientRequest requestContext, URI resolvedRequestUri) {
this.status = status;
this.resolvedUri = resolvedRequestUri;
setWorkers(requestContext.getWorkers());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package org.glassfish.jersey.client;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import javax.ws.rs.Priorities;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Client;
Expand All @@ -44,6 +46,7 @@

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

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

@Test
void testAbortWithMBWWritingHeaders() {
final String entity = "HI";
final String header = "CUSTOM_HEADER";
try (Response response = ClientBuilder.newClient().register(new ClientRequestFilter() {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
requestContext.abortWith(Response.ok(entity, TEXT_HEADER).build());
}
}).register(new MessageBodyWriter<String>() {

@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return mediaType.toString().equals(TEXT_HEADER);
}

@Override
public void writeTo(String s, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
WebApplicationException {
httpHeaders.add(header, entity);
entityStream.write(s.getBytes());
}
}, Priorities.USER - 1).target("http://localhost:8080").request().get()) {
Assertions.assertEquals(entity, response.readEntity(String.class));
Assertions.assertEquals(entity, response.getHeaderString(header));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,22 @@ public InboundMessageContext(Configuration configuration) {
* as required by JAX-RS specification on the server side.
*/
public InboundMessageContext(Configuration configuration, boolean translateNce) {
this(configuration, HeaderUtils.createInbound(), translateNce);
}

/**
* Create new inbound message context.
*
* @param configuration the related client/server side {@link Configuration}. If {@code null},
* the default behaviour is expected.
* @param httpHeaders the http headers map.
* @param translateNce if {@code true}, the {@link javax.ws.rs.core.NoContentException} thrown by a
* selected message body reader will be translated into a {@link javax.ws.rs.BadRequestException}
* as required by JAX-RS specification on the server side.
*/
public InboundMessageContext(Configuration configuration, MultivaluedMap<String, String> httpHeaders, boolean translateNce) {
super(configuration);
this.headers = new GuardianStringKeyMultivaluedMap<>(HeaderUtils.createInbound());
this.headers = new GuardianStringKeyMultivaluedMap<>(httpHeaders);
this.entityContent = new EntityContent();
this.translateNce = translateNce;
this.configuration = configuration;
Expand Down
Loading