1 18 19 package org.apache.struts.action; 20 21 import org.apache.struts.config.ForwardConfig; 22 import org.apache.struts.util.ResponseUtils; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.Arrays ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Iterator ; 32 33 60 public class ActionRedirect extends ActionForward { 61 62 64 67 protected static final Log log = LogFactory.getLog(ActionRedirect.class); 68 69 70 72 77 protected Map parameterValues = null; 78 79 80 82 86 public ActionRedirect() { 87 setRedirect(true); 88 initializeParameters(); 89 } 90 91 97 public ActionRedirect(String path) { 98 super(path); 99 setRedirect(true); 100 initializeParameters(); 101 } 102 103 111 public ActionRedirect(String name, String path, String module) { 112 super(name, path, true); 113 setModule(module); 114 initializeParameters(); 115 } 116 117 118 125 public ActionRedirect(ForwardConfig baseConfig) { 126 setName(baseConfig.getName()); 127 setPath(baseConfig.getPath()); 128 setContextRelative(baseConfig.getContextRelative()); 129 setModule(baseConfig.getModule()); 130 setRedirect(true); 131 initializeParameters(); 132 } 133 134 135 136 138 142 private void initializeParameters() { 143 parameterValues = new HashMap (); 144 } 145 146 147 149 156 public void addParameter(String fieldName, Object valueObj) { 157 158 String value = (valueObj != null) ? valueObj.toString() : ""; 159 if (parameterValues == null) { 160 initializeParameters(); 161 } 162 163 value = ResponseUtils.encodeURL(value); 165 172 Object currentValue = parameterValues.get(fieldName); 173 if (currentValue == null) { 174 parameterValues.put(fieldName, value); 176 177 } else if (currentValue instanceof String ) { 178 String [] newValue = new String [2]; 180 newValue[0] = (String ) currentValue; 181 newValue[1] = value; 182 parameterValues.put(fieldName, newValue); 183 184 } else if (currentValue instanceof String []) { 185 List newValues = new ArrayList (Arrays.asList((Object []) currentValue)); 187 newValues.add(value); 188 parameterValues.put(fieldName, (String []) newValues.toArray(new String [newValues.size()])); 189 } 190 } 191 192 193 198 public String getOriginalPath() { 199 return super.getPath(); 200 } 201 202 203 207 public String getPath() { 208 String originalPath = getOriginalPath(); 210 String parameterString = getParameterString(); 211 212 StringBuffer result = new StringBuffer (originalPath); 213 214 if ((parameterString != null) && (parameterString.length() > 0)) { 215 String paramSeparator = "?"; 217 218 boolean needsParamSeparator = true; 220 221 int paramStartIndex = originalPath.indexOf("?"); 223 if (paramStartIndex > 0) { 224 needsParamSeparator = 226 (paramStartIndex != originalPath.length() - 1); 227 if (needsParamSeparator) { 228 paramSeparator = "&"; 229 } 230 } 231 232 if (needsParamSeparator) { 233 result.append(paramSeparator); 234 } 235 result.append(parameterString); 236 } 237 238 return result.toString(); 239 } 240 241 242 250 public String getParameterString() { 251 StringBuffer strParam = new StringBuffer (256); 252 253 Iterator iterator = parameterValues.keySet().iterator(); 255 while (iterator.hasNext()) { 256 String name = (String ) iterator.next(); 258 259 Object value = parameterValues.get(name); 261 262 if (value instanceof String ) { 263 strParam.append(name) 265 .append("=") 266 .append(value); 267 268 } else if (value instanceof String []) { 269 String [] values = (String []) value; 271 for (int i = 0; i < values.length; i++) { 272 strParam.append(name) 273 .append("=") 274 .append(values[i]); 275 if (i < values.length - 1) 276 strParam.append("&"); 277 } 278 } 279 280 if (iterator.hasNext()) { 281 strParam.append("&"); 282 } 283 } 284 285 return strParam.toString(); 286 } 287 288 289 291 297 public String toString() { 298 StringBuffer result = new StringBuffer (256); 299 result.append("ActionRedirect ["); 300 result.append("originalPath=").append(getOriginalPath()).append(";"); 301 result.append("parameterString=") 302 .append(getParameterString()).append("]"); 303 return result.toString(); 304 } 305 306 307 } 308 | Popular Tags |