Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / tools / development / PrivilegedJspServlet.java
blob2e2c82324ef606d1b697d8d804ed4c6005603450
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;
17 /**
18 * {@code PrivilegedJspServlet} wraps the Jasper {@link JspServlet}
19 * with {@code doPrivileged} blocks.
22 public class PrivilegedJspServlet extends JspServlet {
24 /**
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";
31 @Override
32 public void init(final ServletConfig config) throws ServletException {
33 try {
34 AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
35 @Override
36 public Object run() throws ServletException {
37 PrivilegedJspServlet.super.init(config);
38 return null;
40 });
41 } catch (PrivilegedActionException ex) {
42 Throwable cause = ex.getException();
43 if (cause instanceof ServletException) {
44 throw (ServletException) cause;
45 } else {
46 throw new RuntimeException(cause);
51 @Override
52 public void service (final HttpServletRequest request, final HttpServletResponse response)
53 throws ServletException, IOException {
54 fixupJspFileAttribute(request);
56 try {
57 AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
58 @Override
59 public Object run() throws IOException, ServletException {
60 PrivilegedJspServlet.super.service(request, response);
61 return null;
63 });
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;
70 } else {
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) {
81 jspFile = "/";
82 } else if (jspFile.charAt(0) != '/') {
83 jspFile = "/" + jspFile;
85 request.setAttribute(JASPER_JSP_FILE, jspFile);