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.

BootstrapListenerCode.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2012 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.minitutorials.v7b1;
  17. import java.util.List;
  18. import javax.portlet.PortletException;
  19. import javax.servlet.ServletException;
  20. import org.jsoup.nodes.Comment;
  21. import org.jsoup.nodes.Element;
  22. import org.jsoup.nodes.Node;
  23. import org.jsoup.parser.Tag;
  24. import com.vaadin.server.BootstrapFragmentResponse;
  25. import com.vaadin.server.BootstrapListener;
  26. import com.vaadin.server.BootstrapPageResponse;
  27. import com.vaadin.server.ServiceException;
  28. import com.vaadin.server.SessionInitEvent;
  29. import com.vaadin.server.SessionInitListener;
  30. import com.vaadin.server.VaadinPortlet;
  31. import com.vaadin.server.VaadinServlet;
  32. public class BootstrapListenerCode {
  33. public static BootstrapListener listener = new BootstrapListener() {
  34. @Override
  35. public void modifyBootstrapPage(BootstrapPageResponse response) {
  36. response.getDocument().body()
  37. .appendChild(new Comment("Powered by Vaadin!"));
  38. response.setHeader("X-Powered-By", "Vaadin 7");
  39. }
  40. @Override
  41. public void modifyBootstrapFragment(
  42. BootstrapFragmentResponse response) {
  43. // Wrap the fragment in a custom div element
  44. Element myDiv = new Element(Tag.valueOf("div"), "");
  45. List<Node> nodes = response.getFragmentNodes();
  46. for (Node node : nodes) {
  47. myDiv.appendChild(node);
  48. }
  49. nodes.clear();
  50. nodes.add(myDiv);
  51. }
  52. };
  53. }
  54. class MyVaadinServlet extends VaadinServlet {
  55. @Override
  56. protected void servletInitialized() throws ServletException {
  57. super.servletInitialized();
  58. getService().addSessionInitListener(new SessionInitListener() {
  59. @Override
  60. public void sessionInit(SessionInitEvent event)
  61. throws ServiceException {
  62. event.getSession()
  63. .addBootstrapListener(BootstrapListenerCode.listener);
  64. }
  65. });
  66. }
  67. }
  68. // Or...
  69. class MyVaadinPortlet extends VaadinPortlet {
  70. @Override
  71. protected void portletInitialized() throws PortletException {
  72. super.portletInitialized();
  73. getService().addSessionInitListener(new SessionInitListener() {
  74. @Override
  75. public void sessionInit(SessionInitEvent event)
  76. throws ServiceException {
  77. event.getSession()
  78. .addBootstrapListener(BootstrapListenerCode.listener);
  79. }
  80. });
  81. }
  82. }