Package: DefaultResourceServer
DefaultResourceServer
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
DefaultResourceServer() |
|
|
|
|
|
||||||||||||||||||||
absoluteUrl(String) |
|
|
|
|
|
||||||||||||||||||||
afterPropertiesSet() |
|
|
|
|
|
||||||||||||||||||||
destroy() |
|
|
|
|
|
||||||||||||||||||||
findWebResources() |
|
|
|
|
|
||||||||||||||||||||
getNonLoopbackIPv4Address() |
|
|
|
|
|
||||||||||||||||||||
lambda$findWebResources$0(Resource) |
|
|
|
|
|
||||||||||||||||||||
lambda$findWebResources$1(int) |
|
|
|
|
|
||||||||||||||||||||
onPowerOffNotification(PowerOffNotification) |
|
|
|
|
|
||||||||||||||||||||
onPowerOnNotification(PowerOnNotification) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.rest.impl.server;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import java.util.EnumSet;
32: import java.util.Enumeration;
33: import java.util.stream.Stream;
34: import java.io.IOException;
35: import java.net.Inet4Address;
36: import java.net.InetAddress;
37: import java.net.InetSocketAddress;
38: import java.net.NetworkInterface;
39: import java.net.SocketException;
40: import javax.servlet.DispatcherType;
41: import org.springframework.context.ApplicationContext;
42: import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
43: import org.springframework.core.io.support.ResourcePatternResolver;
44: import org.springframework.web.servlet.DispatcherServlet;
45: import org.eclipse.jetty.server.Server;
46: import org.eclipse.jetty.server.ServerConnector;
47: import org.eclipse.jetty.servlet.DefaultServlet;
48: import org.eclipse.jetty.servlet.FilterHolder;
49: import org.eclipse.jetty.servlet.ServletContextHandler;
50: import org.eclipse.jetty.servlet.ServletHolder;
51: import org.eclipse.jetty.util.resource.Resource;
52: import org.eclipse.jetty.util.resource.ResourceCollection;
53: import it.tidalwave.util.annotation.VisibleForTesting;
54: import it.tidalwave.messagebus.annotation.ListensTo;
55: import it.tidalwave.messagebus.annotation.SimpleMessageSubscriber;
56: import it.tidalwave.bluemarine2.message.PowerOffNotification;
57: import it.tidalwave.bluemarine2.message.PowerOnNotification;
58: import it.tidalwave.bluemarine2.rest.spi.ResourceServer;
59: import lombok.extern.slf4j.Slf4j;
60: import static it.tidalwave.util.FunctionalCheckedExceptionWrappers.*;
61:
62: /***********************************************************************************************************************
63: *
64: * @author Fabrizio Giudici
65: *
66: **********************************************************************************************************************/
67: @SimpleMessageSubscriber @Slf4j
68: public class DefaultResourceServer implements ResourceServer
69: {
70: private String ipAddress = "";
71:
72: private int port;
73:
74: private Server server;
75:
76: @Inject
77: private ApplicationContext applicationContext;
78:
79: /*******************************************************************************************************************
80: *
81: * {@inheritDoc}
82: *
83: ******************************************************************************************************************/
84: @Override @Nonnull
85: public String absoluteUrl (@Nonnull final String type)
86: {
87: return String.format("http://%s:%d/%s", ipAddress, port, type);
88: }
89:
90: /*******************************************************************************************************************
91: *
92: *
93: ******************************************************************************************************************/
94: @VisibleForTesting
95: public void onPowerOnNotification (@ListensTo @Nonnull final PowerOnNotification notification)
96: throws Exception
97: {
98: log.info("onPowerOnNotification({})", notification);
99: ipAddress = getNonLoopbackIPv4Address().getHostAddress();
100: server = new Server(InetSocketAddress.createUnresolved(ipAddress, Integer.getInteger("port", 0)));
101:
102: final ServletContextHandler servletContext = new ServletContextHandler();
103: servletContext.setBaseResource(new ResourceCollection(findWebResources()));
104: log.info("RESOURCE BASE: {}", servletContext.getResourceBase());
105: servletContext.setContextPath("/");
106: servletContext.setWelcomeFiles(new String[] { "index.xhtml" });
107: final DelegateWebApplicationContext wac = new DelegateWebApplicationContext(applicationContext, servletContext.getServletContext());
108: servletContext.addServlet(new ServletHolder("spring", new DispatcherServlet(wac)), "/rest/*");
109: servletContext.addServlet(new ServletHolder("default", new DefaultServlet()), "/*");
110: servletContext.addFilter(new FilterHolder(new LoggingFilter()), "/*", EnumSet.allOf(DispatcherType.class));
111: server.setHandler(servletContext);
112:
113: server.start();
114: port = ((ServerConnector)server.getConnectors()[0]).getLocalPort();
115: log.info(">>>> resource server jetty started at {}:{} ", ipAddress, port);
116: }
117:
118: /*******************************************************************************************************************
119: *
120: *
121: ******************************************************************************************************************/
122: @VisibleForTesting public void onPowerOffNotification (@ListensTo @Nonnull final PowerOffNotification notification)
123: throws Exception
124: {
125: log.info("onPowerOffNotification({})", notification);
126: server.stop();
127: server.destroy();
128: }
129:
130: /*******************************************************************************************************************
131: *
132: *
133: *
134: ******************************************************************************************************************/
135: @Nonnull
136: private InetAddress getNonLoopbackIPv4Address()
137: throws SocketException
138: {
139:• for (final Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements() ; )
140: {
141: final NetworkInterface itf = en.nextElement();
142:
143:• if (!itf.getName().startsWith("docker"))
144: {
145:• for (final Enumeration<InetAddress> ee = itf.getInetAddresses(); ee.hasMoreElements() ;)
146: {
147: final InetAddress address = ee.nextElement();
148:
149:• if (!address.isLoopbackAddress() && (address instanceof Inet4Address))
150: {
151: return address;
152: }
153: }
154: }
155: }
156:
157: log.warn("Returning loopback address!");
158: return InetAddress.getLoopbackAddress();
159: }
160:
161: /*******************************************************************************************************************
162: *
163: *
164: ******************************************************************************************************************/
165: @Nonnull
166: private Resource[] findWebResources()
167: throws IOException
168: {
169: final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
170: final ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(classLoader);
171: return Stream.of(resolver.getResources("classpath*:/webapp"))
172: .map(_f(x -> Resource.newResource(x.getURI())))
173: .toArray(Resource[]::new);
174: }
175: }