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.kernel.util.ContentTypes;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.Tuple;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.webdav.InvalidRequestException;
028: import com.liferay.portal.webdav.WebDAVException;
029: import com.liferay.portal.webdav.WebDAVRequest;
030: import com.liferay.portal.webdav.WebDAVUtil;
031: import com.liferay.util.FileUtil;
032: import com.liferay.util.servlet.ServletResponseUtil;
033: import com.liferay.util.xml.XMLFormatter;
034:
035: import java.io.StringReader;
036:
037: import java.util.HashSet;
038: import java.util.Iterator;
039: import java.util.Set;
040:
041: import javax.servlet.http.HttpServletRequest;
042: import javax.servlet.http.HttpServletResponse;
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.Element;
049: import org.dom4j.Namespace;
050: import org.dom4j.io.SAXReader;
051:
052: /**
053: * <a href="PropfindMethodImpl.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: * @author Alexander Chow
057: *
058: */
059: public class PropfindMethodImpl extends BasePropMethodImpl implements
060: Method {
061:
062: public int process(WebDAVRequest webDavReq) throws WebDAVException {
063: try {
064: HttpServletResponse res = webDavReq
065: .getHttpServletResponse();
066:
067: Set props = getProps(webDavReq);
068:
069: String xml = getResponseXML(webDavReq, props);
070:
071: res.setStatus(WebDAVUtil.SC_MULTI_STATUS);
072: res.setContentType(ContentTypes.TEXT_XML_UTF8);
073:
074: try {
075: ServletResponseUtil.write(res, xml);
076: } catch (Exception e) {
077: if (_log.isWarnEnabled()) {
078: _log.warn(e);
079: }
080: }
081:
082: return -1;
083: } catch (InvalidRequestException ire) {
084: return HttpServletResponse.SC_BAD_REQUEST;
085: } catch (Exception e) {
086: throw new WebDAVException(e);
087: }
088: }
089:
090: protected Set getProps(WebDAVRequest webDavReq)
091: throws InvalidRequestException {
092:
093: try {
094: Set props = new HashSet();
095:
096: HttpServletRequest req = webDavReq.getHttpServletRequest();
097:
098: String xml = new String(FileUtil.getBytes(req
099: .getInputStream()));
100:
101: if (Validator.isNull(xml)) {
102: return props;
103: }
104:
105: if (_log.isDebugEnabled()) {
106: _log.debug("Request XML: \n"
107: + XMLFormatter.toString(xml,
108: StringPool.FOUR_SPACES));
109: }
110:
111: SAXReader reader = new SAXReader();
112:
113: Document doc = reader.read(new StringReader(xml));
114:
115: Element root = doc.getRootElement();
116:
117: Element prop = root.element("prop");
118:
119: Iterator itr = prop.elements().iterator();
120:
121: while (itr.hasNext()) {
122: Element el = (Element) itr.next();
123:
124: String prefix = el.getNamespacePrefix();
125: String uri = el.getNamespaceURI();
126:
127: Namespace namespace = null;
128:
129: if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
130: namespace = WebDAVUtil.DAV_URI;
131: } else if (Validator.isNull(prefix)) {
132: namespace = Namespace.get(uri);
133: } else {
134: namespace = Namespace.get(prefix, uri);
135: }
136:
137: props.add(new Tuple(el.getName(), namespace));
138: }
139:
140: return props;
141: } catch (Exception e) {
142: throw new InvalidRequestException(e);
143: }
144: }
145:
146: private static Log _log = LogFactory
147: .getLog(PropfindMethodImpl.class);
148:
149: }
|