001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.gravity;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    
026    import javax.servlet.ServletConfig;
027    import javax.servlet.ServletException;
028    import javax.servlet.http.HttpServlet;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    
032    import flex.messaging.messages.CommandMessage;
033    import flex.messaging.messages.Message;
034    
035    /**
036     * @author Franck WOLFF
037     */
038    public class AbstractGravityServlet extends HttpServlet {
039    
040            ///////////////////////////////////////////////////////////////////////////
041            // Fields.
042            
043            private static final long serialVersionUID = 1L;
044    
045            ///////////////////////////////////////////////////////////////////////////
046            // Initialization.
047    
048            public void init(ServletConfig config, ChannelFactory channelFactory) throws ServletException {
049                    super.init(config);
050                    
051                    GravityServletUtil.init(config, channelFactory);
052            }
053    
054            ///////////////////////////////////////////////////////////////////////////
055            // Connect messages management (request attribute).
056            
057            public static void setConnectMessage(HttpServletRequest request, Message connect) {
058                    GravityServletUtil.setConnectMessage(request, connect);
059            }
060            
061            public static CommandMessage getConnectMessage(HttpServletRequest request) {
062                    return GravityServletUtil.getConnectMessage(request);
063            }
064            
065            public static void removeConnectMessage(HttpServletRequest request) {
066                    GravityServletUtil.removeConnectMessage(request);
067            }
068    
069            ///////////////////////////////////////////////////////////////////////////
070            // Long polling timeout.
071            
072            protected long getLongPollingTimeout() {
073                    return GravityServletUtil.getLongPollingTimeout(getServletContext());
074            }
075    
076            ///////////////////////////////////////////////////////////////////////////
077            // AMF (de)serialization methods.
078            
079            protected Gravity initializeRequest(Gravity gravity, HttpServletRequest request, HttpServletResponse response) {
080                    return GravityServletUtil.initializeRequest(getServletConfig(), gravity, request, response);
081            }
082    
083            protected Message[] deserialize(Gravity gravity, HttpServletRequest request) throws ClassNotFoundException, IOException {
084                    return GravityServletUtil.deserialize(gravity, request);
085            }
086            
087            protected Message[] deserialize(Gravity gravity, HttpServletRequest request, InputStream is) throws ClassNotFoundException, IOException {
088                    return GravityServletUtil.deserialize(gravity, request, is);
089            }
090            
091            protected void serialize(Gravity gravity, HttpServletResponse response, Message[] messages) throws IOException {
092                    GravityServletUtil.serialize(gravity, response, messages);
093            }
094            
095            protected void cleanupRequest(HttpServletRequest request) {
096                    GravityServletUtil.cleanupRequest(request);
097            }
098            
099            ///////////////////////////////////////////////////////////////////////////
100            // Unsupported HTTP methods.
101    
102            @Override
103            protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
104                    throw new ServletException("Unsupported operation: " + req.getMethod());
105            }
106    
107            @Override
108            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
109                    throw new ServletException("Unsupported operation: " + req.getMethod());
110            }
111    
112            @Override
113            protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
114                    throw new ServletException("Unsupported operation: " + req.getMethod());
115            }
116    
117            @Override
118            protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
119                    throw new ServletException("Unsupported operation: " + req.getMethod());
120            }
121    
122            @Override
123            protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
124                    throw new ServletException("Unsupported operation: " + req.getMethod());
125            }
126    
127            @Override
128            protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
129                    throw new ServletException("Unsupported operation: " + req.getMethod());
130            }
131    }