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

« back to all changes in this revision

Viewing changes to org.restlet.test/src/org/restlet/test/jaxrs/services/providers/AppCrazyPersonProvider.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.test.jaxrs.services.providers;
 
35
 
 
36
import java.io.IOException;
 
37
import java.io.OutputStream;
 
38
import java.lang.annotation.Annotation;
 
39
import java.lang.reflect.Type;
 
40
 
 
41
import javax.ws.rs.Produces;
 
42
import javax.ws.rs.core.Context;
 
43
import javax.ws.rs.core.MediaType;
 
44
import javax.ws.rs.core.MultivaluedMap;
 
45
import javax.ws.rs.ext.MessageBodyWriter;
 
46
import javax.ws.rs.ext.Provider;
 
47
import javax.ws.rs.ext.Providers;
 
48
 
 
49
import org.restlet.test.jaxrs.services.others.Person;
 
50
 
 
51
/**
 
52
 * This provider writes a Persons as XML (by JAXB) and with the
 
53
 * {@link TextCrazyPersonProvider}.<br>
 
54
 * (I've got no better idea for Providers)
 
55
 * 
 
56
 * @author Stephan Koops
 
57
 */
 
58
@Produces("application/crazy-person")
 
59
@Provider
 
60
public class AppCrazyPersonProvider implements MessageBodyWriter<Person> {
 
61
 
 
62
    @Context
 
63
    Providers providers;
 
64
 
 
65
    /**
 
66
     * @see javax.ws.rs.ext.MessageBodyWriter#getSize(java.lang.Object)
 
67
     */
 
68
    public long getSize(Person t, Class<?> type, Type genericType,
 
69
            Annotation[] annotations, MediaType mediaType) {
 
70
        return -1;
 
71
    }
 
72
 
 
73
    /**
 
74
     * @see MessageBodyWriter#isWriteable(Class, Type, Annotation[])
 
75
     */
 
76
    public boolean isWriteable(Class<?> type, Type genericType,
 
77
            Annotation[] annotations, MediaType mediaType) {
 
78
        return Person.class.isAssignableFrom(type);
 
79
    }
 
80
 
 
81
    /**
 
82
     * @see MessageBodyWriter#writeTo(Object, Class, Type, Annotation[],
 
83
     *      MediaType, MultivaluedMap, OutputStream)
 
84
     */
 
85
    public void writeTo(Person t, Class<?> type, Type genericType,
 
86
            Annotation[] annotations, MediaType mediaType,
 
87
            MultivaluedMap<String, Object> httpHeaders,
 
88
            OutputStream entityStream) throws IOException {
 
89
        MessageBodyWriter<Person> mbw;
 
90
        mbw = this.providers.getMessageBodyWriter(Person.class, Person.class,
 
91
                annotations, MediaType.APPLICATION_XML_TYPE);
 
92
        mbw.writeTo(t, Person.class, Person.class, annotations,
 
93
                MediaType.APPLICATION_XML_TYPE, httpHeaders, entityStream);
 
94
 
 
95
        final MediaType mediaType2 = new MediaType("text", "crazy-person");
 
96
        mbw = this.providers.getMessageBodyWriter(Person.class, Person.class,
 
97
                annotations, mediaType2);
 
98
        mbw.writeTo(t, Person.class, Person.class, annotations, mediaType2,
 
99
                httpHeaders, entityStream);
 
100
    }
 
101
}