Skip to content

Commit 6382438

Browse files
committed
PathProvider supports Path entities, in preparation for future default support in JAX-RS 4 / Jersey 4.
Signed-off-by: Markus KARG <[email protected]>
1 parent 1277084 commit 6382438

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2024 Markus KARG and others.
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.message.internal;
18+
19+
import static jakarta.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
20+
import static jakarta.ws.rs.core.MediaType.WILDCARD;
21+
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.OutputStream;
25+
import java.lang.annotation.Annotation;
26+
import java.lang.reflect.Type;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.nio.file.StandardCopyOption;
30+
31+
import jakarta.ws.rs.Consumes;
32+
import jakarta.ws.rs.Produces;
33+
import jakarta.ws.rs.core.MediaType;
34+
import jakarta.ws.rs.core.MultivaluedMap;
35+
36+
import jakarta.inject.Singleton;
37+
38+
/**
39+
* Provider for marshalling/un-marshalling of {@code application/octet-stream}
40+
* entity type to/from a {@link Path} instance.
41+
*
42+
* @author Markus KARG
43+
*/
44+
@Produces({APPLICATION_OCTET_STREAM, WILDCARD})
45+
@Consumes({APPLICATION_OCTET_STREAM, WILDCARD})
46+
@Singleton
47+
public final class PathProvider extends AbstractMessageReaderWriterProvider<Path> {
48+
49+
@Override
50+
public final boolean isReadable(final Class<?> type,
51+
final Type genericType,
52+
final Annotation[] annotations,
53+
final MediaType mediaType) {
54+
return Path.class == type;
55+
}
56+
57+
@Override
58+
public final Path readFrom(final Class<Path> type,
59+
final Type genericType,
60+
final Annotation[] annotations,
61+
final MediaType mediaType,
62+
final MultivaluedMap<String, String> httpHeaders,
63+
final InputStream entityStream) throws IOException {
64+
final var path = Utils.createTempFile().toPath();
65+
Files.copy(entityStream, path, StandardCopyOption.REPLACE_EXISTING);
66+
return path;
67+
}
68+
69+
@Override
70+
public final boolean isWriteable(final Class<?> type,
71+
final Type genericType,
72+
final Annotation[] annotations,
73+
final MediaType mediaType) {
74+
return Path.class.isAssignableFrom(type);
75+
}
76+
77+
@Override
78+
public final void writeTo(final Path t,
79+
final Class<?> type,
80+
final Type genericType,
81+
final Annotation[] annotations,
82+
final MediaType mediaType,
83+
final MultivaluedMap<String, Object> httpHeaders,
84+
final OutputStream entityStream) throws IOException {
85+
Files.copy(t, entityStream);
86+
}
87+
}

core-common/src/main/resources/META-INF/native-image/org.glassfish.jersey.core/jersey-common/reflect-config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
"allDeclaredMethods":true,
5858
"allDeclaredConstructors":true
5959
},
60+
{
61+
"name":"org.glassfish.jersey.message.internal.PathProvider",
62+
"allDeclaredFields":true,
63+
"allDeclaredMethods":true,
64+
"allDeclaredConstructors":true
65+
},
6066
{
6167
"name":"org.glassfish.jersey.message.internal.FormMultivaluedMapProvider",
6268
"allDeclaredFields":true,

tests/e2e-entity/src/test/java/org/glassfish/jersey/tests/e2e/entity/EntityTypesTest.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, 2022 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2024 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
@@ -72,6 +72,7 @@
7272
import org.glassfish.jersey.internal.util.collection.MultivaluedStringMap;
7373
import org.glassfish.jersey.jettison.JettisonFeature;
7474
import org.glassfish.jersey.message.internal.FileProvider;
75+
import org.glassfish.jersey.message.internal.PathProvider;
7576
import org.glassfish.jersey.server.ResourceConfig;
7677

7778
import org.codehaus.jettison.json.JSONArray;
@@ -325,13 +326,13 @@ public JaxbBean post(final JaxbBeanType t) {
325326

326327
@Override
327328
protected Application configure() {
328-
return ((ResourceConfig) super.configure()).register(new JettisonFeature());
329+
return ((ResourceConfig) super.configure()).register(new JettisonFeature()).register(new PathProvider());
329330
}
330331

331332
@Override
332333
protected void configureClient(final ClientConfig config) {
333334
super.configureClient(config);
334-
config.register(new JettisonFeature());
335+
config.register(new JettisonFeature()).register(new PathProvider());
335336
}
336337

337338
@Test
@@ -431,6 +432,20 @@ public void testFileRepresentation() throws IOException {
431432
_test(in, FileResource.class);
432433
}
433434

435+
@Path("PathResource")
436+
public static class PathResource extends AResource<java.nio.file.Path> {
437+
}
438+
439+
@Test
440+
@Execution(ExecutionMode.CONCURRENT)
441+
public void testPathRepresentation() throws IOException {
442+
final var pp = new PathProvider();
443+
final var in = pp.readFrom(java.nio.file.Path.class, java.nio.file.Path.class, null, null, null,
444+
new ByteArrayInputStream("CONTENT".getBytes()));
445+
446+
_test(in, PathResource.class);
447+
}
448+
434449
@Produces("application/x-www-form-urlencoded")
435450
@Consumes("application/x-www-form-urlencoded")
436451
@Path("FormResource")

0 commit comments

Comments
 (0)