001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.webdav.methods;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.webdav.Resource;
025    import com.liferay.portal.kernel.webdav.WebDAVException;
026    import com.liferay.portal.kernel.webdav.WebDAVRequest;
027    import com.liferay.portal.kernel.webdav.WebDAVStorage;
028    import com.liferay.portal.kernel.webdav.WebDAVUtil;
029    import com.liferay.portal.kernel.xml.Document;
030    import com.liferay.portal.kernel.xml.Element;
031    import com.liferay.portal.kernel.xml.Namespace;
032    import com.liferay.portal.kernel.xml.QName;
033    import com.liferay.portal.kernel.xml.SAXReaderUtil;
034    import com.liferay.portal.model.Lock;
035    import com.liferay.portal.model.WebDAVProps;
036    import com.liferay.portal.service.WebDAVPropsLocalServiceUtil;
037    import com.liferay.portal.webdav.InvalidRequestException;
038    import com.liferay.portal.webdav.LockException;
039    import com.liferay.util.xml.XMLFormatter;
040    
041    import java.util.HashSet;
042    import java.util.Iterator;
043    import java.util.List;
044    import java.util.Set;
045    
046    import javax.servlet.http.HttpServletRequest;
047    import javax.servlet.http.HttpServletResponse;
048    
049    /**
050     * @author Alexander Chow
051     */
052    public class ProppatchMethodImpl extends BasePropMethodImpl {
053    
054            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
055                    try {
056                            Set<QName> props = processInstructions(webDavRequest);
057    
058                            return writeResponseXML(webDavRequest, props);
059                    }
060                    catch (InvalidRequestException ire) {
061                            if (_log.isInfoEnabled()) {
062                                    _log.info(ire.getMessage(), ire);
063                            }
064    
065                            return HttpServletResponse.SC_BAD_REQUEST;
066                    }
067                    catch (LockException le) {
068                            return WebDAVUtil.SC_LOCKED;
069                    }
070                    catch (Exception e) {
071                            throw new WebDAVException(e);
072                    }
073            }
074    
075            protected WebDAVProps getStoredProperties(WebDAVRequest webDavRequest)
076                    throws PortalException, SystemException {
077    
078                    WebDAVStorage storage = webDavRequest.getWebDAVStorage();
079    
080                    Resource resource = storage.getResource(webDavRequest);
081    
082                    WebDAVProps webDavProps = null;
083    
084                    if (resource.getPrimaryKey() <= 0) {
085                            if (_log.isWarnEnabled()) {
086                                    _log.warn("There is no primary key set for resource");
087                            }
088    
089                            throw new InvalidRequestException();
090                    }
091                    else if (resource.isLocked()) {
092                            Lock lock = resource.getLock();
093    
094                            if ((lock == null) ||
095                                    !lock.getUuid().equals(webDavRequest.getLockUuid())) {
096    
097                                    throw new LockException();
098                            }
099                    }
100    
101                    webDavProps = WebDAVPropsLocalServiceUtil.getWebDAVProps(
102                            webDavRequest.getCompanyId(), resource.getClassName(),
103                            resource.getPrimaryKey());
104    
105                    return webDavProps;
106            }
107    
108            protected Set<QName> processInstructions(WebDAVRequest webDavRequest)
109                    throws InvalidRequestException, LockException {
110    
111                    try {
112                            Set<QName> newProps = new HashSet<QName>();
113    
114                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
115    
116                            WebDAVProps webDavProps = getStoredProperties(webDavRequest);
117    
118                            String xml = new String(
119                                    FileUtil.getBytes(request.getInputStream()));
120    
121                            if (Validator.isNull(xml)) {
122                                    return newProps;
123                            }
124    
125                            if (_log.isInfoEnabled()) {
126                                    _log.info(
127                                            "Request XML: \n" +
128                                                    XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
129                            }
130    
131                            Document document = SAXReaderUtil.read(xml);
132    
133                            Element rootElement = document.getRootElement();
134    
135                            Iterator<Element> itr = rootElement.elements().iterator();
136    
137                            while (itr.hasNext()) {
138                                    Element instructionElement = itr.next();
139    
140                                    List<Element> propElements = instructionElement.elements();
141    
142                                    if (propElements.size() != 1) {
143                                            throw new InvalidRequestException(
144                                                    "There should only be one <prop /> per set or remove " +
145                                                            "instruction.");
146                                    }
147    
148                                    Element propElement = propElements.get(0);
149    
150                                    if (!propElement.getName().equals("prop") ||
151                                            !propElement.getNamespaceURI().equals(
152                                                    WebDAVUtil.DAV_URI.getURI())) {
153    
154                                            throw new InvalidRequestException(
155                                                    "Invalid <prop /> element " + propElement);
156                                    }
157    
158                                    List<Element> customPropElements = propElement.elements();
159    
160                                    for (Element customPropElement : customPropElements) {
161                                            String name = customPropElement.getName();
162                                            String prefix = customPropElement.getNamespacePrefix();
163                                            String uri = customPropElement.getNamespaceURI();
164                                            String text = customPropElement.getText();
165    
166                                            Namespace namespace = WebDAVUtil.createNamespace(
167                                                    prefix, uri);
168    
169                                            if (instructionElement.getName().equals("set")) {
170                                                    if (Validator.isNull(text)) {
171                                                            webDavProps.addProp(name, prefix, uri);
172                                                    }
173                                                    else {
174                                                            webDavProps.addProp(name, prefix, uri, text);
175                                                    }
176    
177                                                    newProps.add(SAXReaderUtil.createQName(
178                                                            customPropElement.getName(), namespace));
179                                            }
180                                            else if (instructionElement.getName().equals("remove")) {
181                                                    webDavProps.removeProp(name, prefix, uri);
182                                            }
183                                            else {
184                                                    throw new InvalidRequestException(
185                                                            "Instead of set/remove instruction, received " +
186                                                                    instructionElement);
187                                            }
188                                    }
189                            }
190    
191                            WebDAVPropsLocalServiceUtil.storeWebDAVProps(webDavProps);
192    
193                            return newProps;
194                    }
195                    catch (LockException le) {
196                            throw le;
197                    }
198                    catch (Exception e) {
199                            throw new InvalidRequestException(e);
200                    }
201            }
202    
203            private static Log _log = LogFactoryUtil.getLog(ProppatchMethodImpl.class);
204    
205    }