1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
84
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 }