001    /**
002     * Copyright (c) 2000-2013 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.List;
043    import java.util.Set;
044    
045    import javax.servlet.http.HttpServletRequest;
046    import javax.servlet.http.HttpServletResponse;
047    
048    /**
049     * @author Alexander Chow
050     */
051    public class ProppatchMethodImpl extends BasePropMethodImpl {
052    
053            @Override
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                            List<Element> instructionElements = rootElement.elements();
136    
137                            for (Element instructionElement : instructionElements) {
138                                    List<Element> propElements = instructionElement.elements();
139    
140                                    if (propElements.size() != 1) {
141                                            throw new InvalidRequestException(
142                                                    "There should only be one <prop /> per set or remove " +
143                                                            "instruction.");
144                                    }
145    
146                                    Element propElement = propElements.get(0);
147    
148                                    if (!propElement.getName().equals("prop") ||
149                                            !propElement.getNamespaceURI().equals(
150                                                    WebDAVUtil.DAV_URI.getURI())) {
151    
152                                            throw new InvalidRequestException(
153                                                    "Invalid <prop /> element " + propElement);
154                                    }
155    
156                                    List<Element> customPropElements = propElement.elements();
157    
158                                    for (Element customPropElement : customPropElements) {
159                                            String name = customPropElement.getName();
160                                            String prefix = customPropElement.getNamespacePrefix();
161                                            String uri = customPropElement.getNamespaceURI();
162                                            String text = customPropElement.getText();
163    
164                                            Namespace namespace = WebDAVUtil.createNamespace(
165                                                    prefix, uri);
166    
167                                            if (instructionElement.getName().equals("set")) {
168                                                    if (Validator.isNull(text)) {
169                                                            webDavProps.addProp(name, prefix, uri);
170                                                    }
171                                                    else {
172                                                            webDavProps.addProp(name, prefix, uri, text);
173                                                    }
174    
175                                                    newProps.add(
176                                                            SAXReaderUtil.createQName(
177                                                                    customPropElement.getName(), namespace));
178                                            }
179                                            else if (instructionElement.getName().equals("remove")) {
180                                                    webDavProps.removeProp(name, prefix, uri);
181                                            }
182                                            else {
183                                                    throw new InvalidRequestException(
184                                                            "Instead of set/remove instruction, received " +
185                                                                    instructionElement);
186                                            }
187                                    }
188                            }
189    
190                            WebDAVPropsLocalServiceUtil.storeWebDAVProps(webDavProps);
191    
192                            return newProps;
193                    }
194                    catch (LockException le) {
195                            throw le;
196                    }
197                    catch (Exception e) {
198                            throw new InvalidRequestException(e);
199                    }
200            }
201    
202            private static Log _log = LogFactoryUtil.getLog(ProppatchMethodImpl.class);
203    
204    }