001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsResource;
033import org.opencms.file.CmsResourceFilter;
034import org.opencms.file.CmsUser;
035import org.opencms.lock.CmsLockActionRecord;
036import org.opencms.lock.CmsLockUtil;
037import org.opencms.main.CmsException;
038import org.opencms.main.CmsLog;
039import org.opencms.main.I_CmsThrowable;
040import org.opencms.main.OpenCms;
041import org.opencms.security.CmsRole;
042import org.opencms.security.CmsRoleViolationException;
043import org.opencms.util.CmsUUID;
044
045import java.io.IOException;
046import java.util.HashMap;
047import java.util.List;
048import java.util.Locale;
049import java.util.Map;
050
051import javax.servlet.ServletException;
052import javax.servlet.ServletRequest;
053import javax.servlet.ServletResponse;
054import javax.servlet.http.HttpServletRequest;
055import javax.servlet.http.HttpServletResponse;
056
057import org.apache.commons.logging.Log;
058
059import com.google.gwt.user.server.rpc.RemoteServiceServlet;
060import com.google.gwt.user.server.rpc.SerializationPolicy;
061
062/**
063 * Wrapper for GWT services served through OpenCms.<p>
064 *
065 * @since 8.0.0
066 */
067public class CmsGwtService extends RemoteServiceServlet {
068
069    /** The static log object for this class. */
070    private static final Log LOG = CmsLog.getLog(CmsGwtService.class);
071
072    /** Serialization id. */
073    private static final long serialVersionUID = 8119684308154724518L;
074
075    /** The service class context. */
076    private CmsGwtServiceContext m_context;
077
078    /** The current CMS context. */
079    private ThreadLocal<CmsObject> m_perThreadCmsObject;
080
081    /** Stores whether the current request is a broadcast poll. */
082    private ThreadLocal<Boolean> m_perThreadBroadcastPoll;
083
084    /**
085     * Constructor.<p>
086     */
087    public CmsGwtService() {
088
089        super();
090    }
091
092    /**
093     * Checks the permissions of the current user to match the required security level.<p>
094     *
095     * Note that the current request and response are not available yet.<p>
096     *
097     * Override if needed.<p>
098     *
099     * @param cms the current cms object
100     *
101     * @throws CmsRoleViolationException if the security level can not be satisfied
102     */
103    public void checkPermissions(CmsObject cms) throws CmsRoleViolationException {
104
105        OpenCms.getRoleManager().checkRole(cms, CmsRole.ELEMENT_AUTHOR);
106    }
107
108    /**
109     * Logs and re-throws the given exception for RPC responses.<p>
110     *
111     * @param t the exception
112     *
113     * @throws CmsRpcException the converted exception
114     */
115    public void error(Throwable t) throws CmsRpcException {
116
117        logError(t);
118        CmsRpcException e = new CmsRpcException(t);
119        // The CmsRpcException constructor can't do the localization, because it's a shared class
120        CmsObject cms = getCmsObject();
121        if (cms != null) {
122            Locale locale = OpenCms.getWorkplaceManager().getWorkplaceLocale(getCmsObject());
123            if (t instanceof I_CmsThrowable) {
124                String message = ((I_CmsThrowable)t).getLocalizedMessage(locale);
125                e.setOriginalMessage(message);
126            }
127            if (t.getCause() instanceof I_CmsThrowable) {
128                String message = ((I_CmsThrowable)t.getCause()).getLocalizedMessage(locale);
129                e.setOriginalCauseMessage(message);
130            }
131        }
132        throw e;
133    }
134
135    /**
136     * Returns the current cms context.<p>
137     *
138     * @return the current cms context
139     */
140    public CmsObject getCmsObject() {
141
142        return m_perThreadCmsObject.get();
143    }
144
145    /**
146     * Returns the current request.<p>
147     *
148     * @return the current request
149     *
150     * @see #getThreadLocalRequest()
151     */
152    public HttpServletRequest getRequest() {
153
154        return getThreadLocalRequest();
155    }
156
157    /**
158     * Returns the current response.<p>
159     *
160     * @return the current response
161     *
162     * @see #getThreadLocalResponse()
163     */
164    public HttpServletResponse getResponse() {
165
166        return getThreadLocalResponse();
167    }
168
169    /**
170     * Returns whether the current request is a broadcast call.<p>
171     *
172     * @return <code>true</code> if the current request is a broadcast call
173     */
174    public boolean isBroadcastCall() {
175
176        return (m_perThreadBroadcastPoll != null)
177            && (m_perThreadBroadcastPoll.get() != null)
178            && m_perThreadBroadcastPoll.get().booleanValue();
179    }
180
181    /**
182     * @see javax.servlet.GenericServlet#log(java.lang.String)
183     */
184    @Override
185    public void log(String msg) {
186        LOG.info(msg);
187    }
188
189    /**
190     * @see javax.servlet.GenericServlet#log(java.lang.String, java.lang.Throwable)
191     */
192    @Override
193    public void log(String message, Throwable t) {
194        LOG.info(message, t);
195    }
196
197    /**
198     * Logs the given exception.<p>
199     *
200     * @param t the exception to log
201     */
202    public void logError(Throwable t) {
203
204        LOG.error(t.getLocalizedMessage(), t);
205    }
206
207    /**
208     * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
209     */
210    @Override
211    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
212
213        try {
214            response.setCharacterEncoding(request.getCharacterEncoding());
215            super.service(request, response);
216        } finally {
217            clearThreadStorage();
218        }
219    }
220
221    /**
222     * Sets that the current request is a broadcast call.<p>
223     */
224    public void setBroadcastPoll() {
225
226        if (m_perThreadBroadcastPoll == null) {
227            m_perThreadBroadcastPoll = new ThreadLocal<>();
228        }
229        m_perThreadBroadcastPoll.set(Boolean.TRUE);
230    }
231
232    /**
233     * Sets the current cms context.<p>
234     *
235     * @param cms the current cms context to set
236     */
237    public synchronized void setCms(CmsObject cms) {
238
239        if (m_perThreadCmsObject == null) {
240            m_perThreadCmsObject = new ThreadLocal<CmsObject>();
241        }
242        m_perThreadCmsObject.set(cms);
243    }
244
245    /**
246     * Sets the service context.<p>
247     *
248     * @param context the new service context
249     */
250    public synchronized void setContext(CmsGwtServiceContext context) {
251
252        m_context = context;
253    }
254
255    /**
256     * Sets the current request.<p>
257     *
258     * @param request the request to set
259     */
260    public synchronized void setRequest(HttpServletRequest request) {
261
262        if (perThreadRequest == null) {
263            perThreadRequest = new ThreadLocal<HttpServletRequest>();
264        }
265        perThreadRequest.set(request);
266    }
267
268    /**
269     * Sets the current response.<p>
270     *
271     * @param response the response to set
272     */
273    public synchronized void setResponse(HttpServletResponse response) {
274
275        if (perThreadResponse == null) {
276            perThreadResponse = new ThreadLocal<HttpServletResponse>();
277        }
278        perThreadResponse.set(response);
279    }
280
281    /**
282     * Clears the objects stored in thread local.<p>
283     */
284    protected void clearThreadStorage() {
285
286        if (m_perThreadCmsObject != null) {
287            m_perThreadCmsObject.remove();
288        }
289        if (perThreadRequest != null) {
290            perThreadRequest.remove();
291        }
292        if (perThreadResponse != null) {
293            perThreadResponse.remove();
294        }
295        if (m_perThreadBroadcastPoll != null) {
296            m_perThreadBroadcastPoll.remove();
297        }
298    }
299
300    /**
301     * We do not want that the server goes to fetch files from the servlet context.<p>
302     *
303     * @see com.google.gwt.user.server.rpc.RemoteServiceServlet#doGetSerializationPolicy(javax.servlet.http.HttpServletRequest, java.lang.String, java.lang.String)
304     */
305    @Override
306    protected SerializationPolicy doGetSerializationPolicy(
307        HttpServletRequest request,
308        String moduleBaseURL,
309        String strongName) {
310
311        return m_context.getSerializationPolicy(getCmsObject(), moduleBaseURL, strongName);
312    }
313
314    /**
315     * @see com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet#doUnexpectedFailure(java.lang.Throwable)
316     */
317    @Override
318    protected void doUnexpectedFailure(Throwable e) {
319
320        LOG.error(String.valueOf(System.currentTimeMillis()), e);
321        super.doUnexpectedFailure(e);
322    }
323
324    /**
325     * Locks the given resource with a temporary, if not already locked by the current user.
326     * Will throw an exception if the resource could not be locked for the current user.<p>
327     *
328     * @param resource the resource to lock
329     *
330     * @return the assigned lock
331     *
332     * @throws CmsException if the resource could not be locked
333     */
334    protected CmsLockActionRecord ensureLock(CmsResource resource) throws CmsException {
335
336        CmsObject cms = getCmsObject();
337        return CmsLockUtil.ensureLock(cms, resource, false);
338    }
339
340    /**
341     * Locks the given resource with a temporary, if not already locked by the current user.
342     * Will throw an exception if the resource could not be locked for the current user.<p>
343     *
344     * @param resource the resource to lock
345     * @param shallow true if we only want a shallow lock
346     *
347     * @return the assigned lock
348     *
349     * @throws CmsException if the resource could not be locked
350     */
351    protected CmsLockActionRecord ensureLock(CmsResource resource, boolean shallow) throws CmsException {
352
353        CmsObject cms = getCmsObject();
354        return CmsLockUtil.ensureLock(cms, resource, shallow);
355    }
356
357    /**
358     *
359     * Locks the given resource with a temporary, if not already locked by the current user.
360     * Will throw an exception if the resource could not be locked for the current user.<p>
361     *
362     * @param structureId the structure id of the resource
363     *
364     * @return the assigned lock
365     *
366     * @throws CmsException if something goes wrong
367     */
368    protected CmsLockActionRecord ensureLock(CmsUUID structureId) throws CmsException {
369
370        return ensureLock(getCmsObject().readResource(structureId, CmsResourceFilter.IGNORE_EXPIRATION));
371
372    }
373
374    /**
375     * Locks the given resource with a temporary, if not already locked by the current user.
376     * Will throw an exception if the resource could not be locked for the current user.<p>
377     *
378     * @param sitepath the site-path of the resource to lock
379     *
380     * @return the assigned lock
381     *
382     * @throws CmsException if the resource could not be locked
383     */
384    protected CmsLockActionRecord ensureLock(String sitepath) throws CmsException {
385
386        return ensureLock(getCmsObject().readResource(sitepath, CmsResourceFilter.IGNORE_EXPIRATION));
387    }
388
389    /**
390     * Ensures that the user session is still valid.<p>
391     *
392     * @throws CmsException if the current user is the guest user
393     */
394    protected void ensureSession() throws CmsException {
395
396        CmsUser user = getCmsObject().getRequestContext().getCurrentUser();
397        if (user.isGuestUser()) {
398            throw new CmsException(Messages.get().container(Messages.ERR_SESSION_EXPIRED_0));
399        }
400    }
401
402    /**
403     * Converts a list of properties to a map.<p>
404     *
405     * @param properties the list of properties
406     *
407     * @return a map from property names to properties
408     */
409    protected Map<String, CmsProperty> getPropertiesByName(List<CmsProperty> properties) {
410
411        Map<String, CmsProperty> result = new HashMap<String, CmsProperty>();
412        for (CmsProperty property : properties) {
413            String key = property.getName();
414            result.put(key, property.clone());
415        }
416        return result;
417    }
418
419    /**
420     * Tries to unlock a resource.<p>
421     *
422     * @param resource the resource to unlock
423     */
424    protected void tryUnlock(CmsResource resource) {
425
426        try {
427            getCmsObject().unlockResource(resource);
428        } catch (CmsException e) {
429            LOG.debug("Unable to unlock " + resource.getRootPath(), e);
430        }
431    }
432}