001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.webdav.methods;
022:
023: import com.liferay.portal.NoSuchGroupException;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.Tuple;
026: import com.liferay.portal.model.Group;
027: import com.liferay.portal.model.WebDAVProps;
028: import com.liferay.portal.service.GroupLocalServiceUtil;
029: import com.liferay.portal.service.WebDAVPropsLocalServiceUtil;
030: import com.liferay.portal.webdav.BaseResourceImpl;
031: import com.liferay.portal.webdav.Resource;
032: import com.liferay.portal.webdav.WebDAVRequest;
033: import com.liferay.portal.webdav.WebDAVStorage;
034: import com.liferay.portal.webdav.WebDAVUtil;
035: import com.liferay.util.xml.DocUtil;
036: import com.liferay.util.xml.XMLFormatter;
037:
038: import java.util.Arrays;
039: import java.util.HashSet;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.Set;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: import org.dom4j.Document;
048: import org.dom4j.DocumentFactory;
049: import org.dom4j.Element;
050: import org.dom4j.Namespace;
051: import org.dom4j.QName;
052:
053: /**
054: * <a href="BasePropMethodImpl.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Alexander Chow
057: *
058: */
059: public abstract class BasePropMethodImpl implements Method {
060:
061: protected void addResponse(WebDAVStorage storage,
062: WebDAVRequest webDavReq, Resource resource, Set props,
063: Element multistatus, long depth) throws Exception {
064:
065: addResponse(webDavReq, resource, props, multistatus);
066:
067: if (resource.isCollection() && (depth != 0)) {
068: Iterator itr = storage.getResources(webDavReq).iterator();
069:
070: while (itr.hasNext()) {
071: resource = (Resource) itr.next();
072:
073: addResponse(webDavReq, resource, props, multistatus);
074: }
075: }
076: }
077:
078: protected void addResponse(WebDAVRequest webDavReq,
079: Resource resource, Set props, Element multistatus)
080: throws Exception {
081:
082: // Make a deep copy of the props
083:
084: props = new HashSet(props);
085:
086: // Start building multistatus response
087:
088: Element response = DocUtil.add(multistatus, "response",
089: WebDAVUtil.DAV_URI);
090:
091: DocUtil.add(response, "href", WebDAVUtil.DAV_URI, resource
092: .getHREF());
093:
094: // Build success and failure propstat elements
095:
096: Element successStat = DocUtil.add(response, "propstat",
097: WebDAVUtil.DAV_URI);
098: Element successProp = DocUtil.add(successStat, "prop",
099: WebDAVUtil.DAV_URI);
100: Element failureStat = DocUtil.add(response, "propstat",
101: WebDAVUtil.DAV_URI);
102: Element failureProp = DocUtil.add(failureStat, "prop",
103: WebDAVUtil.DAV_URI);
104:
105: boolean hasSuccess = false;
106: boolean hasFailure = false;
107:
108: // Check DAV properties
109:
110: if (props.contains(_ALL_PROPS_PAIR)) {
111: props.remove(_ALL_PROPS_PAIR);
112:
113: if (resource.isCollection()) {
114: props.addAll(_ALL_COLLECTION_PROPS);
115: } else {
116: props.addAll(_ALL_SIMPLE_PROPS);
117: }
118: }
119:
120: if (props.contains(_CREATIONDATE_PAIR)) {
121: props.remove(_CREATIONDATE_PAIR);
122:
123: DocUtil.add(successProp, _CREATIONDATE, WebDAVUtil.DAV_URI,
124: resource.getCreateDate());
125:
126: hasSuccess = true;
127: }
128:
129: if (props.contains(_DISPLAYNAME_PAIR)) {
130: props.remove(_DISPLAYNAME_PAIR);
131:
132: DocUtil.add(successProp, _DISPLAYNAME, WebDAVUtil.DAV_URI,
133: resource.getDisplayName());
134:
135: hasSuccess = true;
136: }
137:
138: if (props.contains(_GETLASTMODIFIED_PAIR)) {
139: props.remove(_GETLASTMODIFIED_PAIR);
140:
141: DocUtil.add(successProp, _GETLASTMODIFIED,
142: WebDAVUtil.DAV_URI, resource.getModifiedDate());
143:
144: hasSuccess = true;
145: }
146:
147: if (props.contains(_GETCONTENTTYPE_PAIR)) {
148: props.remove(_GETCONTENTTYPE_PAIR);
149:
150: DocUtil.add(successProp, _GETCONTENTTYPE,
151: WebDAVUtil.DAV_URI, resource.getContentType());
152:
153: hasSuccess = true;
154: }
155:
156: if (props.contains(_GETCONTENTLENGTH_PAIR)) {
157: props.remove(_GETCONTENTLENGTH_PAIR);
158:
159: if (!resource.isCollection()) {
160: DocUtil.add(successProp, _GETCONTENTLENGTH,
161: WebDAVUtil.DAV_URI, resource.getSize());
162:
163: hasSuccess = true;
164: } else {
165: DocUtil.add(failureProp, _GETCONTENTLENGTH,
166: WebDAVUtil.DAV_URI);
167:
168: hasFailure = true;
169: }
170: }
171:
172: if (props.contains(_RESOURCETYPE_PAIR)) {
173: props.remove(_RESOURCETYPE_PAIR);
174:
175: Element resourceType = DocUtil.add(successProp,
176: _RESOURCETYPE, WebDAVUtil.DAV_URI);
177:
178: if (resource.isCollection()) {
179: DocUtil.add(resourceType, "collection",
180: WebDAVUtil.DAV_URI);
181: }
182:
183: hasSuccess = true;
184: }
185:
186: // Check remaining properties against custom properties
187:
188: WebDAVProps webDavProps = WebDAVPropsLocalServiceUtil
189: .getWebDAVProps(webDavReq.getCompanyId(), resource
190: .getClassName(), resource.getPrimaryKey());
191:
192: Set customProps = webDavProps.getPropsSet();
193:
194: Iterator itr = props.iterator();
195:
196: while (itr.hasNext()) {
197: Tuple tuple = (Tuple) itr.next();
198:
199: String name = (String) tuple.getObject(0);
200: Namespace namespace = (Namespace) tuple.getObject(1);
201:
202: String prefix = namespace.getPrefix();
203: String uri = namespace.getURI();
204:
205: if (customProps.contains(tuple)) {
206: String text = webDavProps.getText(name, prefix, uri);
207:
208: DocUtil.add(successProp, name, namespace, text);
209:
210: hasSuccess = true;
211: } else {
212: DocUtil.add(failureProp, name, namespace);
213:
214: hasFailure = true;
215: }
216: }
217:
218: // Clean up propstats
219:
220: if (hasSuccess) {
221: DocUtil.add(successStat, "status", WebDAVUtil.DAV_URI,
222: "HTTP/1.1 200 OK");
223: } else {
224: response.remove(successStat);
225: }
226:
227: if (hasFailure) {
228: DocUtil.add(failureStat, "status", WebDAVUtil.DAV_URI,
229: "HTTP/1.1 404 Not Found");
230: } else {
231: response.remove(failureStat);
232: }
233: }
234:
235: protected void addResponse(String href, Element multistatus)
236: throws Exception {
237:
238: Element response = DocUtil.add(multistatus, "response",
239: WebDAVUtil.DAV_URI);
240:
241: DocUtil.add(response, "href", WebDAVUtil.DAV_URI, href);
242:
243: Element propstat = DocUtil.add(response, "propstat",
244: WebDAVUtil.DAV_URI);
245:
246: DocUtil.add(propstat, "status", WebDAVUtil.DAV_URI,
247: "HTTP/1.1 404 Not Found");
248: }
249:
250: protected String getResponseXML(WebDAVRequest webDavReq, Set props)
251: throws Exception {
252:
253: WebDAVStorage storage = webDavReq.getWebDAVStorage();
254:
255: long companyId = webDavReq.getCompanyId();
256: long groupId = webDavReq.getGroupId();
257: long depth = WebDAVUtil.getDepth(webDavReq
258: .getHttpServletRequest());
259:
260: DocumentFactory docFactory = DocumentFactory.getInstance();
261:
262: Document doc = docFactory.createDocument();
263:
264: Element multistatus = docFactory.createElement(new QName(
265: "multistatus", WebDAVUtil.DAV_URI));
266:
267: doc.setRootElement(multistatus);
268:
269: if (companyId <= 0) {
270: return getResponseXML(doc);
271: }
272:
273: if (groupId == 0) {
274: addResponse(webDavReq, new BaseResourceImpl(storage
275: .getRootPath(), companyId, companyId), props,
276: multistatus);
277:
278: if (props.size() > 0) {
279: Iterator itr = storage.getCommunities(webDavReq)
280: .iterator();
281:
282: while (itr.hasNext()) {
283: Resource resource = (Resource) itr.next();
284:
285: addResponse(webDavReq, resource, props, multistatus);
286: }
287: }
288:
289: return getResponseXML(doc);
290: }
291:
292: Resource resource = storage.getResource(webDavReq);
293:
294: if ((resource == null) && !webDavReq.isGroupPath()) {
295: String href = storage.getRootPath() + webDavReq.getPath();
296:
297: if (_log.isWarnEnabled()) {
298: _log.warn("No resource found for "
299: + webDavReq.getPath());
300: }
301:
302: addResponse(href, multistatus);
303:
304: return getResponseXML(doc);
305: }
306:
307: try {
308: if (resource != null) {
309: addResponse(storage, webDavReq, resource, props,
310: multistatus, depth);
311: } else if (webDavReq.isGroupPath()) {
312: Group group = GroupLocalServiceUtil.getGroup(groupId);
313:
314: resource = new BaseResourceImpl(storage.getRootPath()
315: + StringPool.SLASH + companyId, groupId, group
316: .getName());
317:
318: addResponse(storage, webDavReq, resource, props,
319: multistatus, depth);
320: }
321: } catch (NoSuchGroupException nsge) {
322: String href = storage.getRootPath() + webDavReq.getPath();
323:
324: if (_log.isWarnEnabled()) {
325: _log.warn("No group found for " + href);
326: }
327:
328: addResponse(href, multistatus);
329: }
330:
331: return getResponseXML(doc);
332: }
333:
334: protected String getResponseXML(Document doc) throws Exception {
335: String xml = XMLFormatter.toString(doc, StringPool.FOUR_SPACES);
336:
337: if (_log.isDebugEnabled()) {
338: _log.debug("Response XML\n" + xml);
339: }
340:
341: return xml;
342: }
343:
344: private static final String _ALLPROPS = "allprops";
345:
346: private static final String _CREATIONDATE = "creationdate";
347:
348: private static final String _DISPLAYNAME = "displayname";
349:
350: private static final String _GETLASTMODIFIED = "getlastmodified";
351:
352: private static final String _GETCONTENTTYPE = "getcontenttype";
353:
354: private static final String _GETCONTENTLENGTH = "getcontentlength";
355:
356: private static final String _RESOURCETYPE = "resourcetype";
357:
358: private static final Tuple _ALL_PROPS_PAIR = new Tuple(_ALLPROPS,
359: WebDAVUtil.DAV_URI);
360:
361: private static final Tuple _CREATIONDATE_PAIR = new Tuple(
362: _CREATIONDATE, WebDAVUtil.DAV_URI);
363:
364: private static final Tuple _DISPLAYNAME_PAIR = new Tuple(
365: _DISPLAYNAME, WebDAVUtil.DAV_URI);
366:
367: private static final Tuple _GETLASTMODIFIED_PAIR = new Tuple(
368: _GETCONTENTLENGTH, WebDAVUtil.DAV_URI);
369:
370: private static final Tuple _GETCONTENTTYPE_PAIR = new Tuple(
371: _GETCONTENTTYPE, WebDAVUtil.DAV_URI);
372:
373: private static final Tuple _GETCONTENTLENGTH_PAIR = new Tuple(
374: _GETLASTMODIFIED, WebDAVUtil.DAV_URI);
375:
376: private static final Tuple _RESOURCETYPE_PAIR = new Tuple(
377: _RESOURCETYPE, WebDAVUtil.DAV_URI);
378:
379: private final List _ALL_COLLECTION_PROPS = Arrays
380: .asList(new Object[] { _CREATIONDATE_PAIR,
381: _DISPLAYNAME_PAIR, _GETLASTMODIFIED_PAIR,
382: _GETCONTENTTYPE_PAIR, _RESOURCETYPE_PAIR });
383:
384: private final List _ALL_SIMPLE_PROPS = Arrays.asList(new Object[] {
385: _CREATIONDATE_PAIR, _DISPLAYNAME_PAIR,
386: _GETLASTMODIFIED_PAIR, _GETCONTENTTYPE_PAIR,
387: _GETCONTENTLENGTH_PAIR, _RESOURCETYPE_PAIR });
388:
389: private static Log _log = LogFactory
390: .getLog(BasePropMethodImpl.class);
391:
392: }
|