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              if (_log.isInfoEnabled()) {
73                  _log.info("Status code " + WebDAVUtil.SC_MULTI_STATUS);
74              }
75  
76              try {
77                  ServletResponseUtil.write(response, xml);
78              }
79              catch (Exception e) {
80                  if (_log.isWarnEnabled()) {
81                      _log.warn(e);
82                  }
83              }
84  
85              return -1;
86          }
87          catch (InvalidRequestException ire) {
88              return HttpServletResponse.SC_BAD_REQUEST;
89          }
90          catch (Exception e) {
91              throw new WebDAVException(e);
92          }
93      }
94  
95      protected Set<Tuple> getProps(WebDAVRequest webDavRequest)
96          throws InvalidRequestException {
97  
98          try {
99              Set<Tuple> props = new HashSet<Tuple>();
100 
101             HttpServletRequest request = webDavRequest.getHttpServletRequest();
102 
103             String xml = new String(
104                 FileUtil.getBytes(request.getInputStream()));
105 
106             if (Validator.isNull(xml)) {
107 
108                 // Windows XP does not generate an xml request so the PROPFIND
109                 // must be generated manually. See LEP-4920.
110 
111                 return generateProps(props);
112             }
113 
114             if (_log.isInfoEnabled()) {
115                 _log.info(
116                     "Request XML: \n" +
117                         XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
118             }
119 
120             Document doc = SAXReaderUtil.read(xml);
121 
122             Element root = doc.getRootElement();
123 
124             if (root.element("allprop") != null) {
125 
126                 // Generate props if <allprop> tag is used. See LEP-6162.
127 
128                 return generateProps(props);
129             }
130 
131             Element prop = root.element("prop");
132 
133             Iterator<Element> itr = prop.elements().iterator();
134 
135             while (itr.hasNext()) {
136                 Element el = itr.next();
137 
138                 String prefix = el.getNamespacePrefix();
139                 String uri = el.getNamespaceURI();
140 
141                 Namespace namespace = null;
142 
143                 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
144                     namespace = WebDAVUtil.DAV_URI;
145                 }
146                 else if (Validator.isNull(prefix)) {
147                     namespace = SAXReaderUtil.createNamespace(uri);
148                 }
149                 else {
150                     namespace = SAXReaderUtil.createNamespace(prefix, uri);
151                 }
152 
153                 props.add(new Tuple(el.getName(), namespace));
154             }
155 
156             return props;
157         }
158         catch (Exception e) {
159             throw new InvalidRequestException(e);
160         }
161     }
162 
163     protected Set<Tuple> generateProps(Set<Tuple> props) {
164         props.add(new Tuple("displayname", WebDAVUtil.DAV_URI));
165         props.add(new Tuple("resourcetype", WebDAVUtil.DAV_URI));
166         props.add(new Tuple("getcontenttype", WebDAVUtil.DAV_URI));
167         props.add(new Tuple("getcontentlength", WebDAVUtil.DAV_URI));
168         props.add(new Tuple("getlastmodified", WebDAVUtil.DAV_URI));
169         props.add(new Tuple("lockdiscovery", WebDAVUtil.DAV_URI));
170         props.add(new Tuple("checked-in", WebDAVUtil.DAV_URI));
171         props.add(new Tuple("checked-out", WebDAVUtil.DAV_URI));
172         props.add(new Tuple("version-name", WebDAVUtil.DAV_URI));
173 
174         return props;
175     }
176 
177     private static Log _log = LogFactory.getLog(PropfindMethodImpl.class);
178 
179 }