org.apache.struts2.interceptor.CreateSessionInterceptor Maven / Gradle / Ivy
/*
* 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.struts2.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import javax.servlet.http.HttpSession;
/**
*
*
*
* This interceptor creates the HttpSession if it doesn't exist, also SessionMap is recreated and put in ServletActionContext.
*
*
*
* This is particular useful when using the <@s.token> tag in freemarker templates.
* The tag do require that a HttpSession is already created since freemarker commits
* the response to the client immediately.
*
*
*
* Interceptor parameters:
*
*
*
*
*
* - None
*
*
*
*
*
*
*
*
* - None
*
*
*
*
* Example:
*
*
*
*
* <action name="someAction" class="com.examples.SomeAction">
* <interceptor-ref name="createSession"/>
* <interceptor-ref name="defaultStack"/>
* <result name="input">input_with_token_tag.ftl</result>
* </action>
*
*
*
*/
public class CreateSessionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -4590322556118858869L;
private static final Logger LOG = LogManager.getLogger(CreateSessionInterceptor.class);
/* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/
public String intercept(ActionInvocation invocation) throws Exception {
HttpSession httpSession = ServletActionContext.getRequest().getSession(false);
if (httpSession == null) {
LOG.debug("Creating new HttpSession and new SessionMap in ServletActionContext");
ServletActionContext.getRequest().getSession(true);
ServletActionContext.getContext().setSession(new SessionMap(ServletActionContext.getRequest()));
}
return invocation.invoke();
}
}