01: /* *****************************************************************************
02: * Converter.java
03: * ****************************************************************************/
04:
05: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
06: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
07: * Use is subject to license terms. *
08: * J_LZ_COPYRIGHT_END *********************************************************/
09:
10: package org.openlaszlo.data;
11:
12: import javax.servlet.http.HttpServletRequest;
13: import javax.servlet.http.HttpServletResponse;
14:
15: import java.io.InputStream;
16: import java.io.File;
17: import java.io.IOException;
18: import java.io.FileNotFoundException;
19:
20: import org.openlaszlo.utils.FileUtils;
21: import org.openlaszlo.utils.TempFileInputStream;
22: import org.openlaszlo.utils.ChainedException;
23:
24: import org.apache.log4j.Logger;
25:
26: /**
27: * Base class for data conversion and encoding
28: */
29: public abstract class Converter {
30:
31: private static Logger mLogger = Logger.getLogger(Converter.class);
32:
33: /**
34: * @return an input stream that can read the
35: * converted data that came from the given request
36: * as SWF.
37: */
38: public abstract InputStream convertToSWF(Data data,
39: HttpServletRequest req, HttpServletResponse res)
40: throws ConversionException, IOException;
41:
42: /**
43: * @return the HTTP content-encoding that should be used when responding
44: * to this request or null for no encoding. For now, the only
45: * acceptable values besides null are "gzip" and "deflate".
46: */
47: public abstract String chooseEncoding(HttpServletRequest req);
48:
49: /**
50: * @return stream of encoded data
51: * @param in input stream to read
52: * @param enc encoding to use
53: * @param req request
54: */
55: public InputStream encode(HttpServletRequest req, InputStream in,
56: String enc) throws IOException {
57:
58: if (enc != null && !enc.equals("")) {
59: File tempFile = File.createTempFile("lzuc-", null);
60: mLogger.debug(
61: /* (non-Javadoc)
62: * @i18n.test
63: * @org-mes="Temporary file is " + p[0]
64: */
65: org.openlaszlo.i18n.LaszloMessages.getMessage(
66: Converter.class.getName(), "051018-65",
67: new Object[] { tempFile.getAbsolutePath() }));
68: try {
69: FileUtils.encode(in, tempFile, enc);
70: } catch (IOException e) {
71: FileUtils.close(in);
72: throw e;
73: }
74: try {
75: in = new TempFileInputStream(tempFile);
76: } catch (FileNotFoundException e) {
77: throw new ChainedException(e);
78: }
79: }
80: return in;
81: }
82: }
|