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 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 getProps(WebDAVRequest webDavReq)
94          throws InvalidRequestException {
95  
96          try {
97              Set props = new HashSet();
98  
99              HttpServletRequest req = webDavReq.getHttpServletRequest();
100 
101             String xml = new String(FileUtil.getBytes(req.getInputStream()));
102 
103             if (Validator.isNull(xml)) {
104                 return props;
105             }
106 
107             if (_log.isDebugEnabled()) {
108                 _log.debug(
109                     "Request XML: \n" +
110                         XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
111             }
112 
113             SAXReader reader = new SAXReader();
114 
115             Document doc = reader.read(new StringReader(xml));
116 
117             Element root = doc.getRootElement();
118 
119             Element prop = root.element("prop");
120 
121             Iterator itr = prop.elements().iterator();
122 
123             while (itr.hasNext()) {
124                 Element el = (Element)itr.next();
125 
126                 String prefix = el.getNamespacePrefix();
127                 String uri = el.getNamespaceURI();
128 
129                 Namespace namespace = null;
130 
131                 if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
132                     namespace = WebDAVUtil.DAV_URI;
133                 }
134                 else if (Validator.isNull(prefix)) {
135                     namespace = Namespace.get(uri);
136                 }
137                 else {
138                     namespace = Namespace.get(prefix, uri);
139                 }
140 
141                 props.add(new Tuple(el.getName(), namespace));
142             }
143 
144             return props;
145         }
146         catch (Exception e) {
147             throw new InvalidRequestException(e);
148         }
149     }
150 
151     private static Log _log = LogFactory.getLog(PropfindMethodImpl.class);
152 
153 }