1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Tuple;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.kernel.xml.Document;
32  import com.liferay.portal.kernel.xml.Element;
33  import com.liferay.portal.kernel.xml.Namespace;
34  import com.liferay.portal.kernel.xml.SAXReaderUtil;
35  import com.liferay.portal.webdav.InvalidRequestException;
36  import com.liferay.portal.webdav.WebDAVException;
37  import com.liferay.portal.webdav.WebDAVRequest;
38  import com.liferay.portal.webdav.WebDAVUtil;
39  import com.liferay.util.xml.XMLFormatter;
40  
41  import java.util.HashSet;
42  import java.util.Iterator;
43  import java.util.Set;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  
48  /**
49   * <a href="PropfindMethodImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   * @author Alexander Chow
53   *
54   */
55  public class PropfindMethodImpl extends BasePropMethodImpl implements Method {
56  
57      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
58          try {
59              Set<Tuple> props = getProps(webDavRequest);
60  
61              return writeResponseXML(webDavRequest, props);
62          }
63          catch (InvalidRequestException ire) {
64              return HttpServletResponse.SC_BAD_REQUEST;
65          }
66          catch (Exception e) {
67              throw new WebDAVException(e);
68          }
69      }
70  
71      protected Set<Tuple> getProps(WebDAVRequest webDavRequest)
72          throws InvalidRequestException {
73  
74          try {
75              Set<Tuple> props = new HashSet<Tuple>();
76  
77              HttpServletRequest request = webDavRequest.getHttpServletRequest();
78  
79              String xml = new String(
80                  FileUtil.getBytes(request.getInputStream()));
81  
82              if (Validator.isNull(xml)) {
83  
84                  // Windows XP does not generate an xml request so the PROPFIND
85                  // must be generated manually. See LEP-4920.
86  
87                  return generateProps(props);
88              }
89  
90              if (_log.isInfoEnabled()) {
91                  _log.info(
92                      "Request XML: \n" +
93                          XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
94              }
95  
96              Document doc = SAXReaderUtil.read(xml);
97  
98              Element root = doc.getRootElement();
99  
100             if (root.element("allprop") != null) {
101 
102                 // Generate props if <allprop> tag is used. See LEP-6162.
103 
104                 return generateProps(props);
105             }
106 
107             Element prop = root.element("prop");
108 
109             Iterator<Element> itr = prop.elements().iterator();
110 
111             while (itr.hasNext()) {
112                 Element el = itr.next();
113 
114                 String prefix = el.getNamespacePrefix();
115                 String uri = el.getNamespaceURI();
116 
117                 Namespace namespace = null;
118 
119                 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
120                     namespace = WebDAVUtil.DAV_URI;
121                 }
122                 else if (Validator.isNull(prefix)) {
123                     namespace = SAXReaderUtil.createNamespace(uri);
124                 }
125                 else {
126                     namespace = SAXReaderUtil.createNamespace(prefix, uri);
127                 }
128 
129                 props.add(new Tuple(el.getName(), namespace));
130             }
131 
132             return props;
133         }
134         catch (Exception e) {
135             throw new InvalidRequestException(e);
136         }
137     }
138 
139     protected Set<Tuple> generateProps(Set<Tuple> props) {
140         props.add(new Tuple("displayname", WebDAVUtil.DAV_URI));
141         props.add(new Tuple("resourcetype", WebDAVUtil.DAV_URI));
142         props.add(new Tuple("getcontenttype", WebDAVUtil.DAV_URI));
143         props.add(new Tuple("getcontentlength", WebDAVUtil.DAV_URI));
144         props.add(new Tuple("getlastmodified", WebDAVUtil.DAV_URI));
145         props.add(new Tuple("lockdiscovery", WebDAVUtil.DAV_URI));
146         props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
147         props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
148         props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
149 
150         return props;
151     }
152 
153     private static Log _log = LogFactoryUtil.getLog(PropfindMethodImpl.class);
154 
155 }