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