View Javadoc
1 /* 2 * Copyright (c) 2003 3 * Information Desire GmbH 4 * All rights reserved. 5 */ 6 package com.infodesire.infobit.servlet; 7 8 import com.infodesire.infobit.AclManager; 9 import com.infodesire.infobit.InfobitConfiguration; 10 import com.infodesire.infobit.InfobitException; 11 import com.infodesire.infobit.InfobitPool; 12 import com.infodesire.infobit.InfobitManager; 13 import com.infodesire.infobit.InfobitSecurityException; 14 15 import com.infodesire.infobit.data.Acl; 16 import com.infodesire.infobit.data.BinaryContent; 17 import com.infodesire.infobit.data.Content; 18 import com.infodesire.infobit.data.Infobit; 19 import com.infodesire.infobit.data.Version; 20 21 import com.infodesire.infobit.render.InfobitRenderer; 22 import com.infodesire.infobit.render.velocity.VelocityRenderer; 23 24 import net.sf.hibernate.Session; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import java.io.IOException; 30 import java.io.InputStream; 31 import java.io.OutputStream; 32 import java.io.PrintWriter; 33 import java.io.Writer; 34 35 import java.util.HashSet; 36 import java.util.Iterator; 37 import java.util.List; 38 import java.util.Properties; 39 import java.util.Set; 40 41 import javax.servlet.ServletConfig; 42 import javax.servlet.ServletContext; 43 import javax.servlet.ServletException; 44 import javax.servlet.ServletOutputStream; 45 46 import javax.servlet.http.HttpServlet; 47 import javax.servlet.http.HttpServletRequest; 48 import javax.servlet.http.HttpServletResponse; 49 50 /*** 51 * Servlet capable to emit binary content of infobit versions. The servlet 52 * supports get/post methids and does no more than just stream out binary 53 * content with appropriate ( set in content ) mime type. 54 * 55 * @author peter2 56 * @created August 28, 2003 57 * @version $Revision: 1.4 $ 58 */ 59 public class BinaryServlet extends HttpServlet { 60 61 /*** 62 * parameter specifying which infobit pool is to be used 63 */ 64 public final static String POOL_NAME = "poolName"; 65 private static Log _log = LogFactory.getLog(BinaryServlet.class); 66 67 InfobitPool _pool; 68 InfobitManager _manager; 69 private String _poolName; 70 71 72 /*** 73 * initialize servlet for binary infobit streaming 74 * 75 * @param config Description of Parameter 76 * @exception ServletException Description of Exception 77 */ 78 public void init(ServletConfig config) throws ServletException { 79 super.init(config); 80 _poolName = config.getInitParameter(POOL_NAME); 81 try { 82 _log.debug("******* loading infobit pool ***************"); 83 _pool = InfobitConfiguration.getInstance().getPool(_poolName); 84 _log.debug("******* retriving infobit manager ***********"); 85 _manager = _pool.getInfobitManager(); 86 87 // flush infobit pool - our resource loader opens connection 88 // just in case it opened connetcions 89 _pool.flush(); 90 } catch (Exception ex) { 91 _log.error("exception while acquiring infobit pool: " + _poolName, ex); 92 throw new ServletException("exception while acquiring infobit pool: " + _poolName, ex); 93 } 94 } 95 96 97 /*** 98 * Handles GET - calls doRequest() 99 * 100 * @param request servlet request 101 * @param response response 102 * @exception ServletException Description of Exception 103 * @exception IOException Description of Exception 104 */ 105 public void doGet(HttpServletRequest request, HttpServletResponse response) 106 throws ServletException, IOException { 107 doRequest(request, response); 108 } 109 110 111 /*** 112 * Handle a POST request - calls doRequest() 113 * 114 * @param request Description of Parameter 115 * @param response Description of Parameter 116 * @exception ServletException Description of Exception 117 * @exception IOException Description of Exception 118 */ 119 public void doPost(HttpServletRequest request, HttpServletResponse response) 120 throws ServletException, IOException { 121 doRequest(request, response); 122 } 123 124 125 /*** 126 * Handles with both GET and POST requests. we just render infobit out using 127 * renderer 128 * 129 * @param request HttpServletRequest object containing client 130 * request 131 * @param response HttpServletResponse object for the response 132 * @exception ServletException Description of Exception 133 * @exception IOException Description of Exception 134 */ 135 protected void doRequest(HttpServletRequest request, 136 HttpServletResponse response) 137 throws ServletException, IOException { 138 139 String pathInfo = (String) request.getAttribute("javax.servlet.include.path_info"); 140 if (pathInfo == null) { 141 if (_log.isDebugEnabled()) { 142 _log.debug("handling original request"); 143 } 144 // and grab path info out of request 145 pathInfo = request.getPathInfo(); 146 } 147 148 String infobitName = pathInfo.substring(1); 149 if (_log.isDebugEnabled()) { 150 _log.debug("rendering binary infobit : " + infobitName); 151 } 152 153 try { 154 Infobit ib = _manager.getInfobit(infobitName); 155 Version version = ib.getActualVersion(); 156 157 if (version == null) { 158 _log.error("infobit " + infobitName + " nas no actual version"); 159 response.sendError(HttpServletResponse.SC_NO_CONTENT, 160 "infobit " + infobitName + " has no actual version"); 161 return; 162 } 163 164 Content content = version.getContent(); 165 166 if (!(content instanceof BinaryContent)) { 167 _log.error("unexpected content: " + content.getClass().getName()); 168 response.sendError(HttpServletResponse.SC_BAD_REQUEST, 169 "Infobit " + infobitName + " does not have " + 170 "binary content"); 171 return; 172 } 173 174 BinaryContent binary = (BinaryContent) content; 175 InputStream data = _manager.getContent(binary); 176 177 // _log.debug("commited after retriewing stream: " + response.isCommitted()); 178 response.reset(); 179 response.setContentType(binary.getMimeType()); 180 response.setBufferSize(2048); 181 int contentLength = _manager.getContentLength(binary); 182 if (_log.isDebugEnabled()) { 183 _log.debug("serving " + contentLength + " bytes of content"); 184 } 185 response.setContentLength(contentLength); 186 187 _log.debug("commited after status: " + response.isCommitted()); 188 OutputStream stream = response.getOutputStream(); 189 byte[] b = new byte[512]; 190 int n; 191 int l = 0; 192 while ((n = data.read(b)) > -1) { 193 stream.write(b, 0, n); 194 l += n; 195 } 196 197 if (_log.isDebugEnabled()) { 198 _log.debug("wrote content of " + l + " bytes"); 199 } 200 stream.flush(); 201 //stream.close(); 202 //response.flushBuffer(); 203 if (_log.isDebugEnabled()) { 204 _log.debug("successfull. flushed and closed"); 205 } 206 } catch (Exception ex) { 207 _log.error("error occured while rendering infobit", ex); 208 209 } 210 } 211 212 } 213

This page was automatically generated by Maven