001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.incoming;
024:
025: import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
026: import com.sun.xml.wss.XWSSecurityException;
027: import com.sun.xml.wss.impl.MessageConstants;
028: import com.sun.xml.wss.impl.c14n.AttributeNS;
029: import com.sun.xml.wss.impl.c14n.StAXAttr;
030: import java.io.ByteArrayInputStream;
031: import java.io.ByteArrayOutputStream;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.util.HashMap;
035: import java.util.Vector;
036: import javax.xml.stream.XMLInputFactory;
037: import javax.xml.stream.XMLOutputFactory;
038: import javax.xml.stream.XMLStreamConstants;
039: import javax.xml.stream.XMLStreamException;
040: import javax.xml.stream.XMLStreamReader;
041: import javax.xml.stream.XMLStreamWriter;
042:
043: /**
044: *
045: * @author Ashutosh.Shahi@sun.com
046: */
047: class EncryptedContentHeaderParser {
048: XMLStreamReader encContentReader = null;
049: boolean parsed = false;
050: String localName;
051: String uri;
052: String prefix;
053: Vector attrList = new Vector();
054: Vector attrNSList = new Vector();
055: EncryptedData ed = null;
056:
057: private HashMap<String, String> parentNS = null;
058: private JAXBFilterProcessingContext context = null;
059:
060: EncryptedContentHeaderParser(XMLStreamReader encContentReader,
061: HashMap<String, String> parentNS,
062: JAXBFilterProcessingContext context) {
063: this .encContentReader = encContentReader;
064: this .parentNS = parentNS;
065: this .context = context;
066: }
067:
068: XMLStreamReader getDecryptedElement(InputStream decryptedIS)
069: throws XMLStreamException, XWSSecurityException {
070: ByteArrayOutputStream out = new ByteArrayOutputStream();
071: XMLOutputFactory factory = XMLOutputFactory.newInstance();
072: XMLStreamWriter writer = factory.createXMLStreamWriter(out);
073: writeStartElement(writer);
074: writeEndElement(writer);
075: writer.flush();
076: writer.close();
077: try {
078: out.close();
079: } catch (IOException ex) {
080: ex.printStackTrace();
081: }
082:
083: ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
084: String outStr = out.toString();
085: int pos = outStr.indexOf('>');
086: String startElem = outStr.substring(0, pos + 1);
087: String endElem = outStr.substring(pos + 1);
088: try {
089: tmpOut.write(startElem.getBytes());
090: byte[] buf = new byte[4096];
091:
092: for (int len = -1; (len = decryptedIS.read(buf)) != -1;)
093: tmpOut.write(buf, 0, len);
094:
095: tmpOut.write(endElem.getBytes());
096: } catch (IOException ex) {
097: ex.printStackTrace();
098: }
099:
100: InputStream finalContent = new ByteArrayInputStream(tmpOut
101: .toByteArray());
102: XMLInputFactory xif = XMLInputFactory.newInstance();
103: XMLStreamReader reader = xif
104: .createXMLStreamReader(finalContent);
105: return reader;
106: }
107:
108: void writeStartElement(XMLStreamWriter xsw)
109: throws XMLStreamException, XWSSecurityException {
110: if (!parsed) {
111: parse();
112: }
113: xsw.writeStartElement(prefix, localName, uri);
114: for (int i = 0; i < attrNSList.size(); i++) {
115: AttributeNS attrNs = (AttributeNS) attrNSList.get(i);
116: xsw.writeNamespace(attrNs.getPrefix(), attrNs.getUri());
117: }
118: for (int i = 0; i < attrList.size(); i++) {
119: StAXAttr attr = (StAXAttr) attrList.get(i);
120: if (parentNS.containsKey(attr.getPrefix())) {
121: xsw.writeNamespace(attr.getPrefix(), parentNS.get(attr
122: .getPrefix()));
123: }
124: }
125: for (int i = 0; i < attrList.size(); i++) {
126: StAXAttr attr = (StAXAttr) attrList.get(i);
127: xsw.writeAttribute(attr.getPrefix(), attr.getUri(), attr
128: .getLocalName(), attr.getValue());
129: }
130: }
131:
132: void writeEndElement(XMLStreamWriter xsw) throws XMLStreamException {
133: xsw.writeEndElement();
134: }
135:
136: EncryptedData getEncryptedData() throws XMLStreamException,
137: XWSSecurityException {
138: if (!parsed) {
139: parse();
140: }
141: return ed;
142: }
143:
144: void parse() throws XMLStreamException, XWSSecurityException {
145: parsed = true;
146: boolean stop = false;
147: boolean parentElem = true;
148: while (encContentReader.hasNext()) {
149: int eventType = XMLStreamConstants.START_ELEMENT;
150: if (!parentElem) {
151: eventType = encContentReader.next();
152: }
153: if (stop) {
154: return;
155: }
156: switch (eventType) {
157: case XMLStreamConstants.START_ELEMENT: {
158: if (parentElem) {
159: localName = encContentReader.getLocalName();
160: uri = encContentReader.getNamespaceURI();
161: prefix = encContentReader.getPrefix();
162: if (prefix == null)
163: prefix = "";
164: int count = encContentReader.getAttributeCount();
165: for (int i = 0; i < count; i++) {
166: String localName = encContentReader
167: .getAttributeLocalName(i);
168: String uri = encContentReader
169: .getAttributeNamespace(i);
170: String prefix = encContentReader
171: .getAttributePrefix(i);
172: if (prefix == null)
173: prefix = "";
174: final String value = encContentReader
175: .getAttributeValue(i);
176: StAXAttr attr = new StAXAttr();
177: attr.setLocalName(localName);
178: attr.setValue(value);
179: attr.setPrefix(prefix);
180: attr.setUri(uri);
181: attrList.add(attr);
182: }
183:
184: count = 0;
185: count = encContentReader.getNamespaceCount();
186: for (int i = 0; i < count; i++) {
187: String prefix = encContentReader
188: .getNamespacePrefix(i);
189: if (prefix == null)
190: prefix = "";
191: String uri = encContentReader
192: .getNamespaceURI(i);
193: AttributeNS attrNS = new AttributeNS();
194: attrNS.setPrefix(prefix);
195: attrNS.setUri(uri);
196: attrNSList.add(attrNS);
197: }
198: parentElem = false; // done reading parentElem
199: } else {
200: if (encContentReader.getLocalName() == MessageConstants.ENCRYPTED_DATA_LNAME
201: && encContentReader.getNamespaceURI() == MessageConstants.XENC_NS) {
202: ed = new EncryptedData(encContentReader,
203: context, parentNS);
204: }
205: }
206: break;
207: }
208: case XMLStreamConstants.END_ELEMENT: {
209: stop = true;
210: break;
211: }
212: }
213:
214: }
215: }
216: }
|