View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.resteasy.hypermedia.renderer;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  import java.lang.annotation.Annotation;
21  import java.lang.reflect.Type;
22  import java.util.Locale;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import javax.ws.rs.WebApplicationException;
27  import javax.ws.rs.core.Context;
28  import javax.ws.rs.core.MediaType;
29  import javax.ws.rs.core.MultivaluedMap;
30  import javax.ws.rs.core.UriInfo;
31  import javax.ws.rs.ext.MessageBodyWriter;
32  
33  import de.smartics.resteasy.hypermedia.resources.ResourceDiscovery;
34  import de.smartics.resteasy.hypermedia.resources.Resources;
35  
36  /**
37   * Base implementation to pass rendering information to representation
38   * renderers.
39   *
40   * @param <T> the type of resource whose instances are written to the stream.
41   */
42  public abstract class AbstractRepresentationBodyWriter<T> implements
43      MessageBodyWriter<T>
44  {
45    // ********************************* Fields *********************************
46  
47    // --- constants ------------------------------------------------------------
48  
49    // --- members --------------------------------------------------------------
50  
51    /**
52     * Helper to construct paths.
53     */
54    @Context
55    private UriInfo uriInfo;
56  
57    /**
58     * Provides access to the client request.
59     */
60    @Context
61    private HttpServletRequest httpRequest;
62  
63    /**
64     * Provides access to the client response.
65     */
66    @Context
67    private HttpServletResponse httpResponse;
68  
69    // ****************************** Initializer *******************************
70  
71    // ****************************** Constructors ******************************
72  
73    /**
74     * Default constructor.
75     */
76    protected AbstractRepresentationBodyWriter()
77    {
78    }
79  
80    // ****************************** Inner Classes *****************************
81  
82    // ********************************* Methods ********************************
83  
84    // --- init -----------------------------------------------------------------
85  
86    // --- get&set --------------------------------------------------------------
87  
88    /**
89     * Returns access to internationalization information.
90     *
91     * @return access to internationalization information.
92     */
93    protected abstract I18nProvider getI18nProvider();
94  
95    // --- business -------------------------------------------------------------
96  
97    @Override
98    public long getSize(final T t, final Class<?> type, final Type genericType,
99        final Annotation[] annotations, final MediaType mediaType)
100   {
101     return -1;
102   }
103 
104   // CHECKSTYLE:OFF
105   @Override
106   public void writeTo(final T t, final Class<?> type, final Type genericType,
107       final Annotation[] annotations, final MediaType mediaType,
108       final MultivaluedMap<String, Object> httpHeaders,
109       final OutputStream entityStream) throws IOException,
110     WebApplicationException
111   {
112     final LocalizationProvider l7nProvider = fetchLocalizationProvider();
113     final Resources discovery = ResourceDiscovery.createDiscovery(t, uriInfo);
114     final ResourceContext context =
115         new ResourceContext.Builder().with(httpRequest).with(httpResponse)
116             .with(uriInfo).with(l7nProvider).with(discovery).withType(type)
117             .withGenericType(genericType).with(annotations).with(mediaType)
118             .withHttpHeaders(httpHeaders).build();
119     final RepresentationRenderer<T> renderer = create(context, t, entityStream);
120     renderer.render();
121   }
122 
123   // CHECKSTYLE:ON
124 
125   private LocalizationProvider fetchLocalizationProvider()
126   {
127     final Locale locale = httpRequest.getLocale();
128     final LocalizationProvider l7nProvider =
129         getI18nProvider().getProvider(locale);
130     return l7nProvider;
131   }
132 
133   /**
134    * Creates an instance of the representation renderer.
135    *
136    * @param context the context information to be passed to the representation
137    *          renderer.
138    * @param domainObject the domain object to render.
139    * @param entityStream the stream to write to.
140    * @return the representation renderer instance.
141    */
142   protected abstract RepresentationRenderer<T> create(ResourceContext context,
143       T domainObject, OutputStream entityStream);
144 
145   // --- object basics --------------------------------------------------------
146 
147 }