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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2000-2014 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. * @version @VERSION@
  26. * @since 7.1
  27. */
  28. public abstract class SynchronizedRequestHandler implements RequestHandler {
  29. @Override
  30. public boolean handleRequest(VaadinSession session, VaadinRequest request,
  31. VaadinResponse response) throws IOException {
  32. if (!canHandleRequest(request)) {
  33. return false;
  34. }
  35. session.lock();
  36. try {
  37. return synchronizedHandleRequest(session, request, response);
  38. } finally {
  39. session.unlock();
  40. }
  41. }
  42. /**
  43. * Identical to
  44. * {@link #handleRequest(VaadinSession, VaadinRequest, VaadinResponse)}
  45. * except the {@link VaadinSession} is locked before this is called and
  46. * unlocked after this has completed.
  47. *
  48. * @see #handleRequest(VaadinSession, VaadinRequest, VaadinResponse)
  49. * @param session
  50. * The session for the request
  51. * @param request
  52. * The request to handle
  53. * @param response
  54. * The response object to which a response can be written.
  55. * @return true if a response has been written and no further request
  56. * handlers should be called, otherwise false
  57. *
  58. * @throws IOException
  59. * If an IO error occurred
  60. */
  61. public abstract boolean synchronizedHandleRequest(VaadinSession session,
  62. VaadinRequest request, VaadinResponse response) throws IOException;
  63. /**
  64. * Check whether a request may be handled by this handler. This can be used
  65. * as an optimization to avoid locking the session just to investigate some
  66. * method property. The default implementation just returns
  67. * <code>true</code> which means that all requests will be handled by
  68. * calling
  69. * {@link #synchronizedHandleRequest(VaadinSession, VaadinRequest, VaadinResponse)}
  70. * with the session locked.
  71. *
  72. * @since 7.2
  73. * @param request
  74. * the request to handle
  75. * @return <code>true</code> if the request handling should continue once
  76. * the session has been locked; <code>false</code> if there's no
  77. * need to lock the session since the request would still not be
  78. * handled.
  79. */
  80. protected boolean canHandleRequest(VaadinRequest request) {
  81. return true;
  82. }
  83. }