1
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
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
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
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 }