001: /*
002: * $Id: TeeFilter.java,v 1.3 2006/09/29 12:04:55 kumarjayanti Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026: package com.sun.xml.wss.impl.filter;
027:
028: import com.sun.xml.wss.logging.LogDomainConstants;
029: import java.io.ByteArrayInputStream;
030: import java.io.OutputStream;
031: import java.util.logging.Level;
032:
033: import javax.xml.soap.SOAPMessage;
034:
035: import javax.xml.transform.Source;
036: import javax.xml.transform.Templates;
037: import javax.xml.transform.Transformer;
038: import javax.xml.transform.TransformerConfigurationException;
039: import javax.xml.transform.TransformerFactory;
040: import javax.xml.transform.stream.StreamResult;
041: import javax.xml.transform.stream.StreamSource;
042:
043: import com.sun.xml.wss.XWSSecurityException;
044: import java.util.logging.Logger;
045:
046: /**
047: * Copies the SOAP message into an OutputStream using an optional stylesheet
048: * to format the message. The original message is not modified. This is
049: * analogous to the "tee" unix command.
050: *
051: * @author Edwin Goei
052: */
053: public class TeeFilter {
054: // TODO Fix the stylesheet to pretty print a SOAP Message
055: private static Logger log = Logger.getLogger(
056: LogDomainConstants.WSS_API_DOMAIN,
057: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
058: private static final String prettyPrintStylesheet = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'\n"
059: + " version='1.0'>\n"
060: + " <xsl:output method='xml' indent='yes'/>\n"
061: + " <xsl:strip-space elements='*'/>\n"
062: + " <xsl:template match='/'>\n"
063: + " <xsl:apply-templates/>\n"
064: + " </xsl:template>\n"
065: + " <xsl:template match='node() | @*'>\n"
066: + " <xsl:choose>\n"
067: + " <xsl:when test='contains(name(current()), \"wsse:Password\")'>\n"
068: + " <wsse:Password Type='{@Type}'>****</wsse:Password>\n"
069: + " </xsl:when>\n"
070: + " <xsl:otherwise>\n"
071: + " <xsl:copy>\n"
072: + " <xsl:apply-templates select='node() | @*'/>\n"
073: + " </xsl:copy>\n"
074: + " </xsl:otherwise>\n"
075: + " </xsl:choose>\n"
076: + " </xsl:template>\n"
077: + "</xsl:stylesheet>\n";
078:
079: /** OutputStream for output. */
080: private OutputStream out;
081:
082: /** Represents a stylesheet */
083: private Templates templates;
084:
085: /**
086: * Copy and optionally format a message
087: *
088: * @param out destination OutputStream
089: * @param stylesheet XSLT stylesheet for format or if null, then does
090: * not format
091: */
092: public TeeFilter(OutputStream out, Source stylesheet)
093: throws XWSSecurityException {
094: init(out, stylesheet);
095: }
096:
097: /**
098: * Copy and optionally pretty print a message
099: *
100: * @param out destination OutputStream
101: * @param prettyPrint true means to use built-in pretty print stylesheet
102: * @throws XWSSecurityException
103: */
104: public TeeFilter(OutputStream out, boolean prettyPrint)
105: throws XWSSecurityException {
106: if (prettyPrint) {
107: init(out, getPrettyPrintStylesheet());
108: } else {
109: init(out, null);
110: }
111: }
112:
113: /**
114: * Saves a copy of message to Outputstream out
115: *
116: * @param out
117: * @throws XWSSecurityException
118: */
119: public TeeFilter(OutputStream out) throws XWSSecurityException {
120: init(out, null);
121: }
122:
123: /**
124: * A no-op
125: *
126: * @throws XWSSecurityException
127: */
128: public TeeFilter() throws XWSSecurityException {
129: init(null, null);
130: }
131:
132: private void init(OutputStream out, Source stylesheet)
133: throws XWSSecurityException {
134: this .out = out;
135:
136: if (stylesheet == null) {
137: templates = null;
138: } else {
139: TransformerFactory tf = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
140: try {
141: templates = tf.newTemplates(stylesheet);
142: } catch (TransformerConfigurationException e) {
143: log.log(Level.SEVERE,
144: "WSS0147.unableto.use.stylesheet",
145: new Object[] { e.getMessage() });
146: throw new XWSSecurityException(
147: "Unable to use stylesheet", e);
148: }
149: }
150: }
151:
152: private Source getPrettyPrintStylesheet() {
153: // if (true) {
154: // if (defaultStylesheetSource == null) {
155: // byte[] xsltBytes = defaultStylesheet.getBytes();
156: // ByteArrayInputStream bais = new ByteArrayInputStream(xsltBytes);
157: // defaultStylesheetSource = new StreamSource(bais);
158: // }
159: // return defaultStylesheetSource;
160: // } else {
161: byte[] xsltBytes = prettyPrintStylesheet.getBytes();
162: ByteArrayInputStream bais = new ByteArrayInputStream(xsltBytes);
163: Source stylesheetSource = new StreamSource(bais);
164: return stylesheetSource;
165: // }
166: }
167:
168: /**
169: * Invokes the MessageFilter on the SOAPMessage sm. A
170: * XWSSecurityException is thrown if the operation did not succeed.
171: *
172: * @param secureMessage SOAPMessage to perform the operation on
173: *
174: * @throws com.sun.xml.wss.XWSSecurityException if the operation did not
175: * succeed
176: */
177: public void process(SOAPMessage secureMessage)
178: throws XWSSecurityException {
179: if (out == null) {
180: return;
181: }
182:
183: Transformer transformer;
184: try {
185: if (secureMessage.countAttachments() > 0) {
186: secureMessage.writeTo(out);
187: } else {
188: if (templates == null) {
189: // Use identity transform
190: transformer = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl()
191: .newTransformer();
192: } else {
193: // Use supplied stylesheet via Templates object
194: transformer = templates.newTransformer();
195: }
196: Source msgSource = secureMessage.getSOAPPart()
197: .getContent();
198: transformer.transform(msgSource, new StreamResult(out));
199: }
200: } catch (Exception ex) {
201: log.log(Level.SEVERE,
202: "WSS0148.unableto.process.soapmessage",
203: new Object[] { ex.getMessage() });
204: throw new XWSSecurityException(
205: "Unable to process SOAPMessage", ex);
206: }
207: }
208: }
|