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.log4j.Logger;
033
034 import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
035 import org.apache.isis.viewer.scimpi.dispatcher.Dispatcher;
036 import org.apache.isis.viewer.scimpi.dispatcher.UserManager;
037 import org.apache.isis.viewer.scimpi.dispatcher.debug.DebugUsers;
038
039 public class DispatcherServlet extends HttpServlet {
040 private static final long serialVersionUID = 1L;
041 private static final Logger LOG = Logger.getLogger(DispatcherServlet.class);
042 private Dispatcher dispatcher;
043 private DebugUsers debugUsers;
044
045 @Override
046 protected void doPost(final HttpServletRequest request, final HttpServletResponse response) 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, IOException {
053 LOG.debug("get " + request.getServletPath() + " " + request.getQueryString());
054 process(request, response);
055 }
056
057 private void process(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
058 try {
059 final ServletRequestContext context = new ServletRequestContext(debugUsers);
060 final HttpSession httpSession = request.getSession(false);
061 // TODO when using version 3.0 of Servlet API use the HttpOnly
062 // setting for improved security
063 if (httpSession != null) {
064 final HashMap<String, Object> data = (HashMap<String, Object>) httpSession.getAttribute("scimpi-context");
065 if (data != null) {
066 context.setSessionData(data);
067 }
068 }
069 context.startRequest(request, response, getServletContext());
070 dispatcher.process(context, request.getServletPath());
071 } catch (final RuntimeException e) {
072 LOG.error("servlet exception", e);
073 throw e;
074 }
075 }
076
077 @Override
078 public void init() throws ServletException {
079 super.init();
080
081 // TODO get directory from servlet parameter
082 ImageLookup.setImageDirectory(getServletContext(), "images");
083
084 debugUsers = new DebugUsers();
085 debugUsers.initialize();
086
087 dispatcher = new Dispatcher();
088 final Enumeration initParameterNames = getInitParameterNames();
089 while (initParameterNames.hasMoreElements()) {
090 final String name = (String) initParameterNames.nextElement();
091 final String value = getInitParameter(name);
092 dispatcher.addParameter(name, value);
093 }
094 final String dir = getServletContext().getRealPath("/WEB-INF");
095 dispatcher.init(dir, debugUsers);
096
097 new UserManager(IsisContext.getAuthenticationManager());
098 }
099 }