View Javadoc

1   /*
2    * $Id: ContextUtil.java 474191 2006-11-13 08:30:40Z mrdon $
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  package org.apache.struts2.views.util;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.struts2.StrutsConstants;
30  import org.apache.struts2.util.StrutsUtil;
31  import org.apache.struts2.views.jsp.ui.OgnlTool;
32  
33  import com.opensymphony.xwork2.ActionContext;
34  import com.opensymphony.xwork2.ActionInvocation;
35  import com.opensymphony.xwork2.inject.Inject;
36  import com.opensymphony.xwork2.util.ValueStack;
37  
38  /***
39   * Value Stack's Context related Utilities.
40   *
41   */
42  public class ContextUtil {
43      public static final String REQUEST = "request";
44      public static final String REQUEST2 = "request";
45      public static final String RESPONSE = "response";
46      public static final String RESPONSE2 = "response";
47      public static final String SESSION = "session";
48      public static final String BASE = "base";
49      public static final String STACK = "stack";
50      public static final String OGNL = "ognl";
51      public static final String STRUTS = "struts";
52      public static final String ACTION = "action";
53      
54      public static boolean altSyntax;
55      
56      @Inject(StrutsConstants.STRUTS_TAG_ALTSYNTAX)
57      public static void setAltSyntax(String val) {
58          altSyntax = "true".equals(val);
59      }
60  
61      public static Map getStandardContext(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
62          HashMap map = new HashMap();
63          map.put(REQUEST, req);
64          map.put(REQUEST2, req);
65          map.put(RESPONSE, res);
66          map.put(RESPONSE2, res);
67          map.put(SESSION, req.getSession(false));
68          map.put(BASE, req.getContextPath());
69          map.put(STACK, stack);
70          map.put(OGNL, OgnlTool.getInstance());
71          map.put(STRUTS, new StrutsUtil(stack, req, res));
72  
73          ActionInvocation invocation = (ActionInvocation) stack.getContext().get(ActionContext.ACTION_INVOCATION);
74          if (invocation != null) {
75              map.put(ACTION, invocation.getAction());
76          }
77          return map;
78      }
79  
80      /***
81       * Return true if either Configuration's altSyntax is on or the stack context's useAltSyntax is on
82       * @param context stack's context
83       * @return boolean
84       */
85      public static boolean isUseAltSyntax(Map context) {
86          // We didn't make altSyntax static cause, if so, struts.configuration.xml.reload will not work
87          // plus the Configuration implementation should cache the properties, which the framework's
88          // configuration implementation does
89          return altSyntax ||(
90                  (context.containsKey("useAltSyntax") &&
91                          context.get("useAltSyntax") != null &&
92                          "true".equals(context.get("useAltSyntax").toString())));
93      }
94  
95      /***
96       * Returns a String for overriding the default templateSuffix if templateSuffix is on the stack
97       * @param context stack's context
98       * @return String
99       */
100     public static String getTemplateSuffix(Map context) {
101         return context.containsKey("templateSuffix") ? (String) context.get("templateSuffix") : null;
102     }
103 }