1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.tools
.development
;
5 import org
.apache
.jasper
.servlet
.JspServlet
;
6 import java
.security
.AccessController
;
7 import java
.security
.PrivilegedExceptionAction
;
8 import java
.security
.PrivilegedActionException
;
10 import javax
.servlet
.ServletException
;
11 import javax
.servlet
.ServletConfig
;
12 import javax
.servlet
.http
.HttpServletRequest
;
13 import javax
.servlet
.http
.HttpServletResponse
;
15 import java
.io
.IOException
;
18 * {@code PrivilegedJspServlet} wraps the Jasper {@link JspServlet}
19 * with {@code doPrivileged} blocks.
22 public class PrivilegedJspServlet
extends JspServlet
{
25 * The request attribute that contains the name of the JSP file, when the
26 * request path doesn't refer directly to the JSP file (for example,
27 * it's instead a servlet mapping).
29 private static final String JASPER_JSP_FILE
= "org.apache.catalina.jsp_file";
32 public void init(final ServletConfig config
) throws ServletException
{
34 AccessController
.doPrivileged(new PrivilegedExceptionAction
<Object
>() {
36 public Object
run() throws ServletException
{
37 PrivilegedJspServlet
.super.init(config
);
41 } catch (PrivilegedActionException ex
) {
42 Throwable cause
= ex
.getException();
43 if (cause
instanceof ServletException
) {
44 throw (ServletException
) cause
;
46 throw new RuntimeException(cause
);
52 public void service (final HttpServletRequest request
, final HttpServletResponse response
)
53 throws ServletException
, IOException
{
54 fixupJspFileAttribute(request
);
57 AccessController
.doPrivileged(new PrivilegedExceptionAction
<Object
>() {
59 public Object
run() throws IOException
, ServletException
{
60 PrivilegedJspServlet
.super.service(request
, response
);
64 } catch (PrivilegedActionException ex
) {
65 Throwable cause
= ex
.getException();
66 if (cause
instanceof IOException
) {
67 throw (IOException
) cause
;
68 } else if (cause
instanceof ServletException
) {
69 throw (ServletException
) cause
;
71 throw new RuntimeException(cause
);
76 private void fixupJspFileAttribute(HttpServletRequest request
) {
77 String jspFile
= (String
)request
.getAttribute(JASPER_JSP_FILE
);
79 if (jspFile
!= null) {
80 if (jspFile
.length() == 0) {
82 } else if (jspFile
.charAt(0) != '/') {
83 jspFile
= "/" + jspFile
;
85 request
.setAttribute(JASPER_JSP_FILE
, jspFile
);