📄 urlprocessor.java
字号:
/*
* Copyright 2005 Joe Walker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.directwebremoting.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.directwebremoting.Calls;
import org.directwebremoting.Container;
import org.directwebremoting.DebugPageGenerator;
import org.directwebremoting.DwrConstants;
import org.directwebremoting.Remoter;
import org.directwebremoting.Replies;
import org.directwebremoting.ScriptSessionManager;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.dwrp.DwrpHtmlJsMarshaller;
import org.directwebremoting.dwrp.DwrpPlainJsMarshaller;
import org.directwebremoting.impl.DefaultScriptSessionManager;
import org.directwebremoting.util.IdGenerator;
import org.directwebremoting.util.JavascriptUtil;
import org.directwebremoting.util.LocalUtil;
import org.directwebremoting.util.Logger;
import org.directwebremoting.util.MimeConstants;
/**
* This is the main servlet that handles all the requests to DWR.
* <p>It is on the large side because it can't use technologies like JSPs etc
* since it all needs to be deployed in a single jar file, and while it might be
* possible to integrate Velocity or similar I think simplicity is more
* important, and there are only 2 real pages both script heavy in this servlet
* anyway.</p>
* <p>There are 5 things to do, in the order that you come across them:</p>
* <ul>
* <li>The index test page that points at the classes</li>
* <li>The class test page that lets you execute methods</li>
* <li>The interface javascript that uses the engine to send requests</li>
* <li>The engine javascript to form the iframe request and process replies</li>
* <li>The exec 'page' that executes the method and returns data to the iframe</li>
* </ul>
* @author Joe Walker [joe at getahead dot ltd dot uk]
*/
public class UrlProcessor
{
/**
* Handle servlet requests aimed at DWR
* @param request The servlet request
* @param response The servlet response
* @throws IOException If there are IO issues
*/
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException
{
try
{
String pathInfo = request.getPathInfo();
String servletPath = request.getServletPath();
String contextPath = request.getContextPath();
if (nullPathInfoWorkaround && pathInfo == null)
{
pathInfo = request.getServletPath();
servletPath = PathConstants.PATH_ROOT;
log.debug("Default servlet suspected. pathInfo=" + pathInfo + "; contextPath=" + contextPath + "; servletPath=" + servletPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
if (pathInfo == null ||
pathInfo.length() == 0 ||
pathInfo.equals(PathConstants.PATH_ROOT))
{
response.sendRedirect(contextPath + servletPath + PathConstants.FILE_INDEX);
}
else if (pathInfo.startsWith(PathConstants.FILE_INDEX))
{
String page = debugPageGenerator.generateIndexPage(contextPath + servletPath);
response.setContentType(MimeConstants.MIME_HTML);
PrintWriter out = response.getWriter();
out.print(page);
response.flushBuffer();
}
else if (pathInfo.startsWith(PathConstants.PATH_TEST))
{
String scriptName = pathInfo;
scriptName = LocalUtil.replace(scriptName, PathConstants.PATH_TEST, ""); //$NON-NLS-1$
scriptName = LocalUtil.replace(scriptName, PathConstants.PATH_ROOT, ""); //$NON-NLS-1$
String page = debugPageGenerator.generateTestPage(contextPath + servletPath, scriptName);
response.setContentType(MimeConstants.MIME_HTML);
PrintWriter out = response.getWriter();
out.print(page);
response.flushBuffer();
}
else if (pathInfo.startsWith(PathConstants.PATH_INTERFACE))
{
String scriptName = pathInfo;
scriptName = LocalUtil.replace(scriptName, PathConstants.PATH_INTERFACE, ""); //$NON-NLS-1$
scriptName = LocalUtil.replace(scriptName, PathConstants.EXTENSION_JS, ""); //$NON-NLS-1$
String path = contextPath + servletPath;
String script = remoter.generateInterfaceScript(scriptName, path);
// Officially we should use MimeConstants.MIME_JS, but if we cheat and
// use MimeConstants.MIME_PLAIN then it will be easier to read in a
// browser window, and will still work just fine.
response.setContentType(MimeConstants.MIME_PLAIN);
PrintWriter out = response.getWriter();
out.print(script);
response.flushBuffer();
}
else if (pathInfo.startsWith(PathConstants.PATH_PLAINJS))
{
Calls calls = plainJsMarshaller.marshallInbound(request, response);
Replies replies = remoter.execute(calls);
plainJsMarshaller.marshallOutbound(replies, request, response);
}
else if (pathInfo.startsWith(PathConstants.PATH_HTMLJS))
{
Calls calls = htmlJsMarshaller.marshallInbound(request, response);
Replies replies = remoter.execute(calls);
htmlJsMarshaller.marshallOutbound(replies, request, response);
}
else if (pathInfo.equalsIgnoreCase(PathConstants.FILE_ENGINE))
{
doFile(request, response, PathConstants.FILE_ENGINE, MimeConstants.MIME_JS, true);
}
else if (pathInfo.equalsIgnoreCase(PathConstants.FILE_UTIL))
{
doFile(request, response, PathConstants.FILE_UTIL, MimeConstants.MIME_JS, false);
}
else if (pathInfo.startsWith(PathConstants.PATH_STATUS))
{
Container container = WebContextFactory.get().getContainer();
ScriptSessionManager manager = (ScriptSessionManager) container.getBean(ScriptSessionManager.class.getName());
if (manager instanceof DefaultScriptSessionManager)
{
DefaultScriptSessionManager dssm = (DefaultScriptSessionManager) manager;
dssm.debug();
}
}
else
{
log.warn("Page not found (" + pathInfo + "). In debug/test mode try viewing /[WEB-APP]/dwr/"); //$NON-NLS-1$ //$NON-NLS-2$
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
catch (Exception ex)
{
log.warn("Error: " + ex); //$NON-NLS-1$
if (ex instanceof SecurityException && log.isDebugEnabled())
{
log.debug("- User Agent: " + request.getHeader(HttpConstants.HEADER_USER_AGENT)); //$NON-NLS-1$
log.debug("- Remote IP: " + request.getRemoteAddr()); //$NON-NLS-1$
log.debug("- Request URL:" + request.getRequestURL()); //$NON-NLS-1$
log.debug("- Query: " + request.getQueryString()); //$NON-NLS-1$
log.debug("- Method: " + request.getMethod()); //$NON-NLS-1$
ex.printStackTrace();
}
response.setContentType(MimeConstants.MIME_HTML);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
PrintWriter out = response.getWriter();
out.println("<html><head><title>Error</title</head><body>"); //$NON-NLS-1$
out.println("<p><b>Error</b>: " + ex.getMessage() + "</p>"); //$NON-NLS-1$ //$NON-NLS-2$
out.println("<p>For further information about DWR see:</p><ul>"); //$NON-NLS-1$
out.println("<li><a href='http://getahead.ltd.uk/dwr/documentation'>DWR Documentation</a></li>"); //$NON-NLS-1$
out.println("<li><a href='http://getahead.ltd.uk/dwr/support'>DWR Mailing List</a></li>"); //$NON-NLS-1$
out.println("</ul>"); //$NON-NLS-1$
out.println("<script type='text/javascript'>"); //$NON-NLS-1$
out.println("alert('" + ex.getMessage() + "');"); //$NON-NLS-1$ //$NON-NLS-2$
out.println("</script>"); //$NON-NLS-1$
out.println("</body></html>"); //$NON-NLS-1$
out.flush();
}
}
/**
* Basically a file servlet component that does some <b>very limitted</b>
* EL type processing on the file. See the source for the cheat.
* @param request The request from the browser
* @param response The response channel
* @param path The path to search for, process and output
* @param mimeType The mime type to use for this output file
* @param dynamic Should the script be recalculated each time?
* @throws IOException If writing to the output fails
*/
protected void doFile(HttpServletRequest request, HttpServletResponse response, String path, String mimeType, boolean dynamic) throws IOException
{
if (!dynamic && isUpToDate(request, path))
{
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
String output;
synchronized (scriptCache)
{
output = (String) scriptCache.get(path);
if (output == null)
{
StringBuffer buffer = new StringBuffer();
String resource = DwrConstants.PACKAGE + path;
InputStream raw = getClass().getResourceAsStream(resource);
if (raw == null)
{
throw new IOException("Failed to find resource: " + resource); //$NON-NLS-1$
}
BufferedReader in = new BufferedReader(new InputStreamReader(raw));
while (true)
{
String line = in.readLine();
if (line == null)
{
break;
}
if (dynamic)
{
if (line.indexOf(PARAM_HTTP_SESSIONID) != -1)
{
line = LocalUtil.replace(line, PARAM_HTTP_SESSIONID, request.getSession(true).getId());
}
if (line.indexOf(PARAM_SCRIPT_SESSIONID) != -1)
{
line = LocalUtil.replace(line, PARAM_SCRIPT_SESSIONID, generator.generateId(pageIdLength));
}
}
buffer.append(line);
buffer.append('\n');
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -