1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.webdav.methods;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.FileUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Tuple;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.Namespace;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
32  import com.liferay.portal.webdav.InvalidRequestException;
33  import com.liferay.portal.webdav.WebDAVException;
34  import com.liferay.portal.webdav.WebDAVRequest;
35  import com.liferay.portal.webdav.WebDAVUtil;
36  import com.liferay.util.xml.XMLFormatter;
37  
38  import java.util.HashSet;
39  import java.util.Iterator;
40  import java.util.Set;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="PropfindMethodImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Alexander Chow
50   *
51   */
52  public class PropfindMethodImpl extends BasePropMethodImpl implements Method {
53  
54      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
55          try {
56              Set<Tuple> props = getProps(webDavRequest);
57  
58              return writeResponseXML(webDavRequest, props);
59          }
60          catch (InvalidRequestException ire) {
61              return HttpServletResponse.SC_BAD_REQUEST;
62          }
63          catch (Exception e) {
64              throw new WebDAVException(e);
65          }
66      }
67  
68      protected Set<Tuple> getProps(WebDAVRequest webDavRequest)
69          throws InvalidRequestException {
70  
71          try {
72              Set<Tuple> props = new HashSet<Tuple>();
73  
74              HttpServletRequest request = webDavRequest.getHttpServletRequest();
75  
76              String xml = new String(
77                  FileUtil.getBytes(request.getInputStream()));
78  
79              if (Validator.isNull(xml)) {
80  
81                  // Windows XP does not generate an xml request so the PROPFIND
82                  // must be generated manually. See LEP-4920.
83  
84                  return generateProps(props);
85              }
86  
87              if (_log.isInfoEnabled()) {
88                  _log.info(
89                      "Request XML: \n" +
90                          XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
91              }
92  
93              Document doc = SAXReaderUtil.read(xml);
94  
95              Element root = doc.getRootElement();
96  
97              if (root.element("allprop") != null) {
98  
99                  // Generate props if <allprop> tag is used. See LEP-6162.
100 
101                 return generateProps(props);
102             }
103 
104             Element prop = root.element("prop");
105 
106             Iterator<Element> itr = prop.elements().iterator();
107 
108             while (itr.hasNext()) {
109                 Element el = itr.next();
110 
111                 String prefix = el.getNamespacePrefix();
112                 String uri = el.getNamespaceURI();
113 
114                 Namespace namespace = null;
115 
116                 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
117                     namespace = WebDAVUtil.DAV_URI;
118                 }
119                 else if (Validator.isNull(prefix)) {
120                     namespace = SAXReaderUtil.createNamespace(uri);
121                 }
122                 else {
123                     namespace = SAXReaderUtil.createNamespace(prefix, uri);
124                 }
125 
126                 props.add(new Tuple(el.getName(), namespace));
127             }
128 
129             return props;
130         }
131         catch (Exception e) {
132             throw new InvalidRequestException(e);
133         }
134     }
135 
136     protected Set<Tuple> generateProps(Set<Tuple> props) {
137         props.add(new Tuple("displayname", WebDAVUtil.DAV_URI));
138         props.add(new Tuple("resourcetype", WebDAVUtil.DAV_URI));
139         props.add(new Tuple("getcontenttype", WebDAVUtil.DAV_URI));
140         props.add(new Tuple("getcontentlength", WebDAVUtil.DAV_URI));
141         props.add(new Tuple("getlastmodified", WebDAVUtil.DAV_URI));
142         props.add(new Tuple("lockdiscovery", WebDAVUtil.DAV_URI));
143         props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
144         props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
145         props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
146 
147         return props;
148     }
149 
150     private static Log _log = LogFactoryUtil.getLog(PropfindMethodImpl.class);
151 
152 }