View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.jsp;
12  
13  import java.io.IOException;
14  import java.util.Collections;
15  
16  import javax.servlet.jsp.JspException;
17  import javax.servlet.jsp.tagext.TagSupport;
18  
19  import org.apache.commons.text.StringEscapeUtils;
20  import org.springframework.web.bind.ServletRequestUtils;
21  
22  /**
23   * The Class AddQueryParamTag.
24   */
25  public class AddQueryParamTag extends TagSupport {
26  
27    /** The Constant serialVersionUID. */
28    private static final long serialVersionUID = 1L;
29  
30    /** The param. */
31    private String param;
32  
33    /** The value. */
34    private String value;
35  
36    @Override
37    public int doStartTag() throws JspException {
38      StringBuilder query = new StringBuilder();
39      query.append(param).append('=').append(value);
40      for (String name : Collections.list(pageContext.getRequest().getParameterNames())) {
41        if (!param.equals(name)) {
42          query.append('&').append(StringEscapeUtils.escapeHtml4(name)).append('=')
43              .append(StringEscapeUtils.escapeHtml4(
44                  ServletRequestUtils.getStringParameter(pageContext.getRequest(), name, "")));
45        }
46      }
47      try {
48        pageContext.getOut().print(query);
49      } catch (IOException e) {
50        throw new JspException("Exception printing query string to JspWriter", e);
51      }
52      return EVAL_BODY_INCLUDE;
53    }
54  
55    /**
56     * Gets the param.
57     *
58     * @return the param
59     */
60    public String getParam() {
61      return param;
62    }
63  
64    /**
65     * Sets the param.
66     *
67     * @param param the new param
68     */
69    public void setParam(String param) {
70      this.param = param;
71    }
72  
73    /**
74     * Gets the value.
75     *
76     * @return the value
77     */
78    public String getValue() {
79      return value;
80    }
81  
82    /**
83     * Sets the value.
84     *
85     * @param value the new value
86     */
87    public void setValue(String value) {
88      this.value = value;
89    }
90  
91  }