1 /*
2 * $Id: ServletUtil.java 1375743 2012-08-21 20:05:58Z nlebas $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 package org.apache.tiles.request.servlet;
23
24 import java.io.IOException;
25
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28
29 import org.apache.tiles.request.ApplicationContext;
30 import org.apache.tiles.request.ApplicationAccess;
31 import org.apache.tiles.request.RequestWrapper;
32 import org.apache.tiles.request.Request;
33
34 /**
35 * Utilities for Tiles request servlet support.
36 *
37 * @version $Rev: 1375743 $ $Date: 2012-08-22 06:05:58 +1000 (Wed, 22 Aug 2012) $
38 */
39 public final class ServletUtil {
40
41 /**
42 * Constructor.
43 */
44 private ServletUtil() {
45 }
46
47 /**
48 * Wraps a ServletException to create an IOException with the root cause if present.
49 *
50 * @param ex The exception to wrap.
51 * @param message The message of the exception.
52 * @return The wrapped exception.
53 */
54 public static IOException wrapServletException(ServletException ex,
55 String message) {
56 IOException retValue;
57 Throwable rootCause = ex.getRootCause();
58 if (rootCause != null) {
59 // Replace the ServletException with an IOException, with the root
60 // cause of the first as the cause of the latter.
61 retValue = new IOException(message, rootCause);
62 } else {
63 retValue = new IOException(message, ex);
64 }
65
66 return retValue;
67 }
68
69 /**
70 * Returns the application context getting it from the servlet context. It must be
71 * first saved creating a {@link ServletApplicationContext} and using
72 * {@link ApplicationAccess#register(ApplicationContext)}.
73 *
74 * @param servletContext The servlet context.
75 * @return The application context, if found, <code>null</code> otherwise.
76 */
77 public static ApplicationContext getApplicationContext(ServletContext servletContext) {
78 return (ApplicationContext) servletContext
79 .getAttribute(ApplicationAccess.APPLICATION_CONTEXT_ATTRIBUTE);
80 }
81
82 /**
83 * Opens a TilesRequestContext until it finds a ServletTilesRequestContext.
84 *
85 * @param request The request to open.
86 * @return The servlet-based request context.
87 * @throws NotAServletEnvironmentException If a servlet-based request
88 * context could not be found.
89 */
90 public static ServletRequest getServletRequest(Request request) {
91 Request currentRequest = request;
92 while (true) {
93 if (currentRequest == null) {
94 throw new NotAServletEnvironmentException("Last Tiles request context is null");
95 }
96
97 if (currentRequest instanceof ServletRequest) {
98 return (ServletRequest) currentRequest;
99 }
100 if (!(currentRequest instanceof RequestWrapper)) {
101 throw new NotAServletEnvironmentException("Not a Servlet environment, not supported");
102 }
103 currentRequest = ((RequestWrapper) currentRequest).getWrappedRequest();
104 }
105 }
106
107 /**
108 * Gets a servlet context from a TilesApplicationContext.
109 *
110 * @param applicationContext The application context to analyze.
111 * @return The servlet context.
112 * @throws NotAServletEnvironmentException If the application context is not
113 * servlet-based.
114 */
115 public static ServletContext getServletContext(ApplicationContext applicationContext) {
116 if (applicationContext instanceof ServletApplicationContext) {
117 return (ServletContext) ((ServletApplicationContext) applicationContext).getContext();
118 }
119
120 throw new NotAServletEnvironmentException("Not a Servlet-based environment");
121 }
122 }