1    /*
2     * Copyright 2009 :torweg free software group
3     *
4     * This program is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     * 
9     * This program is distributed in the hope that it will be useful,
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    * GNU General Public License for more details.
13    * 
14    * You should have received a copy of the GNU General Public License
15    * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16    *
17    */
18   package org.torweg.pulse.webdav.util;
19   
20   import java.io.UnsupportedEncodingException;
21   import java.net.URI;
22   import java.net.URLDecoder;
23   
24   import org.jdom.Element;
25   import org.torweg.pulse.webdav.AbstractDAVServlet;
26   import org.torweg.pulse.webdav.response.IMultiStatusElement;
27   
28   /**
29    * abstract base class for response elements, creating a very basic XML {@code
30    * <d:response><d:href>/uri</d:href></d:response>}.
31    * 
32    * @author Thomas Weber
33    * @version $Revision: 1236 $
34    */
35   public abstract class AbstractResponseElement implements IMultiStatusElement {
36   
37       /**
38        * returns the href the reponse element is for.
39        * 
40        * @return the href
41        */
42       public abstract URI getHref();
43   
44       /**
45        * returns a most basic {@code <d:response />} element with {@code <d:href
46        * />} set (can be used by subclasses to create an initial response
47        * element).
48        * 
49        * @return a most basic XML representation
50        * @see org.torweg.pulse.webdav.response.IMultiStatusElement#getXML()
51        */
52       public Element getXML() {
53           Element response = new Element("response",
54                   AbstractDAVServlet.DAV_NAMESPACE);
55           try {
56               response.addContent(new Element("href",
57                       AbstractDAVServlet.DAV_NAMESPACE).setText(URLDecoder
58                       .decode(getHref().toString(), "utf-8").replaceAll(" ", "%20")));
59           } catch (UnsupportedEncodingException e) {
60               response.addContent(new Element("href",
61                       AbstractDAVServlet.DAV_NAMESPACE).setText(getHref()
62                       .toString()));
63           }
64           return response;
65       }
66   
67   }
68