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.util.Locale;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  import javax.ws.rs.core.Context;
23  import javax.ws.rs.core.Response;
24  import javax.ws.rs.core.UriInfo;
25  import javax.ws.rs.ext.ExceptionMapper;
26  
27  import org.jboss.resteasy.spi.HttpRequest;
28  import org.jboss.resteasy.spi.HttpResponse;
29  
30  import de.smartics.resteasy.hypermedia.resources.ResourceDiscovery;
31  import de.smartics.resteasy.hypermedia.resources.Resources;
32  
33  /**
34   * Base implementation for handlers of raised exceptions.
35   */
36  public abstract class AbstractExceptionHandler implements
37      ExceptionMapper<Throwable>
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    // --- members --------------------------------------------------------------
44  
45    /**
46     * The response to write to.
47     */
48    @Context
49    protected HttpResponse response;
50  
51    /**
52     * The request to access the request headers.
53     */
54    @Context
55    protected HttpRequest request;
56  
57    /**
58     * The HTTP request.
59     */
60    @Context
61    protected HttpServletRequest servletRequest;
62  
63    /**
64     * The HTTP response.
65     */
66    @Context
67    protected HttpServletResponse servletResponse;
68  
69    /**
70     * Helper to construct paths.
71     */
72    @Context
73    protected UriInfo uriInfo;
74  
75    // ****************************** Initializer *******************************
76  
77    // ****************************** Constructors ******************************
78  
79    // ****************************** Inner Classes *****************************
80  
81    // ********************************* Methods ********************************
82  
83    // --- init -----------------------------------------------------------------
84  
85    // --- get&set --------------------------------------------------------------
86  
87    // --- business -------------------------------------------------------------
88  
89    /**
90     * Provides access to internationalization information.
91     *
92     * @return access to internationalization information.
93     */
94    protected abstract I18nProvider getI18nProvider();
95  
96    /**
97     * Creates the resource context from injected information.
98     *
99     * @return the resource context from injected information.
100    */
101   protected ResourceContext createContext()
102   {
103     final LocalizationProvider l7nProvider = fetchLocalizationProvider();
104     final Resources resources = ResourceDiscovery.createDiscovery();
105     final ResourceContext context =
106         new ResourceContext.Builder().with(servletRequest)
107             .with(servletResponse).with(uriInfo).with(l7nProvider)
108             .with(resources).build();
109     return context;
110   }
111 
112   private LocalizationProvider fetchLocalizationProvider()
113   {
114     final Locale locale = servletRequest.getLocale();
115     final LocalizationProvider l7nProvider =
116         getI18nProvider().getProvider(locale);
117     return l7nProvider;
118   }
119 
120   @Override
121   public Response toResponse(final Throwable exception)
122   {
123     return null;
124   }
125 
126   // --- object basics --------------------------------------------------------
127 }