001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *
010     *        http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    
020    package org.apache.isis.viewer.scimpi.servlet;
021    
022    import java.io.IOException;
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    
026    import javax.servlet.ServletException;
027    import javax.servlet.http.HttpServlet;
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    import javax.servlet.http.HttpSession;
031    
032    import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
033    import org.apache.isis.viewer.scimpi.dispatcher.Dispatcher;
034    import org.apache.isis.viewer.scimpi.dispatcher.UserManager;
035    import org.apache.isis.viewer.scimpi.dispatcher.debug.DebugUsers;
036    import org.apache.log4j.Logger;
037    
038    public class DispatcherServlet extends HttpServlet {
039        private static final long serialVersionUID = 1L;
040        private static final Logger LOG = Logger.getLogger(DispatcherServlet.class);
041        private Dispatcher dispatcher;
042        private DebugUsers debugUsers;
043    
044        @Override
045        protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
046            throws ServletException, IOException {
047            LOG.debug("post " + request.getServletPath() + "  " + request.getQueryString());
048            process(request, response);
049        }
050    
051        @Override
052        protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
053            IOException {
054            LOG.debug("get  " + request.getServletPath() + "  " + request.getQueryString());
055            process(request, response);
056        }
057    
058        private void process(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
059            IOException {
060            try {
061                final ServletRequestContext context = new ServletRequestContext(debugUsers);
062                final HttpSession httpSession = request.getSession(false);
063                // TODO when using version 3.0 of Servlet API use the HttpOnly setting for improved security
064                if (httpSession != null) {
065                    final HashMap<String, Object> data =
066                        (HashMap<String, Object>) httpSession.getAttribute("scimpi-context");
067                    if (data != null) {
068                        context.setSessionData(data);
069                    }
070                }
071                context.startRequest(request, response, getServletContext());
072                dispatcher.process(context, request.getServletPath());
073            } catch (final RuntimeException e) {
074                LOG.error("servlet exception", e);
075                throw e;
076            }
077        }
078    
079        @Override
080        public void init() throws ServletException {
081            super.init();
082    
083            // TODO get directory from servlet parameter
084            ImageLookup.setImageDirectory(getServletContext(), "images");
085            
086            debugUsers = new DebugUsers();
087            debugUsers.initialize();
088            
089            dispatcher = new Dispatcher();
090            final Enumeration initParameterNames = getInitParameterNames();
091            while (initParameterNames.hasMoreElements()) {
092                final String name = (String) initParameterNames.nextElement();
093                final String value = getInitParameter(name);
094                dispatcher.addParameter(name, value);
095            }
096            final String dir = getServletContext().getRealPath("/WEB-INF");
097            dispatcher.init(dir, debugUsers);
098    
099            new UserManager(IsisContext.getAuthenticationManager());
100        }
101    }