1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.webdav.xml.dav.response;
14
15 import org.abstracthorizon.danube.http.Status;
16 import org.abstracthorizon.danube.webdav.util.NamespacesProvider;
17 import org.abstracthorizon.danube.webdav.xml.XMLRenderer;
18 import org.abstracthorizon.danube.webdav.xml.common.XMLUtils;
19 import org.abstracthorizon.danube.webdav.xml.dav.DAVNamespace;
20 import org.abstracthorizon.danube.webdav.xml.dav.HRef;
21
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27
28
29
30
31
32
33 public class Response implements XMLRenderer {
34
35
36 public static final String TAG_NAME = "response";
37
38
39 public static final String STATUS_TAG_NAME = "status";
40
41
42 protected MultiStatus multiStatus;
43
44
45 protected Status status;
46
47
48 protected HRef href;
49
50
51 protected List<HRef> additionalHRefs;
52
53
54 protected List<Propstat> propstats;
55
56
57 protected String responseDescription;
58
59
60
61
62
63
64 public Response(MultiStatus multiStatus, HRef href) {
65 this.multiStatus = multiStatus;
66 this.href = href;
67 }
68
69
70
71
72
73 public boolean isDefined() {
74 return (propstats != null);
75 }
76
77
78
79
80
81 public Status getStatus() {
82 return status;
83 }
84
85
86
87
88
89 public void setStatus(Status status) {
90 this.status = status;
91 }
92
93
94
95
96
97 public HRef getHRef() {
98 return href;
99 }
100
101
102
103
104
105 public List<HRef> getAdditionalHRefs() {
106 return additionalHRefs;
107 }
108
109
110
111
112
113 public void setAdditionalHRefs(List<HRef> additionalHRefs) {
114 this.additionalHRefs = additionalHRefs;
115 }
116
117
118
119
120
121 public void addHRef(HRef href) {
122 if (additionalHRefs == null) {
123 additionalHRefs = new ArrayList<HRef>();
124 }
125 additionalHRefs.add(href);
126 }
127
128
129
130
131
132 public String getResponseDescription() {
133 return responseDescription;
134 }
135
136
137
138
139
140 public void setResponseDescription(String responseDescription) {
141 this.responseDescription = responseDescription;
142 }
143
144
145
146
147
148 public List<Propstat> getPropStats() {
149 if (propstats == null) {
150 propstats = new ArrayList<Propstat>();
151 }
152 return propstats;
153 }
154
155 @Override
156 public String toString() {
157 if (status != null) {
158 StringBuffer response = new StringBuffer("Response[");
159 response.append(href);
160 if (additionalHRefs != null) {
161 for (HRef href : additionalHRefs) {
162 response.append(',');
163 response.append(href);
164 }
165 }
166 response.append(',');
167 response.append(status);
168 response.append(']');
169 return response.toString();
170 } else {
171 StringBuffer response = new StringBuffer("Response[");
172 response.append(href);
173 response.append(",{");
174 if (propstats != null) {
175 boolean first = true;
176 for (Propstat propStat : propstats) {
177 if (first) {
178 first = false;
179 } else {
180 response.append(',');
181 }
182 response.append(propStat);
183 }
184 }
185 response.append('}');
186 response.append(']');
187 return response.toString();
188 }
189 }
190
191
192
193
194
195
196 public void render(PrintWriter writer, NamespacesProvider provider) {
197 writer.println(XMLUtils.createStartTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
198 href.render(writer, provider);
199 if (status != null) {
200 if (additionalHRefs != null) {
201 for (HRef href: additionalHRefs) {
202 href.render(writer, provider);
203 }
204 }
205 renderStatus(writer, provider, status);
206 } else {
207 for (Propstat propstat : propstats) {
208 propstat.render(writer, provider);
209 }
210 }
211 if (responseDescription != null) {
212 writer.println(XMLUtils.createTag(provider, DAVNamespace.DAV_NAMESPACE_URL, "responsedescription", responseDescription));
213 }
214 writer.println(XMLUtils.createEndTag(provider, DAVNamespace.DAV_NAMESPACE_URL, TAG_NAME));
215 }
216
217
218
219
220
221
222
223 public void renderStatus(PrintWriter writer, NamespacesProvider provider, Status status) {
224 writer.println(XMLUtils.createTag(provider, DAVNamespace.DAV_NAMESPACE_URL, STATUS_TAG_NAME, multiStatus.getResponseProtocol() + " " + status.getFullStatus()));
225 }
226 }