View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.poc;
35  
36  import info.magnolia.ui.dev.NoScssCacheVaadinServlet;
37  
38  import java.util.HashMap;
39  import java.util.Map;
40  
41  import javax.servlet.ServletContext;
42  import javax.servlet.ServletRegistration;
43  import javax.servlet.annotation.WebServlet;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import com.vaadin.annotations.Theme;
49  import com.vaadin.annotations.VaadinServletConfiguration;
50  import com.vaadin.server.ExternalResource;
51  import com.vaadin.server.VaadinRequest;
52  import com.vaadin.server.VaadinServlet;
53  import com.vaadin.ui.Link;
54  import com.vaadin.ui.UI;
55  import com.vaadin.ui.VerticalLayout;
56  
57  @Theme("valo")
58  public class TestSandboxUI extends UI {
59      private static final Logger log = LoggerFactory.getLogger(TestSandboxUI.class);
60      private static final String DISPLAY_NAME = "Test Sandbox";
61  
62      @WebServlet(value = { "/*" }, asyncSupported = true, displayName = DISPLAY_NAME)
63      @VaadinServletConfiguration(productionMode = false, ui = TestSandboxUI.class)
64      public static class Servlet extends NoScssCacheVaadinServlet {
65      }
66  
67      @Override
68      protected void init(VaadinRequest request) {
69          setSizeFull();
70          VerticalLayout root = new VerticalLayout();
71          root.setSpacing(false);
72          setContent(root);
73  
74          getVaadinServlets().entrySet().stream()
75                  .filter(entry -> !DISPLAY_NAME.equals(entry.getKey()))
76                  .sorted((e1, e2) -> e1.getKey().compareToIgnoreCase(e2.getKey()))
77                  .forEach(entry -> {
78                      root.addComponent(new Link(entry.getKey(), new ExternalResource(entry.getValue())));
79                  });
80      }
81  
82      /**
83       * @return a map whose keys are the servlets' display-names (or simple class name as fallback),
84       * and whose values are their first servlet mapping (minus eventual trailing wildcard).
85       */
86      private Map<String, String> getVaadinServlets() {
87          Map<String, String> vaadinServlets = new HashMap<>();
88          ServletContext servletContext = VaadinServlet.getCurrent().getServletContext();
89  
90          for (ServletRegistration registration : servletContext.getServletRegistrations().values()) {
91              try {
92                  Class<?> servletClass = Class.forName(registration.getClassName());
93                  if (VaadinServlet.class.isAssignableFrom(servletClass)) {
94                      WebServlet annotation = servletClass.getAnnotation(WebServlet.class);
95                      String displayName = annotation.displayName();
96  
97                      if (displayName.isEmpty()) {
98                          Class<?> declaringClass = servletClass.getDeclaringClass();
99                          if (declaringClass != null && UI.class.isAssignableFrom(declaringClass)) {
100                             Class<? extends UI> uiClass = (Class<? extends UI>) declaringClass;
101                             displayName = uiClass.getSimpleName();
102                         }
103                     }
104 
105                     String mapping = registration.getMappings().iterator().next();
106                     int wildcardIndex = mapping.indexOf("/*");
107                     if (wildcardIndex > 0) {
108                         mapping = mapping.substring(0, wildcardIndex);
109                     }
110                     String url = servletContext.getContextPath() + mapping;
111                     vaadinServlets.put(displayName, url);
112                 }
113             } catch (ClassNotFoundException e) {
114                 log.error("Couldn't get registered servlet class: {}", registration.getClassName(), e);
115             }
116         }
117 
118         return vaadinServlets;
119     }
120 }