001package org.apache.turbine.modules.actions.sessionvalidator;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.lang3.StringUtils;
023import org.apache.logging.log4j.Logger;
024import org.apache.logging.log4j.LogManager;
025import org.apache.turbine.Turbine;
026import org.apache.turbine.om.security.User;
027import org.apache.turbine.pipeline.PipelineData;
028import org.apache.turbine.util.RunData;
029
030/**
031 * SessionValidator for use with the Template Service, the
032 * TemplateSessionValidator is virtually identical to the
033 * {@link TemplateSecureSessionValidator} except that it does not transfer to the
034 * login page when it detects a null user (or a user not logged in).
035 *
036 * <p>The Template Service requires a different Session Validator
037 * because of the way it handles screens.
038 *
039 * @see TemplateSecureSessionValidator
040 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
041 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
042 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
043 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
044 * @version $Id$
045 */
046public class TemplateSessionValidator
047    extends SessionValidator
048{
049    /** Logging */
050    private static Logger log = LogManager.getLogger(TemplateSessionValidator.class);
051
052    /**
053     * Execute the action.
054     *
055     * @param pipelineData Turbine information.
056     * @throws Exception The anonymous user could not be obtained
057     *         from the security service
058     */
059    @Override
060    public void doPerform(PipelineData pipelineData) throws Exception
061    {
062        RunData data = pipelineData.getRunData();
063        // Pull user from session.
064        data.populate();
065
066        // The user may have not logged in, so create a "guest/anonymous" user.
067        if (data.getUser() == null)
068        {
069            log.debug("Creating an anonymous user object!");
070            User anonymousUser = security.getAnonymousUser();
071            data.setUser(anonymousUser);
072            data.save();
073        }
074
075        // Make sure we have some way to return a response
076        if (!data.hasScreen() && StringUtils.isEmpty(
077                data.getTemplateInfo().getScreenTemplate()))
078        {
079            if (StringUtils.isNotEmpty(templateHomepage))
080            {
081                data.getTemplateInfo().setScreenTemplate(templateHomepage);
082            }
083            else
084            {
085                data.setScreen(screenHomepage);
086            }
087        } else {
088            handleFormCounterToken(data, false);
089        }
090
091        // We do not want to allow both a screen and template parameter.
092        // The template parameter is dominant.
093        if (data.getTemplateInfo().getScreenTemplate() != null)
094        {
095            data.setScreen(null);
096        }
097
098        // Comply with Turbine 4.0 standards
099        pipelineData.get(Turbine.class).put(User.class, data.getUser());
100    }
101}