View Javadoc

1   /*
2    * $Id: CreateSessionInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.interceptor;
23  
24  import org.apache.struts2.ServletActionContext;
25  
26  import com.opensymphony.xwork2.ActionInvocation;
27  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
28  import com.opensymphony.xwork2.util.logging.Logger;
29  import com.opensymphony.xwork2.util.logging.LoggerFactory;
30  
31  /***
32   * <!-- START SNIPPET: description -->
33   *
34   * This interceptor creates the HttpSession.
35   * <p/>
36   * This is particular usefull when using the &lt;@s.token&gt; tag in freemarker templates.
37   * The tag <b>do</b> require that a HttpSession is already created since freemarker commits
38   * the response to the client immediately.
39   *
40   * <!-- END SNIPPET: description -->
41   *
42   * <p/> <u>Interceptor parameters:</u>
43   *
44   *
45   * <!-- START SNIPPET: extending -->
46   *
47   * <ul>
48   *  <li>none</li>
49   * </ul>
50   *
51   * <!-- END SNIPPET: extending -->
52   *
53   *
54   * <!-- START SNIPPET: parameters -->
55   *
56   * <ul>
57   *
58   * <li>None</li>
59   *
60   * </ul>
61   *
62   * <!-- END SNIPPET: parameters -->
63   *
64   * <b>Example:</b>
65   *
66   * <pre>
67   * <!-- START SNIPPET: example -->
68   *
69   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
70   *     &lt;interceptor-ref name="createSession"/&gt;
71   *     &lt;interceptor-ref name="defaultStack"/&gt;
72   *     &lt;result name="input"&gt;input_with_token_tag.ftl&lt;/result&gt;
73   * &lt;/action&gt;
74   *
75   * <!-- END SNIPPET: example -->
76   * </pre>
77   *
78   * @version $Date: 2008-04-27 13:41:38 +0000 (Sun, 27 Apr 2008) $ $Id: CreateSessionInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
79   */
80  public class CreateSessionInterceptor extends AbstractInterceptor {
81  
82      private static final long serialVersionUID = -4590322556118858869L;
83  
84      private static final Logger LOG = LoggerFactory.getLogger(CreateSessionInterceptor.class);
85  
86  
87      /* (non-Javadoc)
88       * @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
89       */
90      public String intercept(ActionInvocation invocation) throws Exception {
91          LOG.debug("Creating HttpSession");
92          ServletActionContext.getRequest().getSession(true);
93          return invocation.invoke();
94      }
95  
96  }