001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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 org.apache.commons.logging.Log; 052 053import com.google.gwt.user.server.rpc.SerializationPolicy; 054import com.google.gwt.user.server.rpc.jakarta.RemoteServiceServlet; 055 056import jakarta.servlet.ServletException; 057import jakarta.servlet.ServletRequest; 058import jakarta.servlet.ServletResponse; 059import jakarta.servlet.http.HttpServletRequest; 060import jakarta.servlet.http.HttpServletResponse; 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 CmsGwtServiceContext2 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 jakarta.servlet.GenericServlet#log(java.lang.String) 183 */ 184 @Override 185 public void log(String msg) { 186 187 LOG.info(msg); 188 } 189 190 /** 191 * @see jakarta.servlet.GenericServlet#log(java.lang.String, java.lang.Throwable) 192 */ 193 @Override 194 public void log(String message, Throwable t) { 195 196 LOG.info(message, t); 197 } 198 199 /** 200 * Logs the given exception.<p> 201 * 202 * @param t the exception to log 203 */ 204 public void logError(Throwable t) { 205 206 LOG.error(t.getLocalizedMessage(), t); 207 } 208 209 /** 210 * @see jakarta.servlet.http.HttpServlet#service(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse) 211 */ 212 @Override 213 public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { 214 215 try { 216 response.setCharacterEncoding(request.getCharacterEncoding()); 217 super.service(request, response); 218 } finally { 219 clearThreadStorage(); 220 } 221 } 222 223 /** 224 * Sets that the current request is a broadcast call.<p> 225 */ 226 public void setBroadcastPoll() { 227 228 if (m_perThreadBroadcastPoll == null) { 229 m_perThreadBroadcastPoll = new ThreadLocal<>(); 230 } 231 m_perThreadBroadcastPoll.set(Boolean.TRUE); 232 } 233 234 /** 235 * Sets the current cms context.<p> 236 * 237 * @param cms the current cms context to set 238 */ 239 public synchronized void setCms(CmsObject cms) { 240 241 if (m_perThreadCmsObject == null) { 242 m_perThreadCmsObject = new ThreadLocal<CmsObject>(); 243 } 244 m_perThreadCmsObject.set(cms); 245 } 246 247 /** 248 * Sets the service context.<p> 249 * 250 * @param context the new service context 251 */ 252 public synchronized void setContext(CmsGwtServiceContext2 context) { 253 254 m_context = context; 255 } 256 257 /** 258 * Sets the current request.<p> 259 * 260 * @param request the request to set 261 */ 262 public synchronized void setRequest(HttpServletRequest request) { 263 264 if (perThreadRequest == null) { 265 perThreadRequest = new ThreadLocal<HttpServletRequest>(); 266 } 267 perThreadRequest.set(request); 268 } 269 270 /** 271 * Sets the current response.<p> 272 * 273 * @param response the response to set 274 */ 275 public synchronized void setResponse(HttpServletResponse response) { 276 277 if (perThreadResponse == null) { 278 perThreadResponse = new ThreadLocal<HttpServletResponse>(); 279 } 280 perThreadResponse.set(response); 281 } 282 283 /** 284 * Clears the objects stored in thread local.<p> 285 */ 286 protected void clearThreadStorage() { 287 288 if (m_perThreadCmsObject != null) { 289 m_perThreadCmsObject.remove(); 290 } 291 if (perThreadRequest != null) { 292 perThreadRequest.remove(); 293 } 294 if (perThreadResponse != null) { 295 perThreadResponse.remove(); 296 } 297 if (m_perThreadBroadcastPoll != null) { 298 m_perThreadBroadcastPoll.remove(); 299 } 300 } 301 302 /** 303 * We do not want that the server goes to fetch files from the servlet context.<p> 304 * 305 * @see com.google.gwt.user.server.rpc.jakarta.RemoteServiceServlet#doGetSerializationPolicy(jakarta.servlet.http.HttpServletRequest, java.lang.String, java.lang.String) 306 */ 307 @Override 308 protected SerializationPolicy doGetSerializationPolicy( 309 HttpServletRequest request, 310 String moduleBaseURL, 311 String strongName) { 312 313 return m_context.getSerializationPolicy(getCmsObject(), moduleBaseURL, strongName); 314 } 315 316 /** 317 * @see com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet#doUnexpectedFailure(java.lang.Throwable) 318 */ 319 @Override 320 protected void doUnexpectedFailure(Throwable e) { 321 322 LOG.error(String.valueOf(System.currentTimeMillis()), e); 323 super.doUnexpectedFailure(e); 324 } 325 326 /** 327 * Locks the given resource with a temporary, if not already locked by the current user. 328 * Will throw an exception if the resource could not be locked for the current user.<p> 329 * 330 * @param resource the resource to lock 331 * 332 * @return the assigned lock 333 * 334 * @throws CmsException if the resource could not be locked 335 */ 336 protected CmsLockActionRecord ensureLock(CmsResource resource) throws CmsException { 337 338 CmsObject cms = getCmsObject(); 339 return CmsLockUtil.ensureLock(cms, resource, false); 340 } 341 342 /** 343 * Locks the given resource with a temporary, if not already locked by the current user. 344 * Will throw an exception if the resource could not be locked for the current user.<p> 345 * 346 * @param resource the resource to lock 347 * @param shallow true if we only want a shallow lock 348 * 349 * @return the assigned lock 350 * 351 * @throws CmsException if the resource could not be locked 352 */ 353 protected CmsLockActionRecord ensureLock(CmsResource resource, boolean shallow) throws CmsException { 354 355 CmsObject cms = getCmsObject(); 356 return CmsLockUtil.ensureLock(cms, resource, shallow); 357 } 358 359 /** 360 * 361 * Locks the given resource with a temporary, if not already locked by the current user. 362 * Will throw an exception if the resource could not be locked for the current user.<p> 363 * 364 * @param structureId the structure id of the resource 365 * 366 * @return the assigned lock 367 * 368 * @throws CmsException if something goes wrong 369 */ 370 protected CmsLockActionRecord ensureLock(CmsUUID structureId) throws CmsException { 371 372 return ensureLock(getCmsObject().readResource(structureId, CmsResourceFilter.IGNORE_EXPIRATION)); 373 374 } 375 376 /** 377 * Locks the given resource with a temporary, if not already locked by the current user. 378 * Will throw an exception if the resource could not be locked for the current user.<p> 379 * 380 * @param sitepath the site-path of the resource to lock 381 * 382 * @return the assigned lock 383 * 384 * @throws CmsException if the resource could not be locked 385 */ 386 protected CmsLockActionRecord ensureLock(String sitepath) throws CmsException { 387 388 return ensureLock(getCmsObject().readResource(sitepath, CmsResourceFilter.IGNORE_EXPIRATION)); 389 } 390 391 /** 392 * Ensures that the user session is still valid.<p> 393 * 394 * @throws CmsException if the current user is the guest user 395 */ 396 protected void ensureSession() throws CmsException { 397 398 CmsUser user = getCmsObject().getRequestContext().getCurrentUser(); 399 if (user.isGuestUser()) { 400 throw new CmsException(Messages.get().container(Messages.ERR_SESSION_EXPIRED_0)); 401 } 402 } 403 404 /** 405 * Converts a list of properties to a map.<p> 406 * 407 * @param properties the list of properties 408 * 409 * @return a map from property names to properties 410 */ 411 protected Map<String, CmsProperty> getPropertiesByName(List<CmsProperty> properties) { 412 413 Map<String, CmsProperty> result = new HashMap<String, CmsProperty>(); 414 for (CmsProperty property : properties) { 415 String key = property.getName(); 416 result.put(key, property.clone()); 417 } 418 return result; 419 } 420 421 /** 422 * Tries to unlock a resource.<p> 423 * 424 * @param resource the resource to unlock 425 */ 426 protected void tryUnlock(CmsResource resource) { 427 428 try { 429 getCmsObject().unlockResource(resource); 430 } catch (CmsException e) { 431 LOG.debug("Unable to unlock " + resource.getRootPath(), e); 432 } 433 } 434}