~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.ext.jaxrs/src/org/restlet/ext/jaxrs/internal/wrappers/params/EntityGetter.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
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.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
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
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.ext.jaxrs.internal.wrappers.params;
 
35
 
 
36
import java.io.IOException;
 
37
import java.lang.annotation.Annotation;
 
38
import java.lang.reflect.Type;
 
39
import java.lang.reflect.InvocationTargetException;
 
40
 
 
41
import javax.ws.rs.WebApplicationException;
 
42
import javax.ws.rs.core.MultivaluedMap;
 
43
 
 
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;
 
54
 
 
55
/**
 
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.
 
60
 * 
 
61
 * @author Stephan Koops
 
62
 */
 
63
public class EntityGetter implements ParamGetter {
 
64
 
 
65
    private final Annotation[] annotations;
 
66
 
 
67
    /**
 
68
     * The class to convert to, directly as the type in the parameter list.
 
69
     */
 
70
    protected volatile Class<?> convToCl;
 
71
 
 
72
    private final Type convToGen;
 
73
 
 
74
    private final MessageBodyReaderSet mbrs;
 
75
 
 
76
    protected final ThreadLocalizedContext tlContext;
 
77
 
 
78
    EntityGetter(Class<?> convToCl, Type convToGen,
 
79
            ThreadLocalizedContext tlContext, MessageBodyReaderSet mbrs,
 
80
            Annotation[] annotations) {
 
81
        this.tlContext = tlContext;
 
82
        this.mbrs = mbrs;
 
83
        this.convToCl = convToCl;
 
84
        this.convToGen = convToGen;
 
85
        this.annotations = annotations;
 
86
    }
 
87
 
 
88
    /**
 
89
     * @throws ConvertRepresentationException
 
90
     * @throws WebApplicationException
 
91
     * @throws NoMessageBodyReaderException
 
92
     * @see IntoRrcInjector.AbstractInjectObjectGetter#getValue(String)
 
93
     */
 
94
    public Object getValue() throws ConvertRepresentationException, InvocationTargetException {
 
95
        final Request request = this.tlContext.get().getRequest();
 
96
        final Representation entity = request.getEntity();
 
97
        if (entity == null) {
 
98
            return null;
 
99
        }
 
100
        final MediaType mediaType = entity.getMediaType();
 
101
        final MessageBodyReader mbr = this.mbrs.getBestReader(this.convToCl,
 
102
                this.convToGen, this.annotations, mediaType);
 
103
        if (mbr == null) {
 
104
            throw new NoMessageBodyReaderException(mediaType, this.convToCl);
 
105
        }
 
106
        final MultivaluedMap<String, String> httpHeaders = Util
 
107
                .getJaxRsHttpHeaders(request);
 
108
        try {
 
109
            return mbr.readFrom(this.convToCl, this.convToGen,
 
110
                    this.annotations, mediaType, entity.getCharacterSet(), httpHeaders, entity
 
111
                            .getStream());
 
112
        } catch (WebApplicationException wae) {
 
113
            throw wae;
 
114
        } catch (IOException e) {
 
115
            throw ConvertRepresentationException.object(this.convToCl,
 
116
                    "the message body", e);
 
117
        } finally {
 
118
            entity.release();
 
119
        }
 
120
    }
 
121
}