You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SynchronizedRequestHandler.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server;
  17. import java.io.IOException;
  18. /**
  19. * RequestHandler which takes care of locking and unlocking of the VaadinSession
  20. * automatically. The session is locked before
  21. * {@link #synchronizedHandleRequest(VaadinSession, VaadinRequest, VaadinResponse)}
  22. * is called and unlocked after it has completed.
  23. *
  24. * @author Vaadin Ltd
  25. * @since 7.1
  26. */
  27. public abstract class SynchronizedRequestHandler implements RequestHandler {
  28. @Override
  29. public boolean handleRequest(VaadinSession session, VaadinRequest request,
  30. VaadinResponse response) throws IOException {
  31. if (!canHandleRequest(request)) {
  32. return false;
  33. }
  34. session.lock();
  35. try {
  36. return synchronizedHandleRequest(session, request, response);
  37. } finally {
  38. session.unlock();
  39. }
  40. }
  41. /**
  42. * Identical to
  43. * {@link #handleRequest(VaadinSession, VaadinRequest, VaadinResponse)}
  44. * except the {@link VaadinSession} is locked before this is called and
  45. * unlocked after this has completed.
  46. *
  47. * @see #handleRequest(VaadinSession, VaadinRequest, VaadinResponse)
  48. * @param session
  49. * The session for the request
  50. * @param request
  51. * The request to handle
  52. * @param response
  53. * The response object to which a response can be written.
  54. * @return true if a response has been written and no further request
  55. * handlers should be called, otherwise false
  56. *
  57. * @throws IOException
  58. * If an IO error occurred
  59. */
  60. public abstract boolean synchronizedHandleRequest(VaadinSession session,
  61. VaadinRequest request, VaadinResponse response) throws IOException;
  62. /**
  63. * Check whether a request may be handled by this handler. This can be used
  64. * as an optimization to avoid locking the session just to investigate some
  65. * method property. The default implementation just returns
  66. * <code>true</code> which means that all requests will be handled by
  67. * calling
  68. * {@link #synchronizedHandleRequest(VaadinSession, VaadinRequest, VaadinResponse)}
  69. * with the session locked.
  70. *
  71. * @since 7.2
  72. * @param request
  73. * the request to handle
  74. * @return <code>true</code> if the request handling should continue once
  75. * the session has been locked; <code>false</code> if there's no
  76. * need to lock the session since the request would still not be
  77. * handled.
  78. */
  79. protected boolean canHandleRequest(VaadinRequest request) {
  80. return true;
  81. }
  82. }