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