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.

CombinedRequest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2011 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. import java.io.InputStream;
  19. import java.net.URI;
  20. import java.net.URISyntaxException;
  21. import java.util.Locale;
  22. import java.util.Map;
  23. import org.json.JSONException;
  24. /**
  25. * A {@link VaadinRequest} with path and parameters from one request and
  26. * {@link VaadinRequest.BrowserDetails} extracted from another request.
  27. *
  28. * This class is intended to be used for a two request initialization where the
  29. * first request fetches the actual application page and the second request
  30. * contains information extracted from the browser using javascript.
  31. *
  32. */
  33. public class CombinedRequest implements VaadinRequest {
  34. private final VaadinRequest secondRequest;
  35. /**
  36. * Creates a new combined request based on the second request and some
  37. * details from the first request.
  38. *
  39. * @param secondRequest
  40. * the second request which will be used as the foundation of the
  41. * combined request
  42. * @throws JSONException
  43. * if the initialParams parameter can not be decoded
  44. */
  45. public CombinedRequest(VaadinRequest secondRequest) throws JSONException {
  46. this.secondRequest = secondRequest;
  47. }
  48. @Override
  49. public String getParameter(String parameter) {
  50. return secondRequest.getParameter(parameter);
  51. }
  52. @Override
  53. public Map<String, String[]> getParameterMap() {
  54. return secondRequest.getParameterMap();
  55. }
  56. @Override
  57. public int getContentLength() {
  58. return secondRequest.getContentLength();
  59. }
  60. @Override
  61. public InputStream getInputStream() throws IOException {
  62. return secondRequest.getInputStream();
  63. }
  64. @Override
  65. public Object getAttribute(String name) {
  66. return secondRequest.getAttribute(name);
  67. }
  68. @Override
  69. public void setAttribute(String name, Object value) {
  70. secondRequest.setAttribute(name, value);
  71. }
  72. @Override
  73. public String getRequestPathInfo() {
  74. return secondRequest.getRequestPathInfo();
  75. }
  76. @Override
  77. public WrappedSession getWrappedSession() {
  78. return getWrappedSession(true);
  79. }
  80. @Override
  81. public WrappedSession getWrappedSession(boolean allowSessionCreation) {
  82. return secondRequest.getWrappedSession(allowSessionCreation);
  83. }
  84. @Override
  85. public String getContentType() {
  86. return secondRequest.getContentType();
  87. }
  88. @Override
  89. public BrowserDetails getBrowserDetails() {
  90. return new BrowserDetails() {
  91. @Override
  92. public URI getLocation() {
  93. String location = secondRequest.getParameter("loc");
  94. if (location == null) {
  95. return null;
  96. } else {
  97. try {
  98. return new URI(location);
  99. } catch (URISyntaxException e) {
  100. throw new RuntimeException(
  101. "Invalid location URI received from client", e);
  102. }
  103. }
  104. }
  105. @Override
  106. public String getWindowName() {
  107. return secondRequest.getParameter("wn");
  108. }
  109. @Override
  110. public WebBrowser getWebBrowser() {
  111. return VaadinServiceSession.getCurrent().getBrowser();
  112. }
  113. };
  114. }
  115. /**
  116. * Gets the original second request. This can be used e.g. if a request
  117. * parameter from the second request is required.
  118. *
  119. * @return the original second Vaadin request
  120. */
  121. public VaadinRequest getSecondRequest() {
  122. return secondRequest;
  123. }
  124. @Override
  125. public Locale getLocale() {
  126. return secondRequest.getLocale();
  127. }
  128. @Override
  129. public String getRemoteAddr() {
  130. return secondRequest.getRemoteAddr();
  131. }
  132. @Override
  133. public boolean isSecure() {
  134. return secondRequest.isSecure();
  135. }
  136. @Override
  137. public String getHeader(String name) {
  138. return secondRequest.getHeader(name);
  139. }
  140. @Override
  141. public VaadinService getService() {
  142. return secondRequest.getService();
  143. }
  144. @Override
  145. public String getContextPath() {
  146. return secondRequest.getContextPath();
  147. }
  148. }