1 14 package org.wings; 15 16 import org.wings.io.Device; 17 18 import java.io.IOException ; 19 20 27 public class RequestURL extends SimpleURL { 28 private static final String DEFAULT_RESOURCE_NAME = "_"; 29 30 private String baseParameters; 31 32 private boolean hasQuestMark; 33 34 private String epoch; 35 36 private String resource; 37 38 private StringBuffer parameters = null; 39 40 41 public RequestURL() { 42 } 43 44 47 private RequestURL(RequestURL other) { 48 this.baseURL = other.baseURL; 49 this.baseParameters = other.baseParameters; 50 this.hasQuestMark = other.hasQuestMark; 51 this.epoch = other.epoch; 52 this.resource = other.resource; 53 StringBuffer params = other.parameters; 54 parameters = (params == null) ? params : new StringBuffer (params.toString()); 55 } 56 57 58 public RequestURL(String baseURL, String encodedBaseURL) { 59 setBaseURL(baseURL, encodedBaseURL); 60 } 61 62 63 public void setEpoch(String e) { 64 epoch = e; 65 } 66 67 68 public String getEpoch() { 69 return epoch; 70 } 71 72 73 public void setResource(String r) { 74 resource = r; 75 } 76 77 78 public String getResource() { 79 return resource; 80 } 81 82 83 public void setBaseURL(String b, String encoded) { 84 baseURL = b; 85 86 baseParameters = encoded.substring(b.length()); 87 if (baseParameters.length() == 0) 88 baseParameters = null; 89 90 if (baseParameters != null) 91 hasQuestMark = baseParameters.indexOf('?') >= 0; 92 else 93 hasQuestMark = false; 94 } 95 96 97 104 public RequestURL addParameter(String parameter) { 105 if (parameter != null) { 106 if (parameters == null) 107 parameters = new StringBuffer (); 108 else 109 parameters.append("&"); 110 parameters.append(parameter); 111 } 112 return this; 113 } 114 115 123 public RequestURL addParameter(String name, String value) { 124 addParameter(name); 125 parameters.append("=").append(value); 126 return this; 127 } 128 129 137 public RequestURL addParameter(LowLevelEventListener comp, String value) { 138 addParameter(comp.getEncodedLowLevelEventId(), value); 139 140 return this; 141 } 142 143 151 public RequestURL addParameter(String name, int value) { 152 addParameter(name); 153 parameters.append("=").append(value); 154 return this; 155 } 156 157 160 public void clear() { 161 if (parameters != null) { 162 parameters.setLength(0); 163 } 164 setEpoch(null); 165 setResource(null); 166 } 167 168 177 public void write(Device d) throws IOException { 178 super.write(d); 179 180 if (resource != null && epoch != null) { 181 d.print(epoch); 182 d.print(SConstants.UID_DIVIDER); 183 } 184 185 if (resource != null) { 186 d.print(resource); 187 } else { 188 193 d.print(DEFAULT_RESOURCE_NAME); 194 } 195 196 if (baseParameters != null) { 197 d.print(baseParameters); 198 } 199 200 if (parameters != null && parameters.length() > 0) { 201 d.print(hasQuestMark ? "&" : "?"); 202 d.print(parameters.toString()); 203 } 204 } 205 206 210 public String toString() { 211 StringBuffer erg = new StringBuffer (); 212 213 if (baseURL != null) { 214 erg.append(baseURL); 215 } 216 217 if (resource != null && epoch != null) { 218 erg.append(epoch); 219 erg.append("_"); 220 } 221 222 if (resource != null) { 223 erg.append(resource); 224 } else { 225 erg.append(DEFAULT_RESOURCE_NAME); 226 } 227 228 if (baseParameters != null) { 229 erg.append(baseParameters); 230 } 231 232 if (parameters != null && parameters.length() > 0) { 233 erg.append(hasQuestMark ? "&" : "?"); 234 erg.append(parameters.toString()); 235 } 236 237 return erg.toString(); 238 } 239 240 private final boolean eq(Object a, Object b) { 241 return (a == b) || (a != null && a.equals(b)); 242 } 243 244 public boolean equals(Object o) { 245 if (o == null) return false; 246 if (!super.equals(o)) return false; 247 RequestURL other = (RequestURL) o; 248 return (hasQuestMark == other.hasQuestMark 249 && eq(baseParameters, other.baseParameters) 250 && eq(epoch, other.epoch) 251 && eq(resource, other.resource) 252 && eq(parameters, other.parameters)); 253 } 254 255 258 public int hashCode() { 259 return baseURL != null ? baseURL.hashCode() : 0; 260 } 261 262 267 public Object clone() { 268 return new RequestURL(this); 269 } 270 } 271 272 273 | Popular Tags |