View Javadoc

1   /*
2    * $Id: TagUtils.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.jsp;
22  
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.jsp.PageContext;
28  
29  import org.apache.struts2.RequestUtils;
30  import org.apache.struts2.ServletActionContext;
31  import org.apache.struts2.dispatcher.ApplicationMap;
32  import org.apache.struts2.dispatcher.Dispatcher;
33  import org.apache.struts2.dispatcher.RequestMap;
34  import org.apache.struts2.dispatcher.SessionMap;
35  import org.apache.struts2.dispatcher.mapper.ActionMapper;
36  import org.apache.struts2.dispatcher.mapper.ActionMapping;
37  import org.apache.struts2.util.AttributeMap;
38  
39  import com.opensymphony.xwork2.ActionContext;
40  import com.opensymphony.xwork2.ActionInvocation;
41  import com.opensymphony.xwork2.util.ValueStack;
42  import com.opensymphony.xwork2.util.ValueStackFactory;
43  
44  
45  /***
46   */
47  public class TagUtils {
48  
49      public static ValueStack getStack(PageContext pageContext) {
50          HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
51          ValueStack stack = (ValueStack) req.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
52  
53          if (stack == null) {
54              stack = ValueStackFactory.getFactory().createValueStack();
55  
56              HttpServletResponse res = (HttpServletResponse) pageContext.getResponse();
57              Dispatcher du = Dispatcher.getInstance();
58              Map extraContext = du.createContextMap(new RequestMap(req),
59                      req.getParameterMap(),
60                      new SessionMap(req),
61                      new ApplicationMap(pageContext.getServletContext()),
62                      req,
63                      res,
64                      pageContext.getServletContext());
65              extraContext.put(ServletActionContext.PAGE_CONTEXT, pageContext);
66              stack.getContext().putAll(extraContext);
67              req.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
68  
69              // also tie this stack/context to the ThreadLocal
70              ActionContext.setContext(new ActionContext(stack.getContext()));
71          } else {
72              // let's make sure that the current page context is in the action context
73              Map context = stack.getContext();
74              context.put(ServletActionContext.PAGE_CONTEXT, pageContext);
75  
76              AttributeMap attrMap = new AttributeMap(context);
77              context.put("attr", attrMap);
78          }
79  
80          return stack;
81      }
82  
83      public static String buildNamespace(ActionMapper mapper, ValueStack stack, HttpServletRequest request) {
84          ActionContext context = new ActionContext(stack.getContext());
85          ActionInvocation invocation = context.getActionInvocation();
86  
87          if (invocation == null) {
88              ActionMapping mapping = mapper.getMapping(request,
89                      Dispatcher.getInstance().getConfigurationManager());
90  
91              if (mapping != null) {
92                  return mapping.getNamespace();
93              } else {
94                  // well, if the ActionMapper can't tell us, and there is no existing action invocation,
95                  // let's just go with a default guess that the namespace is the last the path minus the
96                  // last part (/foo/bar/baz.xyz -> /foo/bar)
97  
98                  String path = RequestUtils.getServletPath(request);
99                  return path.substring(0, path.lastIndexOf("/"));
100             }
101         } else {
102             return invocation.getProxy().getNamespace();
103         }
104     }
105 }