001: /**
002: * $Id: ContentTag.java,v 1.11 2005/09/21 10:53:14 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.util;
014:
015: import java.util.*;
016: import java.util.logging.Level;
017: import java.util.logging.LogRecord;
018: import java.util.logging.Logger;
019:
020: import javax.servlet.jsp.*;
021: import javax.servlet.*;
022: import javax.servlet.http.*;
023:
024: import com.sun.portal.log.common.PortalLogger;
025: import com.sun.portal.wireless.taglibs.base.*;
026: import com.sun.portal.wireless.taglibs.util.*;
027:
028: /**
029: * ContentTag
030: *
031: * This tag sets the output stream of the jsp to the users charset.
032: * By doing so, it seperates the charset needed for compilation
033: * from the desired output charset (which may often be different).
034: *
035: * @author Bryan Hanks
036: * @version 1.0
037: * @see com.sun.portal.wireless.taglibs.util.ContentTag
038: */
039:
040: public class ContentTag extends BaseTagSupport {
041:
042: private String type = null;
043:
044: private static final String DEFAULT_CONTENT_TYPE = "text/html";
045: private static final String DEFAULT_CHARSET = "ISO-8859-1";
046:
047: private static Logger logger = PortalLogger
048: .getLogger(ContentTag.class);
049:
050: public void setType(String type) {
051: this .type = type;
052: }
053:
054: public int doStartTag() throws JspException {
055: UtilContext context = null;
056:
057: ServletResponse res = pageContext.getResponse();
058:
059: String charset = null;
060: try {
061: context = UtilContext.getContext(pageContext);
062: charset = context.getCharset();
063: if (type == null) {
064: // type = context.getContentType();
065:
066: /**
067: * Begin Workaround for ES6.1 filter problem: If contentType not
068: * set explicitly (happens only with aml jsps) - set it to text/aml
069: * so that the AMLController knows that it should be rendered.
070: */
071: type = "text/aml";
072: /** END Workaround */
073: }
074:
075: ServletRequest req = pageContext.getRequest();
076: req.setCharacterEncoding(charset);
077: res.setContentType(type + "; charset=" + charset);
078: if (logger.isLoggable(Level.FINEST)) {
079: String[] param = { "charset", charset };
080: logger.log(Level.FINEST, "PSMA_CSPWTU0001", param);
081: }
082: } catch (Exception e) {
083: String ct = DEFAULT_CONTENT_TYPE;
084: if (type != null && !type.equals(""))
085: ct = type;
086:
087: String cSet = DEFAULT_CHARSET;
088: if (charset != null && !charset.equals(""))
089: cSet = charset;
090:
091: ct = ct + "; charset=" + cSet;
092:
093: res.setContentType(ct);
094: if (logger.isLoggable(Level.SEVERE)) {
095: LogRecord rec = new LogRecord(Level.SEVERE,
096: "PSMA_CSPWTU0009");
097: rec.setThrown(e);
098: String[] param = { ct };
099: rec.setParameters(param);
100: rec.setLoggerName(logger.getName());
101: logger.log(rec);
102: }
103:
104: throw new JspException(this .getClass().getName()
105: + ".doStartTag(): Unable to determine charset: "
106: + e.toString());
107: }
108:
109: return SKIP_BODY;
110: }
111: }
|