001    /**
002     * Copyright (c) 2000-2011 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.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.WebDAVProps;
035    import com.liferay.portal.service.WebDAVPropsLocalServiceUtil;
036    import com.liferay.portal.webdav.InvalidRequestException;
037    import com.liferay.portal.webdav.LockException;
038    import com.liferay.util.xml.XMLFormatter;
039    
040    import java.util.HashSet;
041    import java.util.Iterator;
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            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
054                    try {
055                            Set<Tuple> 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, SystemException {
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                            throw new LockException();
092                    }
093    
094                    webDavProps = WebDAVPropsLocalServiceUtil.getWebDAVProps(
095                            webDavRequest.getCompanyId(), resource.getClassName(),
096                            resource.getPrimaryKey());
097    
098                    return webDavProps;
099            }
100    
101            protected Set<Tuple> processInstructions(WebDAVRequest webDavRequest)
102                    throws InvalidRequestException, LockException {
103    
104                    try {
105                            Set<Tuple> newProps = new HashSet<Tuple>();
106    
107                            HttpServletRequest request = webDavRequest.getHttpServletRequest();
108    
109                            WebDAVProps webDavProps = getStoredProperties(webDavRequest);
110    
111                            String xml = new String(
112                                    FileUtil.getBytes(request.getInputStream()));
113    
114                            if (Validator.isNull(xml)) {
115                                    return newProps;
116                            }
117    
118                            if (_log.isInfoEnabled()) {
119                                    _log.info(
120                                            "Request XML: \n" +
121                                                    XMLFormatter.toString(xml, StringPool.FOUR_SPACES));
122                            }
123    
124                            Document doc = SAXReaderUtil.read(xml);
125    
126                            Element root = doc.getRootElement();
127    
128                            Iterator<Element> itr = root.elements().iterator();
129    
130                            while (itr.hasNext()) {
131                                    Element instruction = itr.next();
132    
133                                    List<Element> list = instruction.elements();
134    
135                                    if (list.size() != 1) {
136                                            throw new InvalidRequestException(
137                                                    "There should only be one <prop /> per set or remove " +
138                                                            "instruction.");
139                                    }
140    
141                                    Element prop = list.get(0);
142    
143                                    if (!prop.getName().equals("prop") ||
144                                            !prop.getNamespaceURI().equals(
145                                                    WebDAVUtil.DAV_URI.getURI())) {
146    
147                                            throw new InvalidRequestException(
148                                                    "Invalid <prop /> element " + prop);
149                                    }
150    
151                                    list = prop.elements();
152    
153                                    if (list.size() != 1) {
154                                            throw new InvalidRequestException(
155                                                    "<prop /> should only have one subelement.");
156                                    }
157    
158                                    Element customProp = list.get(0);
159    
160                                    String name = customProp.getName();
161                                    String prefix = customProp.getNamespacePrefix();
162                                    String uri = customProp.getNamespaceURI();
163                                    String text = customProp.getText();
164    
165                                    Namespace namespace = null;
166    
167                                    if (uri.equals(WebDAVUtil.DAV_URI.getURI())) {
168                                            namespace = WebDAVUtil.DAV_URI;
169                                    }
170                                    else if (Validator.isNull(prefix)) {
171                                            namespace = SAXReaderUtil.createNamespace(uri);
172                                    }
173                                    else {
174                                            namespace = SAXReaderUtil.createNamespace(prefix, uri);
175                                    }
176    
177                                    if (instruction.getName().equals("set")) {
178                                            if (Validator.isNull(text)) {
179                                                    webDavProps.addProp(name, prefix, uri);
180                                            }
181                                            else {
182                                                    webDavProps.addProp(name, prefix, uri, text);
183                                            }
184    
185                                            newProps.add(new Tuple(customProp.getName(), namespace));
186                                    }
187                                    else if (instruction.getName().equals("remove")) {
188                                            webDavProps.removeProp(name, prefix, uri);
189                                    }
190                                    else {
191                                            throw new InvalidRequestException(
192                                                    "Instead of set/remove instruction, received " +
193                                                            instruction);
194                                    }
195                            }
196    
197                            WebDAVPropsLocalServiceUtil.storeWebDAVProps(webDavProps);
198    
199                            return newProps;
200                    }
201                    catch (LockException le) {
202                            throw le;
203                    }
204                    catch (Exception e) {
205                            throw new InvalidRequestException(e);
206                    }
207            }
208    
209            private static Log _log = LogFactoryUtil.getLog(ProppatchMethodImpl.class);
210    
211    }