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 javax.xml.stream.XMLStreamException;
27  import javax.xml.stream.XMLStreamWriter;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  public class TransportBinding extends AbstractBinding {
32  
33      private TransportToken transportToken;
34  
35      public TransportBinding(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().getTransportBinding();
44      }
45  
46      @Override
47      public boolean equals(Object object) {
48          if (object == this) {
49              return true;
50          }
51  
52          if (!(object instanceof TransportBinding)) {
53              return false;
54          }
55  
56          TransportBinding that = (TransportBinding)object;
57          if (transportToken != null && !transportToken.equals(that.transportToken)
58              || transportToken == null && that.transportToken != null) {
59              return false;
60          }
61  
62          return super.equals(object);
63      }
64  
65      @Override
66      public int hashCode() {
67          int result = 17;
68          if (transportToken != null) {
69              result = 31 * result + transportToken.hashCode();
70          }
71  
72          return 31 * result + super.hashCode();
73      }
74  
75      @Override
76      protected AbstractSecurityAssertion cloneAssertion(Policy nestedPolicy) {
77          return new TransportBinding(getVersion(), nestedPolicy);
78      }
79  
80      protected void parseNestedPolicy(Policy nestedPolicy, TransportBinding transportBinding) {
81          Iterator<List<Assertion>> alternatives = nestedPolicy.getAlternatives();
82          //we just process the first alternative
83          //this means that if we have a compact policy only the first alternative is visible
84          //in contrary to a normalized policy where just one alternative exists
85          if (alternatives.hasNext()) {
86              List<Assertion> assertions = alternatives.next();
87              for (Assertion assertion : assertions) {
88                  String assertionName = assertion.getName().getLocalPart();
89                  String assertionNamespace = assertion.getName().getNamespaceURI();
90  
91                  QName transportToken = getVersion().getSPConstants().getTransportToken();
92                  if (transportToken.getLocalPart().equals(assertionName)
93                      && transportToken.getNamespaceURI().equals(assertionNamespace)) {
94                      if (transportBinding.getTransportToken() != null) {
95                          throw new IllegalArgumentException(SPConstants.ERR_INVALID_POLICY);
96                      }
97                      transportBinding.setTransportToken((TransportToken) assertion);
98                      continue;
99                  }
100             }
101         }
102     }
103 
104     @Override
105     public void serialize(XMLStreamWriter writer) throws XMLStreamException {
106         super.serialize(writer, getPolicy());
107     }
108 
109     public TransportToken getTransportToken() {
110         return transportToken;
111     }
112 
113     protected void setTransportToken(TransportToken transportToken) {
114         this.transportToken = transportToken;
115     }
116 }