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.htmlmonitor; |
---|
16 | |
---|
17 | import org.eclipse.jetty.servlets.CrossOriginFilter; |
---|
18 | import org.mortbay.jetty.Handler; |
---|
19 | import org.mortbay.jetty.Server; |
---|
20 | import org.mortbay.jetty.servlet.Context; |
---|
21 | import org.mortbay.jetty.servlet.FilterHolder; |
---|
22 | import org.mortbay.jetty.servlet.ServletHolder; |
---|
23 | |
---|
24 | import de.ugoe.cs.util.console.Console; |
---|
25 | |
---|
26 | /** |
---|
27 | * <p> |
---|
28 | * this is the web server, that receives the client messages. It also provides the java script |
---|
29 | * that is used by the client via the URL /script/autoquest-htmlmonitor.js. It is initialized |
---|
30 | * with a port on which it shall listen, as well as a message listener to forward the received |
---|
31 | * messages to. Internally it starts a jetty web server with a single {@link HtmlMonitorServlet} |
---|
32 | * to receive the messages as well as a . |
---|
33 | * </p> |
---|
34 | * |
---|
35 | * @author Patrick Harms |
---|
36 | */ |
---|
37 | class HtmlMonitorServer implements HtmlMonitorComponent { |
---|
38 | |
---|
39 | /** |
---|
40 | * the port to listen on |
---|
41 | */ |
---|
42 | private int port; |
---|
43 | |
---|
44 | /** |
---|
45 | * the jetty web server used for receiving messages |
---|
46 | */ |
---|
47 | private Server server; |
---|
48 | |
---|
49 | /** |
---|
50 | * the message listener to forward the messages to |
---|
51 | */ |
---|
52 | private HtmlMonitorMessageListener messageListener; |
---|
53 | |
---|
54 | /** |
---|
55 | * <p> |
---|
56 | * initializes the server with the port to listen on and the message listener to forward |
---|
57 | * the messages to. |
---|
58 | * </p> |
---|
59 | * |
---|
60 | * @param port the port to listen on |
---|
61 | * @param messageListener the message listener to forward the messages to |
---|
62 | */ |
---|
63 | HtmlMonitorServer(int port, HtmlMonitorMessageListener messageListener) { |
---|
64 | this.port = port; |
---|
65 | this.messageListener = messageListener; |
---|
66 | } |
---|
67 | |
---|
68 | /* (non-Javadoc) |
---|
69 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init() |
---|
70 | */ |
---|
71 | @Override |
---|
72 | public synchronized void init() { |
---|
73 | if (server != null) { |
---|
74 | throw new IllegalStateException("already initialized. First call stop()"); |
---|
75 | } |
---|
76 | |
---|
77 | server = new Server(port); |
---|
78 | |
---|
79 | /* |
---|
80 | // the following code can be used to support SSL directly |
---|
81 | server = new Server(); |
---|
82 | |
---|
83 | SslSocketConnector connector = new SslSocketConnector(); |
---|
84 | connector.setPort(port); |
---|
85 | connector.setKeystore("data/keystore"); |
---|
86 | connector.setPassword("123456"); |
---|
87 | connector.setKeyPassword("123456"); |
---|
88 | connector.setTruststore("data/keystore"); |
---|
89 | connector.setTrustPassword("123456"); |
---|
90 | server.addConnector(connector);*/ |
---|
91 | |
---|
92 | Context root = new Context(server, "/", Context.SESSIONS); |
---|
93 | |
---|
94 | HtmlMonitorServlet servlet = new HtmlMonitorServlet(messageListener); |
---|
95 | ServletHolder servletHolder = new ServletHolder(servlet); |
---|
96 | root.addServlet(servletHolder, "/*"); |
---|
97 | |
---|
98 | CrossOriginFilter filter = new CrossOriginFilter(); |
---|
99 | FilterHolder filterHolder = new FilterHolder(filter); |
---|
100 | filterHolder.setInitParameter("allowedOrigins", "*"); |
---|
101 | filterHolder.setInitParameter("allowedMethods", "GET,POST"); |
---|
102 | root.addFilter(filterHolder, "/*", Handler.ALL); |
---|
103 | } |
---|
104 | |
---|
105 | /* (non-Javadoc) |
---|
106 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start() |
---|
107 | */ |
---|
108 | @Override |
---|
109 | public synchronized void start() throws HtmlMonitorException { |
---|
110 | if (server == null) { |
---|
111 | throw new IllegalStateException("server not initialized yet. First call init()"); |
---|
112 | } |
---|
113 | |
---|
114 | try { |
---|
115 | server.start(); |
---|
116 | } |
---|
117 | catch (Exception e) { |
---|
118 | throw new HtmlMonitorException("could not start server", e); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | /* (non-Javadoc) |
---|
124 | * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop() |
---|
125 | */ |
---|
126 | @Override |
---|
127 | public synchronized void stop() { |
---|
128 | try { |
---|
129 | if (server != null) { |
---|
130 | server.stop(); |
---|
131 | } |
---|
132 | } |
---|
133 | catch (Exception e) { |
---|
134 | Console.printerrln("could not stop HTML monitor server: " + e.getMessage()); |
---|
135 | Console.logException(e); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | } |
---|