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.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.ContentTypes;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.Tuple;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.WebDAVProps;
030: import com.liferay.portal.service.WebDAVPropsLocalServiceUtil;
031: import com.liferay.portal.webdav.InvalidRequestException;
032: import com.liferay.portal.webdav.Resource;
033: import com.liferay.portal.webdav.WebDAVException;
034: import com.liferay.portal.webdav.WebDAVRequest;
035: import com.liferay.portal.webdav.WebDAVStorage;
036: import com.liferay.portal.webdav.WebDAVUtil;
037: import com.liferay.util.FileUtil;
038: import com.liferay.util.servlet.ServletResponseUtil;
039: import com.liferay.util.xml.XMLFormatter;
040:
041: import java.io.StringReader;
042:
043: import java.util.HashSet;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.Set;
047:
048: import javax.servlet.http.HttpServletRequest;
049: import javax.servlet.http.HttpServletResponse;
050:
051: import org.apache.commons.logging.Log;
052: import org.apache.commons.logging.LogFactory;
053:
054: import org.dom4j.Document;
055: import org.dom4j.Element;
056: import org.dom4j.Namespace;
057: import org.dom4j.io.SAXReader;
058:
059: /**
060: * <a href="ProppatchMethodImpl.java.html"><b><i>View Source</i></b></a>
061: *
062: * @author Alexander Chow
063: *
064: */
065: public class ProppatchMethodImpl extends BasePropMethodImpl {
066:
067: public int process(WebDAVRequest webDavReq) throws WebDAVException {
068: try {
069: HttpServletResponse res = webDavReq
070: .getHttpServletResponse();
071:
072: Set props = processInstructions(webDavReq);
073:
074: String xml = getResponseXML(webDavReq, props);
075:
076: // Must set the status prior to writing the XML
077:
078: res.setStatus(WebDAVUtil.SC_MULTI_STATUS);
079: res.setContentType(ContentTypes.TEXT_XML_UTF8);
080:
081: try {
082: ServletResponseUtil.write(res, xml);
083: } catch (Exception e) {
084: if (_log.isWarnEnabled()) {
085: _log.warn(e);
086: }
087: }
088:
089: return -1;
090: } catch (InvalidRequestException ire) {
091: if (_log.isInfoEnabled()) {
092: _log.info(ire.getMessage(), ire);
093: }
094:
095: return HttpServletResponse.SC_BAD_REQUEST;
096: } catch (Exception e) {
097: throw new WebDAVException(e);
098: }
099: }
100:
101: protected WebDAVProps getStoredProperties(WebDAVRequest webDavReq)
102: throws PortalException, SystemException {
103:
104: WebDAVStorage storage = webDavReq.getWebDAVStorage();
105:
106: Resource resource = storage.getResource(webDavReq);
107:
108: WebDAVProps webDavProps = null;
109:
110: if (resource.getPrimaryKey() <= 0) {
111: if (_log.isWarnEnabled()) {
112: _log.warn("There is no primary key set for resource");
113: }
114:
115: throw new InvalidRequestException();
116: }
117:
118: webDavProps = WebDAVPropsLocalServiceUtil.getWebDAVProps(
119: webDavReq.getCompanyId(), resource.getClassName(),
120: resource.getPrimaryKey());
121:
122: return webDavProps;
123: }
124:
125: protected Set processInstructions(WebDAVRequest webDavReq)
126: throws InvalidRequestException {
127:
128: try {
129: Set newProps = new HashSet();
130:
131: HttpServletRequest req = webDavReq.getHttpServletRequest();
132:
133: WebDAVProps webDavProps = getStoredProperties(webDavReq);
134:
135: String xml = new String(FileUtil.getBytes(req
136: .getInputStream()));
137:
138: if (Validator.isNull(xml)) {
139: return newProps;
140: }
141:
142: if (_log.isDebugEnabled()) {
143: _log.debug("Request XML: \n"
144: + XMLFormatter.toString(xml,
145: StringPool.FOUR_SPACES));
146: }
147:
148: SAXReader reader = new SAXReader();
149:
150: Document doc = reader.read(new StringReader(xml));
151:
152: Element root = doc.getRootElement();
153:
154: Iterator itr = root.elements().iterator();
155:
156: while (itr.hasNext()) {
157: Element instruction = (Element) itr.next();
158:
159: List list = instruction.elements();
160:
161: if (list.size() != 1) {
162: throw new InvalidRequestException(
163: "There should only be one <prop /> per set or remove "
164: + "instruction.");
165: }
166:
167: Element prop = (Element) list.get(0);
168:
169: if (!prop.getName().equals("prop")
170: || !prop.getNamespaceURI().equals(
171: WebDAVUtil.DAV_URI.getURI())) {
172:
173: throw new InvalidRequestException(
174: "Invalid <prop /> element " + prop);
175: }
176:
177: list = prop.elements();
178:
179: if (list.size() != 1) {
180: throw new InvalidRequestException(
181: "<prop /> should only have one subelement.");
182: }
183:
184: Element customProp = (Element) list.get(0);
185:
186: String name = customProp.getName();
187: String prefix = customProp.getNamespacePrefix();
188: String uri = customProp.getNamespaceURI();
189: String text = customProp.getText();
190:
191: Namespace namespace = null;
192:
193: if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
194: namespace = WebDAVUtil.DAV_URI;
195: } else if (Validator.isNull(prefix)) {
196: namespace = Namespace.get(uri);
197: } else {
198: namespace = Namespace.get(prefix, uri);
199: }
200:
201: if (instruction.getName().equals("set")) {
202: if (Validator.isNull(text)) {
203: webDavProps.addProp(name, prefix, uri);
204: } else {
205: webDavProps.addProp(name, prefix, uri, text);
206: }
207:
208: newProps.add(new Tuple(customProp.getName(),
209: namespace));
210: } else if (instruction.getName().equals("remove")) {
211: webDavProps.removeProp(name, prefix, uri);
212: } else {
213: throw new InvalidRequestException(
214: "Instead of set/remove instruction, received "
215: + instruction);
216: }
217: }
218:
219: WebDAVPropsLocalServiceUtil.storeWebDAVProps(webDavProps);
220:
221: return newProps;
222: } catch (Exception e) {
223: throw new InvalidRequestException(e);
224: }
225: }
226:
227: private static Log _log = LogFactory
228: .getLog(ProppatchMethodImpl.class);
229:
230: }
|