001: /*
002: * $Id: TextPlainCanonicalizer.java,v 1.4 2007/01/08 09:28:49 ashutoshshahi Exp $
003: * $Revision: 1.4 $
004: * $Date: 2007/01/08 09:28:49 $
005: */
006:
007: /*
008: * The contents of this file are subject to the terms
009: * of the Common Development and Distribution License
010: * (the License). You may not use this file except in
011: * compliance with the License.
012: *
013: * You can obtain a copy of the license at
014: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * See the License for the specific language governing
016: * permissions and limitations under the License.
017: *
018: * When distributing Covered Code, include this CDDL
019: * Header Notice in each file and include the License file
020: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
021: * If applicable, add the following below the CDDL Header,
022: * with the fields enclosed by brackets [] replaced by
023: * you own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.wss.impl.c14n;
030:
031: import java.io.IOException;
032: import java.io.ByteArrayOutputStream;
033:
034: import com.sun.mail.util.CRLFOutputStream;
035:
036: import com.sun.xml.wss.XWSSecurityException;
037: import java.io.ByteArrayInputStream;
038: import java.io.InputStream;
039: import java.io.OutputStream;
040:
041: import java.util.logging.Level;
042:
043: /**
044: *
045: * Implementation of a text/plain canonicalizer as per rules
046: * defined in RFC 2046 (http://www.rfc-editor.org/rfc/rfc2046.txt)
047: * Section 4.1.
048: *
049: * @author XWS-Security Team
050: */
051: public class TextPlainCanonicalizer extends Canonicalizer {
052:
053: public TextPlainCanonicalizer() {
054: }
055:
056: public TextPlainCanonicalizer(String charset) {
057: super (charset);
058: }
059:
060: public InputStream canonicalize(InputStream input,
061: OutputStream outputStream)
062: throws javax.xml.crypto.dsig.TransformException {
063:
064: int len = 0;
065: byte[] data = null;
066: try {
067: data = new byte[128];
068: len = input.read(data);
069: } catch (IOException e) {
070: log.log(Level.SEVERE,
071: "WSS1002.error.canonicalizing.textplain",
072: new Object[] { e.getMessage() });
073: throw new javax.xml.crypto.dsig.TransformException(e);
074: }
075: CRLFOutputStream crlfOutStream = null;
076: ByteArrayOutputStream bout = null;
077: if (outputStream == null) {
078: bout = new ByteArrayOutputStream();
079: crlfOutStream = new CRLFOutputStream(bout);
080: } else {
081: crlfOutStream = new CRLFOutputStream(outputStream);
082: }
083:
084: while (len > 0) {
085: try {
086: crlfOutStream.write(data, 0, len);
087: len = input.read(data);
088: } catch (IOException e) {
089: log.log(Level.SEVERE,
090: "WSS1002.error.canonicalizing.textplain",
091: new Object[] { e.getMessage() });
092: throw new javax.xml.crypto.dsig.TransformException(e);
093: }
094: }
095:
096: if (outputStream == null) {
097: byte[] inputData = bout.toByteArray();
098: return new ByteArrayInputStream(inputData);
099: }
100: return null;
101: }
102:
103: /*
104: * Important aspects of "text" media type canonicalization include line
105: * ending normalization to <CR><LF>.
106: * Section 4.1.1. [RFC 2046]
107: */
108: public byte[] canonicalize(byte[] inputBytes)
109: throws XWSSecurityException {
110: ByteArrayOutputStream bout = new ByteArrayOutputStream();
111: CRLFOutputStream crlfOutStream = new CRLFOutputStream(bout);
112: try {
113: crlfOutStream.write(inputBytes);
114: } catch (IOException e) {
115: log.log(Level.SEVERE,
116: "WSS1002.error.canonicalizing.textplain",
117: new Object[] { e.getMessage() });
118: throw new XWSSecurityException(e);
119: }
120: return bout.toByteArray();
121: }
122:
123: }
|