001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.servlet;
017:
018: import java.io.IOException;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Map.Entry;
023:
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.commons.logging.Log;
029: import org.directwebremoting.Container;
030: import org.directwebremoting.extend.Handler;
031: import org.directwebremoting.extend.InitializingBean;
032:
033: /**
034: * This is the main servlet that handles all the requests to DWR.
035: * <p>It is on the large side because it can't use technologies like JSPs etc
036: * since it all needs to be deployed in a single jar file, and while it might be
037: * possible to integrate Velocity or similar I think simplicity is more
038: * important, and there are only 2 real pages both script heavy in this servlet
039: * anyway.</p>
040: * <p>There are 5 things to do, in the order that you come across them:</p>
041: * <ul>
042: * <li>The index test page that points at the classes</li>
043: * <li>The class test page that lets you execute methods</li>
044: * <li>The interface javascript that uses the engine to send requests</li>
045: * <li>The engine javascript to form the iframe request and process replies</li>
046: * <li>The exec 'page' that executes the method and returns data to the iframe</li>
047: * </ul>
048: * @author Joe Walker [joe at getahead dot ltd dot uk]
049: */
050: public class UrlProcessor implements Handler, InitializingBean {
051: /* (non-Javadoc)
052: * @see org.directwebremoting.InitializingBean#afterPropertiesSet(Container)
053: */
054: public void afterContainerSetup(Container container) {
055: Collection<String> beanNames = container.getBeanNames();
056: for (String name : beanNames) {
057: if (name.startsWith(PathConstants.URL_PREFIX)) {
058: Object bean = container.getBean(name);
059:
060: if (bean instanceof Handler) {
061: urlMapping.put(name
062: .substring(PathConstants.URL_PREFIX
063: .length()), bean);
064: } else {
065: log.error("Discarding non Handler for " + name);
066: }
067: }
068: }
069: }
070:
071: /* (non-Javadoc)
072: * @see org.directwebremoting.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
073: */
074: public void handle(HttpServletRequest request,
075: HttpServletResponse response) throws IOException {
076: try {
077: String pathInfo = request.getPathInfo();
078: contextPath = request.getContextPath();
079:
080: if (pathInfo == null || pathInfo.length() == 0
081: || "/".equals(pathInfo)) {
082: response.sendRedirect(contextPath
083: + request.getServletPath() + indexHandlerUrl);
084: } else {
085: // Loop through all the known URLs
086: for (Entry<String, Object> entry : urlMapping
087: .entrySet()) {
088: String url = entry.getKey();
089:
090: // If this URL matches, call the handler
091: if (pathInfo.startsWith(url)) {
092: Handler handler = (Handler) entry.getValue();
093: handler.handle(request, response);
094: return;
095: }
096: }
097:
098: notFoundHandler.handle(request, response);
099: }
100: } catch (Exception ex) {
101: exceptionHandler.setException(ex);
102: exceptionHandler.handle(request, response);
103: }
104: }
105:
106: /**
107: * The contextPath cached from the last HTTP servlet request
108: * @return the contextPath
109: */
110: public String getContextPath() {
111: return contextPath;
112: }
113:
114: /**
115: * The URL for the {@link IndexHandler}
116: * @param indexHandlerUrl the indexHandlerUrl to set
117: */
118: public void setIndexHandlerUrl(String indexHandlerUrl) {
119: this .indexHandlerUrl = indexHandlerUrl;
120: }
121:
122: /**
123: * The URL for the {@link IndexHandler}
124: */
125: protected String indexHandlerUrl;
126:
127: /**
128: * The mapping of URLs to {@link Handler}s
129: */
130: protected Map<String, Object> urlMapping = new HashMap<String, Object>();
131:
132: /**
133: * The default if we have no other action (HTTP-404)
134: */
135: protected Handler notFoundHandler = new NotFoundHandler();
136:
137: /**
138: * If execution fails, we do this (HTTP-501)
139: */
140: protected ExceptionHandler exceptionHandler = new ExceptionHandler();
141:
142: /**
143: * The contextPath cached from the last HTTP servlet request
144: */
145: protected String contextPath = null;
146:
147: /**
148: * The log stream
149: */
150: private static final Log log = LogFactory
151: .getLog(UrlProcessor.class);
152: }
|