Source Code Cross Referenced for BaseWebRequest.java in  » Testing » jakarta-cactus » org » apache » cactus » internal » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta cactus » org.apache.cactus.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * ========================================================================
003:         * 
004:         * Copyright 2001-2004 The Apache Software Foundation.
005:         *
006:         * Licensed under the Apache License, Version 2.0 (the "License");
007:         * you may not use this file except in compliance with the License.
008:         * You may obtain a copy of the License at
009:         * 
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         * 
018:         * ========================================================================
019:         */
020:        package org.apache.cactus.internal;
021:
022:        import java.io.InputStream;
023:
024:        import java.util.Enumeration;
025:        import java.util.Hashtable;
026:        import java.util.Vector;
027:
028:        import org.apache.cactus.Cookie;
029:        import org.apache.cactus.WebRequest;
030:        import org.apache.cactus.client.authentication.Authentication;
031:        import org.apache.cactus.internal.configuration.Configuration;
032:        import org.apache.cactus.util.ChainedRuntimeException;
033:
034:        /**
035:         * Contains all HTTP request data for a test case but independently of
036:         * the fact that there is or there is not a Cactus redirector. It is the 
037:         * data that will be sent to the server side.
038:         *
039:         * @version $Id: BaseWebRequest.java 238991 2004-05-22 11:34:50Z vmassol $
040:         */
041:        public abstract class BaseWebRequest implements  WebRequest {
042:            /**
043:             * Cactus configuration
044:             */
045:            private Configuration configuration;
046:
047:            /**
048:             * The request parameters that need to be sent in the body (POST)
049:             */
050:            private Hashtable parametersPost = new Hashtable();
051:
052:            /**
053:             * The request parameters that need to be sent in the URL (GET)
054:             */
055:            private Hashtable parametersGet = new Hashtable();
056:
057:            /**
058:             * The Cookies
059:             */
060:            private Vector cookies = new Vector();
061:
062:            /**
063:             * HTTP Headers.
064:             */
065:            private Hashtable headers = new Hashtable();
066:
067:            /**
068:             * Binary data to send in the request body (if any)
069:             */
070:            private InputStream dataStream;
071:
072:            /**
073:             * The content type to set in the http request
074:             */
075:            private String contentType = "application/x-www-form-urlencoded";
076:
077:            /**
078:             * The Authentication Object that will configure the http request
079:             */
080:            private Authentication authentication;
081:
082:            /**
083:             * Default constructor that requires that 
084:             * {@link #setConfiguration(Configuration)} be called before the methods
085:             * requiring a configuration object.
086:             * 
087:             */
088:            public BaseWebRequest() {
089:            }
090:
091:            /**
092:             * @param theConfiguration the Cactus configuration
093:             */
094:            public BaseWebRequest(Configuration theConfiguration) {
095:                this .configuration = theConfiguration;
096:            }
097:
098:            /**
099:             * @return the Cactus configuration
100:             */
101:            protected Configuration getConfiguration() {
102:                return this .configuration;
103:            }
104:
105:            /**
106:             * @param theConfiguration the cactus configuration to assign to this 
107:             *        request
108:             */
109:            public void setConfiguration(Configuration theConfiguration) {
110:                this .configuration = theConfiguration;
111:            }
112:
113:            /**
114:             * @see WebRequest#setContentType(String)
115:             */
116:            public void setContentType(String theContentType) {
117:                this .contentType = theContentType;
118:            }
119:
120:            /**
121:             * @see WebRequest#getContentType()
122:             */
123:            public String getContentType() {
124:                return this .contentType;
125:            }
126:
127:            /**
128:             * @see WebRequest#setUserData(InputStream)
129:             */
130:            public void setUserData(InputStream theDataStream) {
131:                this .dataStream = theDataStream;
132:            }
133:
134:            /**
135:             * @see WebRequest#getUserData()
136:             */
137:            public InputStream getUserData() {
138:                return this .dataStream;
139:            }
140:
141:            /**
142:             * @see WebRequest#addParameter(String, String, String)
143:             */
144:            public void addParameter(String theName, String theValue,
145:                    String theMethod) {
146:                Hashtable parameters;
147:
148:                // Decide if the parameter is to be sent using in the url or not
149:                if (theMethod.equalsIgnoreCase(BaseWebRequest.POST_METHOD)) {
150:                    parameters = this .parametersPost;
151:                } else if (theMethod
152:                        .equalsIgnoreCase(BaseWebRequest.GET_METHOD)) {
153:                    parameters = this .parametersGet;
154:                } else {
155:                    throw new ChainedRuntimeException(
156:                            "The method need to be either "
157:                                    + "\"POST\" or \"GET\"");
158:                }
159:
160:                // If there is already a parameter of the same name, add the
161:                // new value to the Vector. If not, create a Vector an add it to the
162:                // hashtable
163:                if (parameters.containsKey(theName)) {
164:                    Vector v = (Vector) parameters.get(theName);
165:
166:                    v.addElement(theValue);
167:                } else {
168:                    Vector v = new Vector();
169:
170:                    v.addElement(theValue);
171:                    parameters.put(theName, v);
172:                }
173:            }
174:
175:            /**
176:             * @see WebRequest#addParameter(String, String)
177:             */
178:            public void addParameter(String theName, String theValue) {
179:                addParameter(theName, theValue, BaseWebRequest.GET_METHOD);
180:            }
181:
182:            /**
183:             * @see WebRequest#getParameterNamesPost()
184:             */
185:            public Enumeration getParameterNamesPost() {
186:                return getParameterNames(this .parametersPost);
187:            }
188:
189:            /**
190:             * @see WebRequest#getParameterNamesGet()
191:             */
192:            public Enumeration getParameterNamesGet() {
193:                return getParameterNames(this .parametersGet);
194:            }
195:
196:            /**
197:             * Returns all the values in the passed hashtable of parameters.
198:             *
199:             * @param theParameters the hashtable of parameters
200:             * @return the parameter names
201:             */
202:            private Enumeration getParameterNames(Hashtable theParameters) {
203:                return theParameters.keys();
204:            }
205:
206:            /**
207:             * @see WebRequest#getParameterGet(String)
208:             */
209:            public String getParameterGet(String theName) {
210:                String[] values = getParameterValuesGet(theName);
211:
212:                if (values != null) {
213:                    return values[0];
214:                }
215:
216:                return null;
217:            }
218:
219:            /**
220:             * @see WebRequest#getParameterPost(String)
221:             */
222:            public String getParameterPost(String theName) {
223:                String[] values = getParameterValuesPost(theName);
224:
225:                if (values != null) {
226:                    return values[0];
227:                }
228:
229:                return null;
230:            }
231:
232:            /**
233:             * @see WebRequest#getParameterValuesGet(String)
234:             */
235:            public String[] getParameterValuesGet(String theName) {
236:                return getParameterValues(theName, this .parametersGet);
237:            }
238:
239:            /**
240:             * @see WebRequest#getParameterValuesPost(String)
241:             */
242:            public String[] getParameterValuesPost(String theName) {
243:                return getParameterValues(theName, this .parametersPost);
244:            }
245:
246:            /**
247:             * Returns all the values corresponding to this parameter's name in the
248:             * provided hashtable.
249:             *
250:             * @param theName the parameter's name
251:             * @param theParameters the hashtable containing the parameters
252:             * @return the first value corresponding to this parameter's name or null
253:             *         if not found in the passed hashtable
254:             */
255:            private String[] getParameterValues(String theName,
256:                    Hashtable theParameters) {
257:                if (theParameters.containsKey(theName)) {
258:                    Vector v = (Vector) theParameters.get(theName);
259:
260:                    Object[] objs = new Object[v.size()];
261:
262:                    v.copyInto(objs);
263:
264:                    String[] result = new String[objs.length];
265:
266:                    for (int i = 0; i < objs.length; i++) {
267:                        result[i] = (String) objs[i];
268:                    }
269:
270:                    return result;
271:                }
272:
273:                return null;
274:            }
275:
276:            /**
277:             * @see WebRequest#addCookie(String, String)
278:             */
279:            public void addCookie(String theName, String theValue) {
280:                addCookie("localhost", theName, theValue);
281:            }
282:
283:            /**
284:             * @see WebRequest#addCookie(String, String, String)
285:             */
286:            public void addCookie(String theDomain, String theName,
287:                    String theValue) {
288:                addCookie(new Cookie(theDomain, theName, theValue));
289:            }
290:
291:            /**
292:             * @see WebRequest#addCookie(Cookie)
293:             */
294:            public void addCookie(Cookie theCookie) {
295:                if (theCookie == null) {
296:                    throw new IllegalStateException("The cookie cannot be null");
297:                }
298:                this .cookies.addElement(theCookie);
299:            }
300:
301:            /**
302:             * @see WebRequest#getCookies()
303:             */
304:            public Vector getCookies() {
305:                return this .cookies;
306:            }
307:
308:            /**
309:             * @see WebRequest#addHeader(String, String)
310:             */
311:            public void addHeader(String theName, String theValue) {
312:                // If the header is "Content-type", then call setContentType() instead.
313:                // This is to prevent the content type to be set twice.
314:                if (theName.equalsIgnoreCase("Content-type")) {
315:                    setContentType(theValue);
316:
317:                    return;
318:                }
319:
320:                // If there is already a header of the same name, add the
321:                // new header to the Vector. If not, create a Vector an add it to the
322:                // hashtable
323:                if (this .headers.containsKey(theName)) {
324:                    Vector v = (Vector) this .headers.get(theName);
325:
326:                    v.addElement(theValue);
327:                } else {
328:                    Vector v = new Vector();
329:
330:                    v.addElement(theValue);
331:                    this .headers.put(theName, v);
332:                }
333:            }
334:
335:            /**
336:             * @see WebRequest#getHeaderNames()
337:             */
338:            public Enumeration getHeaderNames() {
339:                return this .headers.keys();
340:            }
341:
342:            /**
343:             * @see WebRequest#getHeader(String)
344:             */
345:            public String getHeader(String theName) {
346:                String[] values = getHeaderValues(theName);
347:
348:                if (values != null) {
349:                    return values[0];
350:                }
351:
352:                return null;
353:            }
354:
355:            /**
356:             * @see WebRequest#getHeaderValues(String)
357:             */
358:            public String[] getHeaderValues(String theName) {
359:                if (this .headers.containsKey(theName)) {
360:                    Vector v = (Vector) this .headers.get(theName);
361:
362:                    Object[] objs = new Object[v.size()];
363:
364:                    v.copyInto(objs);
365:
366:                    String[] result = new String[objs.length];
367:
368:                    for (int i = 0; i < objs.length; i++) {
369:                        result[i] = (String) objs[i];
370:                    }
371:
372:                    return result;
373:                }
374:
375:                return null;
376:            }
377:
378:            /**
379:             * @return a string representation of the request
380:             */
381:            public String toString() {
382:                StringBuffer buffer = new StringBuffer();
383:
384:                // Append cookies
385:                buffer.append("cookies = [");
386:                buffer.append(toStringAppendCookies());
387:                buffer.append("], ");
388:
389:                // Append headers
390:                buffer.append("headers = [");
391:                buffer.append(toStringAppendHeaders());
392:                buffer.append("], ");
393:
394:                // Append parameters
395:                buffer.append("GET parameters = [");
396:                buffer.append(toStringAppendParametersGet());
397:                buffer.append("], ");
398:                buffer.append("POST parameters = [");
399:                buffer.append(toStringAppendParametersPost());
400:                buffer.append("]");
401:
402:                return buffer.toString();
403:            }
404:
405:            /**
406:             * @return a string representation of the headers
407:             */
408:            private String toStringAppendHeaders() {
409:                StringBuffer buffer = new StringBuffer();
410:
411:                Enumeration headers = getHeaderNames();
412:
413:                while (headers.hasMoreElements()) {
414:                    buffer.append("[");
415:
416:                    String headerName = (String) headers.nextElement();
417:                    String[] headerValues = getHeaderValues(headerName);
418:
419:                    buffer.append("[" + headerName + "] = [");
420:
421:                    for (int i = 0; i < (headerValues.length - 1); i++) {
422:                        buffer.append("[" + headerValues[i] + "], ");
423:                    }
424:
425:                    buffer.append("[" + headerValues[headerValues.length - 1]
426:                            + "]]");
427:                    buffer.append("]");
428:                }
429:
430:                return buffer.toString();
431:            }
432:
433:            /**
434:             * @return a string representation of the cookies
435:             */
436:            private String toStringAppendCookies() {
437:                StringBuffer buffer = new StringBuffer();
438:
439:                Enumeration cookies = getCookies().elements();
440:
441:                while (cookies.hasMoreElements()) {
442:                    Cookie cookie = (Cookie) cookies.nextElement();
443:
444:                    buffer.append("[" + cookie + "]");
445:                }
446:
447:                return buffer.toString();
448:            }
449:
450:            /**
451:             * @return a string representation of the parameters to be added in the
452:             *         request body
453:             */
454:            private String toStringAppendParametersPost() {
455:                return toStringAppendParameters(this .parametersPost);
456:            }
457:
458:            /**
459:             * @return a string representation of the parameters to be added in the
460:             *         URL
461:             */
462:            private String toStringAppendParametersGet() {
463:                return toStringAppendParameters(this .parametersGet);
464:            }
465:
466:            /**
467:             * @param theParameters the HTTP parameters
468:             * @return a string representation of the HTTP parameters passed as
469:             *         parameters
470:             */
471:            private String toStringAppendParameters(Hashtable theParameters) {
472:                StringBuffer buffer = new StringBuffer();
473:
474:                Enumeration parameters = getParameterNames(theParameters);
475:
476:                while (parameters.hasMoreElements()) {
477:                    buffer.append("[");
478:
479:                    String parameterName = (String) parameters.nextElement();
480:                    String[] parameterValues = getParameterValues(
481:                            parameterName, theParameters);
482:
483:                    buffer.append("[" + parameterName + "] = [");
484:
485:                    for (int i = 0; i < (parameterValues.length - 1); i++) {
486:                        buffer.append("[" + parameterValues[i] + "], ");
487:                    }
488:
489:                    buffer.append("["
490:                            + parameterValues[parameterValues.length - 1]
491:                            + "]]");
492:                    buffer.append("]");
493:                }
494:
495:                return buffer.toString();
496:            }
497:
498:            /**
499:             * @see WebRequest#setAuthentication(Authentication)
500:             */
501:            public void setAuthentication(Authentication theAuthentication) {
502:                this .authentication = theAuthentication;
503:            }
504:
505:            /**
506:             * @see WebRequest#getAuthentication()
507:             */
508:            public Authentication getAuthentication() {
509:                return this.authentication;
510:            }
511:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.