001: package org.mandarax.jdbc.server.http;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.io.*;
022: import javax.servlet.*;
023: import javax.servlet.http.*;
024: import org.mandarax.jdbc.server.*;
025: import org.mandarax.jdbc.rpc.*;
026:
027: /**
028: * This servlet is the gateway to the server part of the mandarax jdbc driver.
029: * A ServerFacade is attached to the session.
030: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
031: * @version 3.3.2 <29 December 2004>
032: * @since 3.0
033: */
034:
035: public class Server extends HttpServlet {
036: private static final String FACADE = "facade";
037: private static final String SERIALIZER = "serializer";
038:
039: /**
040: * Initializes the servlet.
041: */
042: public void init(ServletConfig config) throws ServletException {
043: super .init(config);
044: }
045:
046: /**
047: * Destroys the servlet.
048: */
049: public void destroy() {
050: }
051:
052: /**
053: * Handles the HTTP <code>GET</code> method.
054: * @param request servlet request
055: * @param response servlet response
056: */
057: protected void doGet(HttpServletRequest request,
058: HttpServletResponse response) throws ServletException,
059: IOException {
060: // this is useful to test throws server from a browser
061: response.setContentType("text/plain");
062: PrintWriter out = response.getWriter();
063: out.print("Mandarax JDBC Server ok");
064: out.close();
065: }
066:
067: /**
068: * Handles the HTTP <code>POST</code> method.
069: * @param request servlet request
070: * @param response servlet response
071: */
072: protected void doPost(HttpServletRequest request,
073: HttpServletResponse response) throws ServletException,
074: IOException {
075:
076: OutputStream out = response.getOutputStream();
077: HttpSession session = request.getSession();
078: ServerFacade facade = (ServerFacade) session
079: .getAttribute(FACADE);
080: if (facade == null) {
081: facade = new WebServerFacade(this .getServletContext());
082: session.setAttribute(FACADE, facade);
083: }
084:
085: // in this version, the xml serializer is hard coded !
086: // TODO make this configurable
087: Serializer serializer = (Serializer) session
088: .getAttribute(SERIALIZER);
089: if (serializer == null) {
090: serializer = new XMLSerializer();
091: session.setAttribute(SERIALIZER, serializer);
092: }
093: response.setContentType(serializer.getContentType());
094:
095: // deserialize call
096: InputStream in = request.getInputStream();
097: Call call = (Call) serializer.read(in);
098:
099: // perform call
100: CallResult result = facade.perform(call);
101:
102: // serialize result
103: serializer.write(result, out);
104: // careful: serializer may close stream (e.g. XMLEncoder)
105: try {
106: out.close();
107: } catch (IOException x) {
108: }
109: }
110:
111: /**
112: * Returns a short description of the servlet.
113: */
114: public String getServletInfo() {
115: return "The Mandarax JDBC via Http Server";
116: }
117:
118: }
|