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.0KB

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