~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/provider/SingletonProvider.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.provider;
 
35
 
 
36
import java.io.IOException;
 
37
import java.io.InputStream;
 
38
import java.io.OutputStream;
 
39
import java.lang.annotation.Annotation;
 
40
import java.lang.reflect.InvocationTargetException;
 
41
import java.lang.reflect.Type;
 
42
import java.util.logging.Logger;
 
43
 
 
44
import javax.ws.rs.WebApplicationException;
 
45
import javax.ws.rs.core.Context;
 
46
import javax.ws.rs.core.MultivaluedMap;
 
47
import javax.ws.rs.ext.ExceptionMapper;
 
48
import javax.ws.rs.ext.Providers;
 
49
 
 
50
import org.restlet.data.CharacterSet;
 
51
import org.restlet.data.MediaType;
 
52
import org.restlet.ext.jaxrs.internal.core.CallContext;
 
53
import org.restlet.ext.jaxrs.internal.core.ThreadLocalizedContext;
 
54
import org.restlet.ext.jaxrs.internal.exceptions.IllegalTypeException;
 
55
import org.restlet.ext.jaxrs.internal.exceptions.InjectException;
 
56
import org.restlet.ext.jaxrs.internal.util.Converter;
 
57
import org.restlet.ext.jaxrs.internal.util.Util;
 
58
 
 
59
/**
 
60
 * @author Stephan
 
61
 * 
 
62
 */
 
63
public class SingletonProvider extends AbstractProviderWrapper implements
 
64
        MessageBodyReader, MessageBodyWriter, ContextResolver {
 
65
 
 
66
    /**
 
67
     * the {@link ContextResolver}, if this providers is a
 
68
     * {@link ContextResolver}
 
69
     */
 
70
    private final javax.ws.rs.ext.ContextResolver<?> contextResolver;
 
71
 
 
72
    private final javax.ws.rs.ext.ExceptionMapper<? extends Throwable> excMapper;
 
73
 
 
74
    private final Object jaxRsProvider;
 
75
 
 
76
    /**
 
77
     * The JAX-RS {@link javax.ws.rs.ext.MessageBodyReader} this wrapper
 
78
     * represent.
 
79
     */
 
80
    private final javax.ws.rs.ext.MessageBodyReader<?> reader;
 
81
 
 
82
    private final javax.ws.rs.ext.MessageBodyWriter<Object> writer;
 
83
 
 
84
    /**
 
85
     * Creates a new wrapper for a ProviderWrapper and initializes the provider.
 
86
     * If the given class is not a provider, an {@link IllegalArgumentException}
 
87
     * is thrown.
 
88
     * 
 
89
     * @param jaxRsProvider
 
90
     *            the JAX-RS provider class.
 
91
     * @param logger
 
92
     *            the logger to use.
 
93
     * @throws IllegalArgumentException
 
94
     *             if the class is not a valid provider, may not be instantiated
 
95
     *             or what ever.
 
96
     * @throws WebApplicationException
 
97
     * @see javax.ws.rs.ext.MessageBodyReader
 
98
     * @see javax.ws.rs.ext.MessageBodyWriter
 
99
     * @see javax.ws.rs.ext.ContextResolver
 
100
     */
 
101
    /**
 
102
     * @param jaxRsProvider
 
103
     * @param logger
 
104
     *            needed, if the provider implements no provider interface
 
105
     * @throws IllegalArgumentException
 
106
     * @throws WebApplicationException
 
107
     */
 
108
    @SuppressWarnings("unchecked")
 
109
    public SingletonProvider(Object jaxRsProvider, Logger logger)
 
110
            throws IllegalArgumentException, WebApplicationException {
 
111
        super((jaxRsProvider == null) ? null : jaxRsProvider.getClass());
 
112
        if (jaxRsProvider == null) {
 
113
            throw new IllegalArgumentException(
 
114
                    "The JAX-RS provider class must not be null");
 
115
        }
 
116
        this.jaxRsProvider = jaxRsProvider;
 
117
        boolean isProvider = false;
 
118
        if (jaxRsProvider instanceof javax.ws.rs.ext.MessageBodyWriter) {
 
119
            this.writer = (javax.ws.rs.ext.MessageBodyWriter<Object>) jaxRsProvider;
 
120
            isProvider = true;
 
121
        } else {
 
122
            this.writer = null;
 
123
        }
 
124
        if (jaxRsProvider instanceof javax.ws.rs.ext.MessageBodyReader) {
 
125
            this.reader = (javax.ws.rs.ext.MessageBodyReader<?>) jaxRsProvider;
 
126
            isProvider = true;
 
127
        } else {
 
128
            this.reader = null;
 
129
        }
 
130
        if (jaxRsProvider instanceof javax.ws.rs.ext.ExceptionMapper) {
 
131
            this.excMapper = (javax.ws.rs.ext.ExceptionMapper<? extends Throwable>) jaxRsProvider;
 
132
            isProvider = true;
 
133
        } else {
 
134
            this.excMapper = null;
 
135
        }
 
136
        if (jaxRsProvider instanceof javax.ws.rs.ext.ContextResolver) {
 
137
            this.contextResolver = (javax.ws.rs.ext.ContextResolver<?>) jaxRsProvider;
 
138
            isProvider = true;
 
139
        } else {
 
140
            this.contextResolver = null;
 
141
        }
 
142
        if (!isProvider) {
 
143
            logger.config("The provider "
 
144
                    + jaxRsProvider.getClass()
 
145
                    + " is neither a MessageBodyWriter nor a MessageBodyReader nor a ContextResolver nor an ExceptionMapper");
 
146
        }
 
147
    }
 
148
 
 
149
    /**
 
150
     * Checks, if this MessageBodyReader could read the given type.
 
151
     * 
 
152
     * @param type
 
153
     * @param genericType
 
154
     * @param annotations
 
155
     * @return true, if the wrapped message body reader supports reading for the
 
156
     *         given class with the given parameters.
 
157
     * @see javax.ws.rs.ext.MessageBodyReader#isReadable(Class, Type,
 
158
     *      Annotation[])
 
159
     */
 
160
    public boolean isReadable(Class<?> type, Type genericType,
 
161
            Annotation[] annotations, javax.ws.rs.core.MediaType mediaType) {
 
162
        try {
 
163
            return this.getJaxRsReader().isReadable(type, genericType,
 
164
                    annotations, mediaType);
 
165
        } catch (NullPointerException e) {
 
166
            if (genericType == null || annotations == null) {
 
167
                // interpreted as not readable for the given combination
 
168
                return false;
 
169
            }
 
170
            throw e;
 
171
        } catch (IllegalArgumentException e) {
 
172
            if (genericType == null || annotations == null) {
 
173
                // interpreted as not readable for the given combination
 
174
                return false;
 
175
            }
 
176
            throw e;
 
177
        }
 
178
    }
 
179
 
 
180
    /**
 
181
     * Checks, if the given class could be written by this MessageBodyWriter.
 
182
     * 
 
183
     * @param type
 
184
     * @param genericType
 
185
     * @param annotations
 
186
     * @return true, if the wrapped message writer reader supports writing for
 
187
     *         the given class with the given parameters.
 
188
     * @see javax.ws.rs.ext.MessageBodyWriter#isWriteable(Class)
 
189
     */
 
190
    public boolean isWriteable(Class<?> type, Type genericType,
 
191
            Annotation[] annotations, javax.ws.rs.core.MediaType mediaType) {
 
192
        try {
 
193
            return this.getJaxRsWriter().isWriteable(type, genericType,
 
194
                    annotations, mediaType);
 
195
        } catch (NullPointerException e) {
 
196
            if (genericType == null || annotations == null) {
 
197
                // interpreted as not writable for the given combination
 
198
                return false;
 
199
            }
 
200
            throw e;
 
201
        } catch (IllegalArgumentException e) {
 
202
            if (genericType == null || annotations == null) {
 
203
                // interpreted as not writable for the given combination
 
204
                return false;
 
205
            }
 
206
            throw e;
 
207
        }
 
208
    }
 
209
 
 
210
    @Override
 
211
    public final boolean equals(Object otherProvider) {
 
212
        if (this == otherProvider) {
 
213
            return true;
 
214
        }
 
215
        if (!(otherProvider instanceof SingletonProvider)) {
 
216
            return false;
 
217
        }
 
218
        return this.jaxRsProvider.getClass().equals(
 
219
                ((SingletonProvider) otherProvider).getClass());
 
220
    }
 
221
 
 
222
    /**
 
223
     * @return the JAX-RS provider class name
 
224
     */
 
225
    @Override
 
226
    public String getClassName() {
 
227
        return jaxRsProvider.getClass().getName();
 
228
    }
 
229
 
 
230
    /**
 
231
     * @return the contextResolver
 
232
     */
 
233
    public javax.ws.rs.ext.ContextResolver<?> getContextResolver() {
 
234
        return this.contextResolver;
 
235
    }
 
236
 
 
237
    /**
 
238
     * Returns the {@link ExceptionMapper}, or null, if this provider is not an
 
239
     * {@link ExceptionMapper}.
 
240
     * 
 
241
     * @return the {@link ExceptionMapper}, or null, if this provider is not an
 
242
     *         {@link ExceptionMapper}.
 
243
     */
 
244
    public javax.ws.rs.ext.ExceptionMapper<? extends Throwable> getExcMapper() {
 
245
        return this.excMapper;
 
246
    }
 
247
 
 
248
    /**
 
249
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.ProviderWrapper#getInitializedReader()
 
250
     */
 
251
    public MessageBodyReader getInitializedReader() {
 
252
        return this;
 
253
    }
 
254
 
 
255
    /**
 
256
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.ProviderWrapper#getInitializedWriter()
 
257
     */
 
258
    public MessageBodyWriter getInitializedWriter() {
 
259
        return this;
 
260
    }
 
261
 
 
262
    /**
 
263
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.MessageBodyReader#getJaxRsReader()
 
264
     */
 
265
    public javax.ws.rs.ext.MessageBodyReader<?> getJaxRsReader() {
 
266
        return this.reader;
 
267
    }
 
268
 
 
269
    /**
 
270
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.MessageBodyWriter#getJaxRsWriter()
 
271
     */
 
272
    public javax.ws.rs.ext.MessageBodyWriter<Object> getJaxRsWriter() {
 
273
        return this.writer;
 
274
    }
 
275
 
 
276
    @Override
 
277
    public final int hashCode() {
 
278
        return this.jaxRsProvider.hashCode();
 
279
    }
 
280
 
 
281
    /**
 
282
     * 
 
283
     * @param type
 
284
     * @param genericType
 
285
     *            The generic {@link Type} to convert to.
 
286
     * @param annotations
 
287
     *            the annotations of the artifact to convert to
 
288
     * @param mediaType
 
289
     * @param httpHeaders
 
290
     * @param entityStream
 
291
     * @return the read object
 
292
     * @throws IOException
 
293
     * @see javax.ws.rs.ext.MessageBodyReader#readFrom(Class, Type,
 
294
     *      javax.ws.rs.core.MediaType, Annotation[], MultivaluedMap,
 
295
     *      InputStream)
 
296
     */
 
297
    @SuppressWarnings({ "unchecked", "rawtypes" })
 
298
    public Object readFrom(Class<?> type, Type genericType,
 
299
            Annotation[] annotations, MediaType mediaType,
 
300
            CharacterSet characterSet,
 
301
            MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
 
302
            throws IOException, InvocationTargetException {
 
303
        try {
 
304
            return this.getJaxRsReader().readFrom((Class) type, genericType,
 
305
                    annotations, Converter.toJaxRsMediaType(mediaType),
 
306
                    httpHeaders, entityStream);
 
307
        } catch (Throwable t) {
 
308
            if (t instanceof IOException)
 
309
                throw (IOException) t;
 
310
            if (t instanceof WebApplicationException)
 
311
                throw (WebApplicationException) t;
 
312
 
 
313
            throw new InvocationTargetException(t);
 
314
        }
 
315
    }
 
316
 
 
317
    /**
 
318
     * Write a type to an HTTP response. The response header map is mutable but
 
319
     * any changes must be made before writing to the output stream since the
 
320
     * headers will be flushed prior to writing the response body.
 
321
     * 
 
322
     * @param genericType
 
323
     *            The generic {@link Type} to convert to.
 
324
     * @param annotations
 
325
     *            the annotations of the artifact to convert to
 
326
     * @param mediaType
 
327
     *            the media type of the HTTP entity.
 
328
     * @param httpHeaders
 
329
     *            a mutable map of the HTTP response headers.
 
330
     * @param entityStream
 
331
     *            the {@link OutputStream} for the HTTP entity.
 
332
     * @param object
 
333
     *            the object to write.
 
334
     * 
 
335
     * @throws java.io.IOException
 
336
     *             if an IO error arises
 
337
     * @see javax.ws.rs.ext.MessageBodyWriter#writeTo(Object, Type,
 
338
     *      Annotation[], javax.ws.rs.core.MediaType, MultivaluedMap,
 
339
     *      OutputStream)
 
340
     */
 
341
    public void writeTo(Object object, Class<?> type, Type genericType,
 
342
            Annotation[] annotations, MediaType mediaType,
 
343
            MultivaluedMap<String, Object> httpHeaders,
 
344
            OutputStream entityStream) throws IOException {
 
345
        this.getJaxRsWriter().writeTo(object, type, genericType, annotations,
 
346
                Converter.toJaxRsMediaType(mediaType), httpHeaders,
 
347
                entityStream);
 
348
    }
 
349
 
 
350
    /**
 
351
     * Injects the supported dependencies into this provider and calls the
 
352
     * method annotated with &#64;{@link PostConstruct}.
 
353
     * 
 
354
     * @param tlContext
 
355
     *            The thread local wrapped {@link CallContext}
 
356
     * @param allProviders
 
357
     *            all providers.
 
358
     * @param extensionBackwardMapping
 
359
     *            the extension backward mapping
 
360
     * @throws InjectException
 
361
     * @throws InvocationTargetException
 
362
     *             if a bean setter throws an exception
 
363
     * @throws IllegalTypeException
 
364
     *             if the given class is not valid to be annotated with &#64;
 
365
     *             {@link Context}.
 
366
     * @see ProviderWrapper#initAtAppStartUp(ThreadLocalizedContext, Providers,
 
367
     *      ExtensionBackwardMapping)
 
368
     */
 
369
    public void initAtAppStartUp(ThreadLocalizedContext tlContext,
 
370
            Providers allProviders,
 
371
            ExtensionBackwardMapping extensionBackwardMapping)
 
372
            throws InjectException, InvocationTargetException,
 
373
            IllegalTypeException {
 
374
        initProvider(this.jaxRsProvider, tlContext, allProviders,
 
375
                extensionBackwardMapping);
 
376
    }
 
377
 
 
378
    /**
 
379
     * Returns true, if this ProviderWrapper is also a
 
380
     * {@link javax.ws.rs.ext.ContextResolver}, otherwise false.
 
381
     * 
 
382
     * @return true, if this ProviderWrapper is also a
 
383
     *         {@link javax.ws.rs.ext.ContextResolver}, otherwise false.
 
384
     */
 
385
    @Override
 
386
    public final boolean isContextResolver() {
 
387
        return this.contextResolver != null;
 
388
    }
 
389
 
 
390
    /**
 
391
     * Checks, if this provider represents an {@link ExceptionMapper}.
 
392
     * 
 
393
     * @return true, if this provider is an {@link ExceptionMapper}, or false if
 
394
     *         not.
 
395
     */
 
396
    @Override
 
397
    public final boolean isExceptionMapper() {
 
398
        return this.excMapper != null;
 
399
    }
 
400
 
 
401
    /**
 
402
     * Returns true, if this ProviderWrapper is also a
 
403
     * {@link javax.ws.rs.ext.MessageBodyReader}, otherwise false.
 
404
     * 
 
405
     * @return true, if this ProviderWrapper is also a
 
406
     *         {@link javax.ws.rs.ext.MessageBodyReader}, otherwise false.
 
407
     */
 
408
    @Override
 
409
    public final boolean isReader() {
 
410
        return this.reader != null;
 
411
    }
 
412
 
 
413
    /**
 
414
     * Returns true, if this ProviderWrapper is also a
 
415
     * {@link javax.ws.rs.ext.MessageBodyWriter}, otherwise false.
 
416
     * 
 
417
     * @return true, if this ProviderWrapper is also a
 
418
     *         {@link javax.ws.rs.ext.MessageBodyWriter}, otherwise false.
 
419
     */
 
420
    @Override
 
421
    public final boolean isWriter() {
 
422
        return this.writer != null;
 
423
    }
 
424
 
 
425
    /**
 
426
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.MessageBodyWriter#getSize(java.lang.Object,
 
427
     *      Class, Type, Annotation[], MediaType)
 
428
     */
 
429
    public long getSize(Object t, Class<?> type, Type genericType,
 
430
            Annotation[] annotations, MediaType mediaType) {
 
431
        return this.writer.getSize(t, type, genericType, annotations,
 
432
                Converter.toJaxRsMediaType(mediaType));
 
433
    }
 
434
 
 
435
    /**
 
436
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.ProviderWrapper#getInitializedCtxResolver()
 
437
     */
 
438
    public ContextResolver getInitializedCtxResolver() {
 
439
        return this;
 
440
    }
 
441
 
 
442
    /**
 
443
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.ProviderWrapper#getInitializedExcMapper()
 
444
     */
 
445
    public ExceptionMapper<? extends Throwable> getInitializedExcMapper() {
 
446
        return excMapper;
 
447
    }
 
448
 
 
449
    /**
 
450
     * @see org.restlet.ext.jaxrs.internal.wrappers.provider.ProviderWrapper#getExcMapperType()
 
451
     */
 
452
    public Class<?> getExcMapperType() {
 
453
        return Util.getGenericClass(jaxRsProvider.getClass(),
 
454
                ExceptionMapper.class);
 
455
    }
 
456
}