1 22 23 package de.laures.cewolf; 24 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 import java.io.Writer ; 28 import java.util.Enumeration ; 29 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServlet ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import de.laures.cewolf.util.RenderingHelper; 37 38 53 public class CewolfRenderer extends HttpServlet implements WebConstants 54 { 55 56 public static final String INIT_CONFIG = "CewolfRenderer_Init_Config"; 57 private static final String STATE = "state"; 58 private boolean debugged = false; 59 private int requestCount = 0; 60 private Byte lock = Byte.valueOf("0"); 61 private Configuration config = null; 62 63 public void init( ServletConfig servletCfg ) throws ServletException 64 { 65 super.init(servletCfg); 66 67 servletCfg.getServletContext().setAttribute(INIT_CONFIG, servletCfg); 69 config = Configuration.getInstance(servletCfg.getServletContext()); 70 71 if (config != null) 72 this.debugged = config.isDebugged(); 73 else 74 this.debugged = false; 75 } 76 77 89 90 public void printParameters(HttpServletRequest request) 91 { 92 Enumeration enumeration = request.getParameterNames(); 93 while (enumeration.hasMoreElements()) 94 { 95 String cur = (String )enumeration.nextElement(); 96 Object obj = request.getParameter(cur); 97 98 log("Request Parameter -> " + cur + " Value -> " + obj.toString()); 99 } 100 } 101 102 protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException , IOException 103 { 104 if ( debugged ) 105 { 106 logRequest(request); 107 } 108 addHeaders(response); 109 if ( request.getParameter(STATE) != null || !request.getParameterNames().hasMoreElements() ) 110 { 111 requestState(response); 112 return; 113 } 114 synchronized (lock) { 115 requestCount++; 116 } 117 118 int width = 400; 119 int height = 400; 120 boolean removeAfterRendering = false; 121 if ( request.getParameter(REMOVE_AFTER_RENDERING) != null ) 122 { 123 removeAfterRendering = true; 124 } 125 if ( request.getParameter(WIDTH_PARAM) != null ) 126 { 127 width = Integer.parseInt(request.getParameter(WIDTH_PARAM)); 128 } 129 if ( request.getParameter(HEIGHT_PARAM) != null ) 130 { 131 height = Integer.parseInt(request.getParameter(HEIGHT_PARAM)); 132 } 133 134 String imgKey = request.getParameter(IMG_PARAM); 136 if ( imgKey == null ) 137 { 138 logAndRenderException(new ServletException ("no '" + IMG_PARAM + "' parameter provided for Cewolf servlet."), response, width, height); 139 return; 140 } 141 Storage storage = config.getStorage(); 142 ChartImage chartImage = storage.getChartImage(imgKey, request); 143 if ( chartImage == null ) 144 { 145 renderImageExpiry(response, width, height); 146 return; 147 } 148 try 150 { 151 long start = System.currentTimeMillis(); 152 final int size = chartImage.getSize(); 154 response.setContentType(chartImage.getMimeType()); 155 response.setContentLength(size); 156 response.setBufferSize(size); 157 response.setStatus(HttpServletResponse.SC_OK); 158 response.getOutputStream().write(chartImage.getBytes()); 159 long last = System.currentTimeMillis() - start; 160 if ( debugged ) 161 { 162 log("creation time for chart " + imgKey + ": " + last + "ms."); 163 } 164 } 165 catch (Throwable t) 166 { 167 logAndRenderException(t, response, width, height); 168 } 169 finally 170 { 171 if (removeAfterRendering) 172 { 173 try { 174 storage.removeChartImage(imgKey , request); 175 } catch (CewolfException e) { 176 log("Removal of image failed", e); 177 } 178 } 179 } 180 } 181 182 187 private void addHeaders( HttpServletResponse response ) 188 { 189 response.setDateHeader("Expires", System.currentTimeMillis()); 190 } 191 192 198 private void requestState( HttpServletResponse response ) throws IOException 199 { 200 Writer writer = response.getWriter(); 201 writer.write("<HTML><BODY>"); 202 207 writer.write("<b>Cewolf servlet up and running.</b><br>"); 208 writer.write("Requests served so far: " + requestCount); 209 writer.write("</HTML></BODY>"); 210 writer.close(); 211 } 212 213 private void logAndRenderException( Throwable ex, HttpServletResponse response, int width, int height ) throws IOException 214 { 215 log(ex.getMessage(), ex); 216 response.setContentType("image/jpg"); 217 OutputStream out = response.getOutputStream(); 218 RenderingHelper.renderException(ex, width, height, out); 219 out.close(); 220 } 221 222 229 private void renderImageExpiry( HttpServletResponse response, int width, int height ) throws IOException 230 { 231 response.setContentType("image/jpg"); 232 OutputStream out = response.getOutputStream(); 233 RenderingHelper.renderMessage("This chart has expired. Please reload.", width, height, out); 234 out.close(); 235 } 236 237 private void logRequest( HttpServletRequest request ) throws IOException 238 { 239 log("Cewolf request:"); 240 log("Actual Request values:"); 241 printParameters(request); 242 Enumeration headerNames = request.getHeaderNames(); 243 while ( headerNames.hasMoreElements() ) 244 { 245 String name = (String ) headerNames.nextElement(); 246 Enumeration values = request.getHeaders(name); 247 StringBuffer value = new StringBuffer (); 248 while ( values.hasMoreElements() ) 249 { 250 value.append((String ) values.nextElement() + ","); 251 } 252 if ( value.length() > 0 ) 254 value.setLength(value.length() - 1); 255 log(name + ": " + value); 256 } 257 263 } 264 265 } | Popular Tags |