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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.util.Collections;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.Locale;
  23. import java.util.Map;
  24. import com.vaadin.Application;
  25. import com.vaadin.external.json.JSONArray;
  26. import com.vaadin.external.json.JSONException;
  27. import com.vaadin.external.json.JSONObject;
  28. /**
  29. * A {@link WrappedRequest} with path and parameters from one request and
  30. * {@link WrappedRequest.BrowserDetails} extracted from another request.
  31. *
  32. * This class is intended to be used for a two request initialization where the
  33. * first request fetches the actual application page and the second request
  34. * contains information extracted from the browser using javascript.
  35. *
  36. */
  37. public class CombinedRequest implements WrappedRequest {
  38. private final WrappedRequest secondRequest;
  39. private Map<String, String[]> parameterMap;
  40. /**
  41. * Creates a new combined request based on the second request and some
  42. * details from the first request.
  43. *
  44. * @param secondRequest
  45. * the second request which will be used as the foundation of the
  46. * combined request
  47. * @throws JSONException
  48. * if the initialParams parameter can not be decoded
  49. */
  50. public CombinedRequest(WrappedRequest secondRequest) throws JSONException {
  51. this.secondRequest = secondRequest;
  52. HashMap<String, String[]> map = new HashMap<String, String[]>();
  53. JSONObject initialParams = new JSONObject(
  54. secondRequest.getParameter("initialParams"));
  55. for (Iterator<?> keys = initialParams.keys(); keys.hasNext();) {
  56. String name = (String) keys.next();
  57. JSONArray jsonValues = initialParams.getJSONArray(name);
  58. String[] values = new String[jsonValues.length()];
  59. for (int i = 0; i < values.length; i++) {
  60. values[i] = jsonValues.getString(i);
  61. }
  62. map.put(name, values);
  63. }
  64. parameterMap = Collections.unmodifiableMap(map);
  65. }
  66. @Override
  67. public String getParameter(String parameter) {
  68. String[] strings = getParameterMap().get(parameter);
  69. if (strings == null || strings.length == 0) {
  70. return null;
  71. } else {
  72. return strings[0];
  73. }
  74. }
  75. @Override
  76. public Map<String, String[]> getParameterMap() {
  77. return parameterMap;
  78. }
  79. @Override
  80. public int getContentLength() {
  81. return secondRequest.getContentLength();
  82. }
  83. @Override
  84. public InputStream getInputStream() throws IOException {
  85. return secondRequest.getInputStream();
  86. }
  87. @Override
  88. public Object getAttribute(String name) {
  89. return secondRequest.getAttribute(name);
  90. }
  91. @Override
  92. public void setAttribute(String name, Object value) {
  93. secondRequest.setAttribute(name, value);
  94. }
  95. @Override
  96. public String getRequestPathInfo() {
  97. return secondRequest.getParameter("initialPath");
  98. }
  99. @Override
  100. public int getSessionMaxInactiveInterval() {
  101. return secondRequest.getSessionMaxInactiveInterval();
  102. }
  103. @Override
  104. public Object getSessionAttribute(String name) {
  105. return secondRequest.getSessionAttribute(name);
  106. }
  107. @Override
  108. public void setSessionAttribute(String name, Object attribute) {
  109. secondRequest.setSessionAttribute(name, attribute);
  110. }
  111. @Override
  112. public String getContentType() {
  113. return secondRequest.getContentType();
  114. }
  115. @Override
  116. public BrowserDetails getBrowserDetails() {
  117. return new BrowserDetails() {
  118. @Override
  119. public String getUriFragment() {
  120. String fragment = secondRequest.getParameter("fr");
  121. if (fragment == null) {
  122. return "";
  123. } else {
  124. return fragment;
  125. }
  126. }
  127. @Override
  128. public String getWindowName() {
  129. return secondRequest.getParameter("wn");
  130. }
  131. @Override
  132. public WebBrowser getWebBrowser() {
  133. WebApplicationContext context = (WebApplicationContext) Application
  134. .getCurrent().getContext();
  135. return context.getBrowser();
  136. }
  137. };
  138. }
  139. /**
  140. * Gets the original second request. This can be used e.g. if a request
  141. * parameter from the second request is required.
  142. *
  143. * @return the original second wrapped request
  144. */
  145. public WrappedRequest getSecondRequest() {
  146. return secondRequest;
  147. }
  148. @Override
  149. public Locale getLocale() {
  150. return secondRequest.getLocale();
  151. }
  152. @Override
  153. public String getRemoteAddr() {
  154. return secondRequest.getRemoteAddr();
  155. }
  156. @Override
  157. public boolean isSecure() {
  158. return secondRequest.isSecure();
  159. }
  160. @Override
  161. public String getHeader(String name) {
  162. return secondRequest.getHeader(name);
  163. }
  164. @Override
  165. public DeploymentConfiguration getDeploymentConfiguration() {
  166. return secondRequest.getDeploymentConfiguration();
  167. }
  168. }