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