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.Constants;
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.ArrayList;
29  import java.util.List;
30  
31  public class RequiredParts extends AbstractSecurityAssertion {
32  
33      private final List<Header> headers = new ArrayList<>();
34  
35      public RequiredParts(SPConstants.SPVersion version, List<Header> headers) {
36          super(version);
37          this.headers.addAll(headers);
38      }
39  
40      @Override
41      public QName getName() {
42          return getVersion().getSPConstants().getRequiredParts();
43      }
44  
45      @Override
46      public boolean equals(Object object) {
47          if (object == this) {
48              return true;
49          }
50          if (!(object instanceof RequiredParts)) {
51              return false;
52          }
53  
54          RequiredParts that = (RequiredParts)object;
55          if (!headers.equals(that.headers)) {
56              return false;
57          }
58  
59          return super.equals(object);
60      }
61  
62      @Override
63      public int hashCode() {
64          int result = 17;
65          result = 31 * result + headers.hashCode();
66  
67          return 31 * result + super.hashCode();
68      }
69  
70      @Override
71      public void serialize(XMLStreamWriter writer) throws XMLStreamException {
72          writer.writeStartElement(getName().getPrefix(), getName().getLocalPart(), getName().getNamespaceURI());
73          writer.writeNamespace(getName().getPrefix(), getName().getNamespaceURI());
74          if (!isNormalized() && isOptional()) {
75              writer.writeAttribute(Constants.ATTR_WSP,
76                                    writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
77                                    Constants.ATTR_OPTIONAL,
78                                    "true");
79          }
80          if (isIgnorable()) {
81              writer.writeAttribute(Constants.ATTR_WSP,
82                                    writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
83                                    Constants.ATTR_IGNORABLE,
84                                    "true");
85          }
86          for (int i = 0; i < getHeaders().size(); i++) {
87              Header header = getHeaders().get(i);
88              final QName headerName = getVersion().getSPConstants().getHeader();
89              writer.writeEmptyElement(headerName.getPrefix(), headerName.getLocalPart(), headerName.getNamespaceURI());
90              if (header.getName() != null) {
91                  writer.writeAttribute(SPConstants.NAME, header.getName());
92              }
93              writer.writeAttribute(SPConstants.NAMESPACE, header.getNamespace());
94          }
95          writer.writeEndElement();
96      }
97  
98      @Override
99      protected AbstractSecurityAssertion cloneAssertion(Policy nestedPolicy) {
100         return new RequiredParts(getVersion(), getHeaders());
101     }
102 
103     public List<Header> getHeaders() {
104         return this.headers;
105     }
106 
107     protected void addHeader(Header header) {
108         this.headers.add(header);
109     }
110 }