2 package net.sourceforge.guacamole.net;
5 * Guacamole - Clientless Remote Desktop
6 * Copyright (C) 2010 Michael Jumper
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.
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.
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/>.
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;
31 public abstract class GuacamoleServlet extends HttpServlet {
33 private GuacamoleConfiguration config;
36 public void init() throws ServletException {
38 this.config = new GuacamoleConfiguration();
40 catch (GuacamoleException e) {
41 throw new ServletException(e);
46 protected final void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
48 handleRequest(req, resp);
50 catch (GuacamoleException e) {
51 throw new ServletException(e);
56 protected final void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
58 handleRequest(req, resp);
60 catch (GuacamoleException e) {
61 throw new ServletException(e);
65 private final void handleRequest(HttpServletRequest request, HttpServletResponse response) throws GuacamoleException {
67 HttpSession httpSession = request.getSession(shouldCreateSession());
69 if (httpSession != null) {
70 GuacamoleSession session = config.createSession(httpSession);
71 handleRequest(session, request, response);
74 throw new GuacamoleException("No session");
77 protected abstract void handleRequest(GuacamoleSession session, HttpServletRequest request, HttpServletResponse response) throws GuacamoleException;
79 protected boolean shouldCreateSession() {