/
AbstractJaxbElementProvider.java
177 lines (160 loc) · 6.93 KB
/
AbstractJaxbElementProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Copyright (c) 2010, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.jaxb.internal;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NoContentException;
import javax.ws.rs.ext.Providers;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import org.glassfish.jersey.internal.LocalizationMessages;
import org.glassfish.jersey.message.internal.EntityInputStream;
/**
* An abstract provider for {@link JAXBElement}.
* <p/>
* Implementing classes may extend this class to provide specific marshalling
* and unmarshalling behaviour.
* <p/>
* When unmarshalling a {@link UnmarshalException} will result in a
* {@link WebApplicationException} being thrown with a status of 400
* (Client error), and a {@link JAXBException} will result in a
* {@link WebApplicationException} being thrown with a status of 500
* (Internal Server error).
* <p/>
* When marshalling a {@link JAXBException} will result in a
* {@link WebApplicationException} being thrown with a status of 500
* (Internal Server error).
*
* @author Paul Sandoz
* @author Marek Potociar
*/
public abstract class AbstractJaxbElementProvider extends AbstractJaxbProvider<JAXBElement<?>> {
/**
* Inheritance constructor.
*
* @param providers JAX-RS providers.
*/
public AbstractJaxbElementProvider(Providers providers, Configuration config) {
super(providers, config);
}
/**
* Inheritance constructor.
*
* @param providers JAX-RS providers.
* @param resolverMediaType JAXB component context resolver media type to be used.
*/
public AbstractJaxbElementProvider(Providers providers, MediaType resolverMediaType, Configuration config) {
super(providers, resolverMediaType, config);
}
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return type == JAXBElement.class && genericType instanceof ParameterizedType && isSupported(mediaType);
}
@Override
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return JAXBElement.class.isAssignableFrom(type) && isSupported(mediaType);
}
@Override
public final JAXBElement<?> readFrom(
Class<JAXBElement<?>> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream inputStream) throws IOException {
final EntityInputStream entityStream = EntityInputStream.create(inputStream);
if (entityStream.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
}
final ParameterizedType pt = (ParameterizedType) genericType;
final Class ta = (Class) pt.getActualTypeArguments()[0];
try {
return readFrom(ta, mediaType, getUnmarshaller(ta, mediaType), entityStream);
} catch (UnmarshalException ex) {
throw new BadRequestException(ex);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
/**
* Read JAXB element from an entity stream.
*
* @param type the type that is to be read from the entity stream.
* @param mediaType the media type of the HTTP entity.
* @param unmarshaller JAXB unmarshaller to be used.
* @param entityStream the {@link InputStream} of the HTTP entity. The
* caller is responsible for ensuring that the input stream ends when the
* entity has been consumed. The implementation should not close the input
* stream.
* @return JAXB element representing the entity.
* @throws JAXBException in case entity unmarshalling fails.
*/
protected abstract JAXBElement<?> readFrom(
Class<?> type, MediaType mediaType, Unmarshaller unmarshaller, InputStream entityStream) throws JAXBException;
@Override
public final void writeTo(
JAXBElement<?> t,
Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException {
try {
final Marshaller m = getMarshaller(t.getDeclaredType(), mediaType);
final Charset c = getCharset(mediaType);
if (c != UTF8) {
m.setProperty(Marshaller.JAXB_ENCODING, c.name());
}
setHeader(m, annotations);
writeTo(t, mediaType, c, m, entityStream);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
/**
* Write JAXB element to an entity stream.
*
* @param element JAXB element to be written to an entity stream.
* @param mediaType the media type of the HTTP entity.
* @param charset character set to be used.
* @param marshaller JAXB unmarshaller to be used.
* @param entityStream the {@link InputStream} of the HTTP entity. The
* caller is responsible for ensuring that the input stream ends when the
* entity has been consumed. The implementation should not close the input
* stream.
* @throws JAXBException in case entity marshalling fails.
*/
protected abstract void writeTo(JAXBElement<?> element,
MediaType mediaType,
Charset charset,
Marshaller marshaller,
OutputStream entityStream) throws JAXBException;
}