2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.ext.jaxrs.internal.wrappers.params;
36
import java.io.IOException;
37
import java.lang.annotation.Annotation;
38
import java.lang.reflect.Type;
39
import java.lang.reflect.InvocationTargetException;
41
import javax.ws.rs.WebApplicationException;
42
import javax.ws.rs.core.MultivaluedMap;
44
import org.restlet.Request;
45
import org.restlet.data.MediaType;
46
import org.restlet.ext.jaxrs.internal.core.ThreadLocalizedContext;
47
import org.restlet.ext.jaxrs.internal.exceptions.ConvertRepresentationException;
48
import org.restlet.ext.jaxrs.internal.exceptions.NoMessageBodyReaderException;
49
import org.restlet.ext.jaxrs.internal.util.Util;
50
import org.restlet.ext.jaxrs.internal.wrappers.params.ParameterList.ParamGetter;
51
import org.restlet.ext.jaxrs.internal.wrappers.provider.MessageBodyReader;
52
import org.restlet.ext.jaxrs.internal.wrappers.provider.MessageBodyReaderSet;
53
import org.restlet.representation.Representation;
56
* An EntityGetter converts the given entity from the request to the type
57
* requested by the resource method.<br>
58
* This class is not used, if a subclass of {@link Representation} is requested,
59
* see {@link ReprEntityGetter} and its subclasses.
61
* @author Stephan Koops
63
public class EntityGetter implements ParamGetter {
65
private final Annotation[] annotations;
68
* The class to convert to, directly as the type in the parameter list.
70
protected volatile Class<?> convToCl;
72
private final Type convToGen;
74
private final MessageBodyReaderSet mbrs;
76
protected final ThreadLocalizedContext tlContext;
78
EntityGetter(Class<?> convToCl, Type convToGen,
79
ThreadLocalizedContext tlContext, MessageBodyReaderSet mbrs,
80
Annotation[] annotations) {
81
this.tlContext = tlContext;
83
this.convToCl = convToCl;
84
this.convToGen = convToGen;
85
this.annotations = annotations;
89
* @throws ConvertRepresentationException
90
* @throws WebApplicationException
91
* @throws NoMessageBodyReaderException
92
* @see IntoRrcInjector.AbstractInjectObjectGetter#getValue(String)
94
public Object getValue() throws ConvertRepresentationException, InvocationTargetException {
95
final Request request = this.tlContext.get().getRequest();
96
final Representation entity = request.getEntity();
100
final MediaType mediaType = entity.getMediaType();
101
final MessageBodyReader mbr = this.mbrs.getBestReader(this.convToCl,
102
this.convToGen, this.annotations, mediaType);
104
throw new NoMessageBodyReaderException(mediaType, this.convToCl);
106
final MultivaluedMap<String, String> httpHeaders = Util
107
.getJaxRsHttpHeaders(request);
109
return mbr.readFrom(this.convToCl, this.convToGen,
110
this.annotations, mediaType, entity.getCharacterSet(), httpHeaders, entity
112
} catch (WebApplicationException wae) {
114
} catch (IOException e) {
115
throw ConvertRepresentationException.object(this.convToCl,
116
"the message body", e);