1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.webdav.methods;
24  
25  import com.liferay.portal.kernel.util.ContentTypes;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Tuple;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.webdav.InvalidRequestException;
31  import com.liferay.portal.webdav.WebDAVException;
32  import com.liferay.portal.webdav.WebDAVRequest;
33  import com.liferay.portal.webdav.WebDAVUtil;
34  import com.liferay.util.servlet.ServletResponseUtil;
35  import com.liferay.util.xml.XMLFormatter;
36  
37  import java.io.StringReader;
38  
39  import java.util.HashSet;
40  import java.util.Iterator;
41  import java.util.Set;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import org.dom4j.Document;
50  import org.dom4j.Element;
51  import org.dom4j.Namespace;
52  import org.dom4j.io.SAXReader;
53  
54  /**
55   * <a href="PropfindMethodImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Alexander Chow
59   *
60   */
61  public class PropfindMethodImpl extends BasePropMethodImpl implements Method {
62  
63      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
64          try {
65              HttpServletResponse response =
66                  webDavRequest.getHttpServletResponse();
67  
68              Set<Tuple> props = getProps(webDavRequest);
69  
70              String xml = getResponseXML(webDavRequest, props);
71  
72              response.setStatus(WebDAVUtil.SC_MULTI_STATUS);
73              response.setContentType(ContentTypes.TEXT_XML_UTF8);
74  
75              try {
76                  ServletResponseUtil.write(response, xml);
77              }
78              catch (Exception e) {
79                  if (_log.isWarnEnabled()) {
80                      _log.warn(e);
81                  }
82              }
83  
84              return -1;
85          }
86          catch (InvalidRequestException ire) {
87              return HttpServletResponse.SC_BAD_REQUEST;
88          }
89          catch (Exception e) {
90              throw new WebDAVException(e);
91          }
92      }
93  
94      protected Set<Tuple> getProps(WebDAVRequest webDavRequest)
95          throws InvalidRequestException {
96  
97          try {
98              Set<Tuple> props = new HashSet<Tuple>();
99  
100             HttpServletRequest request = webDavRequest.getHttpServletRequest();
101 
102             String xml = new String(
103                 FileUtil.getBytes(request.getInputStream()));
104 
105             if (Validator.isNull(xml)) {
106 
107                 // Windows XP does not generate an xml request so the PROPFIND
108                 // must be generated manually. See LEP-4920.
109 
110                 return generateProps(props);
111             }
112 
113             if (_log.isDebugEnabled()) {
114                 _log.debug(
115                     "Request XML: \n" +
116                         XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
117             }
118 
119             SAXReader reader = new SAXReader();
120 
121             Document doc = reader.read(new StringReader(xml));
122 
123             Element root = doc.getRootElement();
124 
125             if (root.element("allprop") != null) {
126 
127                 // Generate props if <allprop> tag is used. See LEP-6162.
128 
129                 return generateProps(props);
130             }
131 
132             Element prop = root.element("prop");
133 
134             Iterator<Element> itr = prop.elements().iterator();
135 
136             while (itr.hasNext()) {
137                 Element el = itr.next();
138 
139                 String prefix = el.getNamespacePrefix();
140                 String uri = el.getNamespaceURI();
141 
142                 Namespace namespace = null;
143 
144                 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
145                     namespace = WebDAVUtil.DAV_URI;
146                 }
147                 else if (Validator.isNull(prefix)) {
148                     namespace = Namespace.get(uri);
149                 }
150                 else {
151                     namespace = Namespace.get(prefix, uri);
152                 }
153 
154                 props.add(new Tuple(el.getName(), namespace));
155             }
156 
157             return props;
158         }
159         catch (Exception e) {
160             throw new InvalidRequestException(e);
161         }
162     }
163 
164     protected Set<Tuple> generateProps(Set<Tuple> props) {
165         props.add(new Tuple("displayname", WebDAVUtil.DAV_URI));
166         props.add(new Tuple("resourcetype", WebDAVUtil.DAV_URI));
167         props.add(new Tuple("getcontenttype", WebDAVUtil.DAV_URI));
168         props.add(new Tuple("getcontentlength", WebDAVUtil.DAV_URI));
169         props.add(new Tuple("getlastmodified", WebDAVUtil.DAV_URI));
170         props.add(new Tuple("lockdiscovery", WebDAVUtil.DAV_URI));
171         props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
172         props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
173         props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
174 
175         return props;
176     }
177 
178     private static Log _log = LogFactory.getLog(PropfindMethodImpl.class);
179 
180 }