2121import lombok .extern .slf4j .Slf4j ;
2222import org .eclipse .jetty .http .HttpScheme ;
2323import org .eclipse .jetty .http .HttpVersion ;
24- import org .eclipse .jetty .server .Connector ;
2524import org .eclipse .jetty .server .ForwardedRequestCustomizer ;
2625import org .eclipse .jetty .server .HttpConfiguration ;
2726import org .eclipse .jetty .server .HttpConnectionFactory ;
3332import org .eclipse .jetty .util .thread .QueuedThreadPool ;
3433import org .eclipse .jetty .util .thread .ScheduledExecutorScheduler ;
3534
36- import java .net .DatagramSocket ;
37- import java .net .Inet4Address ;
38- import java .net .InetAddress ;
39- import java .net .InetSocketAddress ;
40- import java .net .NetworkInterface ;
41- import java .net .Socket ;
42- import java .util .ArrayList ;
43- import java .util .Arrays ;
44- import java .util .Collection ;
45- import java .util .Collections ;
46- import java .util .Enumeration ;
47- import java .util .HashSet ;
48- import java .util .List ;
49- import java .util .Set ;
5035import java .util .concurrent .TimeUnit ;
51- import java .util .stream .Collectors ;
5236
5337import static de .rwth .idsg .steve .SteveConfiguration .CONFIG ;
5438
@@ -158,7 +142,6 @@ public void start() throws Exception {
158142 if (server != null ) {
159143 server .start ();
160144 steveAppContext .configureWebSocket ();
161- populateEndpointInfo ();
162145 }
163146 }
164147
@@ -176,124 +159,4 @@ public void stop() throws Exception {
176159 server .stop ();
177160 }
178161 }
179-
180- public boolean isStarted () {
181- return server != null && server .isStarted ();
182- }
183-
184- public void populateEndpointInfo () {
185- List <String > list = getConnectorPathList ();
186-
187- // EndpointInfo info = EndpointInfo.INSTANCE;
188- //
189- // info.getWebInterface().setData(buildList(list, false));
190- // info.getOcppSoap().setData(buildList(list, false));
191- // info.getOcppWebSocket().setData(buildList(list, true));
192- }
193-
194- private List <String > getConnectorPathList () {
195- if (server == null ) {
196- return Collections .emptyList ();
197- }
198-
199- return Arrays .stream (server .getConnectors ())
200- .map (JettyServer ::getConnectorPath )
201- .flatMap (Collection ::stream )
202- .collect (Collectors .toList ());
203- }
204-
205- private static List <String > getConnectorPath (Connector c ) {
206- ServerConnector sc = (ServerConnector ) c ;
207-
208- final String prefix ;
209- if (sc .getDefaultConnectionFactory () instanceof SslConnectionFactory ) {
210- prefix = "https" ;
211- } else {
212- prefix = "http" ;
213- }
214-
215- Set <String > ips = new HashSet <>();
216- String host = sc .getHost ();
217- if (host == null || host .equals ("0.0.0.0" )) {
218- ips .addAll (getPossibleIpAddresses ());
219- } else {
220- ips .add (host );
221- }
222-
223- String layout = "%s://%s:%d" + CONFIG .getContextPath ();
224-
225- return ips .stream ()
226- .map (k -> String .format (layout , prefix , k , sc .getPort ()))
227- .collect (Collectors .toList ());
228- }
229-
230- private List <String > buildList (List <String > list , boolean replaceHttp ) {
231- return list .stream ()
232- .map (s -> getElementPrefix (s , replaceHttp ))
233- .collect (Collectors .toList ());
234- }
235-
236- private String getElementPrefix (String str , boolean replaceHttp ) {
237- if (replaceHttp ) {
238- return str .replaceFirst ("http" , "ws" );
239- } else {
240- return str ;
241- }
242- }
243-
244- /**
245- * Uses different APIs to find out the IP of this machine.
246- */
247- private static List <String > getPossibleIpAddresses () {
248- final String host = "treibhaus.informatik.rwth-aachen.de" ;
249- final List <String > ips = new ArrayList <>();
250-
251- try {
252- ips .add (InetAddress .getLocalHost ().getHostAddress ());
253- } catch (Exception e ) {
254- // fail silently
255- }
256-
257- try {
258- Socket socket = new Socket ();
259- socket .connect (new InetSocketAddress (host , 80 ));
260- ips .add (socket .getLocalAddress ().getHostAddress ());
261- } catch (Exception e ) {
262- // fail silently
263- }
264-
265- // https://stackoverflow.com/a/38342964
266- try (DatagramSocket socket = new DatagramSocket ()) {
267- socket .connect (InetAddress .getByName ("8.8.8.8" ), 10002 );
268- ips .add (socket .getLocalAddress ().getHostAddress ());
269- } catch (Exception e ) {
270- // fail silently
271- }
272-
273- // https://stackoverflow.com/a/20418809
274- try {
275- for (Enumeration <NetworkInterface > ifaces = NetworkInterface .getNetworkInterfaces (); ifaces .hasMoreElements ();) {
276- NetworkInterface iface = ifaces .nextElement ();
277- for (Enumeration <InetAddress > inetAddrs = iface .getInetAddresses (); inetAddrs .hasMoreElements ();) {
278- InetAddress inetAddr = inetAddrs .nextElement ();
279- if (!inetAddr .isLoopbackAddress () && (inetAddr instanceof Inet4Address )) {
280- ips .add (inetAddr .getHostAddress ());
281- }
282- }
283- }
284-
285- } catch (Exception e ) {
286- // fail silently
287- }
288-
289- ips .removeIf ("0.0.0.0" ::equals );
290-
291- if (ips .isEmpty ()) {
292- // Well, we failed to read from system, fall back to main.properties.
293- // Better than nothing
294- ips .add (CONFIG .getJetty ().getServerHost ());
295- }
296-
297- return ips ;
298- }
299162}
0 commit comments