KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > RequestURL


1 /*
2  * $Id: RequestURL.java,v 1.6 2005/02/11 15:16:08 blueshift Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.wings.io.Device;
17
18 import java.io.IOException JavaDoc;
19
20 /**
21  * This class handles a HTTP GET Address that can be updated
22  * with additional GET parameters.
23  *
24  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
25  * @version $Revision: 1.6 $
26  */

27 public class RequestURL extends SimpleURL {
28     private static final String JavaDoc DEFAULT_RESOURCE_NAME = "_";
29
30     private String JavaDoc baseParameters;
31
32     private boolean hasQuestMark;
33
34     private String JavaDoc epoch;
35
36     private String JavaDoc resource;
37
38     private StringBuffer JavaDoc parameters = null;
39
40
41     public RequestURL() {
42     }
43
44     /**
45      * copy constructor.
46      */

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 JavaDoc params = other.parameters;
54         parameters = (params == null) ? params : new StringBuffer JavaDoc(params.toString());
55     }
56
57
58     public RequestURL(String JavaDoc baseURL, String JavaDoc encodedBaseURL) {
59         setBaseURL(baseURL, encodedBaseURL);
60     }
61
62
63     public void setEpoch(String JavaDoc e) {
64         epoch = e;
65     }
66
67
68     public String JavaDoc getEpoch() {
69         return epoch;
70     }
71
72
73     public void setResource(String JavaDoc r) {
74         resource = r;
75     }
76
77
78     public String JavaDoc getResource() {
79         return resource;
80     }
81
82
83     public void setBaseURL(String JavaDoc b, String JavaDoc 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     /**
98      * Add an additional parameter to be included in the GET paramter
99      * list. Usually, this paramter will be in the form 'name=value'.
100      *
101      * @param parameter to be included in the GET parameters.
102      * @return a reference to <code>this</code> to simplify 'call chaining'
103      */

104     public RequestURL addParameter(String JavaDoc parameter) {
105         if (parameter != null) {
106             if (parameters == null)
107                 parameters = new StringBuffer JavaDoc();
108             else
109                 parameters.append("&amp;");
110             parameters.append(parameter);
111         }
112         return this;
113     }
114
115     /**
116      * Add an additional name/value pair to be included in the GET paramter
117      * list. The added parameter will be 'name=value'
118      *
119      * @param name the name of the parameter
120      * @param value the value of the parameter
121      * @return a reference to <code>this</code> to simplify 'call chaining'
122      */

123     public RequestURL addParameter(String JavaDoc name, String JavaDoc value) {
124         addParameter(name);
125         parameters.append("=").append(value);
126         return this;
127     }
128
129     /**
130      * Add an additional name/value pair to be included in the GET paramter
131      * list. The added name will be the encoded LowLevelEventId of the
132      * LowLevelEventListener.
133      *
134      * @param value the value of the parameter
135      * @return a reference to <code>this</code> to simplify 'call chaining'
136      */

137     public RequestURL addParameter(LowLevelEventListener comp, String JavaDoc value) {
138         addParameter(comp.getEncodedLowLevelEventId(), value);
139
140         return this;
141     }
142
143     /**
144      * Add an additional name/value pair to be included in the GET paramter
145      * list. The added parameter will be 'name=value'
146      *
147      * @param name the name of the parameter
148      * @param value the value of the parameter
149      * @return a reference to <code>this</code> to simplify 'call chaining'
150      */

151     public RequestURL addParameter(String JavaDoc name, int value) {
152         addParameter(name);
153         parameters.append("=").append(value);
154         return this;
155     }
156
157     /**
158      * clear all additional paramters given in the {@link #addParameter(String)} call.
159      */

160     public void clear() {
161         if (parameters != null) {
162             parameters.setLength(0);
163         }
164         setEpoch(null);
165         setResource(null);
166     }
167
168     /**
169      * Writes the context Address to the output Device. Appends all
170      * parameters given. Only the context URL is given, since all GET urls generated
171      * by wings are relative to the WingS servlet.
172      * Tries to avoid charset conversion as much as possible by precalculating the
173      * byteArray representation of the non-parameter part.
174      *
175      * @param d the Device to write to
176      */

177     public void write(Device d) throws IOException JavaDoc {
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             /*
189              * The default resource name. Work around a bug in some
190              * browsers that fail to assemble URLs.
191              * (TODO: verify and give better explanation here).
192              */

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 ? "&amp;" : "?");
202             d.print(parameters.toString());
203         }
204     }
205
206     /**
207      * Returns the string representation of the context URL plus
208      * all paramters given.
209      */

210     public String JavaDoc toString() {
211         StringBuffer JavaDoc erg = new StringBuffer JavaDoc();
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 JavaDoc a, Object JavaDoc b) {
241         return (a == b) || (a != null && a.equals(b));
242     }
243
244     public boolean equals(Object JavaDoc 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     /**
256      * @see java.lang.Object#hashCode()
257      */

258     public int hashCode() {
259         return baseURL != null ? baseURL.hashCode() : 0;
260     }
261
262     /**
263      * Deep copy.
264      *
265      * @return object with cloned contents
266      */

267     public Object JavaDoc clone() {
268         return new RequestURL(this);
269     }
270 }
271
272
273
Popular Tags