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.NoSuchLockException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021    import com.liferay.portal.kernel.util.ContentTypes;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.Time;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.webdav.Status;
028    import com.liferay.portal.kernel.webdav.WebDAVException;
029    import com.liferay.portal.kernel.webdav.WebDAVRequest;
030    import com.liferay.portal.kernel.webdav.WebDAVStorage;
031    import com.liferay.portal.kernel.webdav.WebDAVUtil;
032    import com.liferay.portal.kernel.xml.Document;
033    import com.liferay.portal.kernel.xml.Element;
034    import com.liferay.portal.kernel.xml.SAXReaderUtil;
035    import com.liferay.portal.model.Lock;
036    import com.liferay.util.xml.XMLFormatter;
037    
038    import java.util.List;
039    
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    
043    /**
044     * @author Alexander Chow
045     */
046    public class LockMethodImpl implements Method {
047    
048            public int process(WebDAVRequest webDavRequest) throws WebDAVException {
049                    try {
050                            return doProcess(webDavRequest);
051                    }
052                    catch (Exception e) {
053                            throw new WebDAVException(e);
054                    }
055            }
056    
057            protected int doProcess(WebDAVRequest webDavRequest) throws Exception {
058                    WebDAVStorage storage = webDavRequest.getWebDAVStorage();
059    
060                    if (!storage.isSupportsClassTwo()) {
061                            return HttpServletResponse.SC_METHOD_NOT_ALLOWED;
062                    }
063    
064                    HttpServletRequest request = webDavRequest.getHttpServletRequest();
065                    HttpServletResponse response = webDavRequest.getHttpServletResponse();
066    
067                    Lock lock = null;
068                    Status status = null;
069    
070                    String lockUuid = webDavRequest.getLockUuid();
071                    long timeout = WebDAVUtil.getTimeout(request);
072    
073                    if (Validator.isNull(lockUuid)) {
074    
075                            // Create new lock
076    
077                            String owner = null;
078                            String xml = new String(
079                                    FileUtil.getBytes(request.getInputStream()));
080    
081                            if (Validator.isNotNull(xml)) {
082                                    if (_log.isDebugEnabled()) {
083                                            _log.debug("Request XML\n" + XMLFormatter.toString(xml));
084                                    }
085    
086                                    Document document = SAXReaderUtil.read(xml);
087    
088                                    Element rootElement = document.getRootElement();
089    
090                                    boolean exclusive = false;
091    
092                                    Element lockscopeElement = rootElement.element("lockscope");
093    
094                                    for (Element element : lockscopeElement.elements()) {
095                                            String name = GetterUtil.getString(element.getName());
096    
097                                            if (name.equals("exclusive")) {
098                                                    exclusive = true;
099                                            }
100                                    }
101    
102                                    if (!exclusive) {
103                                            return HttpServletResponse.SC_BAD_REQUEST;
104                                    }
105    
106                                    Element ownerElement = rootElement.element("owner");
107    
108                                    owner = ownerElement.getTextTrim();
109    
110                                    if (Validator.isNull(owner)) {
111                                            List<Element> hrefElements = ownerElement.elements("href");
112    
113                                            for (Element hrefElement : hrefElements) {
114                                                    owner =
115                                                            "<D:href>" + hrefElement.getTextTrim() +
116                                                                    "</D:href>";
117                                            }
118                                    }
119                            }
120                            else {
121                                    _log.error("Empty request XML");
122    
123                                    return HttpServletResponse.SC_PRECONDITION_FAILED;
124                            }
125    
126                            status = storage.lockResource(webDavRequest, owner, timeout);
127    
128                            lock = (Lock)status.getObject();
129                    }
130                    else {
131                            try {
132                                    // Refresh existing lock
133    
134                                    lock = storage.refreshResourceLock(
135                                            webDavRequest, lockUuid, timeout);
136    
137                                    status = new Status(HttpServletResponse.SC_OK);
138                            }
139                            catch (WebDAVException wde) {
140                                    if (wde.getCause() instanceof NoSuchLockException) {
141                                            return HttpServletResponse.SC_PRECONDITION_FAILED;
142                                    }
143                                    else {
144                                            throw wde;
145                                    }
146                            }
147                    }
148    
149                    // Return lock details
150    
151                    if (lock == null) {
152                            return status.getCode();
153                    }
154    
155                    long depth = WebDAVUtil.getDepth(request);
156    
157                    String xml = getResponseXML(lock, depth);
158    
159                    if (_log.isDebugEnabled()) {
160                            _log.debug("Response XML\n" + xml);
161                    }
162    
163                    String lockToken = "<" + WebDAVUtil.TOKEN_PREFIX + lock.getUuid() + ">";
164    
165                    response.setContentType(ContentTypes.TEXT_XML_UTF8);
166                    response.setHeader("Lock-Token", lockToken);
167                    response.setStatus(status.getCode());
168    
169                    if (_log.isDebugEnabled()) {
170                            _log.debug("Returning lock token " + lockToken);
171                    }
172    
173                    try {
174                            ServletResponseUtil.write(response, xml);
175                    }
176                    catch (Exception e) {
177                            if (_log.isWarnEnabled()) {
178                                    _log.warn(e);
179                            }
180                    }
181    
182                    return status.getCode();
183            }
184    
185            protected String getResponseXML(Lock lock, long depth) throws Exception {
186                    StringBundler sb = new StringBundler(20);
187    
188                    long timeoutSecs = lock.getExpirationTime() / Time.SECOND;
189    
190                    sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
191                    sb.append("<D:prop xmlns:D=\"DAV:\">");
192                    sb.append("<D:lockdiscovery>");
193                    sb.append("<D:activelock>");
194                    sb.append("<D:locktype><D:write/></D:locktype>");
195                    sb.append("<D:lockscope><D:exclusive/></D:lockscope>");
196    
197                    if (depth < 0) {
198                            sb.append("<D:depth>Infinity</D:depth>");
199                    }
200    
201                    sb.append("<D:owner>");
202                    sb.append(lock.getOwner());
203                    sb.append("</D:owner>");
204                    sb.append("<D:timeout>");
205    
206                    if (timeoutSecs > 0) {
207                            sb.append("Second-" + timeoutSecs);
208                    }
209                    else {
210                            sb.append("Infinite");
211                    }
212    
213                    sb.append("</D:timeout>");
214                    sb.append("<D:locktoken><D:href>");
215                    sb.append(WebDAVUtil.TOKEN_PREFIX);
216                    sb.append(lock.getUuid());
217                    sb.append("</D:href></D:locktoken>");
218                    sb.append("</D:activelock>");
219                    sb.append("</D:lockdiscovery>");
220                    sb.append("</D:prop>");
221    
222                    return XMLFormatter.toString(sb.toString());
223            }
224    
225            private static Log _log = LogFactoryUtil.getLog(LockMethodImpl.class);
226    
227    }