1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.genericeventmonitor; |
---|
16 | |
---|
17 | import java.util.EnumSet; |
---|
18 | |
---|
19 | import javax.servlet.DispatcherType; |
---|
20 | |
---|
21 | import org.eclipse.jetty.server.Server; |
---|
22 | import org.eclipse.jetty.servlet.FilterHolder; |
---|
23 | import org.eclipse.jetty.servlet.ServletContextHandler; |
---|
24 | import org.eclipse.jetty.servlet.ServletHolder; |
---|
25 | import org.eclipse.jetty.servlets.CrossOriginFilter; |
---|
26 | |
---|
27 | import de.ugoe.cs.util.console.Console; |
---|
28 | |
---|
29 | /** |
---|
30 | * <p> |
---|
31 | * this is the web server, that receives the events. It is initialized with a port on which it |
---|
32 | * shall listen, as well as a message listener to forward the received messages to. Internally it |
---|
33 | * starts a jetty web server with a single {@link GenericEventMonitorServlet} to receive the |
---|
34 | * events. |
---|
35 | * </p> |
---|
36 | * |
---|
37 | * @author Patrick Harms |
---|
38 | */ |
---|
39 | class GenericEventMonitorServer implements GenericEventMonitorComponent { |
---|
40 | |
---|
41 | /** |
---|
42 | * the port to listen on |
---|
43 | */ |
---|
44 | private int port; |
---|
45 | |
---|
46 | /** |
---|
47 | * the jetty web server used for receiving messages |
---|
48 | */ |
---|
49 | private Server server; |
---|
50 | |
---|
51 | /** |
---|
52 | * the message listener to forward the messages to |
---|
53 | */ |
---|
54 | private GenericEventMonitorMessageListener messageListener; |
---|
55 | |
---|
56 | /** |
---|
57 | * <p> |
---|
58 | * initializes the server with the port to listen on and the message listener to forward |
---|
59 | * the messages to. |
---|
60 | * </p> |
---|
61 | * |
---|
62 | * @param port the port to listen on |
---|
63 | * @param messageListener the message listener to forward the messages to |
---|
64 | */ |
---|
65 | GenericEventMonitorServer(int port, GenericEventMonitorMessageListener messageListener) { |
---|
66 | this.port = port; |
---|
67 | this.messageListener = messageListener; |
---|
68 | } |
---|
69 | |
---|
70 | /* (non-Javadoc) |
---|
71 | * @see de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitorComponent#init() |
---|
72 | */ |
---|
73 | @Override |
---|
74 | public synchronized void init() { |
---|
75 | if (server != null) { |
---|
76 | throw new IllegalStateException("already initialized. First call stop()"); |
---|
77 | } |
---|
78 | |
---|
79 | server = new Server(port); |
---|
80 | |
---|
81 | /* |
---|
82 | // the following code can be used to support SSL directly |
---|
83 | server = new Server(); |
---|
84 | |
---|
85 | SslSocketConnector connector = new SslSocketConnector(); |
---|
86 | connector.setPort(port); |
---|
87 | connector.setKeystore("data/keystore"); |
---|
88 | connector.setPassword("123456"); |
---|
89 | connector.setKeyPassword("123456"); |
---|
90 | connector.setTruststore("data/keystore"); |
---|
91 | connector.setTrustPassword("123456"); |
---|
92 | server.addConnector(connector);*/ |
---|
93 | |
---|
94 | ServletContextHandler root = |
---|
95 | new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS); |
---|
96 | |
---|
97 | GenericEventMonitorServlet servlet = new GenericEventMonitorServlet(messageListener); |
---|
98 | ServletHolder servletHolder = new ServletHolder(servlet); |
---|
99 | root.addServlet(servletHolder, "/*"); |
---|
100 | |
---|
101 | CrossOriginFilter filter = new CrossOriginFilter(); |
---|
102 | FilterHolder filterHolder = new FilterHolder(filter); |
---|
103 | filterHolder.setInitParameter("allowedOrigins", "*"); |
---|
104 | filterHolder.setInitParameter("allowedMethods", "GET,POST"); |
---|
105 | root.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class)); |
---|
106 | } |
---|
107 | |
---|
108 | /* (non-Javadoc) |
---|
109 | * @see de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitorComponent#start() |
---|
110 | */ |
---|
111 | @Override |
---|
112 | public synchronized void start() throws GenericEventMonitorException { |
---|
113 | if (server == null) { |
---|
114 | throw new IllegalStateException("server not initialized yet. First call init()"); |
---|
115 | } |
---|
116 | |
---|
117 | try { |
---|
118 | server.start(); |
---|
119 | } |
---|
120 | catch (Exception e) { |
---|
121 | throw new GenericEventMonitorException("could not start server", e); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | /* (non-Javadoc) |
---|
127 | * @see de.ugoe.cs.autoquest.genericeventmonitor.GenericEventMonitorComponent#stop() |
---|
128 | */ |
---|
129 | @Override |
---|
130 | public synchronized void stop() { |
---|
131 | try { |
---|
132 | if (server != null) { |
---|
133 | server.stop(); |
---|
134 | } |
---|
135 | } |
---|
136 | catch (Exception e) { |
---|
137 | Console.printerrln("could not stop generic event monitor server: " + e.getMessage()); |
---|
138 | Console.logException(e); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|