1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }