Removed native components (now in own repositories)
[guacamole.git] / web / guacamole-common / src / main / java / net / sourceforge / guacamole / net / GuacamoleServlet.java
1
2 package net.sourceforge.guacamole.net;
3
4 /*
5  *  Guacamole - Clientless Remote Desktop
6  *  Copyright (C) 2010  Michael Jumper
7  *
8  *  This program is free software: you can redistribute it and/or modify
9  *  it under the terms of the GNU Affero General Public License as published by
10  *  the Free Software Foundation, either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU Affero General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Affero General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 import java.io.IOException;
23 import javax.servlet.ServletConfig;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
29 import net.sourceforge.guacamole.GuacamoleException;
30
31 public abstract class GuacamoleServlet extends HttpServlet  {
32
33     private GuacamoleConfiguration config;
34
35     @Override
36     public void init() throws ServletException {
37         try {
38             this.config = new GuacamoleConfiguration();
39         }
40         catch (GuacamoleException e) {
41             throw new ServletException(e);
42         }
43     }
44
45     @Override
46     protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
47         try {
48             handleRequest(req, resp);
49         }
50         catch (GuacamoleException e) {
51             throw new ServletException(e);
52         }
53     }
54
55     @Override
56     protected final void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
57         try {
58             handleRequest(req, resp);
59         }
60         catch (GuacamoleException e) {
61             throw new ServletException(e);
62         }
63     }
64
65     private final void handleRequest(HttpServletRequest request, HttpServletResponse response) throws GuacamoleException {
66
67         HttpSession httpSession = request.getSession(shouldCreateSession());
68
69         if (httpSession != null) {
70             GuacamoleSession session = config.createSession(httpSession);
71             handleRequest(session, request, response);
72         }
73         else
74             throw new GuacamoleException("No session");
75     }
76
77     protected abstract void handleRequest(GuacamoleSession session, HttpServletRequest request, HttpServletResponse response) throws GuacamoleException;
78
79     protected boolean shouldCreateSession() {
80         return false;
81     }
82
83 }