001: /*
002: * $Id: ContextUtil.java 474191 2006-11-13 08:30:40Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.util;
022:
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.struts2.StrutsConstants;
030: import org.apache.struts2.util.StrutsUtil;
031: import org.apache.struts2.views.jsp.ui.OgnlTool;
032:
033: import com.opensymphony.xwork2.ActionContext;
034: import com.opensymphony.xwork2.ActionInvocation;
035: import com.opensymphony.xwork2.inject.Inject;
036: import com.opensymphony.xwork2.util.ValueStack;
037:
038: /**
039: * Value Stack's Context related Utilities.
040: *
041: */
042: public class ContextUtil {
043: public static final String REQUEST = "request";
044: public static final String REQUEST2 = "request";
045: public static final String RESPONSE = "response";
046: public static final String RESPONSE2 = "response";
047: public static final String SESSION = "session";
048: public static final String BASE = "base";
049: public static final String STACK = "stack";
050: public static final String OGNL = "ognl";
051: public static final String STRUTS = "struts";
052: public static final String ACTION = "action";
053:
054: public static boolean altSyntax;
055:
056: @Inject(StrutsConstants.STRUTS_TAG_ALTSYNTAX)
057: public static void setAltSyntax(String val) {
058: altSyntax = "true".equals(val);
059: }
060:
061: public static Map getStandardContext(ValueStack stack,
062: HttpServletRequest req, HttpServletResponse res) {
063: HashMap map = new HashMap();
064: map.put(REQUEST, req);
065: map.put(REQUEST2, req);
066: map.put(RESPONSE, res);
067: map.put(RESPONSE2, res);
068: map.put(SESSION, req.getSession(false));
069: map.put(BASE, req.getContextPath());
070: map.put(STACK, stack);
071: map.put(OGNL, OgnlTool.getInstance());
072: map.put(STRUTS, new StrutsUtil(stack, req, res));
073:
074: ActionInvocation invocation = (ActionInvocation) stack
075: .getContext().get(ActionContext.ACTION_INVOCATION);
076: if (invocation != null) {
077: map.put(ACTION, invocation.getAction());
078: }
079: return map;
080: }
081:
082: /**
083: * Return true if either Configuration's altSyntax is on or the stack context's useAltSyntax is on
084: * @param context stack's context
085: * @return boolean
086: */
087: public static boolean isUseAltSyntax(Map context) {
088: // We didn't make altSyntax static cause, if so, struts.configuration.xml.reload will not work
089: // plus the Configuration implementation should cache the properties, which the framework's
090: // configuration implementation does
091: return altSyntax
092: || ((context.containsKey("useAltSyntax")
093: && context.get("useAltSyntax") != null && "true"
094: .equals(context.get("useAltSyntax").toString())));
095: }
096:
097: /**
098: * Returns a String for overriding the default templateSuffix if templateSuffix is on the stack
099: * @param context stack's context
100: * @return String
101: */
102: public static String getTemplateSuffix(Map context) {
103: return context.containsKey("templateSuffix") ? (String) context
104: .get("templateSuffix") : null;
105: }
106: }
|