KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > data > Converter


1 /* *****************************************************************************
2  * Converter.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.data;
11
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13 import javax.servlet.http.HttpServletResponse JavaDoc;
14
15 import java.io.InputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
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 JavaDoc convertToSWF(Data data,
39             HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
40        throws ConversionException, IOException JavaDoc;
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 JavaDoc chooseEncoding(HttpServletRequest JavaDoc 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 JavaDoc encode(HttpServletRequest JavaDoc req, InputStream JavaDoc in,
56             String JavaDoc enc) throws IOException JavaDoc {
57
58         if (enc != null && !enc.equals("")) {
59             File JavaDoc tempFile = File.createTempFile("lzuc-", null);
60             mLogger.debug("Temporary file is " + tempFile.getAbsolutePath());
61             try {
62                 FileUtils.encode(in, tempFile, enc);
63             } catch (IOException JavaDoc e) {
64                 FileUtils.close(in);
65                 throw e;
66             }
67             try {
68                 in = new TempFileInputStream(tempFile);
69             } catch (FileNotFoundException JavaDoc e) {
70                 throw new ChainedException(e);
71             }
72         }
73         return in;
74     }
75 }
76
Popular Tags