001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.security.impl.policyconv;
038:
039: import com.sun.xml.ws.security.policy.AlgorithmSuite;
040: import com.sun.xml.ws.security.policy.EncryptedElements;
041: import com.sun.xml.ws.security.policy.EncryptedParts;
042: import com.sun.xml.ws.security.policy.Header;
043: import com.sun.xml.wss.impl.policy.mls.EncryptionPolicy;
044: import com.sun.xml.wss.impl.policy.mls.EncryptionTarget;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.logging.Level;
048: import javax.xml.namespace.QName;
049: import static com.sun.xml.ws.security.impl.policy.Constants.logger;
050:
051: /**
052: *
053: * @author K.Venugopal@sun.com
054: */
055: public class EncryptionAssertionProcessor {
056: private AlgorithmSuite algorithmSuite = null;
057: private boolean bodyEncrypted = false;
058: private boolean enforce = false;
059: private HashSet<Header> encryptedParts = new HashSet<Header>();
060: // private EncryptionTargetCreator etc =null;
061: private EncryptionTargetCreator etCreator = null;
062:
063: /** Creates a new instance of EncryptionAssertionProcessor */
064: public EncryptionAssertionProcessor(AlgorithmSuite algorithmSuite,
065: boolean enforce) {
066: this .algorithmSuite = algorithmSuite;
067: this .enforce = enforce;
068: this .etCreator = new EncryptionTargetCreator(algorithmSuite,
069: enforce);
070: }
071:
072: public EncryptionTargetCreator getTargetCreator() {
073: return etCreator;
074: }
075:
076: public void process(EncryptedParts encryptParts,
077: EncryptionPolicy.FeatureBinding binding) {
078: if (SecurityPolicyUtil.isEncryptedPartsEmpty(encryptParts)) {
079: if (!bodyEncrypted) {
080: EncryptionTarget target = etCreator
081: .newQNameEncryptionTarget(EncryptionTarget.BODY_QNAME);
082: target.setContentOnly(true);
083: binding.addTargetBinding(target);
084: bodyEncrypted = true;
085: }
086: }
087: Iterator tv = encryptParts.getTargets();
088: while (tv.hasNext()) {
089: Header ht = (Header) tv.next();
090: if (!seenEncryptedParts(ht)) {
091: EncryptionTarget target = etCreator
092: .newQNameEncryptionTarget(new QName(
093: ht.getURI(), ht.getLocalName()));
094: target.isSOAPHeadersOnly(true);
095: binding.addTargetBinding(target);
096: }
097: }
098:
099: if (encryptParts.hasBody() && !bodyEncrypted) {
100: EncryptionTarget target = etCreator
101: .newQNameEncryptionTarget(EncryptionTarget.BODY_QNAME);
102: target.setContentOnly(true);
103: binding.addTargetBinding(target);
104: bodyEncrypted = true;
105: }
106: }
107:
108: //TODO:merge multiple EncryptedElements
109: public void process(EncryptedElements encryptedElements,
110: EncryptionPolicy.FeatureBinding binding) {
111: Iterator<String> eeItr = encryptedElements.getTargets();
112: while (eeItr.hasNext()) {
113: String xpathTarget = eeItr.next();
114: EncryptionTarget target = etCreator
115: .newXpathEncryptionTarget(xpathTarget);
116: binding.addTargetBinding(target);
117: }
118: }
119:
120: private boolean seenEncryptedParts(Header header) {
121: if (encryptedParts.contains(header)) {
122: return true;
123: }
124: encryptedParts.add(header);
125: return false;
126: }
127:
128: public void process(QName targetName,
129: EncryptionPolicy.FeatureBinding binding) {
130: EncryptionTarget target = etCreator
131: .newQNameEncryptionTarget(targetName);
132: binding.addTargetBinding(target);
133: }
134: }
|