View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.wss4j.policy.model;
20  
21  import org.apache.neethi.Assertion;
22  import org.apache.neethi.Policy;
23  import org.apache.wss4j.policy.SPConstants;
24  
25  import javax.xml.namespace.QName;
26  import java.util.Iterator;
27  import java.util.List;
28  
29  public class SymmetricBinding extends AbstractSymmetricAsymmetricBinding {
30  
31      private EncryptionToken encryptionToken;
32      private SignatureToken signatureToken;
33      private ProtectionToken protectionToken;
34  
35      public SymmetricBinding(SPConstants.SPVersion version, Policy nestedPolicy) {
36          super(version, nestedPolicy);
37  
38          parseNestedPolicy(nestedPolicy, this);
39      }
40  
41      @Override
42      public QName getName() {
43          return getVersion().getSPConstants().getSymmetricBinding();
44      }
45  
46      @Override
47      public boolean equals(Object object) {
48          if (object == this) {
49              return true;
50          }
51  
52          if (!(object instanceof SymmetricBinding)) {
53              return false;
54          }
55  
56          SymmetricBinding that = (SymmetricBinding)object;
57          if (encryptionToken != null && !encryptionToken.equals(that.encryptionToken)
58              || encryptionToken == null && that.encryptionToken != null) {
59              return false;
60          }
61          if (signatureToken != null && !signatureToken.equals(that.signatureToken)
62              || signatureToken == null && that.signatureToken != null) {
63              return false;
64          }
65          if (protectionToken != null && !protectionToken.equals(that.protectionToken)
66              || protectionToken == null && that.protectionToken != null) {
67              return false;
68          }
69  
70          return super.equals(object);
71      }
72  
73      @Override
74      public int hashCode() {
75          int result = 17;
76          if (encryptionToken != null) {
77              result = 31 * result + encryptionToken.hashCode();
78          }
79          if (signatureToken != null) {
80              result = 31 * result + signatureToken.hashCode();
81          }
82          if (protectionToken != null) {
83              result = 31 * result + protectionToken.hashCode();
84          }
85  
86          return 31 * result + super.hashCode();
87      }
88  
89      @Override
90      protected AbstractSecurityAssertion cloneAssertion(Policy nestedPolicy) {
91          return new SymmetricBinding(getVersion(), nestedPolicy);
92      }
93  
94      protected void parseNestedPolicy(Policy nestedPolicy, SymmetricBinding symmetricBinding) {
95          Iterator<List<Assertion>> alternatives = nestedPolicy.getAlternatives();
96          //we just process the first alternative
97          //this means that if we have a compact policy only the first alternative is visible
98          //in contrary to a normalized policy where just one alternative exists
99          if (alternatives.hasNext()) {
100             List<Assertion> assertions = alternatives.next();
101             for (Assertion assertion : assertions) {
102                 String assertionName = assertion.getName().getLocalPart();
103                 String assertionNamespace = assertion.getName().getNamespaceURI();
104 
105                 QName encryptionToken = getVersion().getSPConstants().getEncryptionToken();
106                 if (encryptionToken.getLocalPart().equals(assertionName)
107                     && encryptionToken.getNamespaceURI().equals(assertionNamespace)) {
108                     if (symmetricBinding.getEncryptionToken() != null
109                         || symmetricBinding.getProtectionToken() != null) {
110                         throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
111                     }
112                     symmetricBinding.setEncryptionToken((EncryptionToken) assertion);
113                     continue;
114                 }
115 
116                 QName signatureToken = getVersion().getSPConstants().getSignatureToken();
117                 if (signatureToken.getLocalPart().equals(assertionName)
118                     && signatureToken.getNamespaceURI().equals(assertionNamespace)) {
119                     if (symmetricBinding.getSignatureToken() != null
120                         || symmetricBinding.getProtectionToken() != null) {
121                         throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
122                     }
123                     symmetricBinding.setSignatureToken((SignatureToken) assertion);
124                     continue;
125                 }
126 
127                 QName protectionToken = getVersion().getSPConstants().getProtectionToken();
128                 if (protectionToken.getLocalPart().equals(assertionName)
129                     && protectionToken.getNamespaceURI().equals(assertionNamespace)) {
130                     if (symmetricBinding.getProtectionToken() != null
131                             || symmetricBinding.getEncryptionToken() != null
132                             || symmetricBinding.getSignatureToken() != null) {
133                         throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
134                     }
135                     symmetricBinding.setProtectionToken((ProtectionToken) assertion);
136                     continue;
137                 }
138             }
139         }
140     }
141 
142     public EncryptionToken getEncryptionToken() {
143         return encryptionToken;
144     }
145 
146     protected void setEncryptionToken(EncryptionToken encryptionToken) {
147         this.encryptionToken = encryptionToken;
148     }
149 
150     public SignatureToken getSignatureToken() {
151         return signatureToken;
152     }
153 
154     protected void setSignatureToken(SignatureToken signatureToken) {
155         this.signatureToken = signatureToken;
156     }
157 
158     public ProtectionToken getProtectionToken() {
159         return protectionToken;
160     }
161 
162     protected void setProtectionToken(ProtectionToken protectionToken) {
163         this.protectionToken = protectionToken;
164     }
165 }