View Javadoc

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