001    /**
002     * Copyright (c) 2000-present 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.lock.Lock;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.model.WebDAVProps;
022    import com.liferay.portal.kernel.service.WebDAVPropsLocalServiceUtil;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.webdav.Resource;
027    import com.liferay.portal.kernel.webdav.WebDAVException;
028    import com.liferay.portal.kernel.webdav.WebDAVRequest;
029    import com.liferay.portal.kernel.webdav.WebDAVStorage;
030    import com.liferay.portal.kernel.webdav.WebDAVUtil;
031    import com.liferay.portal.kernel.xml.Document;
032    import com.liferay.portal.kernel.xml.Element;
033    import com.liferay.portal.kernel.xml.Namespace;
034    import com.liferay.portal.kernel.xml.QName;
035    import com.liferay.portal.kernel.xml.SAXReaderUtil;
036    import com.liferay.portal.webdav.InvalidRequestException;
037    import com.liferay.portal.webdav.LockException;
038    import com.liferay.util.xml.Dom4jUtil;
039    
040    import java.util.HashSet;
041    import java.util.List;
042    import java.util.Set;
043    
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpServletResponse;
046    
047    /**
048     * @author Alexander Chow
049     */
050    public class ProppatchMethodImpl extends BasePropMethodImpl {
051    
052            @Override
053            public int process(WebDAVRequest webDAVRequest) throws WebDAVException {
054                    try {
055                            Set<QName> props = processInstructions(webDAVRequest);
056    
057                            return writeResponseXML(webDAVRequest, props);
058                    }
059                    catch (InvalidRequestException ire) {
060                            if (_log.isInfoEnabled()) {
061                                    _log.info(ire.getMessage(), ire);
062                            }
063    
064                            return HttpServletResponse.SC_BAD_REQUEST;
065                    }
066                    catch (LockException le) {
067                            return WebDAVUtil.SC_LOCKED;
068                    }
069                    catch (Exception e) {
070                            throw new WebDAVException(e);
071                    }
072            }
073    
074            protected WebDAVProps getStoredProperties(WebDAVRequest webDAVRequest)
075                    throws PortalException {
076    
077                    WebDAVStorage storage = webDAVRequest.getWebDAVStorage();
078    
079                    Resource resource = storage.getResource(webDAVRequest);
080    
081                    WebDAVProps webDavProps = null;
082    
083                    if (resource.getPrimaryKey() <= 0) {
084                            if (_log.isWarnEnabled()) {
085                                    _log.warn("There is no primary key set for resource");
086                            }
087    
088                            throw new InvalidRequestException();
089                    }
090                    else if (resource.isLocked()) {
091                            Lock lock = resource.getLock();
092    
093                            if ((lock == null) ||
094                                    !lock.getUuid().equals(webDAVRequest.getLockUuid())) {
095    
096                                    throw new LockException();
097                            }
098                    }
099    
100                    webDavProps = WebDAVPropsLocalServiceUtil.getWebDAVProps(
101                            webDAVRequest.getCompanyId(), resource.getClassName(),
102                            resource.getPrimaryKey());
103    
104                    return webDavProps;
105            }
106    
107            protected Set<QName> processInstructions(WebDAVRequest webDAVRequest)
108                    throws InvalidRequestException, LockException {
109    
110                    try {
111                            Set<QName> newProps = new HashSet<>();
112    
113                            HttpServletRequest request = webDAVRequest.getHttpServletRequest();
114    
115                            WebDAVProps webDavProps = getStoredProperties(webDAVRequest);
116    
117                            String xml = new String(
118                                    FileUtil.getBytes(request.getInputStream()));
119    
120                            if (Validator.isNull(xml)) {
121                                    return newProps;
122                            }
123    
124                            if (_log.isInfoEnabled()) {
125                                    _log.info(
126                                            "Request XML: \n" +
127                                                    Dom4jUtil.toString(xml, StringPool.FOUR_SPACES));
128                            }
129    
130                            Document document = SAXReaderUtil.read(xml);
131    
132                            Element rootElement = document.getRootElement();
133    
134                            List<Element> instructionElements = rootElement.elements();
135    
136                            for (Element instructionElement : instructionElements) {
137                                    List<Element> propElements = instructionElement.elements();
138    
139                                    if (propElements.size() != 1) {
140                                            throw new InvalidRequestException(
141                                                    "There should only be one <prop /> per set or remove " +
142                                                            "instruction.");
143                                    }
144    
145                                    Element propElement = propElements.get(0);
146    
147                                    if (!propElement.getName().equals("prop") ||
148                                            !propElement.getNamespaceURI().equals(
149                                                    WebDAVUtil.DAV_URI.getURI())) {
150    
151                                            throw new InvalidRequestException(
152                                                    "Invalid <prop /> element " + propElement);
153                                    }
154    
155                                    List<Element> customPropElements = propElement.elements();
156    
157                                    for (Element customPropElement : customPropElements) {
158                                            String name = customPropElement.getName();
159                                            String prefix = customPropElement.getNamespacePrefix();
160                                            String uri = customPropElement.getNamespaceURI();
161                                            String text = customPropElement.getText();
162    
163                                            Namespace namespace = WebDAVUtil.createNamespace(
164                                                    prefix, uri);
165    
166                                            if (instructionElement.getName().equals("set")) {
167                                                    if (Validator.isNull(text)) {
168                                                            webDavProps.addProp(name, prefix, uri);
169                                                    }
170                                                    else {
171                                                            webDavProps.addProp(name, prefix, uri, text);
172                                                    }
173    
174                                                    newProps.add(
175                                                            SAXReaderUtil.createQName(
176                                                                    customPropElement.getName(), namespace));
177                                            }
178                                            else if (instructionElement.getName().equals("remove")) {
179                                                    webDavProps.removeProp(name, prefix, uri);
180                                            }
181                                            else {
182                                                    throw new InvalidRequestException(
183                                                            "Instead of set/remove instruction, received " +
184                                                                    instructionElement);
185                                            }
186                                    }
187                            }
188    
189                            WebDAVPropsLocalServiceUtil.storeWebDAVProps(webDavProps);
190    
191                            return newProps;
192                    }
193                    catch (LockException le) {
194                            throw le;
195                    }
196                    catch (Exception e) {
197                            throw new InvalidRequestException(e);
198                    }
199            }
200    
201            private static final Log _log = LogFactoryUtil.getLog(
202                    ProppatchMethodImpl.class);
203    
204    }