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.io.UnsupportedEncodingException;
15  import java.net.URLEncoder;
16  import java.util.Collections;
17  
18  import javax.servlet.jsp.JspException;
19  import javax.servlet.jsp.tagext.TagSupport;
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          try {
44            String value = ServletRequestUtils.getStringParameter(pageContext.getRequest(), name, "");
45            String encodedValue = URLEncoder.encode(value, encoding);
46            if (query.length() > 0) {
47              query.append('&');
48            }
49            query.append(name).append('=').append(encodedValue);
50          } catch (UnsupportedEncodingException e) {
51            throw new JspException(e);
52          }
53        }
54      }
55  
56      if (query.length() > 0) {
57        query.append('&');
58      }
59      // size param has to be last
60      query.append(param).append('=').append(!getSize);
61  
62      try {
63        pageContext.getOut().print(query);
64      } catch (IOException e) {
65        throw new JspException("Exception printing query string to JspWriter", e);
66      }
67      return EVAL_BODY_INCLUDE;
68    }
69  
70    /**
71     * Gets the param.
72     *
73     * @return the param
74     */
75    public String getParam() {
76      return param;
77    }
78  
79    /**
80     * Sets the param.
81     *
82     * @param param the new param
83     */
84    public void setParam(String param) {
85      this.param = param;
86    }
87  
88  }