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 jakarta.servlet.jsp.JspException;
14  import jakarta.servlet.jsp.tagext.TagSupport;
15  
16  import java.io.IOException;
17  import java.net.URLEncoder;
18  import java.nio.charset.Charset;
19  import java.util.Collections;
20  
21  import org.springframework.web.bind.ServletRequestUtils;
22  
23  /**
24   * The Class ParamToggleTag.
25   */
26  public class ParamToggleTag extends TagSupport {
27  
28    /** The Constant serialVersionUID. */
29    private static final long serialVersionUID = 1L;
30  
31    /** The param. */
32    private String param = "size";
33  
34    @Override
35    public int doStartTag() throws JspException {
36      boolean getSize =
37          ServletRequestUtils.getBooleanParameter(pageContext.getRequest(), param, false);
38      StringBuilder query = new StringBuilder();
39  
40      String encoding = pageContext.getResponse().getCharacterEncoding();
41      for (String name : Collections.list(pageContext.getRequest().getParameterNames())) {
42        if (!param.equals(name)) {
43          String value = ServletRequestUtils.getStringParameter(pageContext.getRequest(), name, "");
44          String encodedValue = URLEncoder.encode(value, Charset.forName(encoding));
45          if (query.length() > 0) {
46            query.append('&');
47          }
48          query.append(name).append('=').append(encodedValue);
49        }
50      }
51  
52      if (query.length() > 0) {
53        query.append('&');
54      }
55      // size param has to be last
56      query.append(param).append('=').append(!getSize);
57  
58      try {
59        pageContext.getOut().print(query);
60      } catch (IOException e) {
61        throw new JspException("Exception printing query string to JspWriter", e);
62      }
63      return EVAL_BODY_INCLUDE;
64    }
65  
66    /**
67     * Gets the param.
68     *
69     * @return the param
70     */
71    public String getParam() {
72      return param;
73    }
74  
75    /**
76     * Sets the param.
77     *
78     * @param param the new param
79     */
80    public void setParam(String param) {
81      this.param = param;
82    }
83  
84  }