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.api.message.Header;
026: import com.sun.xml.ws.api.message.HeaderList;
027: import com.sun.xml.ws.security.opt.api.NamespaceContextInfo;
028: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
029: import com.sun.xml.ws.security.opt.impl.JAXBFilterProcessingContext;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import javax.xml.crypto.Data;
033: import javax.xml.crypto.URIDereferencer;
034: import javax.xml.crypto.URIReference;
035: import javax.xml.crypto.URIReferenceException;
036: import javax.xml.crypto.XMLCryptoContext;
037:
038: /**
039: *
040: * @author K.Venugopal@sun.com
041: */
042: public class URIResolver implements URIDereferencer {
043: private SecurityContext securityContext;
044: private JAXBFilterProcessingContext pc = null;
045:
046: /** Creates a new instance of resolver */
047: public URIResolver(JAXBFilterProcessingContext pc) {
048: this .pc = pc;
049: this .securityContext = pc.getSecurityContext();
050: }
051:
052: public Data dereference(URIReference uRIReference,
053: XMLCryptoContext xMLCryptoContext)
054: throws URIReferenceException {
055:
056: HeaderList headers = securityContext.getNonSecurityHeaders();
057: String tmpId = uRIReference.getURI();
058: String id = "";
059: int index = tmpId.indexOf("#");
060: if (index >= 0) {
061: id = tmpId.substring(index + 1);
062: } else {
063: id = tmpId;
064: }
065: if (headers != null && headers.size() > 0) {
066: Iterator<Header> listItr = headers.listIterator();
067: boolean found = false;
068: while (listItr.hasNext()) {
069: GenericSecuredHeader header = (GenericSecuredHeader) listItr
070: .next();
071: if (header.hasID(id) && !header.hasEncData()) {
072: return new StreamWriterData(header,
073: ((NamespaceContextInfo) header)
074: .getInscopeNSContext());
075: }
076: }
077: }
078:
079: ArrayList pshList = securityContext
080: .getProcessedSecurityHeaders();
081: for (int j = 0; j < pshList.size(); j++) {
082: SecurityHeaderElement header = (SecurityHeaderElement) pshList
083: .get(j);
084: if (id.equals(header.getId())) {
085: if (header instanceof NamespaceContextInfo) {
086: return new StreamWriterData(header,
087: ((NamespaceContextInfo) header)
088: .getInscopeNSContext());
089: } else {
090: throw new URIReferenceException(
091: "Cannot derefernce this MessagePart and use if for any crypto operation "
092: + "as the message part is not cached");
093: }
094: }
095: }
096:
097: // looking into buffered headers for - (Should be used only for getting the key)
098: // What will happen when encrypting the content but signing the entire element? Can go wrong
099: ArrayList bufList = securityContext
100: .getBufferedSecurityHeaders();
101: for (int j = 0; j < bufList.size(); j++) {
102: SecurityHeaderElement header = (SecurityHeaderElement) bufList
103: .get(j);
104: if (id.equals(header.getId())) {
105: if (header instanceof NamespaceContextInfo) {
106: return new StreamWriterData(header,
107: ((NamespaceContextInfo) header)
108: .getInscopeNSContext());
109: } else {
110: throw new URIReferenceException(
111: "Cannot derefernce this MessagePart and use if for any crypto operation "
112: + "as the message part is not cached");
113: }
114: }
115: }
116:
117: Data data = null;
118: data = (Data) pc.getElementCache().get(id);
119: return data;
120: }
121:
122: }
|