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