View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.struts2.jasper.tagplugins.jstl.core;
20  
21  import org.apache.struts2.jasper.compiler.tagplugin.TagPlugin;
22  import org.apache.struts2.jasper.compiler.tagplugin.TagPluginContext;
23  
24  public class Redirect implements TagPlugin {
25  
26      public void doTag(TagPluginContext ctxt) {
27  
28          //flag for the existence of the "context"
29          boolean hasContext = ctxt.isAttributeSpecified("context");
30  
31          //names of the temp variables
32          String urlName = ctxt.getTemporaryVariableName();
33          String contextName = ctxt.getTemporaryVariableName();
34          String baseUrlName = ctxt.getTemporaryVariableName();
35          String resultName = ctxt.getTemporaryVariableName();
36          String responseName = ctxt.getTemporaryVariableName();
37  
38          //get context
39          ctxt.generateJavaSource("String " + contextName + " = null;");
40          if (hasContext) {
41              ctxt.generateJavaSource(contextName + " = ");
42              ctxt.generateAttribute("context");
43              ctxt.generateJavaSource(";");
44          }
45  
46          //get the url
47          ctxt.generateJavaSource("String " + urlName + " = ");
48          ctxt.generateAttribute("url");
49          ctxt.generateJavaSource(";");
50  
51          //get the raw url according to "url" and "context"
52          ctxt.generateJavaSource("String " + baseUrlName + " = " +
53                  "org.apache.struts2.jasper.tagplugins.jstl.Util.resolveUrl(" + urlName + ", " + contextName + ", pageContext);");
54          ctxt.generateJavaSource("pageContext.setAttribute" +
55                  "(\"url_without_param\", " + baseUrlName + ");");
56  
57          //add params
58          ctxt.generateBody();
59  
60          ctxt.generateJavaSource("String " + resultName + " = " +
61                  "(String)pageContext.getAttribute(\"url_without_param\");");
62          ctxt.generateJavaSource("pageContext.removeAttribute" +
63                  "(\"url_without_param\");");
64  
65          //get the response object
66          ctxt.generateJavaSource("HttpServletResponse " + responseName + " = " +
67                  "((HttpServletResponse) pageContext.getResponse());");
68  
69          //if the url is relative, encode it
70          ctxt.generateJavaSource("if(!org.apache.struts2.jasper.tagplugins.jstl.Util.isAbsoluteUrl(" + resultName + ")){");
71          ctxt.generateJavaSource("    " + resultName + " = "
72                  + responseName + ".encodeRedirectURL(" + resultName + ");");
73          ctxt.generateJavaSource("}");
74  
75          //do redirect
76          ctxt.generateJavaSource("try{");
77          ctxt.generateJavaSource("    " + responseName + ".sendRedirect(" + resultName + ");");
78          ctxt.generateJavaSource("}catch(java.io.IOException ex){");
79          ctxt.generateJavaSource("    throw new JspTagException(ex.toString(), ex);");
80          ctxt.generateJavaSource("}");
81      }
82  
83  }