KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > HttpMethod


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  *
29  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
30  */

31 public abstract class HttpMethod {
32
33     private String JavaDoc name;
34     private String JavaDoc uri;
35     private Hashtable JavaDoc parameters = new Hashtable JavaDoc();
36     private Vector JavaDoc parameterNames = new Vector JavaDoc();
37
38     public HttpMethod(String JavaDoc name, String JavaDoc uri) {
39         this.uri = uri;
40         this.name = name;
41     }
42
43     public String JavaDoc getName() {
44         return name;
45     }
46
47     public String JavaDoc getURI() {
48         return uri;
49     }
50
51     public String JavaDoc getVersion() {
52         return "1.1"; //$NON-NLS-1$
53
}
54
55     public HttpResponse execute(HttpRequest request, HttpConnection con) throws IOException JavaDoc {
56
57         // Add default headers
58
request.setHeaderField("Host", con.getHostHeaderValue()); //$NON-NLS-1$
59
request.setHeaderField("User-Agent", HttpClient.USER_AGENT); //$NON-NLS-1$
60
request.performRequest(this, con);
61         return new HttpResponse(con);
62     }
63
64     /**
65      * Add a parameter. <b>If parameters with the same name already exist
66      * they will not be replaced</b>.
67      *
68      * @param name parameter name
69      * @param value parameter value
70      */

71     public void addParameter(String JavaDoc name, String JavaDoc value) {
72         if(value == null) {
73             throw new IllegalArgumentException JavaDoc("Null value");
74         }
75
76         if (!parameters.containsKey(name)) {
77             parameters.put(name, new Vector JavaDoc());
78             parameterNames.addElement(name);
79         }
80
81         Vector JavaDoc v = (Vector JavaDoc)parameters.get(name);
82         v.addElement(value);
83     }
84
85     /**
86      * Get an enumeration of all unique parameter names. Each parameter may have
87      * multiple values.
88      *
89      * @return parameter names
90      */

91     public Enumeration JavaDoc getParameterNames() {
92         return parameterNames.elements();
93     }
94
95     /**
96      * Each parameter may have multiple values. This method allows you
97      * to retrieve a specific index in the list of values for the required
98      * parameter. If not such parameter exists <code>null</code> will be returned.
99      *
100      * @param name parameter name
101      * @param num index of parameter value
102      * @return parameter value
103      */

104     public String JavaDoc getParameter(String JavaDoc headerName, int num) {
105         Vector JavaDoc f = (Vector JavaDoc) parameters.get(headerName);
106         if (f == null)
107             return null;
108         else {
109             if (f.size() > num)
110                 return (String JavaDoc)f.elementAt(num);
111             else
112                 return null;
113         }
114     }
115
116     /**
117      * Get the first parameter for a given name.
118      *
119      * @param name parameter name
120      */

121     public String JavaDoc getParameter(String JavaDoc name) {
122         return getParameter(name, 0);
123     }
124
125     /**
126      * Set the value of a parameter. <b>This will replace any current
127      * value for this parameter, regardless of how many times it occurs</b>.
128      *
129      * @param name parameter name
130      * @param value header value
131      */

132     public void setParameter(String JavaDoc name, String JavaDoc value) {
133         if(value == null) {
134             throw new IllegalArgumentException JavaDoc("Null value");
135         }
136         Vector JavaDoc v = new Vector JavaDoc();
137         parameters.put(name, v);
138         if(!parameterNames.contains(name)) {
139             parameterNames.addElement(name);
140         }
141         v.addElement(value);
142     }
143
144     /**
145      * Remove all values for the named parameter.
146      *
147      * @param name parameter name
148      */

149     public void removeParameter(String JavaDoc name) {
150         if (parameters.containsKey(name)) {
151             parameterNames.removeElement(name);
152         }
153     }
154     
155     /**
156      * Remove <b>ALL</b> parameters
157      */

158     public void clearParameters() {
159         parameters.clear();
160         parameterNames.removeAllElements();
161     }
162
163     /**
164      * Get the number of values the provided parameter name has.
165      *
166      * @param name parameter name
167      * @return number of values
168      */

169     public int getHeaderFieldCount(String JavaDoc name) {
170         Vector JavaDoc f = (Vector JavaDoc) parameters.get(name);
171         return f == null ? 0 : f.size();
172     }
173
174     /**
175      * Get an array of all the parameter values. <code>null</code> will be
176      * returned if no such parameter exists.
177      *
178      * @param name parameter name
179      * @return values
180      */

181     public String JavaDoc[] getParameterValues(String JavaDoc name) {
182         Vector JavaDoc f = (Vector JavaDoc) parameters.get(name);
183         if (f != null) {
184             String JavaDoc[] values = new String JavaDoc[f.size()];
185             f.copyInto(values);
186             return values;
187         } else {
188             return null;
189         }
190     }
191
192     /**
193      * Get a vector of all the parameter values. <code>null</code> will be
194      * returned if no such parameter exists.
195      *
196      * @param name parameter name
197      * @return values
198      */

199     public Vector JavaDoc getParameterValueList(String JavaDoc name) {
200         return (Vector JavaDoc) parameters.get(name);
201     }
202
203 }
204
Popular Tags