001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Tuple;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.webdav.Resource;
026    import com.liferay.portal.kernel.webdav.WebDAVException;
027    import com.liferay.portal.kernel.webdav.WebDAVRequest;
028    import com.liferay.portal.kernel.webdav.WebDAVStorage;
029    import com.liferay.portal.kernel.webdav.WebDAVUtil;
030    import com.liferay.portal.kernel.xml.Document;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.kernel.xml.Namespace;
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<Tuple> 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<Tuple> processInstructions(WebDAVRequest webDavRequest)
109                    throws InvalidRequestException, LockException {
110    
111                    try {
112                            Set<Tuple> newProps = new HashSet<Tuple>();
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 = null;
167    
168                                            if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
169                                                    namespace = WebDAVUtil.DAV_URI;
170                                            }
171                                            else if (Validator.isNull(prefix)) {
172                                                    namespace = SAXReaderUtil.createNamespace(uri);
173                                            }
174                                            else {
175                                                    namespace = SAXReaderUtil.createNamespace(prefix, uri);
176                                            }
177    
178                                            if (instructionElement.getName().equals("set")) {
179                                                    if (Validator.isNull(text)) {
180                                                            webDavProps.addProp(name, prefix, uri);
181                                                    }
182                                                    else {
183                                                            webDavProps.addProp(name, prefix, uri, text);
184                                                    }
185    
186                                                    newProps.add(
187                                                            new Tuple(customPropElement.getName(), namespace));
188                                            }
189                                            else if (instructionElement.getName().equals("remove")) {
190                                                    webDavProps.removeProp(name, prefix, uri);
191                                            }
192                                            else {
193                                                    throw new InvalidRequestException(
194                                                            "Instead of set/remove instruction, received " +
195                                                                    instructionElement);
196                                            }
197                                    }
198                            }
199    
200                            WebDAVPropsLocalServiceUtil.storeWebDAVProps(webDavProps);
201    
202                            return newProps;
203                    }
204                    catch (LockException le) {
205                            throw le;
206                    }
207                    catch (Exception e) {
208                            throw new InvalidRequestException(e);
209                    }
210            }
211    
212            private static Log _log = LogFactoryUtil.getLog(ProppatchMethodImpl.class);
213    
214    }