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.dsig;
024:
025: import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
026: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
027: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
028: import com.sun.xml.ws.security.opt.api.SignedData;
029: import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
030: import com.sun.xml.ws.security.opt.impl.util.NamespaceContextEx;
031: import com.sun.xml.ws.security.opt.impl.util.XMLStreamFilterWithId;
032: import java.io.OutputStream;
033: import java.util.HashMap;
034: import com.sun.istack.NotNull;
035:
036: import com.sun.xml.ws.api.message.Header;
037: import javax.xml.stream.XMLStreamWriter;
038:
039: /**
040: *
041: * A wrapper over a <CODE>Header</CODE> or a <CODE>SecurityHeaderElement</CODE>
042: * @author K.Venugopal@sun.com
043: */
044:
045: public class SignedMessageHeader extends SignedMessagePart implements
046: SecurityHeaderElement, SignedData, SecurityElementWriter {
047:
048: private Header header = null;
049: private SecurityHeaderElement she = null;
050:
051: private byte[] digestValue;
052:
053: private String id;
054:
055: JAXBFilterProcessingContext context = null;
056: private MutableXMLStreamBuffer buffer = null;
057:
058: /**
059: * Creates a new instance of SignedMessageHeader
060: * @param header The SOAP Header which is to be signed
061: * @param id The id assigned to the SOAP header
062: * @param context JAXBFilterProcessingContext
063: */
064: public SignedMessageHeader(Header header, String id,
065: JAXBFilterProcessingContext context) {
066: this .header = header;
067: this .id = id;
068: this .context = context;
069: }
070:
071: /**
072: *
073: * Sign a <CODE>SecurityHeaderElement</CODE>
074: * @param she The SecurityHeaderElement to be signed
075: */
076: public SignedMessageHeader(SecurityHeaderElement she) {
077: this .she = she;
078: }
079:
080: /**
081: *
082: * @return the id of the SignedMessageHeader
083: */
084: public String getId() {
085: if (header != null) {
086: return id;
087: } else {
088: return she.getId();
089: }
090: }
091:
092: /**
093: * Assign an id to the SignedMessageHeader
094: */
095: public void setId(final String id) {
096: if (header != null) {
097: this .id = id;
098: } else {
099: she.setId(id);
100: }
101: }
102:
103: /**
104: *
105: * @return the namespace of the underlying SOAP header or SecurityHeaderElement
106: */
107: @NotNull
108: public String getNamespaceURI() {
109: if (header != null) {
110: return header.getNamespaceURI();
111: } else {
112: return she.getNamespaceURI();
113: }
114: }
115:
116: /**
117: *
118: * @return The localname of the underlying SOAP Header or SecurityHeaderElement
119: */
120: @NotNull
121: public String getLocalPart() {
122: if (header != null) {
123: return header.getLocalPart();
124: } else {
125: return she.getLocalPart();
126: }
127: }
128:
129: /**
130: *
131: * @return The header as as XMLStreamReader
132: */
133: public javax.xml.stream.XMLStreamReader readHeader()
134: throws javax.xml.stream.XMLStreamException {
135: if (buffer == null) {
136: buffer = new MutableXMLStreamBuffer();
137: XMLStreamWriter writer = buffer.createFromXMLStreamWriter();
138: this .writeTo(writer);
139: }
140: return buffer.readAsXMLStreamReader();
141: }
142:
143: /**
144: * Write the header to the passed outputStream
145: */
146: public void writeTo(OutputStream os) {
147: throw new UnsupportedOperationException("Not implemented yet");
148: }
149:
150: /**
151: * Write the header to an XMLStreamWriter
152: */
153: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
154: throws javax.xml.stream.XMLStreamException {
155: if (header != null) {
156: XMLStreamFilterWithId xmlStreamFilterWithId = new XMLStreamFilterWithId(
157: streamWriter, (NamespaceContextEx) context
158: .getNamespaceContext(), id);
159: header.writeTo(xmlStreamFilterWithId);
160: } else {
161: ((SecurityElementWriter) she).writeTo(streamWriter);
162: }
163:
164: }
165:
166: /**
167: * Write the header to an XMLStreamWriter
168: */
169: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
170: final HashMap props)
171: throws javax.xml.stream.XMLStreamException {
172: /*Marshaller marshaller = getMarshaller();
173: Iterator<String> itr = props.keySet().iterator();
174: while(itr.hasNext()){
175: String key = itr.next();
176: Object value = props.get(key);
177: marshaller.setProperty(key,value);
178: }*/
179: if (header != null) {
180: XMLStreamFilterWithId xmlStreamFilterWithId = new XMLStreamFilterWithId(
181: streamWriter, (NamespaceContextEx) context
182: .getNamespaceContext(), id);
183: header.writeTo(xmlStreamFilterWithId);
184: } else {
185: ((SecurityElementWriter) she).writeTo(streamWriter, props);
186: }
187: }
188:
189: public void setDigestValue(final byte[] digestValue) {
190: this .digestValue = digestValue;
191: }
192:
193: /**
194: *
195: * @return The DigestValue of this Header
196: */
197: public byte[] getDigestValue() {
198: return digestValue;
199: }
200:
201: /**
202: *
203: * @param id The id of the SecurityHeaderElement against which to compare
204: * @return true if the current SecurityHeaderElement has reference to the
205: * SecurityHeaderElement with passed id
206: */
207: public boolean refersToSecHdrWithId(String id) {
208: return she.refersToSecHdrWithId(id);
209: }
210:
211: public Header getSignedHeader() {
212: return header;
213: }
214: }
|