|
| 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