View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
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   * This class models WebDAV response tag. Response tag exist only as
28   * a part of multistatus tag but values from it can be used to create
29   * partial response or header response only.
30   *
31   * @author Daniel Sendula
32   */
33  public class Response implements XMLRenderer {
34  
35      /** Tag name */
36      public static final String TAG_NAME = "response";
37  
38      /** Status tag's name */
39      public static final String STATUS_TAG_NAME = "status";
40  
41      /** Multistatus response belongs to */
42      protected MultiStatus multiStatus;
43  
44      /** Status of the response. May be <code>null</code>. */
45      protected Status status;
46  
47      /** Response's href */
48      protected HRef href;
49  
50      /** List of additional hrefs. May be <code>null</code>. */
51      protected List<HRef> additionalHRefs;
52  
53      /** List of propstats. May be <code>null</code>. */
54      protected List<Propstat> propstats;
55  
56      /** Response description */
57      protected String responseDescription;
58  
59      /**
60       * Constructor
61       * @param multiStatus parent multistatus
62       * @param href href
63       */
64      public Response(MultiStatus multiStatus, HRef href) {
65          this.multiStatus = multiStatus;
66          this.href = href;
67      }
68  
69      /**
70       * Returns defined only if there are propstats in this response
71       * @return defined only if there are propstats in this response
72       */
73      public boolean isDefined() {
74          return (propstats != null);
75      }
76  
77      /**
78       * Returns response's status. May be <code>null</code>.
79       * @return status
80       */
81      public Status getStatus() {
82          return status;
83      }
84  
85      /**
86       * Sets status
87       * @param status status
88       */
89      public void setStatus(Status status) {
90          this.status = status;
91      }
92  
93      /**
94       * Returns response's href
95       * @return response's href
96       */
97      public HRef getHRef() {
98          return href;
99      }
100 
101     /**
102      * Returns additional hrefs. May be <code>null</code>.
103      * @return additional hrefs
104      */
105     public List<HRef> getAdditionalHRefs() {
106         return additionalHRefs;
107     }
108 
109     /**
110      * Sets list of additional hrefs
111      * @param additionalHRefs list of additional hrefs
112      */
113     public void setAdditionalHRefs(List<HRef> additionalHRefs) {
114         this.additionalHRefs = additionalHRefs;
115     }
116 
117     /**
118      * Adds additional href.
119      * @param href additional href
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      * Returns response description
130      * @return response description
131      */
132     public String getResponseDescription() {
133         return responseDescription;
134     }
135 
136     /**
137      * Sets resoinse description.
138      * @param responseDescription response description
139      */
140     public void setResponseDescription(String responseDescription) {
141         this.responseDescription = responseDescription;
142     }
143 
144     /**
145      * Returns list of propstats. It will always return the list - won't be <code>null</code>.
146      * @return list of propstats.
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      * Renders the tag
193      * @param writer writer
194      * @param provider namespace provider
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      * Renders status
219      * @param writer writer
220      * @param provider provider
221      * @param status status
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 }