001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo
018 *     Florent Guillaume
019 */
020
021package org.nuxeo.ecm.platform.ui.web.restAPI;
022
023import org.dom4j.dom.DOMDocument;
024import org.dom4j.dom.DOMDocumentFactory;
025import org.joda.time.DateTime;
026import org.joda.time.format.ISODateTimeFormat;
027import org.nuxeo.ecm.core.api.Lock;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.restlet.data.CharacterSet;
030import org.restlet.data.MediaType;
031import org.restlet.data.Method;
032import org.restlet.data.Request;
033import org.restlet.data.Response;
034import org.w3c.dom.Element;
035
036/**
037 * Restlet returning the locking information on a document.
038 */
039public class LockingRestlet extends BaseStatelessNuxeoRestlet {
040
041    public static final String LOCK = "lock";
042
043    public static final String UNLOCK = "unlock";
044
045    public static final String STATUS = "status";
046
047    public static final String STATE = "state";
048
049    public static final String SC_LOCKINFO_LOCKED = "LOCKED";
050
051    public static final String SC_LOCKINFO_NOT_LOCKED = "NOTLOCKED";
052
053    public static final String SC_LOCKED_OK = "OK";
054
055    public static final String SC_ALREADY_LOCKED_KO = "ALREADYLOCKED";
056
057    public static final String SC_ALREADY_LOCKED_OK = "ALREADYLOCKEDBYYOU";
058
059    public static final String SC_UNLOCKED_OK = "OK";
060
061    public static final String SC_ALREADY_UNLOCKED_OK = "NOT LOCKED";
062
063    @Override
064    protected void doHandleStatelessRequest(Request req, Response res) {
065
066        String repoId = (String) req.getAttributes().get("repo");
067        String docid = (String) req.getAttributes().get("docid");
068
069        DOMDocumentFactory domFactory = new DOMDocumentFactory();
070        DOMDocument result = (DOMDocument) domFactory.createDocument();
071
072        // init repo and document
073        boolean initOk = initRepositoryAndTargetDocument(res, repoId, docid);
074        if (!initOk) {
075            return;
076        }
077
078        String cUserName = getUserPrincipal(req).getName();
079
080        // get Action
081        String action = STATUS;
082        if (req.getResourceRef().getSegments().size() > 5) {
083            action = req.getResourceRef().getSegments().get(5).toLowerCase();
084        }
085        if (req.getMethod().equals(Method.LOCK)) {
086            action = LOCK;
087        }
088        if (req.getMethod().equals(Method.UNLOCK)) {
089            action = UNLOCK;
090        }
091
092        String response = "";
093        String code = "";
094        if (action.equals(LOCK)) {
095            try {
096                Lock lock = session.getLockInfo(targetDocRef);
097                if (lock == null) {
098                    session.setLock(targetDocRef);
099                    session.save();
100                    response = "lock acquired on document " + docid;
101                    code = SC_LOCKED_OK;
102                } else if (lock.getOwner().equals(cUserName)) {
103                    response = "document " + docid + " is already locked by you";
104                    code = SC_ALREADY_LOCKED_OK;
105                } else {
106                    response = "document " + docid + " is already locked by " + lock.getOwner();
107                    code = SC_ALREADY_LOCKED_KO;
108                }
109            } catch (NuxeoException e) {
110                handleError(result, res, e);
111                return;
112            }
113        } else if (action.equals(UNLOCK)) {
114
115            try {
116                Lock lock = session.getLockInfo(targetDocRef);
117                if (lock == null) {
118                    response = "document " + docid + " is not locked";
119                    code = SC_ALREADY_UNLOCKED_OK;
120                } else if (lock.getOwner().equals(cUserName)) {
121                    session.removeLock(targetDocRef);
122                    session.save();
123                    response = "document " + docid + " unlocked";
124                    code = SC_UNLOCKED_OK;
125                } else {
126                    response = "document " + docid + " is locked by " + lock.getOwner();
127                    code = SC_ALREADY_LOCKED_KO;
128                }
129            } catch (NuxeoException e) {
130                handleError(result, res, e);
131                return;
132            }
133
134        } else if (action.equals(STATUS)) {
135            try {
136                Lock lock = session.getLockInfo(targetDocRef);
137                response = session.getLock(targetDocRef);
138                if (lock == null) {
139                    code = SC_LOCKINFO_NOT_LOCKED;
140                } else {
141                    code = SC_LOCKINFO_LOCKED;
142                }
143            } catch (NuxeoException e) {
144                handleError(result, res, e);
145                return;
146            }
147
148        } else if (action.equals(STATE)) {
149            try {
150                Lock lock = session.getLockInfo(targetDocRef);
151                if (lock == null) {
152                    code = SC_LOCKINFO_NOT_LOCKED;
153                    response = "";
154                } else {
155                    code = SC_LOCKINFO_LOCKED;
156                    response = lock.getOwner() + '/'
157                            + ISODateTimeFormat.dateTime().print(new DateTime(lock.getCreated()));
158                }
159            } catch (NuxeoException e) {
160                handleError(result, res, e);
161                return;
162            }
163
164        } else {
165            handleError(result, res, "Unsupported operation");
166            return;
167        }
168
169        Element current = result.createElement("document");
170        current.setAttribute("code", code);
171        current.setAttribute("message", response);
172        result.setRootElement((org.dom4j.Element) current);
173        res.setEntity(result.asXML(), MediaType.TEXT_XML);
174        res.getEntity().setCharacterSet(CharacterSet.UTF_8);
175    }
176
177}