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

Last change on this file since 1430 was 1364, checked in by pharms, 10 years ago
  • corrected formating
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 =
96            new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
97
98        HtmlMonitorServlet servlet = new HtmlMonitorServlet(messageListener);
99        ServletHolder servletHolder = new ServletHolder(servlet);
100        root.addServlet(servletHolder, "/*");
101       
102        CrossOriginFilter filter = new CrossOriginFilter();
103        FilterHolder filterHolder = new FilterHolder(filter);
104        filterHolder.setInitParameter("allowedOrigins", "*");
105        filterHolder.setInitParameter("allowedMethods", "GET,POST");
106        root.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class));
107    }
108
109    /* (non-Javadoc)
110     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#start()
111     */
112    @Override
113    public synchronized void start() throws HtmlMonitorException {
114        if (server == null) {
115            throw new IllegalStateException("server not initialized yet. First call init()");
116        }
117       
118        try {
119            server.start();
120        }
121        catch (Exception e) {
122            throw new HtmlMonitorException("could not start server", e);
123        }
124    }
125
126
127    /* (non-Javadoc)
128     * @see de.ugoe.cs.autoquest.htmlmonitor.HtmlMonitorComponent#stop()
129     */
130    @Override
131    public synchronized void stop() {
132        try {
133            if (server != null) {
134                server.stop();
135            }
136        }
137        catch (Exception e) {
138            Console.printerrln("could not stop HTML monitor server: " + e.getMessage());
139            Console.logException(e);
140        }
141    }
142
143}
Note: See TracBrowser for help on using the repository browser.