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.

Parameters.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.tests;
  17. import java.io.IOException;
  18. import java.net.URL;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import com.vaadin.server.ExternalResource;
  22. import com.vaadin.server.RequestHandler;
  23. import com.vaadin.server.VaadinRequest;
  24. import com.vaadin.server.VaadinResponse;
  25. import com.vaadin.server.VaadinSession;
  26. import com.vaadin.ui.Label;
  27. import com.vaadin.ui.LegacyWindow;
  28. import com.vaadin.ui.Link;
  29. import com.vaadin.ui.Panel;
  30. import com.vaadin.ui.VerticalLayout;
  31. import com.vaadin.v7.ui.Table;
  32. /**
  33. * This is a demonstration of how URL parameters can be recieved and handled.
  34. * Parameters and URL:s can be received trough the windows by registering
  35. * URIHandler and ParameterHandler classes window.
  36. *
  37. * @since 3.1.1
  38. */
  39. public class Parameters extends com.vaadin.server.LegacyApplication
  40. implements RequestHandler {
  41. private final Label context = new Label();
  42. private final Label relative = new Label();
  43. private final Table params = new Table();
  44. @Override
  45. public void init() {
  46. final LegacyWindow main = new LegacyWindow("Parameters demo");
  47. setMainWindow(main);
  48. // This class acts both as URI handler and parameter handler
  49. VaadinSession.getCurrent().addRequestHandler(this);
  50. final VerticalLayout layout = new VerticalLayout();
  51. final Label info = new Label("To test URI and Parameter Handlers, "
  52. + "add get parameters to URL. For example try examples below: ");
  53. info.setCaption("Usage info");
  54. layout.addComponent(info);
  55. try {
  56. final URL u1 = new URL(getURL(), "test/uri?test=1&test=2");
  57. final URL u2 = new URL(getURL(), "foo/bar?mary=john&count=3");
  58. layout.addComponent(
  59. new Link(u1.toString(), new ExternalResource(u1)));
  60. layout.addComponent(new Label("Or this: "));
  61. layout.addComponent(
  62. new Link(u2.toString(), new ExternalResource(u2)));
  63. } catch (final Exception e) {
  64. System.out.println(
  65. "Couldn't get hostname for this machine: " + e.toString());
  66. e.printStackTrace();
  67. }
  68. // URI
  69. final VerticalLayout panel1Layout = new VerticalLayout();
  70. panel1Layout.setMargin(true);
  71. final Panel panel1 = new Panel("URI Handler", panel1Layout);
  72. context.setCaption("Last URI handler context");
  73. panel1Layout.addComponent(context);
  74. relative.setCaption("Last relative URI");
  75. panel1Layout.addComponent(relative);
  76. layout.addComponent(panel1);
  77. params.addContainerProperty("Key", String.class, "");
  78. params.addContainerProperty("Value", String.class, "");
  79. final VerticalLayout panel2Layout = new VerticalLayout();
  80. panel2Layout.setMargin(true);
  81. final Panel panel2 = new Panel("Parameter Handler", panel2Layout);
  82. params.setSizeFull();
  83. params.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_ID);
  84. panel2Layout.addComponent(params);
  85. layout.addComponent(panel2);
  86. // expand parameter panel and its table
  87. layout.setExpandRatio(panel2, 1);
  88. layout.setMargin(true);
  89. layout.setSpacing(true);
  90. main.setContent(layout);
  91. }
  92. @Override
  93. public boolean handleRequest(VaadinSession session, VaadinRequest request,
  94. VaadinResponse response) throws IOException {
  95. context.setValue("Context not available");
  96. relative.setValue(request.getPathInfo());
  97. params.removeAllItems();
  98. Map<String, String[]> parameters = request.getParameterMap();
  99. for (final Iterator<String> i = parameters.keySet().iterator(); i
  100. .hasNext();) {
  101. final String name = i.next();
  102. final String[] values = parameters.get(name);
  103. String v = "";
  104. for (int j = 0; j < values.length; j++) {
  105. if (v.length() > 0) {
  106. v += ", ";
  107. }
  108. v += "'" + values[j] + "'";
  109. }
  110. params.addItem(new Object[] { name, v }, name);
  111. }
  112. return false;
  113. }
  114. }