1 4 5 9 10 package org.openlaszlo.data; 11 12 import java.io.*; 13 import java.net.MalformedURLException ; 14 import java.util.HashMap ; 15 import java.util.StringTokenizer ; 16 import javax.servlet.http.HttpServletRequest ; 17 import javax.servlet.http.HttpServletResponse ; 18 import org.apache.commons.httpclient.URI; 19 import org.apache.commons.httpclient.URIException; 20 import org.apache.log4j.Logger; 21 import org.openlaszlo.utils.LZHttpUtils; 22 import org.openlaszlo.utils.FileUtils; 23 import org.openlaszlo.media.MimeType; 24 25 28 abstract public class DataSource 29 { 30 private static Logger mLogger = Logger.getLogger(DataSource.class); 31 32 37 public abstract String name(); 38 39 55 public abstract Data getData(String app, HttpServletRequest req, 56 HttpServletResponse res, long lastModifiedTime) 57 throws InterruptedIOException, IOException, DataSourceException; 58 59 60 73 final public void getAsSWF( 74 String app, 75 HttpServletRequest req, 76 HttpServletResponse res, 77 Converter converter) 78 throws DataSourceException, ConversionException, IOException { 79 80 Data data = null; 81 InputStream input = null; 82 OutputStream output = null; 83 long size = -1; 84 long since = -1; 85 86 boolean doClientCache = isClientCacheable(req); 88 if (doClientCache) { 89 String hdr = req.getHeader(LZHttpUtils.IF_MODIFIED_SINCE); 90 if (hdr != null) { 91 mLogger.debug("req last modified time: " + hdr); 92 since = LZHttpUtils.getDate(hdr); 93 } 94 } 95 96 mLogger.info("requesting URL: '" + DataSource.getURL(req) + "'"); 97 try { 98 data = getData(app, req, res, since); 99 100 if (data.notModified()) { 101 mLogger.info("NOT_MODIFIED"); 102 res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 103 return; 104 } 105 106 mLogger.debug("got data"); 107 108 if (!data.getMimeType().equals(MimeType.SWF)) { 109 110 input = converter.convertToSWF(data, req, res); 111 size = input.available(); 112 mLogger.debug("converted to " + size + " bytes of SWF"); 113 115 String enc = converter.chooseEncoding(req); 116 if (enc != null && !enc.equals("")) { 117 input = converter.encode(req, input, enc); 118 res.setHeader(LZHttpUtils.CONTENT_ENCODING, enc); 119 size = input.available(); 120 } 121 122 } else { 123 mLogger.debug("remote content was SWF"); 124 input = data.getInputStream(); 125 size = data.size(); 126 } 127 128 if (size != -1) { 129 mLogger.debug("setting content length: " + size); 130 res.setContentLength((int)size); 131 } 132 133 if (doClientCache) { 134 long t = data.lastModified(); 135 if (t != -1) { 136 res.setDateHeader(LZHttpUtils.LAST_MODIFIED, t); 137 } 138 } else { 139 LZHttpUtils.noStore(res); 140 } 141 142 try { 143 output = res.getOutputStream(); 144 long n = FileUtils.sendToStream(input, output); 145 mLogger.info(n + " bytes sent"); 146 } catch (FileUtils.StreamWritingException e) { 147 mLogger.warn("StreamWritingException while responding: " + 148 e.getMessage()); 149 } 150 151 } finally { 152 if (data != null) { 153 data.release(); 154 } 155 FileUtils.close(output); 156 FileUtils.close(input); 157 } 158 } 159 160 171 final public void get( 172 String app, 173 HttpServletRequest req, 174 HttpServletResponse res) 175 throws DataSourceException, IOException { 176 177 Data data = null; 178 InputStream input = null; 179 OutputStream output = null; 180 long size = -1; 181 182 mLogger.info("requesting URL: '" + DataSource.getURL(req) + "'"); 183 184 try { 185 186 data = getData(app, req, res, -1); 187 188 input = data.getInputStream(); 189 size = -1; 198 199 if (size != -1) { 200 mLogger.debug("setting content length: " + size); 201 res.setContentLength((int)size); 202 } 203 204 res.setContentType(data.getMimeType()); 206 207 output = res.getOutputStream(); 208 long n = FileUtils.send(input, output); 209 mLogger.info(n + " bytes sent"); 210 211 } finally { 212 if (data != null) { 213 data.release(); 214 } 215 FileUtils.close(output); 216 FileUtils.close(input); 217 } 218 } 219 220 221 224 final static public boolean isClientCacheable(HttpServletRequest req) { 225 String str = req.getParameter("ccache"); 226 return (str != null && str.equals("true")); 227 } 228 229 230 237 final static public String getURL(HttpServletRequest req) throws 238 MalformedURLException { 239 240 String surl = req.getParameter("url"); 241 if (surl == null) { 242 throw new MalformedURLException ("'url' parameter missing from request"); 243 } 244 245 if (surl.startsWith("file:")) { 248 String protocol = (req.isSecure()?"https":"http"); 249 String host = req.getServerName(); 250 int port = req.getServerPort(); 251 String cp = req.getContextPath(); 252 253 String fpath = surl.substring(5); 255 String uri = req.getRequestURI(); 256 int floc = uri.lastIndexOf("/"); 257 258 surl = protocol + "://" + host + ":" + port 261 + uri.substring(0,floc) + "/" + fpath; 262 263 } 264 265 266 mLogger.debug("'url' is " + surl); 267 return LZHttpUtils.modifyWEBAPP(req, surl); 268 } 269 270 271 279 final static public HashMap getQueryString(String url) 280 { 281 HashMap map = new HashMap (); 282 try { 283 URI uri = new URI(url.toCharArray()); 284 String query = uri.getQuery(); 285 if (query != null) { 286 StringTokenizer st = new StringTokenizer (query, "&"); 287 while (st.hasMoreTokens()) { 288 String param = st.nextToken(); 289 int i = param.indexOf("="); 290 if (i != -1) { 291 String k = param.substring(0, i); 292 String v = param.substring(i+1, param.length()); 293 map.put(k, v); 294 } 295 } 296 } 297 } catch (URIException e) { 298 mLogger.debug("URIException: " + e.getMessage()); 299 } 300 return map; 301 } 302 } 303 | Popular Tags |