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 015package org.apache.activemq.web; 016 017import java.io.ByteArrayOutputStream; 018import java.io.IOException; 019import java.io.InputStream; 020import java.net.URL; 021import java.util.HashMap; 022import java.util.Map; 023 024import javax.servlet.ServletException; 025import javax.servlet.http.HttpServletRequest; 026import 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 */ 037public class AjaxServlet extends MessageListenerServlet { 038 039 private static final long serialVersionUID = -3875280764356406114L; 040 private Map<String, byte[]> jsCache = new HashMap<String, byte[]>(); 041 private long jsLastModified = 1000 * (System.currentTimeMillis() / 1000); 042 043 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 044 if (request.getPathInfo() != null && request.getPathInfo().endsWith(".js")) { 045 doJavaScript(request, response); 046 } else { 047 super.doGet(request, response); 048 } 049 } 050 051 protected void doJavaScript(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 052 053 // Look for a local resource first. 054 String js = request.getServletPath() + request.getPathInfo(); 055 URL url = getServletContext().getResource(js); 056 if (url != null) { 057 getServletContext().getNamedDispatcher("default").forward(request, response); 058 return; 059 } 060 061 // Serve from the classpath resources 062 String resource = "org/apache/activemq/web" + request.getPathInfo(); 063 synchronized (jsCache) { 064 065 byte[] data = jsCache.get(resource); 066 if (data == null) { 067 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); 068 if (in != null) { 069 ByteArrayOutputStream out = new ByteArrayOutputStream(); 070 byte[] buf = new byte[4096]; 071 int len = in.read(buf); 072 while (len >= 0) { 073 out.write(buf, 0, len); 074 len = in.read(buf); 075 } 076 in.close(); 077 out.close(); 078 data = out.toByteArray(); 079 jsCache.put(resource, data); 080 } 081 } 082 083 if (data != null) { 084 085 long ifModified = request.getDateHeader("If-Modified-Since"); 086 087 if (ifModified == jsLastModified) { 088 response.sendError(HttpServletResponse.SC_NOT_MODIFIED); 089 } else { 090 response.setContentType("application/x-javascript"); 091 response.setContentLength(data.length); 092 response.setDateHeader("Last-Modified", jsLastModified); 093 response.getOutputStream().write(data); 094 } 095 } else { 096 response.sendError(HttpServletResponse.SC_NOT_FOUND); 097 } 098 } 099 } 100}