source: trunk/autoquest-htmlmonitor/src/main/java/de/ugoe/cs/autoquest/htmlmonitor/HtmlMonitorServer.java @ 1351

Last change on this file since 1351 was 1351, checked in by pharms, 10 years ago
  • update to newer Jetty version
File size: 4.6 KB
Line 
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
15package de.ugoe.cs.autoquest.htmlmonitor;
16
17import java.util.EnumSet;
18
19import javax.servlet.DispatcherType;
20
21import org.eclipse.jetty.server.Server;
22import org.eclipse.jetty.servlet.FilterHolder;
23import org.eclipse.jetty.servlet.ServletContextHandler;
24import org.eclipse.jetty.servlet.ServletHolder;
25import org.eclipse.jetty.servlets.CrossOriginFilter;
26
27import de.ugoe.cs.util.console.Console;
28
29/**
30 * <p>
31 * this is the web server, that receives the client messages. It also provides the java script
32 * that is used by the client via the URL /script/autoquest-htmlmonitor.js. It is initialized
33 * with a port on which it shall listen, as well as a message listener to forward the received
34 * messages to. Internally it starts a jetty web server with a single {@link HtmlMonitorServlet}
35 * to receive the messages as well as a .
36 * </p>
37 *
38 * @author Patrick Harms
39 */
40class HtmlMonitorServer implements HtmlMonitorComponent {
41   
42    /**
43     * the port to listen on
44     */
45    private int port;
46
47    /**
48     * the jetty web server used for receiving messages
49     */
50    private Server server;
51
52    /**
53     * the message listener to forward the messages to
54     */
55    private HtmlMonitorMessageListener messageListener;
56
57    /**
58     * <p>
59     * initializes the server with the port to listen on and the message listener to forward
60     * the messages to.
61     * </p>
62     *
63     * @param port            the port to listen on
64     * @param messageListener the message listener to forward the messages to
65     */
66    HtmlMonitorServer(int port, HtmlMonitorMessageListener messageListener) {
67        this.port = port;
68        this.messageListener = messageListener;
69    }
70
71    /* (non-Javadoc)
72     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#init()
73     */
74    @Override
75    public synchronized void init() {
76        if (server != null) {
77            throw new IllegalStateException("already initialized. First call stop()");
78        }
79
80        server = new Server(port);
81       
82        /*
83        // the following code can be used to support SSL directly
84        server = new Server();
85       
86        SslSocketConnector connector = new SslSocketConnector();
87        connector.setPort(port);
88        connector.setKeystore("data/keystore");
89        connector.setPassword("123456");
90        connector.setKeyPassword("123456");
91        connector.setTruststore("data/keystore");
92        connector.setTrustPassword("123456");
93        server.addConnector(connector);*/
94
95        ServletContextHandler root = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
96
97        HtmlMonitorServlet servlet = new HtmlMonitorServlet(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.htmlmonitor.HtmlMonitorComponent#start()
110     */
111    @Override
112    public synchronized void start() throws HtmlMonitorException {
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 HtmlMonitorException("could not start server", e);
122        }
123    }
124
125
126    /* (non-Javadoc)
127     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#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 HTML monitor server: " + e.getMessage());
138            Console.logException(e);
139        }
140    }
141
142}
Note: See TracBrowser for help on using the repository browser.