001: /*
002: * $Id: SecurityHeader.java,v 1.3 2006/09/29 12:04:45 kumarjayanti Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.wss.core;
028:
029: import com.sun.xml.wss.logging.LogDomainConstants;
030: import com.sun.xml.wss.XWSSecurityException;
031: import java.util.Iterator;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034:
035: import javax.xml.soap.Name;
036: import javax.xml.soap.SOAPElement;
037: import javax.xml.soap.SOAPException;
038:
039: import org.w3c.dom.Attr;
040: import org.w3c.dom.DOMException;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.NamedNodeMap;
043: import org.w3c.dom.Node;
044: import org.w3c.dom.NodeList;
045: import org.w3c.dom.UserDataHandler;
046: import org.w3c.dom.TypeInfo;
047:
048: import com.sun.xml.wss.impl.misc.SecurityHeaderBlockImpl;
049: import com.sun.xml.wss.impl.misc.SOAPElementExtension;
050: import com.sun.xml.wss.impl.MessageConstants;
051:
052: /**
053: * @author XWS-Security Development Team
054: */
055: public class SecurityHeader extends SOAPElementExtension implements
056: SOAPElement {
057:
058: private final SOAPElement delegateHeader;
059: private Document ownerDoc;
060:
061: private static Logger log = Logger.getLogger(
062: LogDomainConstants.WSS_API_DOMAIN,
063: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
064:
065: /**
066: * The child element of security header to be processed next.
067: */
068: private SOAPElement currentSoapElement;
069:
070: /**
071: * The first child element of the security header.
072: */
073: private SOAPElement topMostSoapElement;
074:
075: public SecurityHeader(SOAPElement delegateHeader) {
076: this .delegateHeader = delegateHeader;
077: this .ownerDoc = delegateHeader.getOwnerDocument();
078: topMostSoapElement = getFirstChildElement();
079: currentSoapElement = null;
080: }
081:
082: /**
083: * Inserts the header block at the top of the security header, i.e,
084: * the block becomes the first child element of the security header.
085: * This method will be used on the sender side.
086: */
087: public void insertHeaderBlock(SecurityHeaderBlock block)
088: throws XWSSecurityException {
089: SOAPElement elementToInsert = block.getAsSoapElement();
090: try {
091: if (elementToInsert.getOwnerDocument() != ownerDoc) {
092: elementToInsert = (SOAPElement) ownerDoc.importNode(
093: elementToInsert, true);
094: }
095:
096: updateTopMostSoapElement();
097:
098: insertBefore(elementToInsert, topMostSoapElement);
099:
100: } catch (DOMException e) {
101: log.log(Level.SEVERE, "WSS0376.error.inserting.header", e
102: .getMessage());
103: throw new XWSSecurityException(e);
104: }
105: topMostSoapElement = elementToInsert;
106: }
107:
108: public void insertBefore(SecurityHeaderBlock block, Node elem)
109: throws XWSSecurityException {
110: SOAPElement elementToInsert = block.getAsSoapElement();
111: try {
112: if (elementToInsert.getOwnerDocument() != ownerDoc) {
113: elementToInsert = (SOAPElement) ownerDoc.importNode(
114: elementToInsert, true);
115: }
116: } catch (DOMException e) {
117: log.log(Level.SEVERE, "WSS0376.error.inserting.header", e
118: .getMessage());
119: throw new XWSSecurityException(e);
120: }
121: insertBefore(elementToInsert, elem);
122: }
123:
124: public void appendChild(SecurityHeaderBlock block)
125: throws XWSSecurityException {
126: SOAPElement elementToInsert = block.getAsSoapElement();
127: try {
128: if (elementToInsert.getOwnerDocument() != ownerDoc) {
129: elementToInsert = (SOAPElement) ownerDoc.importNode(
130: elementToInsert, true);
131: }
132: appendChild(elementToInsert);
133:
134: } catch (DOMException e) {
135: log.log(Level.SEVERE, "WSS0376.error.inserting.header", e
136: .getMessage());
137: throw new XWSSecurityException(e);
138: }
139: }
140:
141: public void insertHeaderBlockElement(SOAPElement blockElement)
142: throws XWSSecurityException {
143: try {
144: if (blockElement.getOwnerDocument() != ownerDoc) {
145: blockElement = (SOAPElement) ownerDoc.importNode(
146: blockElement, true);
147: }
148: updateTopMostSoapElement();
149:
150: insertBefore(blockElement, topMostSoapElement);
151:
152: } catch (DOMException e) {
153: log.log(Level.SEVERE, "WSS0376.error.inserting.header", e
154: .getMessage());
155: throw new XWSSecurityException(e);
156: }
157: topMostSoapElement = blockElement;
158: }
159:
160: /**
161: * Get the header block to be processed next.
162: * This method will be used on the receiver side.
163: */
164: public SecurityHeaderBlock getCurrentHeaderBlock(Class implType)
165: throws XWSSecurityException {
166: if (null == currentSoapElement)
167: currentSoapElement = getFirstChildElement();
168: else {
169: Node nextChild = currentSoapElement.getNextSibling();
170: while ((null != nextChild)
171: && (nextChild.getNodeType() != Node.ELEMENT_NODE))
172: nextChild = nextChild.getNextSibling();
173: currentSoapElement = (SOAPElement) nextChild;
174: }
175: return SecurityHeaderBlockImpl.fromSoapElement(
176: currentSoapElement, implType);
177: }
178:
179: public SOAPElement getCurrentHeaderBlockElement() {
180: if (null == currentSoapElement)
181: currentSoapElement = getFirstChildElement();
182: else {
183: Node nextChild = currentSoapElement.getNextSibling();
184: while ((null != nextChild)
185: && (nextChild.getNodeType() != Node.ELEMENT_NODE))
186: nextChild = nextChild.getNextSibling();
187: currentSoapElement = (SOAPElement) nextChild;
188: }
189: return currentSoapElement;
190: }
191:
192: public void setCurrentHeaderElement(SOAPElement currentElement)
193: throws XWSSecurityException {
194: if (currentElement != null
195: && currentElement.getParentNode() != delegateHeader) {
196: log.log(Level.SEVERE, "WSS0396.notchild.securityHeader",
197: new Object[] { currentElement.toString() });
198: throw new XWSSecurityException(
199: "Element set is not a child of SecurityHeader");
200: }
201: currentSoapElement = currentElement;
202: }
203:
204: public SOAPElement getCurrentHeaderElement() {
205: return currentSoapElement;
206: }
207:
208: // TODO : Obsolete method -
209: // To be removed once we get rid of the OldEncryptFilter.
210: public void updateTopMostSoapElement() {
211: topMostSoapElement = getNextSiblingOfTimestamp();
212:
213: }
214:
215: public SOAPElement getFirstChildElement() {
216: Iterator eachChild = getChildElements();
217: javax.xml.soap.Node node = null;
218:
219: if (eachChild.hasNext()) {
220: node = (javax.xml.soap.Node) eachChild.next();
221: } else {
222: return null;
223: }
224:
225: while ((node.getNodeType() != Node.ELEMENT_NODE)
226: && eachChild.hasNext()) {
227: node = (javax.xml.soap.Node) eachChild.next();
228: }
229: if ((null != node) /*&& (node.getNodeType() == Node.ELEMENT_NODE)*/)
230: return (SOAPElement) node;
231: else
232: return null;
233: }
234:
235: public SOAPElement getNextSiblingOfTimestamp() {
236: SOAPElement firstElement = getFirstChildElement();
237: Node temp;
238: if (firstElement != null
239: && MessageConstants.TIMESTAMP_LNAME.equals(firstElement
240: .getLocalName())) {
241: temp = firstElement.getNextSibling();
242: if (temp == null)
243: return null;
244: while (temp.getNodeType() != Node.ELEMENT_NODE
245: && temp.getNextSibling() != null) {
246: temp = (javax.xml.soap.Node) temp.getNextSibling();
247: }
248: if (null != temp) {
249: while ((temp != null)
250: && (MessageConstants.SIGNATURE_CONFIRMATION_LNAME
251: .equals(temp.getLocalName()))) {
252: temp = temp.getNextSibling();
253: if (temp == null)
254: return null;
255: while (temp.getNodeType() != Node.ELEMENT_NODE
256: && temp.getNextSibling() != null) {
257: temp = (javax.xml.soap.Node) temp
258: .getNextSibling();
259: }
260: }
261: if (temp == null)
262: return null;
263: return (SOAPElement) temp;
264: } else
265: return null;
266: } else {
267: return firstElement;
268: }
269: }
270:
271: // This method was introduced to use a work-around for the
272: // selectSingleNode() problem.
273: public SOAPElement getAsSoapElement() {
274: return delegateHeader;
275: }
276:
277: // Mimic SOAPHeaderElement (almost)
278: public void setRole(String roleURI) {
279: throw new UnsupportedOperationException();
280: }
281:
282: public String getRole() {
283: throw new UnsupportedOperationException();
284: }
285:
286: public void setMustUnderstand(boolean mustUnderstand) {
287: throw new UnsupportedOperationException();
288: }
289:
290: public boolean isMustUnderstand() {
291: throw new UnsupportedOperationException();
292: }
293:
294: // All of the following methods are generated delegate methods...
295: public SOAPElement addAttribute(Name arg0, String arg1)
296: throws SOAPException {
297: return delegateHeader.addAttribute(arg0, arg1);
298: }
299:
300: public SOAPElement addChildElement(String arg0)
301: throws SOAPException {
302: return delegateHeader.addChildElement(arg0);
303: }
304:
305: public SOAPElement addChildElement(String arg0, String arg1)
306: throws SOAPException {
307: return delegateHeader.addChildElement(arg0, arg1);
308: }
309:
310: public SOAPElement addChildElement(String arg0, String arg1,
311: String arg2) throws SOAPException {
312: return delegateHeader.addChildElement(arg0, arg1, arg2);
313: }
314:
315: public SOAPElement addChildElement(Name arg0) throws SOAPException {
316: return delegateHeader.addChildElement(arg0);
317: }
318:
319: public SOAPElement addChildElement(SOAPElement arg0)
320: throws SOAPException {
321: return delegateHeader.addChildElement(arg0);
322: }
323:
324: public SOAPElement addNamespaceDeclaration(String arg0, String arg1)
325: throws SOAPException {
326: return delegateHeader.addNamespaceDeclaration(arg0, arg1);
327: }
328:
329: public SOAPElement addTextNode(String arg0) throws SOAPException {
330: return delegateHeader.addTextNode(arg0);
331: }
332:
333: public Node appendChild(Node arg0) throws DOMException {
334: return delegateHeader.appendChild(arg0);
335: }
336:
337: public SOAPElement makeUsable(SOAPElement elem)
338: throws XWSSecurityException {
339: SOAPElement elementToInsert = elem;
340: try {
341: if (elem.getOwnerDocument() != ownerDoc) {
342: elementToInsert = (SOAPElement) ownerDoc.importNode(
343: elem, true);
344: }
345: return elementToInsert;
346: } catch (DOMException e) {
347: log.log(Level.SEVERE, "WSS0376.error.inserting.header", e
348: .getMessage());
349: throw new XWSSecurityException(e);
350: }
351: }
352:
353: public Node cloneNode(boolean arg0) {
354: return delegateHeader.cloneNode(arg0);
355: }
356:
357: public void detachNode() {
358: delegateHeader.detachNode();
359: }
360:
361: public boolean equals(Object obj) {
362: return delegateHeader.equals(obj);
363: }
364:
365: public Iterator getAllAttributes() {
366: return delegateHeader.getAllAttributes();
367: }
368:
369: public String getAttribute(String arg0) {
370: return delegateHeader.getAttribute(arg0);
371: }
372:
373: public Attr getAttributeNode(String arg0) {
374: return delegateHeader.getAttributeNode(arg0);
375: }
376:
377: public Attr getAttributeNodeNS(String arg0, String arg1) {
378: return delegateHeader.getAttributeNodeNS(arg0, arg1);
379: }
380:
381: public String getAttributeNS(String arg0, String arg1) {
382: return delegateHeader.getAttributeNS(arg0, arg1);
383: }
384:
385: public NamedNodeMap getAttributes() {
386: return delegateHeader.getAttributes();
387: }
388:
389: public String getAttributeValue(Name arg0) {
390: return delegateHeader.getAttributeValue(arg0);
391: }
392:
393: public Iterator getChildElements() {
394: return delegateHeader.getChildElements();
395: }
396:
397: public Iterator getChildElements(Name arg0) {
398: return delegateHeader.getChildElements(arg0);
399: }
400:
401: public NodeList getChildNodes() {
402: return delegateHeader.getChildNodes();
403: }
404:
405: public Name getElementName() {
406: return delegateHeader.getElementName();
407: }
408:
409: public NodeList getElementsByTagName(String arg0) {
410: return delegateHeader.getElementsByTagName(arg0);
411: }
412:
413: public NodeList getElementsByTagNameNS(String arg0, String arg1) {
414: return delegateHeader.getElementsByTagNameNS(arg0, arg1);
415: }
416:
417: public String getEncodingStyle() {
418: return delegateHeader.getEncodingStyle();
419: }
420:
421: public Node getFirstChild() {
422: return delegateHeader.getFirstChild();
423: }
424:
425: public Node getLastChild() {
426: return delegateHeader.getLastChild();
427: }
428:
429: public String getLocalName() {
430: return delegateHeader.getLocalName();
431: }
432:
433: public Iterator getNamespacePrefixes() {
434: return delegateHeader.getNamespacePrefixes();
435: }
436:
437: public String getNamespaceURI() {
438: return delegateHeader.getNamespaceURI();
439: }
440:
441: public String getNamespaceURI(String arg0) {
442: return delegateHeader.getNamespaceURI(arg0);
443: }
444:
445: public Node getNextSibling() {
446: return delegateHeader.getNextSibling();
447: }
448:
449: public String getNodeName() {
450: return delegateHeader.getNodeName();
451: }
452:
453: public short getNodeType() {
454: return delegateHeader.getNodeType();
455: }
456:
457: public String getNodeValue() throws DOMException {
458: return delegateHeader.getNodeValue();
459: }
460:
461: public Document getOwnerDocument() {
462: return delegateHeader.getOwnerDocument();
463: }
464:
465: public SOAPElement getParentElement() {
466: return delegateHeader.getParentElement();
467: }
468:
469: public Node getParentNode() {
470: return delegateHeader.getParentNode();
471: }
472:
473: public String getPrefix() {
474: return delegateHeader.getPrefix();
475: }
476:
477: public Node getPreviousSibling() {
478: return delegateHeader.getPreviousSibling();
479: }
480:
481: public String getTagName() {
482: return delegateHeader.getTagName();
483: }
484:
485: public String getValue() {
486: return delegateHeader.getValue();
487: }
488:
489: public Iterator getVisibleNamespacePrefixes() {
490: return delegateHeader.getVisibleNamespacePrefixes();
491: }
492:
493: public boolean hasAttribute(String arg0) {
494: return delegateHeader.hasAttribute(arg0);
495: }
496:
497: public boolean hasAttributeNS(String arg0, String arg1) {
498: return delegateHeader.hasAttributeNS(arg0, arg1);
499: }
500:
501: public boolean hasAttributes() {
502: return delegateHeader.hasAttributes();
503: }
504:
505: public boolean hasChildNodes() {
506: return delegateHeader.hasChildNodes();
507: }
508:
509: public int hashCode() {
510: return delegateHeader.hashCode();
511: }
512:
513: public Node insertBefore(Node arg0, Node arg1) throws DOMException {
514:
515: return delegateHeader.insertBefore(arg0, arg1);
516: }
517:
518: public boolean isSupported(String arg0, String arg1) {
519: return delegateHeader.isSupported(arg0, arg1);
520: }
521:
522: public void normalize() {
523: delegateHeader.normalize();
524: }
525:
526: public void recycleNode() {
527: delegateHeader.recycleNode();
528: }
529:
530: public void removeAttribute(String arg0) throws DOMException {
531: delegateHeader.removeAttribute(arg0);
532: }
533:
534: public boolean removeAttribute(Name arg0) {
535: return delegateHeader.removeAttribute(arg0);
536: }
537:
538: public Attr removeAttributeNode(Attr arg0) throws DOMException {
539: return delegateHeader.removeAttributeNode(arg0);
540: }
541:
542: public void removeAttributeNS(String arg0, String arg1)
543: throws DOMException {
544: delegateHeader.removeAttributeNS(arg0, arg1);
545: }
546:
547: public Node removeChild(Node arg0) throws DOMException {
548: return delegateHeader.removeChild(arg0);
549: }
550:
551: public void removeContents() {
552: delegateHeader.removeContents();
553: }
554:
555: public boolean removeNamespaceDeclaration(String arg0) {
556: return delegateHeader.removeNamespaceDeclaration(arg0);
557: }
558:
559: public Node replaceChild(Node arg0, Node arg1) throws DOMException {
560: return delegateHeader.replaceChild(arg0, arg1);
561: }
562:
563: public void setAttribute(String arg0, String arg1)
564: throws DOMException {
565: delegateHeader.setAttribute(arg0, arg1);
566: }
567:
568: public Attr setAttributeNode(Attr arg0) throws DOMException {
569: return delegateHeader.setAttributeNode(arg0);
570: }
571:
572: public Attr setAttributeNodeNS(Attr arg0) throws DOMException {
573: return delegateHeader.setAttributeNodeNS(arg0);
574: }
575:
576: public void setAttributeNS(String arg0, String arg1, String arg2)
577: throws DOMException {
578: delegateHeader.setAttributeNS(arg0, arg1, arg2);
579: }
580:
581: public void setEncodingStyle(String arg0) throws SOAPException {
582: delegateHeader.setEncodingStyle(arg0);
583: }
584:
585: public void setNodeValue(String arg0) throws DOMException {
586: delegateHeader.setNodeValue(arg0);
587: }
588:
589: public void setParentElement(SOAPElement arg0) throws SOAPException {
590: delegateHeader.setParentElement(arg0);
591: }
592:
593: public void setPrefix(String arg0) throws DOMException {
594: delegateHeader.setPrefix(arg0);
595: }
596:
597: public void setValue(String arg0) {
598: delegateHeader.setValue(arg0);
599: }
600:
601: public String toString() {
602: return delegateHeader.toString();
603: }
604:
605: // DOM L3 methods from org.w3c.dom.Node
606: public String getBaseURI() {
607: return delegateHeader.getBaseURI();
608: }
609:
610: public short compareDocumentPosition(org.w3c.dom.Node other)
611: throws DOMException {
612: return delegateHeader.compareDocumentPosition(other);
613: }
614:
615: public String getTextContent() throws DOMException {
616: return delegateHeader.getTextContent();
617: }
618:
619: public void setTextContent(String textContent) throws DOMException {
620: delegateHeader.setTextContent(textContent);
621: }
622:
623: public boolean isSameNode(org.w3c.dom.Node other) {
624: return delegateHeader.isSameNode(other);
625: }
626:
627: public String lookupPrefix(String namespaceURI) {
628: return delegateHeader.lookupPrefix(namespaceURI);
629: }
630:
631: public boolean isDefaultNamespace(String namespaceURI) {
632: return delegateHeader.isDefaultNamespace(namespaceURI);
633: }
634:
635: public String lookupNamespaceURI(String prefix) {
636: return delegateHeader.lookupNamespaceURI(prefix);
637: }
638:
639: public boolean isEqualNode(org.w3c.dom.Node arg) {
640: return delegateHeader.isEqualNode(arg);
641: }
642:
643: public Object getFeature(String feature, String version) {
644: return delegateHeader.getFeature(feature, version);
645: }
646:
647: public Object setUserData(String key, Object data,
648: UserDataHandler handler) {
649: return delegateHeader.setUserData(key, data, handler);
650: }
651:
652: public Object getUserData(String key) {
653: return delegateHeader.getUserData(key);
654: }
655:
656: // DOM L3 methods from org.w3c.dom.Element
657:
658: public void setIdAttribute(String name, boolean isId)
659: throws DOMException {
660: delegateHeader.setIdAttribute(name, isId);
661: }
662:
663: public void setIdAttributeNode(Attr idAttr, boolean isId)
664: throws DOMException {
665: delegateHeader.setIdAttributeNode(idAttr, isId);
666: }
667:
668: public void setIdAttributeNS(String namespaceURI, String localName,
669: boolean isId) throws DOMException {
670: delegateHeader.setIdAttributeNS(namespaceURI, localName, isId);
671: }
672:
673: public TypeInfo getSchemaTypeInfo() {
674: return delegateHeader.getSchemaTypeInfo();
675: }
676:
677: public Iterator getAllAttributesAsQNames() {
678: return delegateHeader.getAllAttributesAsQNames();
679: }
680:
681: }
|