001: /*
002: * EncryptedSignedMessageHeader.java
003: *
004: * Created on May 22, 2007, 12:13 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.sun.xml.ws.security.opt.impl.message;
011:
012: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
013: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
014: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
015: import com.sun.xml.ws.security.opt.impl.dsig.SignedMessageHeader;
016: import com.sun.xml.wss.impl.c14n.AttributeNS;
017: import com.sun.xml.wss.impl.c14n.StAXAttr;
018: import java.util.HashMap;
019: import java.util.Vector;
020: import javax.xml.stream.XMLStreamConstants;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.stream.XMLStreamReader;
023: import javax.xml.stream.XMLStreamWriter;
024:
025: /**
026: *
027: * @author Ashutosh.Shahi@sun.com
028: */
029: public class EncryptedSignedMessageHeader extends SignedMessageHeader {
030:
031: private SecurityHeaderElement encHeader = null;
032: private boolean parsed = false;
033: private String localName;
034: private String uri;
035: private String prefix;
036: private Vector attrList = new Vector();
037: private Vector attrNSList = new Vector();
038:
039: private MutableXMLStreamBuffer buffer = null;
040:
041: /** Creates a new instance of EncryptedSignedMessageHeader */
042: public EncryptedSignedMessageHeader(SignedMessageHeader hdr,
043: SecurityHeaderElement she) {
044: super (hdr);
045: encHeader = she;
046: }
047:
048: /**
049: *
050: * @return The header as as XMLStreamReader
051: */
052: public javax.xml.stream.XMLStreamReader readHeader()
053: throws javax.xml.stream.XMLStreamException {
054: if (buffer == null) {
055: buffer = new MutableXMLStreamBuffer();
056: XMLStreamWriter writer = buffer.createFromXMLStreamWriter();
057: super .writeTo(writer);
058: }
059: return buffer.readAsXMLStreamReader();
060: }
061:
062: /**
063: * Write the header to an XMLStreamWriter
064: */
065: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
066: throws javax.xml.stream.XMLStreamException {
067: if (!parsed) {
068: parse();
069: }
070: writeStartElement(streamWriter);
071: ((SecurityElementWriter) encHeader).writeTo(streamWriter);
072: writeEndElement(streamWriter);
073: }
074:
075: /**
076: * Write the header to an XMLStreamWriter
077: */
078: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
079: final HashMap props)
080: throws javax.xml.stream.XMLStreamException {
081: if (!parsed) {
082: parse();
083: }
084: writeStartElement(streamWriter);
085: ((SecurityElementWriter) encHeader)
086: .writeTo(streamWriter, props);
087: writeEndElement(streamWriter);
088:
089: }
090:
091: protected void parse() throws XMLStreamException {
092: XMLStreamReader reader = readHeader();
093: parsed = true;
094: boolean stop = false;
095: while (reader.hasNext()) {
096: int eventType = reader.next();
097: if (stop) {
098: return;
099: }
100: switch (eventType) {
101: case XMLStreamConstants.START_ELEMENT: {
102: localName = reader.getLocalName();
103: uri = reader.getNamespaceURI();
104: prefix = reader.getPrefix();
105: if (prefix == null)
106: prefix = "";
107: int count = reader.getAttributeCount();
108: for (int i = 0; i < count; i++) {
109: String localName = reader.getAttributeLocalName(i);
110: String uri = reader.getAttributeNamespace(i);
111: String prefix = reader.getAttributePrefix(i);
112: if (prefix == null)
113: prefix = "";
114: final String value = reader.getAttributeValue(i);
115: StAXAttr attr = new StAXAttr();
116: attr.setLocalName(localName);
117: attr.setValue(value);
118: attr.setPrefix(prefix);
119: attr.setUri(uri);
120: attrList.add(attr);
121: }
122:
123: count = 0;
124: count = reader.getNamespaceCount();
125: for (int i = 0; i < count; i++) {
126: String prefix = reader.getNamespacePrefix(i);
127: if (prefix == null)
128: prefix = "";
129: String uri = reader.getNamespaceURI(i);
130: AttributeNS attrNS = new AttributeNS();
131: attrNS.setPrefix(prefix);
132: attrNS.setUri(uri);
133: attrNSList.add(attrNS);
134: }
135: stop = true;
136: break;
137: }
138: case XMLStreamConstants.END_ELEMENT: {
139: stop = true;
140: break;
141: }
142: }
143:
144: }
145: }
146:
147: private void writeEndElement(XMLStreamWriter xsw)
148: throws XMLStreamException {
149: xsw.writeEndElement();
150: }
151:
152: private void writeStartElement(XMLStreamWriter xsw)
153: throws XMLStreamException {
154: xsw.writeStartElement(prefix, localName, uri);
155: for (int i = 0; i < attrNSList.size(); i++) {
156: AttributeNS attrNs = (AttributeNS) attrNSList.get(i);
157: xsw.writeNamespace(attrNs.getPrefix(), attrNs.getUri());
158: }
159: for (int i = 0; i < attrList.size(); i++) {
160: StAXAttr attr = (StAXAttr) attrList.get(i);
161: xsw.writeAttribute(attr.getPrefix(), attr.getUri(), attr
162: .getLocalName(), attr.getValue());
163: }
164: }
165: }
|