Server.java :  » Rule-Engine » Mandarax » org » mandarax » jdbc » server » http » Java Open Source

Java Open Source » Rule Engine » Mandarax 
Mandarax » org » mandarax » jdbc » server » http » Server.java
package org.mandarax.jdbc.server.http;

/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.mandarax.jdbc.server.*;
import org.mandarax.jdbc.rpc.*;

/**
 * This servlet is the gateway to the server part of the mandarax jdbc driver.
 * A ServerFacade is attached to the session. 
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.2 <29 December 2004>
 * @since 3.0
 */

public class Server extends HttpServlet {
    private static final String FACADE = "facade";
    private static final String SERIALIZER = "serializer";
    
    /** 
     * Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);        
    }
    
    /** 
     * Destroys the servlet.
     */
    public void destroy() {
    }
      
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // this is useful to test throws server from a browser
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        out.print("Mandarax JDBC Server ok");
        out.close();
    }
    
    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        OutputStream out = response.getOutputStream();
        HttpSession session = request.getSession();
        ServerFacade facade = (ServerFacade)session.getAttribute(FACADE);
        if (facade==null) {
            facade = new WebServerFacade(this.getServletContext());
            session.setAttribute(FACADE, facade);
        }
        
        
        // in this version, the xml serializer is hard coded !
        // TODO make this configurable
        Serializer serializer = (Serializer)session.getAttribute(SERIALIZER);
        if (serializer==null) {
            serializer = new XMLSerializer();
            session.setAttribute(SERIALIZER,serializer);
        }
        response.setContentType(serializer.getContentType());
        
        // deserialize call
        InputStream in = request.getInputStream();
        Call call = (Call)serializer.read(in);
        
        // perform call
        CallResult result = facade.perform(call);
        
        // serialize result
        serializer.write(result,out);
        // careful: serializer may close stream (e.g. XMLEncoder)
        try {            
            out.close();
        }
        catch (IOException x) {}
    }
    
    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "The Mandarax JDBC via Http Server";
    }
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.