1 /*
2 * $Id: CheckboxInterceptor.java 768855 2009-04-27 02:09:35Z wesw $
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.interceptor;
23
24 import com.opensymphony.xwork2.ActionInvocation;
25 import com.opensymphony.xwork2.util.logging.Logger;
26 import com.opensymphony.xwork2.util.logging.LoggerFactory;
27 import com.opensymphony.xwork2.interceptor.Interceptor;
28
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.HashMap;
32 import java.util.Iterator;
33
34 /**
35 * <!-- START SNIPPET: description -->
36 * Looks for a hidden identification field that specifies the original value of the checkbox.
37 * If the checkbox isn't submitted, insert it into the parameters as if it was with the value
38 * of 'false'.
39 * <!-- END SNIPPET: description -->
40 * <p/>
41 * <!-- START SNIPPET: parameters -->
42 * <ul><li>setUncheckedValue -
43 * The default value of an unchecked box can be overridden by setting the 'uncheckedValue' property.
44 * </li></ul>
45 * <!-- END SNIPPET: parameters -->
46 * <p/>
47 * <!-- START SNIPPET: extending -->
48 * <p/>
49 * <!-- END SNIPPET: extending -->
50 */
51 public class CheckboxInterceptor implements Interceptor {
52
53 /** Auto-generated serialization id */
54 private static final long serialVersionUID = -586878104807229585L;
55
56 private String uncheckedValue = Boolean.FALSE.toString();
57
58 private static final Logger LOG = LoggerFactory.getLogger(CheckboxInterceptor.class);
59
60 public void destroy() {
61 }
62
63 public void init() {
64 }
65
66 public String intercept(ActionInvocation ai) throws Exception {
67 Map parameters = ai.getInvocationContext().getParameters();
68 Map<String, String[]> newParams = new HashMap<String, String[]>();
69 Set<Map.Entry> entries = parameters.entrySet();
70 for (Iterator<Map.Entry> iterator = entries.iterator(); iterator.hasNext();) {
71 Map.Entry entry = iterator.next();
72 String key = (String)entry.getKey();
73
74 if (key.startsWith("__checkbox_")) {
75 String name = key.substring("__checkbox_".length());
76
77 Object values = entry.getValue();
78 iterator.remove();
79 if (values != null && values instanceof String[] && ((String[])values).length > 1) {
80 LOG.debug("Bypassing automatic checkbox detection due to multiple checkboxes of the same name: #1", name);
81 continue;
82 }
83
84 // is this checkbox checked/submitted?
85 if (!parameters.containsKey(name)) {
86 // if not, let's be sure to default the value to false
87 newParams.put(name, new String[]{uncheckedValue});
88 }
89 }
90 }
91
92 parameters.putAll(newParams);
93
94 return ai.invoke();
95 }
96
97 /**
98 * Overrides the default value for an unchecked checkbox
99 *
100 * @param uncheckedValue The uncheckedValue to set
101 */
102 public void setUncheckedValue(String uncheckedValue) {
103 this.uncheckedValue = uncheckedValue;
104 }
105 }