KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > Response


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit;
8
9
10 import java.net.HttpURLConnection JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.servlet.http.Cookie JavaDoc;
15
16
17 /**
18  * This is a basic response that the local client returns.
19  *
20  * @author Brian Pontarelli
21  * @since 2.0
22  * @version 2.0
23  */

24 public class Response {
25
26     private Request request;
27     private int status;
28     String JavaDoc text;
29     private HttpURLConnection JavaDoc connection;
30     private InputStream JavaDoc inputStream;
31     private Map JavaDoc cookies;
32
33
34     /**
35      * This constructs a new client with the given values. Only status is
36      * really required, however, this could be set to -1. The rest of the
37      * parameters can be null if need be.
38      *
39      * @param request The request that created this response
40      * @param status The status code of the response
41      * @param text The body of the response as text
42      * @param connection The http connection to the server, if applicable
43      * @param inputStream The InputStream used to read the response from
44      * @param cookies A Map of all the cookie in the response, keyed off
45      * the name of the cookie
46      */

47     public Response(Request request, int status, String JavaDoc text,
48         HttpURLConnection JavaDoc connection, InputStream JavaDoc inputStream, Map JavaDoc cookies)
49     {
50         this.request = request;
51         this.status = status;
52         this.text = text;
53         this.connection = connection;
54         this.inputStream = inputStream;
55         this.cookies = cookies;
56     }
57
58
59     /**
60      * Returns the http status code from the test.
61      *
62      * @return The http status code.
63      */

64     public int getStatus() {
65         return status;
66     }
67
68     /**
69      * Returns the request that was submitted to the client
70      *
71      * @return The original request to the client
72      */

73     public Request getRequest() {
74         return request;
75     }
76
77     /**
78      * Returns the connection that was used from the client to the server (in
79      * a client-server setup). This is an HTTP specific method and therefore
80      * can return null or throw an exception
81      *
82      * @return The connection to the server
83      */

84     public HttpURLConnection JavaDoc getConnection() {
85         return connection;
86     }
87
88     /**
89      * Returns the response as a text string. Normally this would be the HTML
90      * or other information returned from the server. This is an HTTP specific
91      * method and therefore can return null or throw an exception.
92      *
93      * @return The text of the response (excluding headers) as a string.
94      */

95     public String JavaDoc getText() {
96         return text;
97     }
98
99     /**
100      * Returns an input stream that can be used for reading the response data.
101      * Usually this stream will contain the response of an HTTP request which
102      * makes this method an HTTP specific method. Therefore, this method can
103      * return null or throw an exception.
104      *
105      * @return An input stream for reading the response data.
106      * @throws IOException If there was a problem getting the stream
107      */

108     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
109         return inputStream;
110     }
111
112     /**
113      * Return the first cookie found that has the specified name or null if not
114      * found. This method is an HTTP specific method and therefore can return
115      * null or throw an exception.
116      *
117      * @param name The name of the cookie to find
118      * @return The cookie or null if not found
119      */

120     public Cookie JavaDoc getCookie(String JavaDoc name) {
121         return (Cookie JavaDoc) cookies.get(name);
122     }
123
124     /**
125      * Returns all the cookies in the response. This is an HTTP specific method
126      * and therefore can return null or throw an exception.
127      *
128      * @return All the cookies in the response
129      */

130     public Cookie JavaDoc[] getCookies() {
131         return (Cookie JavaDoc []) cookies.values().toArray(new Cookie JavaDoc[0]);
132     }
133 }
134
Popular Tags