View Javadoc

1   /*
2    * $Id: ContextUtil.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.util;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.struts2.StrutsConstants;
27  import org.apache.struts2.config.Settings;
28  import org.apache.struts2.util.StrutsUtil;
29  import org.apache.struts2.views.jsp.ui.OgnlTool;
30  
31  import com.opensymphony.xwork2.ActionContext;
32  import com.opensymphony.xwork2.ActionInvocation;
33  import com.opensymphony.xwork2.util.ValueStack;
34  
35  /***
36   * Value Stack's Context related Utilities.
37   * 
38   */
39  public class ContextUtil {
40      public static final String REQUEST = "request";
41      public static final String REQUEST2 = "request";
42      public static final String RESPONSE = "response";
43      public static final String RESPONSE2 = "response";
44      public static final String SESSION = "session";
45      public static final String BASE = "base";
46      public static final String STACK = "stack";
47      public static final String OGNL = "ognl";
48      public static final String STRUTS = "struts";
49      public static final String ACTION = "action";
50  
51      public static Map getStandardContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
52          HashMap map = new HashMap();
53          map.put(REQUEST, req);
54          map.put(REQUEST2, req);
55          map.put(RESPONSE, res);
56          map.put(RESPONSE2, res);
57          map.put(SESSION, req.getSession(false));
58          map.put(BASE, req.getContextPath());
59          map.put(STACK, stack);
60          map.put(OGNL, OgnlTool.getInstance());
61          map.put(STRUTS, new StrutsUtil(stack, req, res));
62  
63          ActionInvocation invocation = (ActionInvocation) stack.getContext().get(ActionContext.ACTION_INVOCATION);
64          if (invocation != null) {
65              map.put(ACTION, invocation.getAction());
66          }
67          return map;
68      }
69      
70      /***
71       * Return true if either Configuration's altSyntax is on or the stack context's useAltSyntax is on
72       * @param context stack's context
73       * @return boolean
74       */
75      public static boolean isUseAltSyntax(Map context) {
76          // We didn't make altSyntax static cause, if so, struts.configuration.xml.reload will not work
77          // plus the Configuration implementation should cache the properties, which the framework's
78          // configuration implementation does
79          boolean altSyntax = "true".equals(Settings.get(StrutsConstants.STRUTS_TAG_ALTSYNTAX));
80          return altSyntax ||(
81                  (context.containsKey("useAltSyntax") &&
82                          context.get("useAltSyntax") != null &&
83                          "true".equals(context.get("useAltSyntax").toString())));
84      }
85  
86      /***
87       * Returns a String for overriding the default templateSuffix if templateSuffix is on the stack
88       * @param context stack's context
89       * @return String
90       */
91      public static String getTemplateSuffix(Map context) {
92          return context.containsKey("templateSuffix") ? (String) context.get("templateSuffix") : null;
93      }
94  }