1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class PropfindMethodImpl extends BasePropMethodImpl implements Method {
55  
56      public int process(WebDAVRequest webDavRequest) throws WebDAVException {
57          try {
58              Set<Tuple> props = getProps(webDavRequest);
59  
60              return writeResponseXML(webDavRequest, props);
61          }
62          catch (InvalidRequestException ire) {
63              return HttpServletResponse.SC_BAD_REQUEST;
64          }
65          catch (Exception e) {
66              throw new WebDAVException(e);
67          }
68      }
69  
70      protected Set<Tuple> getProps(WebDAVRequest webDavRequest)
71          throws InvalidRequestException {
72  
73          try {
74              Set<Tuple> props = new HashSet<Tuple>();
75  
76              HttpServletRequest request = webDavRequest.getHttpServletRequest();
77  
78              String xml = new String(
79                  FileUtil.getBytes(request.getInputStream()));
80  
81              if (Validator.isNull(xml)) {
82  
83                  // Windows XP does not generate an xml request so the PROPFIND
84                  // must be generated manually. See LEP-4920.
85  
86                  return generateProps(props);
87              }
88  
89              if (_log.isDebugEnabled()) {
90                  _log.debug(
91                      "Request XML: \n" +
92                          XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
93              }
94  
95              Document doc = SAXReaderUtil.read(xml);
96  
97              Element root = doc.getRootElement();
98  
99              if (root.element("allprop") != null) {
100 
101                 // Generate props if <allprop> tag is used. See LEP-6162.
102 
103                 return generateProps(props);
104             }
105 
106             Element prop = root.element("prop");
107 
108             Iterator<Element> itr = prop.elements().iterator();
109 
110             while (itr.hasNext()) {
111                 Element el = itr.next();
112 
113                 String prefix = el.getNamespacePrefix();
114                 String uri = el.getNamespaceURI();
115 
116                 Namespace namespace = null;
117 
118                 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
119                     namespace = WebDAVUtil.DAV_URI;
120                 }
121                 else if (Validator.isNull(prefix)) {
122                     namespace = SAXReaderUtil.createNamespace(uri);
123                 }
124                 else {
125                     namespace = SAXReaderUtil.createNamespace(prefix, uri);
126                 }
127 
128                 props.add(new Tuple(el.getName(), namespace));
129             }
130 
131             return props;
132         }
133         catch (Exception e) {
134             throw new InvalidRequestException(e);
135         }
136     }
137 
138     protected Set<Tuple> generateProps(Set<Tuple> props) {
139         props.add(new Tuple("displayname", WebDAVUtil.DAV_URI));
140         props.add(new Tuple("resourcetype", WebDAVUtil.DAV_URI));
141         props.add(new Tuple("getcontenttype", WebDAVUtil.DAV_URI));
142         props.add(new Tuple("getcontentlength", WebDAVUtil.DAV_URI));
143         props.add(new Tuple("getlastmodified", WebDAVUtil.DAV_URI));
144         props.add(new Tuple("lockdiscovery", WebDAVUtil.DAV_URI));
145         props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
146         props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
147         props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
148 
149         return props;
150     }
151 
152     private static Log _log = LogFactoryUtil.getLog(PropfindMethodImpl.class);
153 
154 }