📄 include.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: Include.java
package org.apache.struts2.components;
import com.opensymphony.xwork2.util.ValueStack;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.RequestUtils;
import org.apache.struts2.util.FastByteArrayOutputStream;
// Referenced classes of package org.apache.struts2.components:
// Component
public class Include extends Component
{
static final class PageResponse extends HttpServletResponseWrapper
{
protected PrintWriter pagePrintWriter;
protected ServletOutputStream outputStream;
private PageOutputStream pageOutputStream;
public FastByteArrayOutputStream getContent()
throws IOException
{
if (pagePrintWriter != null)
pagePrintWriter.flush();
return ((PageOutputStream)getOutputStream()).getBuffer();
}
public ServletOutputStream getOutputStream()
throws IOException
{
if (pageOutputStream == null)
pageOutputStream = new PageOutputStream();
return pageOutputStream;
}
public PrintWriter getWriter()
throws IOException
{
if (pagePrintWriter == null)
pagePrintWriter = new PrintWriter(new OutputStreamWriter(getOutputStream(), getCharacterEncoding()));
return pagePrintWriter;
}
public PageResponse(HttpServletResponse response)
{
super(response);
pageOutputStream = null;
}
}
static final class PageOutputStream extends ServletOutputStream
{
private FastByteArrayOutputStream buffer;
public FastByteArrayOutputStream getBuffer()
throws IOException
{
flush();
return buffer;
}
public void close()
throws IOException
{
buffer.close();
}
public void flush()
throws IOException
{
buffer.flush();
}
public void write(byte b[], int o, int l)
throws IOException
{
buffer.write(b, o, l);
}
public void write(int i)
throws IOException
{
buffer.write(i);
}
public void write(byte b[])
throws IOException
{
buffer.write(b);
}
public PageOutputStream()
{
buffer = new FastByteArrayOutputStream();
}
}
private static final Log _log = LogFactory.getLog(org/apache/struts2/components/Include);
private static String encoding;
private static boolean encodingDefined = true;
protected String value;
private HttpServletRequest req;
private HttpServletResponse res;
private static String defaultEncoding;
public Include(ValueStack stack, HttpServletRequest req, HttpServletResponse res)
{
super(stack);
this.req = req;
this.res = res;
}
public static void setDefaultEncoding(String encoding)
{
defaultEncoding = encoding;
}
public boolean end(Writer writer, String body)
{
String page = findString(value, "value", "You must specify the URL to include. Example: /foo.jsp");
StringBuffer urlBuf = new StringBuffer();
urlBuf.append(page);
if (parameters.size() > 0)
{
urlBuf.append('?');
String concat = "";
for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();)
{
java.util.Map.Entry entry = (java.util.Map.Entry)iter.next();
Object name = entry.getKey();
List values = (List)entry.getValue();
int i = 0;
while (i < values.size())
{
urlBuf.append(concat);
urlBuf.append(name);
urlBuf.append('=');
try
{
urlBuf.append(URLEncoder.encode(values.get(i).toString(), "UTF-8"));
}
catch (Exception e)
{
_log.warn((new StringBuilder()).append("unable to url-encode ").append(values.get(i).toString()).append(", it will be ignored").toString());
}
concat = "&";
i++;
}
}
}
String result = urlBuf.toString();
try
{
include(result, writer, req, res);
}
catch (Exception e)
{
LogFactory.getLog(getClass()).warn((new StringBuilder()).append("Exception thrown during include of ").append(result).toString(), e);
}
return super.end(writer, body);
}
public void setValue(String value)
{
this.value = value;
}
public static String getContextRelativePath(ServletRequest request, String relativePath)
{
String returnValue;
if (relativePath.startsWith("/"))
returnValue = relativePath;
else
if (!(request instanceof HttpServletRequest))
{
returnValue = relativePath;
} else
{
HttpServletRequest hrequest = (HttpServletRequest)request;
String uri = (String)request.getAttribute("javax.servlet.include.servlet_path");
if (uri == null)
uri = RequestUtils.getServletPath(hrequest);
returnValue = (new StringBuilder()).append(uri.substring(0, uri.lastIndexOf('/'))).append('/').append(relativePath).toString();
}
if (returnValue.indexOf("..") != -1)
{
Stack stack = new Stack();
StringTokenizer pathParts = new StringTokenizer(returnValue.replace('\\', '/'), "/");
do
{
if (!pathParts.hasMoreTokens())
break;
String part = pathParts.nextToken();
if (!part.equals("."))
if (part.equals(".."))
stack.pop();
else
stack.push(part);
} while (true);
StringBuffer flatPathBuffer = new StringBuffer();
for (int i = 0; i < stack.size(); i++)
flatPathBuffer.append("/").append(stack.elementAt(i));
returnValue = flatPathBuffer.toString();
}
return returnValue;
}
public void addParameter(String key, Object value)
{
if (value != null)
{
List currentValues = (List)parameters.get(key);
if (currentValues == null)
{
currentValues = new ArrayList();
parameters.put(key, currentValues);
}
currentValues.add(value);
}
}
public static void include(String aResult, Writer writer, ServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String resourcePath = getContextRelativePath(request, aResult);
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
if (rd == null)
throw new ServletException((new StringBuilder()).append("Not a valid resource path:").append(resourcePath).toString());
PageResponse pageResponse = new PageResponse(response);
rd.include((HttpServletRequest)request, pageResponse);
String encoding = getEncoding();
if (encoding != null)
pageResponse.getContent().writeTo(writer, encoding);
else
pageResponse.getContent().writeTo(writer, null);
}
private static String getEncoding()
{
if (encodingDefined)
try
{
encoding = defaultEncoding;
}
catch (IllegalArgumentException e)
{
encoding = System.getProperty("file.encoding");
encodingDefined = false;
}
return encoding;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -