KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > xmlhttprequest > XmlHttpRequestServlet


1 /*
2  * Copyright 2005 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.xmlhttprequest;
19
20 import org.apache.beehive.netui.core.urls.URLRewriterService;
21 import org.apache.beehive.netui.pageflow.interceptor.request.RequestInterceptorContext;
22 import org.apache.beehive.netui.pageflow.interceptor.request.RequestInterceptor;
23 import org.apache.beehive.netui.pageflow.interceptor.Interceptors;
24 import org.apache.beehive.netui.pageflow.interceptor.InterceptorException;
25 import org.apache.beehive.netui.pageflow.interceptor.InterceptorChain;
26 import org.apache.beehive.netui.pageflow.internal.DefaultURLRewriter;
27 import org.apache.beehive.netui.util.logging.Logger;
28
29 import javax.servlet.http.HttpServlet JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.ServletContext JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.List JavaDoc;
36
37 /**
38  * Servlet to handle XMLHttpRequests sent from pages.
39  */

40 public class XmlHttpRequestServlet extends HttpServlet JavaDoc
41 {
42     private static final Logger logger = Logger.getInstance(XmlHttpRequestServlet.class);
43
44     public void init() throws ServletException JavaDoc
45     {
46         ServletContext JavaDoc ctxt = getServletContext();
47         RequestInterceptorContext.init(ctxt);
48         // TODO: does ErrorCRT really need to be an interceptor?
49
RequestInterceptorContext.addInterceptor(ctxt, new ErrorCRI());
50     }
51
52     public void doGet(HttpServletRequest JavaDoc request,
53                       HttpServletResponse JavaDoc response)
54         throws IOException JavaDoc, ServletException JavaDoc
55     {
56         //System.err.println("Inside the XmlHppRequestServlet:" + request.getRequestURI());
57

58         // create an XML empty document, that isn't cached on the client
59
response.setContentType("text/xml");
60         response.setHeader("Pragma", "No-cache");
61         response.setHeader("Cache-Control", "no-cache");
62         
63         ServletContext JavaDoc ctxt = getServletContext();
64         RequestInterceptorContext context = new RequestInterceptorContext(request, response, ctxt);
65         List JavaDoc/*< Interceptor >*/ interceptors = context.getRequestInterceptors();
66
67         // Register the default URLRewriter
68
URLRewriterService.registerURLRewriter(0, request, new DefaultURLRewriter());
69
70         try
71         {
72             Interceptors.doPreIntercept(context, interceptors);
73         }
74         catch (InterceptorException e)
75         {
76             throw new ServletException JavaDoc(e);
77         }
78         
79         // Note that we're not worrying about post-intercept or whether any of the pre-interceptors cancelled the
80
// request, since there's no further processing in the request.
81
}
82
83     public void doPost(HttpServletRequest JavaDoc request,
84                       HttpServletResponse JavaDoc response)
85         throws IOException JavaDoc, ServletException JavaDoc
86     {
87         doGet(request, response);
88     }
89
90     class ErrorCRI extends RequestInterceptor
91     {
92         public void preRequest(RequestInterceptorContext ctxt, InterceptorChain chain) throws InterceptorException
93         {
94             // Create the command by striping off the context path and the extension
95
HttpServletRequest JavaDoc request = ctxt.getRequest();
96             String JavaDoc cmd = request.getRequestURI();
97             String JavaDoc ctxtPath = request.getContextPath();
98
99             // catch any runtime errors here and return.
100
try {
101                 cmd = cmd.substring(ctxtPath.length() + 1);
102                 int idx = cmd.lastIndexOf('.');
103                 if (idx != -1) {
104                     cmd = cmd.substring(0, idx);
105                 }
106
107                 if ("netuiError".equals(cmd)) {
108                     String JavaDoc error = request.getParameter("error");
109                     logger.error("NetUI JavaScript Error:" + error);
110                     System.err.println("NetUI JavaScript Error:" + error);
111                 }
112             }
113             catch (RuntimeException JavaDoc e) {
114                 logger.error("Runtime Error creating XmlRequestServlet Command:" + e.getClass().getName(),e);
115             }
116
117             chain.continueChain();
118         }
119
120         public void postRequest(RequestInterceptorContext context, InterceptorChain chain) throws InterceptorException
121         {
122             chain.continueChain();
123         }
124     }
125 }
126
Popular Tags