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