| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* The contents of this file are subject to the terms of either the GNU |
| 7 |
* General Public License Version 2 only ("GPL") or the Common Development |
| 8 |
* and Distribution License("CDDL") (collectively, the "License"). You |
| 9 |
* may not use this file except in compliance with the License. You can |
| 10 |
* obtain a copy of the License at |
| 11 |
* http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 |
* or packager/legal/LICENSE.txt. See the License for the specific |
| 13 |
* language governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* When distributing the software, include this License Header Notice in each |
| 16 |
* file and include the License file at packager/legal/LICENSE.txt. |
| 17 |
* |
| 18 |
* GPL Classpath Exception: |
| 19 |
* Oracle designates this particular file as subject to the "Classpath" |
| 20 |
* exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 |
* file that accompanied this code. |
| 22 |
* |
| 23 |
* Modifications: |
| 24 |
* If applicable, add the following below the License Header, with the fields |
| 25 |
* enclosed by brackets [] replaced by your own identifying information: |
| 26 |
* "Portions Copyright [year] [name of copyright owner]" |
| 27 |
* |
| 28 |
* Contributor(s): |
| 29 |
* If you wish your version of this file to be governed by only the CDDL or |
| 30 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 |
* elects to include this software in this distribution under the [CDDL or GPL |
| 32 |
* Version 2] license." If you don't indicate a single choice of license, a |
| 33 |
* recipient has the option to distribute your version of this file under |
| 34 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 |
* its licensees as provided above. However, if you add GPL Version 2 code |
| 36 |
* and therefore, elected the GPL Version 2 license, then the option applies |
| 37 |
* only if the new code is made subject to such option by the copyright |
| 38 |
* holder. |
| 39 |
*/ |
| 40 |
package com.sun.jersey.server.impl.container.servlet; |
| 41 |
|
| 42 |
import javax.servlet.RequestDispatcher; |
| 43 |
import javax.servlet.ServletConfig; |
| 44 |
import javax.servlet.ServletContext; |
| 45 |
import javax.servlet.ServletException; |
| 46 |
import javax.servlet.http.HttpServletRequest; |
| 47 |
import javax.servlet.http.HttpServletResponse; |
| 48 |
import javax.servlet.http.HttpServletResponseWrapper; |
| 49 |
import javax.servlet.jsp.JspException; |
| 50 |
import javax.servlet.jsp.PageContext; |
| 51 |
import javax.servlet.jsp.tagext.SimpleTagSupport; |
| 52 |
import java.io.IOException; |
| 53 |
import java.io.PrintWriter; |
| 54 |
|
| 55 |
/** |
| 56 |
* Includes a side JSP file for the "resolvingClass" class. |
| 57 |
* |
| 58 |
* <p> |
| 59 |
* This tag looks for a side JSP file of the given name |
| 60 |
* from the inheritance hierarchy of the "resolvingClass" class, |
| 61 |
* and includes the contents of it, just like <jsp:include>. |
| 62 |
* |
| 63 |
* <p> |
| 64 |
* For example, if the "resolvingClass" class is the <tt>Foo</tt> class, |
| 65 |
* which looks like the following: |
| 66 |
* |
| 67 |
* <pre> |
| 68 |
* class Foo extends Bar { ... } |
| 69 |
* class Bar extends Zot { ... } |
| 70 |
* </pre> |
| 71 |
* |
| 72 |
* <p> |
| 73 |
* And if you write: |
| 74 |
* <pre><xmp> |
| 75 |
* <st:include page="abc.jsp"/> |
| 76 |
* </xmp></pre> |
| 77 |
* then, it looks for the following files in this order, |
| 78 |
* and includes the first one found. |
| 79 |
* <ol> |
| 80 |
* <li>a side-file of the Foo class named abc.jsp (/WEB-INF/Foo/abc.jsp) |
| 81 |
* <li>a side-file of the Bar class named abc.jsp (/WEB-INF/Bar/abc.jsp) |
| 82 |
* <li>a side-file of the Zot class named abc.jsp (/WEB-INF/Zot/abc.jsp) |
| 83 |
* </ol> |
| 84 |
* |
| 85 |
* @author Kohsuke Kawaguchi |
| 86 |
* @author [email protected] |
| 87 |
*/ |
| 88 |
public class Include extends SimpleTagSupport { |
| 89 |
|
| 90 |
private Class<?> resolvingClass; |
| 91 |
private String page; |
| 92 |
|
| 93 |
/** |
| 94 |
* Specifies the name of the JSP to be included. |
| 95 |
*/ |
| 96 |
public void setPage(String page) { |
| 97 |
this.page = page; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Specifies the resolving class for which JSP will be included. |
| 102 |
*/ |
| 103 |
public void setResolvingClass(Class<?> resolvingClass) { |
| 104 |
this.resolvingClass = resolvingClass; |
| 105 |
} |
| 106 |
|
| 107 |
private Object getPageObject(String name) { |
| 108 |
return getJspContext().getAttribute(name, PageContext.PAGE_SCOPE); |
| 109 |
} |
| 110 |
|
| 111 |
public void doTag() throws JspException, IOException { |
| 112 |
Class<?> resolvingClass = (Class<?>)getJspContext().getAttribute("resolvingClass", PageContext.REQUEST_SCOPE); |
| 113 |
final Class<?> oldResolvingClass = resolvingClass; |
| 114 |
if (this.resolvingClass != null) { |
| 115 |
resolvingClass = this.resolvingClass; |
| 116 |
} |
| 117 |
|
| 118 |
ServletConfig cfg = (ServletConfig) getPageObject(PageContext.CONFIG); |
| 119 |
ServletContext sc = cfg.getServletContext(); |
| 120 |
|
| 121 |
String basePath = (String)getJspContext().getAttribute("_basePath", PageContext.REQUEST_SCOPE); |
| 122 |
for (Class c = resolvingClass; c != Object.class; c = c.getSuperclass()) { |
| 123 |
String name = basePath + "/" + c.getName().replace('.', '/') + '/' + page; |
| 124 |
if (sc.getResource(name) != null) { |
| 125 |
// Tomcat returns a RequestDispatcher even if the JSP file doesn't exist. |
| 126 |
// so check if the resource exists first. |
| 127 |
RequestDispatcher disp = sc.getRequestDispatcher(name); |
| 128 |
if (disp != null) { |
| 129 |
getJspContext().setAttribute("resolvingClass", resolvingClass, PageContext.REQUEST_SCOPE); |
| 130 |
try { |
| 131 |
HttpServletRequest request = (HttpServletRequest) getPageObject(PageContext.REQUEST); |
| 132 |
disp.include( |
| 133 |
request, |
| 134 |
new Wrapper( |
| 135 |
(HttpServletResponse) getPageObject(PageContext.RESPONSE), |
| 136 |
new PrintWriter(getJspContext().getOut()))); |
| 137 |
} catch (ServletException e) { |
| 138 |
throw new JspException(e); |
| 139 |
} finally { |
| 140 |
getJspContext().setAttribute("resolvingClass", oldResolvingClass, PageContext.REQUEST_SCOPE); |
| 141 |
} |
| 142 |
return; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
throw new JspException("Unable to find '" + page + "' for " + resolvingClass); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
class Wrapper extends HttpServletResponseWrapper { |
| 152 |
|
| 153 |
private final PrintWriter pw; |
| 154 |
|
| 155 |
public Wrapper(HttpServletResponse httpServletResponse, PrintWriter w) { |
| 156 |
super(httpServletResponse); |
| 157 |
this.pw = w; |
| 158 |
} |
| 159 |
|
| 160 |
public PrintWriter getWriter() throws IOException { |
| 161 |
return pw; |
| 162 |
} |
| 163 |
} |