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