View Javadoc

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