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.message;
024:
025: import com.sun.xml.security.core.xenc.ReferenceList;
026: import com.sun.xml.security.core.xenc.ReferenceType;
027: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
028: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
029: import com.sun.xml.ws.security.opt.impl.util.DOMUtil;
030: import com.sun.xml.ws.security.opt.impl.util.JAXBUtil;
031: import com.sun.xml.wss.impl.MessageConstants;
032: import com.sun.xml.wss.impl.XWSSecurityRuntimeException;
033: import java.io.OutputStream;
034: import java.util.ArrayList;
035: import java.util.HashMap;
036: import java.util.List;
037: import javax.xml.bind.JAXBElement;
038: import javax.xml.bind.Marshaller;
039: import javax.xml.namespace.QName;
040: import org.w3c.dom.Element;
041:
042: import com.sun.xml.ws.api.SOAPVersion;
043:
044: /**
045: *
046: * @author K.Venugopal@sun.com
047: */
048: public class GSHeaderElement implements SecurityHeaderElement,
049: SecurityElementWriter {
050: JAXBElement element = null;
051: Object obj = null;
052: private String id = "";
053: private SOAPVersion soapVersion = SOAPVersion.SOAP_11;
054: private Element domElement = null;
055:
056: public GSHeaderElement(JAXBElement el, SOAPVersion sv) {
057: this .element = el;
058: this .soapVersion = sv;
059: }
060:
061: public GSHeaderElement(Object obj, SOAPVersion sv) {
062: this .obj = obj;
063: this .soapVersion = sv;
064: }
065:
066: public GSHeaderElement(Element obj, SOAPVersion sv) {
067: this .domElement = obj;
068: this .soapVersion = sv;
069: if (domElement.getLocalName().equals(
070: MessageConstants.SAML_ASSERTION_LNAME)) {
071: id = domElement
072: .getAttribute(MessageConstants.SAML_ASSERTIONID_LNAME);
073: if (id == null || id.equals(""))
074: id = domElement.getAttribute("ID");
075: }
076: }
077:
078: public GSHeaderElement(Element obj) {
079: this .domElement = obj;
080: if (domElement.getLocalName() == MessageConstants.SAML_ASSERTION_LNAME) {
081: id = domElement
082: .getAttribute(MessageConstants.SAML_ASSERTIONID_LNAME);
083: if (id == null || id.equals(""))
084: id = domElement.getAttribute("ID");
085: }
086: }
087:
088: public String getId() {
089: return id;
090: }
091:
092: public void setId(String id) {
093: this .id = id;
094: }
095:
096: public String getNamespaceURI() {
097: if (element != null) {
098: return element.getName().getNamespaceURI();
099: }
100: if (domElement != null) {
101: return domElement.getNamespaceURI();
102: }
103:
104: return "";
105: }
106:
107: public String getLocalPart() {
108: if (element != null) {
109: return element.getName().getLocalPart();
110: }
111:
112: if (domElement != null) {
113: return domElement.getLocalName();
114: }
115:
116: if (obj != null) {
117: if (obj instanceof ReferenceList) {
118: return MessageConstants.XENC_REFERENCE_LIST_LNAME;
119: }
120: }
121: return "";
122: }
123:
124: public javax.xml.stream.XMLStreamReader readHeader()
125: throws javax.xml.stream.XMLStreamException {
126: throw new UnsupportedOperationException();
127: }
128:
129: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
130: throws javax.xml.stream.XMLStreamException {
131: try {
132: Marshaller writer = getMarshaller();
133: if (element != null) {
134: writer.marshal(element, streamWriter);
135: } else if (domElement != null) {
136: DOMUtil.serializeNode(domElement, streamWriter);
137: } else {
138: writer.marshal(obj, streamWriter);
139: }
140: } catch (javax.xml.bind.JAXBException ex) {
141: throw new XWSSecurityRuntimeException(ex);
142: }
143: }
144:
145: public void writeTo(javax.xml.soap.SOAPMessage saaj)
146: throws javax.xml.soap.SOAPException {
147: throw new UnsupportedOperationException();
148: }
149:
150: public byte[] canonicalize(String algorithm,
151: List<com.sun.xml.wss.impl.c14n.AttributeNS> namespaceDecls) {
152: throw new UnsupportedOperationException();
153: }
154:
155: public boolean isCanonicalized() {
156: throw new UnsupportedOperationException();
157: }
158:
159: public void writeTo(OutputStream os) {
160: try {
161: Marshaller writer = getMarshaller();
162: if (element != null) {
163: writer.marshal(element, os);
164: } else {
165: writer.marshal(obj, os);
166: }
167: } catch (javax.xml.bind.JAXBException ex) {
168: throw new XWSSecurityRuntimeException(ex);
169: }
170: }
171:
172: public String getAttribute(String nsUri, String localName) {
173: throw new UnsupportedOperationException();
174: }
175:
176: public String getAttribute(QName name) {
177: throw new UnsupportedOperationException();
178: }
179:
180: public boolean refersToSecHdrWithId(String id) {
181: String tmpId = "#" + id;
182: if (element != null) {
183: if (element.getName().getLocalPart() == MessageConstants.XENC_REFERENCE_LIST_LNAME) {
184: ReferenceList list = (ReferenceList) element.getValue();
185: List<JAXBElement<ReferenceType>> listElems = list
186: .getDataReferenceOrKeyReference();
187: for (int i = 0; i < listElems.size(); i++) {
188: JAXBElement<ReferenceType> ref = listElems.get(i);
189: ReferenceType rt = ref.getValue();
190: if (rt.getURI().equals(tmpId)) {
191: return true;
192: }
193: }
194: }
195: }
196: if (obj != null) {
197: if (obj instanceof ReferenceList) {
198: ReferenceList rl = (ReferenceList) obj;
199: List<JAXBElement<ReferenceType>> listElems = rl
200: .getDataReferenceOrKeyReference();
201: for (int i = 0; i < listElems.size(); i++) {
202: JAXBElement<ReferenceType> ref = listElems.get(i);
203: ReferenceType rt = ref.getValue();
204: if (rt.getURI().equals(tmpId)) {
205: return true;
206: }
207: }
208: }
209: }
210: return false;
211: }
212:
213: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
214: HashMap props) throws javax.xml.stream.XMLStreamException {
215: writeTo(streamWriter);
216: }
217:
218: private Marshaller getMarshaller()
219: throws javax.xml.bind.JAXBException {
220: return JAXBUtil.createMarshaller(soapVersion);
221: }
222: }
|