001: /* Todo
002: + make search for null bytes optional
003:
004: */
005:
006: /* *****************************************************************************
007: * XMLConverter.java
008: * ****************************************************************************/
009:
010: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
011: * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
012: * Use is subject to license terms. *
013: * J_LZ_COPYRIGHT_END *********************************************************/
014:
015: package org.openlaszlo.data;
016:
017: import java.io.*;
018: import java.util.Properties;
019:
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.apache.log4j.*;
024:
025: import org.openlaszlo.xml.internal.DataCompiler;
026: import org.openlaszlo.xml.internal.DataCompilerException;
027: import org.openlaszlo.utils.ContentEncoding;
028:
029: import org.openlaszlo.media.MimeType;
030: import org.openlaszlo.server.LPS;
031:
032: /**
033: * XML Converter
034: *
035: */
036: public class XMLConverter extends Converter {
037:
038: private static Logger mLogger = Logger
039: .getLogger(XMLConverter.class);
040:
041: /** Data compiler */
042: private DataCompiler mCompiler = new DataCompiler();
043:
044: /**
045: * Convert incoming XML to SWF, adding some HTTP info as well.
046: */
047: public InputStream convertToSWF(Data data, HttpServletRequest req,
048: HttpServletResponse res) throws ConversionException,
049: IOException {
050:
051: String mimeType = data.getMimeType();
052:
053: if (mimeType != null && mimeType.equals(MimeType.SWF)) {
054: return data.getInputStream();
055: }
056:
057: if (mimeType == null || !mimeType.equals(MimeType.XML)) {
058: mLogger.warn("back-end mime-type is " + mimeType
059: + ", treating as text/xml");
060: }
061:
062: String body = data.getAsString();
063:
064: // TODO: [2003-04-26 bloch] perhaps there should be a config
065: // to turn this check on/off.
066: //
067: // Strip out possible null characters from input. Null
068: // characters are bad because they get passed by JGenerator
069: // through to Flash and really screw up zero-terminated string
070: // constants, causing corrupted Flash byte codes.
071: if ("true".equals(LPS.getProperty("data.removenulls", "false"))) {
072: StringBuffer buf = new StringBuffer();
073: int nchars = body.length();
074: for (int i = 0; i < nchars; i++) {
075: char ch = body.charAt(i);
076: if (ch != 0) {
077: buf.append(ch);
078: }
079: }
080: body = buf.toString();
081: }
082:
083: // Get headers
084: String sendheaders = req.getParameter("sendheaders");
085: StringBuffer headerbuf = new StringBuffer();
086: headerbuf.append("<headers>\n");
087: if (sendheaders == null || sendheaders.equals("true")) {
088: data.appendResponseHeadersAsXML(headerbuf);
089: }
090: headerbuf.append("</headers>");
091: String headers = headerbuf.toString();
092:
093: if (mLogger.isDebugEnabled()) {
094: mLogger.info("Output:" + body.length());
095: mLogger.info("Output:\n" + body);
096: mLogger.info("Output Headers:" + headers.length());
097: mLogger.info("Output Headers:\n" + headers);
098: }
099:
100: // Default to true, for back compatibility (sigh)
101: boolean trimWhitespace = true;
102: String trimval = req.getParameter("trimwhitespace");
103: if ("false".equals(trimval)) {
104: trimWhitespace = false;
105: }
106:
107: boolean compress = "true".equals(req.getParameter("compress"));
108:
109: // nsprefix now defaults to true
110: boolean nsprefix = true;
111: if ("false".equals(req.getParameter("nsprefix"))) {
112: nsprefix = false;
113: }
114:
115: try {
116: return mCompiler.compile(body, headers, LPS
117: .getSWFVersionNum(req), true, trimWhitespace,
118: compress, nsprefix);
119:
120: } catch (DataCompilerException dce) {
121: throw new ConversionException(dce.getMessage());
122: } catch (IOException ie) {
123: throw new ConversionException(ie.getMessage());
124: }
125: }
126:
127: /**
128: * @return the encoding that should be used when responding
129: * to this request or null for no encoding. For now, the only
130: * acceptable values besides null are "gzip" and "deflate".
131: */
132: public String chooseEncoding(HttpServletRequest req) {
133:
134: String e = req.getParameter("enc");
135: if (e == null || e.equals("false")) {
136: return null;
137: }
138:
139: String enc = ContentEncoding.chooseEncoding(req);
140: mLogger.debug("Encoding: " + enc);
141: return enc;
142: }
143:
144: }
|