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.wss4j.policy.SPConstants;
23  
24  import javax.xml.namespace.QName;
25  import javax.xml.stream.XMLStreamException;
26  import javax.xml.stream.XMLStreamWriter;
27  import java.util.List;
28  
29  public abstract class AbstractSecuredParts extends RequiredParts {
30  
31      private boolean body;
32      private Attachments attachments;
33  
34      public AbstractSecuredParts(SPConstants.SPVersion version, boolean body, Attachments attachments,
35                         List<Header> headers) {
36          super(version, headers);
37  
38          this.body = body;
39          this.attachments = attachments;
40      }
41  
42      @Override
43      public QName getName() {
44          return getVersion().getSPConstants().getSignedParts();
45      }
46  
47      @Override
48      public boolean equals(Object object) {
49          if (object == this) {
50              return true;
51          }
52          if (!(object instanceof AbstractSecuredParts)) {
53              return false;
54          }
55  
56          AbstractSecuredParts that = (AbstractSecuredParts)object;
57          if (body != that.body) {
58              return false;
59          }
60          if (attachments != null && !attachments.equals(that.attachments)
61              || attachments == null && that.attachments != null) {
62              return false;
63          }
64  
65          return super.equals(object);
66      }
67  
68      @Override
69      public int hashCode() {
70          int result = 17;
71          if (attachments != null) {
72              result = 31 * result + attachments.hashCode();
73          }
74          result = 31 * result + Boolean.hashCode(body);
75  
76          return 31 * result + super.hashCode();
77      }
78  
79      @Override
80      public void serialize(XMLStreamWriter writer) throws XMLStreamException {
81          writer.writeStartElement(getName().getPrefix(), getName().getLocalPart(), getName().getNamespaceURI());
82          writer.writeNamespace(getName().getPrefix(), getName().getNamespaceURI());
83          if (!isNormalized() && isOptional()) {
84              writer.writeAttribute(Constants.ATTR_WSP,
85                                    writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
86                                    Constants.ATTR_OPTIONAL, "true");
87          }
88          if (isIgnorable()) {
89              writer.writeAttribute(Constants.ATTR_WSP,
90                                    writer.getNamespaceContext().getNamespaceURI(Constants.ATTR_WSP),
91                                    Constants.ATTR_IGNORABLE, "true");
92          }
93          if (isBody()) {
94              final QName body = getVersion().getSPConstants().getBody();
95              writer.writeEmptyElement(body.getPrefix(), body.getLocalPart(), body.getNamespaceURI());
96          }
97          for (int i = 0; i < getHeaders().size(); i++) {
98              Header header = getHeaders().get(i);
99              final QName headerName = getVersion().getSPConstants().getHeader();
100             writer.writeEmptyElement(headerName.getPrefix(), headerName.getLocalPart(), headerName.getNamespaceURI());
101             if (header.getName() != null) {
102                 writer.writeAttribute(SPConstants.NAME, header.getName());
103             }
104             writer.writeAttribute(SPConstants.NAMESPACE, header.getNamespace());
105         }
106         if (getAttachments() != null) {
107             getAttachments().serialize(writer);
108         }
109         writer.writeEndElement();
110     }
111 
112     public boolean isBody() {
113         return body;
114     }
115 
116     protected void setBody(boolean body) {
117         this.body = body;
118     }
119 
120     public Attachments getAttachments() {
121         return attachments;
122     }
123 
124     protected void setAttachments(Attachments attachments) {
125         this.attachments = attachments;
126     }
127 
128 }