001: package com.meterware.servletunit;
002:
003: /********************************************************************************************************************
004: * $Id: ServletUnitWebResponse.java,v 1.13 2004/06/13 20:57:28 russgold Exp $
005: *
006: * Copyright (c) 2000-2004, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import com.meterware.httpunit.WebResponse;
024: import com.meterware.httpunit.FrameSelector;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.IOException;
028: import java.net.URL;
029: import java.net.HttpURLConnection;
030:
031: import javax.servlet.http.HttpServletResponse;
032:
033: /**
034: * A response from to a request from the simulated servlet environment.
035: **/
036: class ServletUnitWebResponse extends WebResponse {
037:
038: /**
039: * Constructs a response object from a servlet response.
040: * @param frame the target frame on which the response will be displayed
041: * @param url the url from which the response was received
042: * @param response the response populated by the servlet
043: **/
044: ServletUnitWebResponse(ServletUnitClient client,
045: FrameSelector frame, URL url, HttpServletResponse response,
046: boolean throwExceptionOnError) throws IOException {
047: super (client, frame, url);
048: _response = (ServletUnitHttpResponse) response;
049: /** make sure that any IO exception for HTML received page happens here, not later. **/
050: if (getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
051: || !throwExceptionOnError) {
052: defineRawInputStream(new ByteArrayInputStream(_response
053: .getContents()));
054: if (getContentType().startsWith("text"))
055: loadResponseText();
056: }
057: }
058:
059: /**
060: * Constructs a response object from a servlet response.
061: * @param frame the target frame on which the response will be displayed
062: * @param url the url from which the response was received
063: * @param response the response populated by the servlet
064: **/
065: ServletUnitWebResponse(ServletUnitClient client,
066: FrameSelector frame, URL url, HttpServletResponse response)
067: throws IOException {
068: this (client, frame, url, response, true);
069: }
070:
071: /**
072: * Returns the response code associated with this response.
073: **/
074: public int getResponseCode() {
075: return _response.getStatus();
076: }
077:
078: /**
079: * Returns the response message associated with this response.
080: **/
081: public String getResponseMessage() {
082: return _response.getMessage();
083: }
084:
085: public String[] getHeaderFieldNames() {
086: return _response.getHeaderFieldNames();
087: }
088:
089: /**
090: * Returns the value for the specified header field. If no such field is defined, will return null.
091: **/
092: public String getHeaderField(String fieldName) {
093: return _response.getHeaderField(fieldName);
094: }
095:
096: public String[] getHeaderFields(String fieldName) {
097: return _response.getHeaderFields(fieldName);
098: }
099:
100: public String toString() {
101: return "[ _response = " + _response + "]";
102: }
103:
104: //-------------------------------------------- private members ------------------------------------------------
105:
106: private ServletUnitHttpResponse _response;
107:
108: }
|