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.stream.buffer.XMLStreamBuffer;
026: import com.sun.xml.ws.security.opt.api.NamespaceContextInfo;
027: import com.sun.xml.ws.security.opt.api.PolicyBuilder;
028: import com.sun.xml.ws.security.opt.api.SecurityElementWriter;
029: import com.sun.xml.ws.security.opt.api.SecurityHeaderElement;
030: import com.sun.xml.ws.security.opt.api.TokenValidator;
031: import com.sun.xml.wss.ProcessingContext;
032: import com.sun.xml.wss.XWSSecurityException;
033: import com.sun.xml.wss.impl.MessageConstants;
034: import com.sun.xml.wss.impl.policy.mls.SignatureConfirmationPolicy;
035: import com.sun.xml.wss.impl.policy.mls.WSSPolicy;
036: import java.io.OutputStream;
037: import java.util.ArrayList;
038: import java.util.List;
039: import javax.xml.stream.XMLStreamReader;
040: import com.sun.xml.stream.buffer.stax.StreamReaderBufferCreator;
041: import javax.xml.stream.XMLInputFactory;
042: import com.sun.xml.ws.security.opt.impl.util.XMLStreamReaderFactory;
043: import javax.xml.stream.XMLStreamException;
044: import java.util.HashMap;
045: import javax.xml.stream.StreamFilter;
046: import com.sun.xml.stream.buffer.XMLStreamBufferMark;
047: import java.util.logging.Level;
048: import java.util.logging.Logger;
049: import com.sun.xml.wss.logging.LogDomainConstants;
050:
051: /**
052: *
053: * @author Ashutosh.Shahi@sun.com
054: */
055: public class SignatureConfirmation implements SecurityHeaderElement,
056: TokenValidator, PolicyBuilder, NamespaceContextInfo,
057: SecurityElementWriter {
058:
059: protected static final Logger log = Logger.getLogger(
060: LogDomainConstants.FILTER_DOMAIN,
061: LogDomainConstants.FILTER_DOMAIN_BUNDLE);
062:
063: private String id = "";
064: private String namespaceURI = "";
065: private String localName = "";
066: private String signatureValue = null;
067:
068: private SignatureConfirmationPolicy scPolicy = null;
069: private HashMap<String, String> nsDecls;
070: private XMLStreamBuffer mark = null;
071:
072: /**
073: * Creates a new instance of SignatureConfirmation
074: */
075: public SignatureConfirmation(XMLStreamReader reader,
076: StreamReaderBufferCreator creator, HashMap nsDecls,
077: XMLInputFactory staxIF) throws XMLStreamException {
078:
079: namespaceURI = reader.getNamespaceURI();
080: localName = reader.getLocalName();
081: id = reader.getAttributeValue(MessageConstants.WSU_NS, "Id");
082:
083: mark = new XMLStreamBufferMark(nsDecls, creator);
084: creator.createElementFragment(XMLStreamReaderFactory
085: .createFilteredXMLStreamReader(reader,
086: new SCProcessor()), false);
087:
088: this .nsDecls = nsDecls;
089:
090: scPolicy = new SignatureConfirmationPolicy();
091: scPolicy.setSignatureValue(signatureValue);
092: }
093:
094: public String getSignatureValue() {
095: return signatureValue;
096: }
097:
098: public boolean refersToSecHdrWithId(String id) {
099: throw new UnsupportedOperationException();
100: }
101:
102: public String getId() {
103: return id;
104: }
105:
106: public void setId(String id) {
107: throw new UnsupportedOperationException();
108: }
109:
110: public String getNamespaceURI() {
111: return namespaceURI;
112: }
113:
114: public String getLocalPart() {
115: return localName;
116: }
117:
118: public javax.xml.stream.XMLStreamReader readHeader()
119: throws javax.xml.stream.XMLStreamException {
120: return mark.readAsXMLStreamReader();
121: }
122:
123: public void writeTo(OutputStream os) {
124: throw new UnsupportedOperationException();
125: }
126:
127: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter)
128: throws javax.xml.stream.XMLStreamException {
129: mark.writeToXMLStreamWriter(streamWriter);
130: }
131:
132: public void validate(ProcessingContext context)
133: throws XWSSecurityException {
134: Object temp = context
135: .getExtraneousProperty("SignatureConfirmation");
136: List scList = null;
137: if (temp != null && temp instanceof ArrayList)
138: scList = (ArrayList) temp;
139: if (scList != null) {
140: if (signatureValue == null) {
141: if (!scList.isEmpty()) {
142: log
143: .log(Level.SEVERE,
144: "Failure in SignatureConfirmation Validation");
145: throw new XWSSecurityException(
146: "Failure in SignatureConfirmation Validation");
147: }
148: } else if (scList.contains(signatureValue)) {// match the Value in received message
149: //with the stored value
150: scList.remove(signatureValue);
151: } else {
152: log.log(Level.SEVERE,
153: "Failure in SignatureConfirmation Validation");
154: throw new XWSSecurityException(
155: "Mismatch in SignatureConfirmation Element");
156: }
157: }
158: }
159:
160: public WSSPolicy getPolicy() {
161: return scPolicy;
162: }
163:
164: public HashMap<String, String> getInscopeNSContext() {
165: return nsDecls;
166: }
167:
168: public void writeTo(javax.xml.stream.XMLStreamWriter streamWriter,
169: HashMap props) throws javax.xml.stream.XMLStreamException {
170: throw new UnsupportedOperationException();
171: }
172:
173: class SCProcessor implements StreamFilter {
174: boolean elementRead = false;
175:
176: public boolean accept(XMLStreamReader reader) {
177: if (reader.getEventType() == XMLStreamReader.END_ELEMENT) {
178: if (reader.getLocalName() == localName
179: && reader.getNamespaceURI() == namespaceURI) {
180: elementRead = true;
181: }
182: }
183: if (!elementRead
184: && reader.getEventType() == XMLStreamReader.START_ELEMENT) {
185: signatureValue = reader
186: .getAttributeValue(null, "Value");
187: }
188: return true;
189: }
190: }
191:
192: }
|