KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > servlet > UrlProcessor


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.servlet;
17
18 import java.io.IOException JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.directwebremoting.Container;
28 import org.directwebremoting.extend.Handler;
29 import org.directwebremoting.extend.InitializingBean;
30 import org.directwebremoting.util.Logger;
31
32 /**
33  * This is the main servlet that handles all the requests to DWR.
34  * <p>It is on the large side because it can't use technologies like JSPs etc
35  * since it all needs to be deployed in a single jar file, and while it might be
36  * possible to integrate Velocity or similar I think simplicity is more
37  * important, and there are only 2 real pages both script heavy in this servlet
38  * anyway.</p>
39  * <p>There are 5 things to do, in the order that you come across them:</p>
40  * <ul>
41  * <li>The index test page that points at the classes</li>
42  * <li>The class test page that lets you execute methods</li>
43  * <li>The interface javascript that uses the engine to send requests</li>
44  * <li>The engine javascript to form the iframe request and process replies</li>
45  * <li>The exec 'page' that executes the method and returns data to the iframe</li>
46  * </ul>
47  * @author Joe Walker [joe at getahead dot ltd dot uk]
48  */

49 public class UrlProcessor implements Handler, InitializingBean
50 {
51     /* (non-Javadoc)
52      * @see org.directwebremoting.InitializingBean#afterPropertiesSet(Container)
53      */

54     public void afterContainerSetup(Container container)
55     {
56         Collection JavaDoc beanNames = container.getBeanNames();
57         for (Iterator JavaDoc it = beanNames.iterator(); it.hasNext();)
58         {
59             String JavaDoc name = (String JavaDoc) it.next();
60             if (name.startsWith(PathConstants.URL_PREFIX))
61             {
62                 Object JavaDoc bean = container.getBean(name);
63
64                 if (bean instanceof Handler)
65                 {
66                     urlMapping.put(name.substring(PathConstants.URL_PREFIX.length()), bean);
67                 }
68                 else
69                 {
70                     log.error("Discarding non Handler for " + name);
71                 }
72             }
73         }
74     }
75
76     /* (non-Javadoc)
77      * @see org.directwebremoting.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
78      */

79     public void handle(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc
80     {
81         try
82         {
83             String JavaDoc pathInfo = request.getPathInfo();
84
85             if (pathInfo == null || pathInfo.length() == 0 || pathInfo.equals("/"))
86             {
87                 response.sendRedirect(request.getContextPath() + request.getServletPath() + indexHandlerUrl);
88             }
89             else
90             {
91                 // Loop through all the known URLs
92
for (Iterator JavaDoc it = urlMapping.entrySet().iterator(); it.hasNext();)
93                 {
94                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
95                     String JavaDoc url = (String JavaDoc) entry.getKey();
96
97                     // If this URL matches, call the handler
98
if (pathInfo.startsWith(url))
99                     {
100                         Handler handler = (Handler) entry.getValue();
101                         handler.handle(request, response);
102                         return;
103                     }
104                 }
105
106                 notFoundHandler.handle(request, response);
107             }
108         }
109         catch (Exception JavaDoc ex)
110         {
111             exceptionHandler.setException(ex);
112             exceptionHandler.handle(request, response);
113         }
114     }
115
116     /**
117      * The URL for the {@link IndexHandler}
118      * @param indexHandlerUrl the indexHandlerUrl to set
119      */

120     public void setIndexHandlerUrl(String JavaDoc indexHandlerUrl)
121     {
122         this.indexHandlerUrl = indexHandlerUrl;
123     }
124
125     /**
126      * The URL for the {@link IndexHandler}
127      */

128     private String JavaDoc indexHandlerUrl;
129
130     /**
131      * The mapping of URLs to {@link Handler}s
132      */

133     private Map JavaDoc urlMapping = new HashMap JavaDoc();
134
135     /**
136      * The default if we have no other action (HTTP-404)
137      */

138     private Handler notFoundHandler = new NotFoundHandler();
139
140     /**
141      * If execution fails, we do this (HTTP-501)
142      */

143     private ExceptionHandler exceptionHandler = new ExceptionHandler();
144
145     /**
146      * The log stream
147      */

148     private static final Logger log = Logger.getLogger(UrlProcessor.class);
149 }
150
Popular Tags