View Javadoc

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