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.

CommunicationError.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2000-2014 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.requesthandlers;
  17. import java.io.IOException;
  18. import java.io.PrintWriter;
  19. import com.vaadin.launcher.ApplicationRunnerServlet;
  20. import com.vaadin.server.CustomizedSystemMessages;
  21. import com.vaadin.server.SystemMessages;
  22. import com.vaadin.server.UIClassSelectionEvent;
  23. import com.vaadin.server.UIProvider;
  24. import com.vaadin.server.VaadinRequest;
  25. import com.vaadin.server.VaadinService;
  26. import com.vaadin.server.VaadinServletRequest;
  27. import com.vaadin.tests.components.AbstractTestUI;
  28. import com.vaadin.ui.Button;
  29. import com.vaadin.ui.Button.ClickEvent;
  30. import com.vaadin.ui.Label;
  31. import com.vaadin.ui.UI;
  32. /**
  33. * Test UI provider to check communication error json object null values.
  34. *
  35. * @author Vaadin Ltd
  36. */
  37. public class CommunicationError extends UIProvider {
  38. @Override
  39. public Class<? extends UI> getUIClass(UIClassSelectionEvent event) {
  40. VaadinServletRequest request = (VaadinServletRequest) event
  41. .getRequest();
  42. String currentUrl = request.getRequestURL().toString();
  43. StringBuilder redirectClass = new StringBuilder(
  44. CommunicationError.class.getSimpleName());
  45. redirectClass.append('$');
  46. redirectClass.append(RedirectedUI.class.getSimpleName());
  47. String restartApplication = "?restartApplication";
  48. if (!currentUrl.contains(restartApplication)) {
  49. redirectClass.append(restartApplication);
  50. }
  51. final String url = currentUrl.replace(
  52. CommunicationError.class.getSimpleName(), redirectClass);
  53. request.setAttribute(
  54. ApplicationRunnerServlet.CUSTOM_SYSTEM_MESSAGES_PROPERTY,
  55. createSystemMessages(url));
  56. return CommunicationErrorUI.class;
  57. }
  58. public static class CommunicationErrorUI extends AbstractTestUI {
  59. @Override
  60. protected void setup(VaadinRequest request) {
  61. Button button = new Button("Send bad request",
  62. new Button.ClickListener() {
  63. @Override
  64. public void buttonClick(ClickEvent event) {
  65. try {
  66. // An unparseable response will cause
  67. // communication error
  68. PrintWriter writer = VaadinService
  69. .getCurrentResponse().getWriter();
  70. writer.write("for(;;)[{FOOBAR}]");
  71. writer.flush();
  72. writer.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. });
  78. addComponent(button);
  79. }
  80. @Override
  81. protected Integer getTicketNumber() {
  82. return 14594;
  83. }
  84. @Override
  85. protected String getTestDescription() {
  86. return "Null values should be wrapped into JsonNull objects.";
  87. }
  88. }
  89. public static class RedirectedUI extends UI {
  90. @Override
  91. protected void init(VaadinRequest request) {
  92. Label label = new Label("redirected");
  93. label.addStyleName("redirected");
  94. setContent(label);
  95. }
  96. }
  97. private SystemMessages createSystemMessages(String url) {
  98. CustomizedSystemMessages messages = new CustomizedSystemMessages();
  99. messages.setCommunicationErrorCaption(null);
  100. messages.setCommunicationErrorMessage(null);
  101. messages.setCommunicationErrorURL(url);
  102. return messages;
  103. }
  104. }