KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > web > AjaxServlet


1 //========================================================================
2
//Copyright 2006 Mort Bay Consulting Pty. Ltd.
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
//http://www.apache.org/licenses/LICENSE-2.0
8
//Unless required by applicable law or agreed to in writing, software
9
//distributed under the License is distributed on an "AS IS" BASIS,
10
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
//See the License for the specific language governing permissions and
12
//limitations under the License.
13
//========================================================================
14

15 package org.apache.activemq.web;
16
17 import java.io.ByteArrayOutputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28
29 /* ------------------------------------------------------------ */
30 /** AjaxServlet.
31  * The AjaxServlet extends the {@link MessageListenerServlet} with the capability to
32  * server the <code>amq.js</code> script and associated scripts from resources
33  * within the activemq-web jar. The amq.js script is the client side companion to
34  * the MessageListenerServlet and supports sending messages and long polling for
35  * receiving messages (Also called Comet style Ajax).
36  *
37  */

38 public class AjaxServlet extends MessageListenerServlet {
39
40     private Map JavaDoc jsCache = new HashMap JavaDoc();
41     private long jsLastModified = 1000*(System.currentTimeMillis()/1000);
42
43
44     protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
45         if (request.getPathInfo()!=null && request.getPathInfo().endsWith(".js"))
46             doJavaScript(request,response);
47         else
48             super.doGet(request, response);
49     }
50     
51     protected void doJavaScript(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)throws IOException JavaDoc, ServletException JavaDoc {
52         
53         // Look for a local resource first.
54
String JavaDoc js = request.getServletPath()+request.getPathInfo();
55         URL JavaDoc url = getServletContext().getResource(js);
56         if (url!=null)
57         {
58             getServletContext().getNamedDispatcher("default").forward(request,response);
59             return;
60         }
61         
62         // Serve from the classpath resources
63
String JavaDoc resource="org/apache/activemq/web"+request.getPathInfo();
64         synchronized(jsCache){
65             
66             byte[] data = (byte[])jsCache.get(resource);
67             if (data==null) {
68                 InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
69                 if (in!=null)
70                 {
71                     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
72                     byte[] buf=new byte[4096];
73                     int len=in.read(buf);
74                     while (len>=0) {
75                         out.write(buf, 0, len);
76                         len=in.read(buf);
77                     }
78                     in.close();
79                     out.close();
80                     data=out.toByteArray();
81                     jsCache.put(resource, data);
82                 }
83             }
84             
85             if (data!=null){
86                 
87                 long if_modified = request.getDateHeader("If-Modified-Since");
88                 
89                 if (if_modified == jsLastModified) {
90                     response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
91                 }
92                 else {
93                     response.setContentType("application/x-javascript");
94                     response.setContentLength(data.length);
95                     response.setDateHeader("Last-Modified",jsLastModified);
96                     response.getOutputStream().write(data);
97                 }
98             }
99             else
100                 response.sendError(HttpServletResponse.SC_NOT_FOUND);
101         }
102     }
103 }
104
Popular Tags