001 //========================================================================
002 //Copyright 2006 Mort Bay Consulting Pty. Ltd.
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 //http://www.apache.org/licenses/LICENSE-2.0
008 //Unless required by applicable law or agreed to in writing, software
009 //distributed under the License is distributed on an "AS IS" BASIS,
010 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 //See the License for the specific language governing permissions and
012 //limitations under the License.
013 //========================================================================
014
015 package org.apache.activemq.web;
016
017 import java.io.ByteArrayOutputStream;
018 import java.io.IOException;
019 import java.io.InputStream;
020 import java.net.URL;
021 import java.util.HashMap;
022 import java.util.Map;
023
024 import javax.servlet.ServletException;
025 import javax.servlet.http.HttpServletRequest;
026 import javax.servlet.http.HttpServletResponse;
027
028 /* ------------------------------------------------------------ */
029 /**
030 * AjaxServlet. The AjaxServlet extends the {@link MessageListenerServlet} with
031 * the capability to server the <code>amq.js</code> script and associated
032 * scripts from resources within the activemq-web jar. The amq.js script is the
033 * client side companion to the MessageListenerServlet and supports sending
034 * messages and long polling for receiving messages (Also called Comet style
035 * Ajax).
036 */
037 public class AjaxServlet extends MessageListenerServlet {
038
039 private Map<String, byte[]> jsCache = new HashMap<String, byte[]>();
040 private long jsLastModified = 1000 * (System.currentTimeMillis() / 1000);
041
042 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
043 if (request.getPathInfo() != null && request.getPathInfo().endsWith(".js")) {
044 doJavaScript(request, response);
045 } else {
046 super.doGet(request, response);
047 }
048 }
049
050 protected void doJavaScript(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
051
052 // Look for a local resource first.
053 String js = request.getServletPath() + request.getPathInfo();
054 URL url = getServletContext().getResource(js);
055 if (url != null) {
056 getServletContext().getNamedDispatcher("default").forward(request, response);
057 return;
058 }
059
060 // Serve from the classpath resources
061 String resource = "org/apache/activemq/web" + request.getPathInfo();
062 synchronized (jsCache) {
063
064 byte[] data = jsCache.get(resource);
065 if (data == null) {
066 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
067 if (in != null) {
068 ByteArrayOutputStream out = new ByteArrayOutputStream();
069 byte[] buf = new byte[4096];
070 int len = in.read(buf);
071 while (len >= 0) {
072 out.write(buf, 0, len);
073 len = in.read(buf);
074 }
075 in.close();
076 out.close();
077 data = out.toByteArray();
078 jsCache.put(resource, data);
079 }
080 }
081
082 if (data != null) {
083
084 long ifModified = request.getDateHeader("If-Modified-Since");
085
086 if (ifModified == jsLastModified) {
087 response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
088 } else {
089 response.setContentType("application/x-javascript");
090 response.setContentLength(data.length);
091 response.setDateHeader("Last-Modified", jsLastModified);
092 response.getOutputStream().write(data);
093 }
094 } else {
095 response.sendError(HttpServletResponse.SC_NOT_FOUND);
096 }
097 }
098 }
099 }