Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CombinedRequest.java 5.3KB

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