Skip to content

Commit 8f38b23

Browse files
jansupolsenivam
authored andcommitted
Pass Generic Type of OutboundResponse entity to MBW.
Signed-off-by: jansupol <[email protected]>
1 parent c354bd6 commit 8f38b23

File tree

2 files changed

+115
-11
lines changed

2 files changed

+115
-11
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -22,6 +22,7 @@
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
2424
import java.lang.annotation.Annotation;
25+
import java.lang.reflect.Type;
2526
import java.net.URI;
2627
import java.util.Collections;
2728
import java.util.Map;
@@ -77,18 +78,19 @@ public int read() throws IOException {
7778
ByteArrayOutputStream baos = new ByteArrayOutputStream();
7879
OutputStream stream = null;
7980
try {
80-
try {
81-
stream = requestContext.getWorkers().writeTo(
82-
entity, entity.getClass(), null, null, response.getMediaType(),
83-
response.getMetadata(), requestContext.getPropertiesDelegate(), baos,
84-
Collections.<WriterInterceptor>emptyList());
85-
} finally {
86-
if (stream != null) {
87-
stream.close();
88-
}
89-
}
81+
final Type t = response instanceof OutboundJaxrsResponse
82+
? ((OutboundJaxrsResponse) response).getContext().getEntityType()
83+
: null;
84+
stream = requestContext.getWorkers().writeTo(
85+
entity, entity.getClass(), t, null, response.getMediaType(),
86+
response.getMetadata(), requestContext.getPropertiesDelegate(), baos,
87+
Collections.<WriterInterceptor>emptyList());
9088
} catch (IOException e) {
9189
// ignore
90+
} finally {
91+
if (stream != null) {
92+
stream.close();
93+
}
9294
}
9395

9496
byteArrayInputStream = new ByteArrayInputStream(baos.toByteArray());
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.client;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import javax.ws.rs.Produces;
22+
import javax.ws.rs.WebApplicationException;
23+
import javax.ws.rs.client.Client;
24+
import javax.ws.rs.client.ClientBuilder;
25+
import javax.ws.rs.client.ClientRequestContext;
26+
import javax.ws.rs.client.ClientRequestFilter;
27+
import javax.ws.rs.core.GenericEntity;
28+
import javax.ws.rs.core.MediaType;
29+
import javax.ws.rs.core.MultivaluedMap;
30+
import javax.ws.rs.core.Response;
31+
import javax.ws.rs.ext.MessageBodyWriter;
32+
import java.io.IOException;
33+
import java.io.OutputStream;
34+
import java.lang.annotation.Annotation;
35+
import java.lang.reflect.ParameterizedType;
36+
import java.lang.reflect.Type;
37+
import java.nio.charset.StandardCharsets;
38+
import java.util.ArrayList;
39+
import java.util.Arrays;
40+
import java.util.List;
41+
42+
//import static java.nio.charset.StandardCharsets;
43+
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
45+
public class AbortTest {
46+
private static final String TEXT_CSV = "text/csv";
47+
private static final String EXPECTED_CSV = "hello;goodbye\nsalutations;farewell";
48+
private static final List<List<String>> CSV_LIST = Arrays.asList(
49+
Arrays.asList("hello", "goodbye"),
50+
Arrays.asList("salutations", "farewell")
51+
);
52+
53+
@Test
54+
void testAbortWithGenericEntity() {
55+
Client client = ClientBuilder.newBuilder()
56+
.register(AbortRequestFilter.class)
57+
.register(CsvWriter.class)
58+
.build();
59+
String csvString = client.target("http://localhost:8080")
60+
.request(TEXT_CSV)
61+
.get(String.class);
62+
assertEquals(EXPECTED_CSV, csvString);
63+
client.close();
64+
}
65+
66+
public static class AbortRequestFilter implements ClientRequestFilter {
67+
68+
@Override
69+
public void filter(ClientRequestContext requestContext) {
70+
requestContext.abortWith(Response.ok(new GenericEntity<List<List<String>>>(CSV_LIST) {
71+
}).type(TEXT_CSV).build());
72+
}
73+
}
74+
75+
@Produces(TEXT_CSV)
76+
public static class CsvWriter implements MessageBodyWriter<List<List<String>>> {
77+
78+
@Override
79+
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
80+
System.out.println(genericType.getTypeName());
81+
return List.class.isAssignableFrom(type) && genericType instanceof ParameterizedType
82+
&& ((ParameterizedType) genericType).getActualTypeArguments()[0] instanceof ParameterizedType
83+
&& String.class.equals(((ParameterizedType) ((ParameterizedType) genericType).getActualTypeArguments()[0])
84+
.getActualTypeArguments()[0]);
85+
}
86+
87+
@Override
88+
public void writeTo(List<List<String>> csvList, Class<?> type, Type genericType, Annotation[] annotations,
89+
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
90+
throws IOException, WebApplicationException {
91+
List<String> rows = new ArrayList<>();
92+
for (List<String> row : csvList) {
93+
rows.add(String.join(";", row));
94+
}
95+
String csv = String.join("\n", rows);
96+
97+
entityStream.write(csv.getBytes(StandardCharsets.UTF_8));
98+
entityStream.flush();
99+
}
100+
}
101+
102+
}

0 commit comments

Comments
 (0)