Package org.apache.isis.viewer.html.servlet

Source Code of org.apache.isis.viewer.html.servlet.ControllerServlet

/*
*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you under the Apache License, Version 2.0 (the
*  "License"); you may not use this file except in compliance
*  with the License.  You may obtain a copy of the License at
*
*        http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing,
*  software distributed under the License is distributed on an
*  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
*  KIND, either express or implied.  See the License for the
*  specific language governing permissions and limitations
*  under the License.
*/

package org.apache.isis.viewer.html.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import org.apache.isis.core.commons.authentication.AuthenticationSession;
import org.apache.isis.viewer.html.component.Page;
import org.apache.isis.viewer.html.component.html.HtmlComponentFactory;
import org.apache.isis.viewer.html.context.Context;
import org.apache.isis.viewer.html.request.Request;
import org.apache.isis.viewer.html.request.ServletRequest;
import org.apache.isis.viewer.html.servlet.internal.WebController;

public class ControllerServlet extends AbstractHtmlViewerServlet {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(ControllerServlet.class);

    private String encoding = HtmlServletConstants.ENCODING_DEFAULT;
    private WebController controller;

    // //////////////////////////////////////////////////////////////////
    // init
    // //////////////////////////////////////////////////////////////////

    @Override
    public void init(final ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        encoding = getConfiguration().getString(HtmlServletConstants.ENCODING_KEY, encoding);

        controller = getNewWebController();
        controller.setDebug(getConfiguration().getBoolean(HtmlServletConstants.DEBUG_KEY));
        controller.init();
    }

    protected WebController getNewWebController() {
        return new WebController(getPathBuilder());
    }

    // //////////////////////////////////////////////////////////////////
    // doGet, doPost
    // //////////////////////////////////////////////////////////////////

    @Override
    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding(encoding);
        processRequest(request, response);
    }

    @Override
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    private void processRequest(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        LOG.info("request: " + request.getServletPath() + "?" + request.getQueryString());

        final Request req = new ServletRequest(request);

        if (req.getRequestType() == null) {
            throw new ServletException("No action specified");
        } else if (!controller.actionExists(req)) {
            throw new ServletException("No such action " + req.getRequestType());
        } else {
            try {
                final Context context = getContextForRequest(request);
                processRequest(request, response, req, context);
            } catch (final Exception e) {
                LOG.error("exception during request handling", e);
                throw new ServletException("Internal exception", e);
            }
        }
    }

    private Context getContextForRequest(final HttpServletRequest request) {
        final AuthenticationSession authenticationSession = getAuthenticationSession();
        Context context = (Context) authenticationSession.getAttribute(HtmlServletConstants.AUTHENTICATION_SESSION_CONTEXT_KEY);
        if (context == null || !context.isValid()) {
            // TODO reuse the component factory
            context = new Context(getNewHtmlComponentFactory());
            authenticationSession.setAttribute(HtmlServletConstants.AUTHENTICATION_SESSION_CONTEXT_KEY, context);
        }
        return context;
    }

    protected HtmlComponentFactory getNewHtmlComponentFactory() {
        return new HtmlComponentFactory(getPathBuilder());
    }

    private void processRequest(final HttpServletRequest request, final HttpServletResponse response, final Request req, final Context context) throws IOException, ServletException {
        response.setContentType("text/html");

        // no need to check if logged in; the IsisSessionFilter would
        // have prevented us from getting here.

        try {
            // REVIEW: why was this commented out?
            // SessionAccess.startRequest(context.getSession());
            final Page page = controller.generatePage(context, req);
            if (context.isValid()) {
                if (controller.isDebug()) {
                    controller.addDebug(page, req);
                    addDebug(request, page);
                }
                PrintWriter writer;
                writer = response.getWriter();
                page.write(writer);
            } else {
                response.sendRedirect(getLogonPage());
            }
        } finally {
            // REVIEW: why was this commented out?
            // SessionAccess.endRequest(context.getSession());
            if (!context.isLoggedIn()) {
                final HttpSession httpSession = request.getSession(false);
                LOG.info("dropping session: " + httpSession);
            }
        }
    }

    protected String getLogonPage() {
        return pathTo(HtmlServletConstants.LOGON_PAGE);
    }

    private void addDebug(final HttpServletRequest request, final Page page) {
        page.addDebug("Servlet path", request.getServletPath());
        page.addDebug("Query string", request.getQueryString());
        page.addDebug("Context path", request.getContextPath());
        page.addDebug("Path info", request.getPathInfo());
    }

}
TOP

Related Classes of org.apache.isis.viewer.html.servlet.ControllerServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.