001: package com.meterware.httpunit;
002:
003: /********************************************************************************************************************
004: * $Id: MessageBodyWebRequest.java,v 1.12 2004/09/29 17:15:24 russgold Exp $
005: *
006: * Copyright (c) 2001-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:
024: import java.io.InputStream;
025: import java.io.IOException;
026: import java.io.OutputStream;
027:
028: import java.net.HttpURLConnection;
029: import java.net.URL;
030: import java.net.URLConnection;
031:
032: /**
033: * A web request which contains a non-empty message body. Note that such requests
034: * <em>must</em> use the <code>http</code> or <code>https</code> protocols.
035: **/
036: abstract public class MessageBodyWebRequest extends WebRequest {
037:
038: /**
039: * Constructs a web request using a specific absolute url string.
040: **/
041: protected MessageBodyWebRequest(String urlString) {
042: super (urlString);
043: }
044:
045: /**
046: * Constructs a web request with a specific target.
047: **/
048: protected MessageBodyWebRequest(URL urlBase, String urlString,
049: String target) {
050: super (urlBase, urlString, target);
051: }
052:
053: /**
054: * Constructs a web request for a form submitted via a button.
055: **/
056: protected MessageBodyWebRequest(WebForm sourceForm,
057: SubmitButton button, int x, int y) {
058: this (sourceForm, WebRequest.newParameterHolder(sourceForm),
059: button, x, y);
060: }
061:
062: /**
063: * Constructs a web request for a form submitted via a button.
064: *
065: * @since 1.6
066: **/
067: protected MessageBodyWebRequest(WebForm sourceForm,
068: ParameterHolder parameterHolder, SubmitButton button,
069: int x, int y) {
070: super (sourceForm, parameterHolder, button, x, y);
071: }
072:
073: /**
074: * Constructs a web request for a form submitted via script.
075: **/
076: protected MessageBodyWebRequest(WebForm sourceForm) {
077: super (sourceForm, WebRequest.newParameterHolder(sourceForm));
078: }
079:
080: /**
081: * Subclasses must override this method to provide a message body for the
082: * request.
083: **/
084: abstract protected MessageBody getMessageBody();
085:
086: //---------------------------------- WebRequest methods --------------------------------
087:
088: protected void writeMessageBody(OutputStream stream)
089: throws IOException {
090: getMessageBody().writeTo(stream);
091: }
092:
093: /**
094: * Performs any additional processing necessary to complete the request.
095: **/
096: protected void completeRequest(URLConnection connection)
097: throws IOException {
098: super .completeRequest(connection);
099: connection.setDoInput(true);
100: connection.setDoOutput(true);
101:
102: OutputStream stream = connection.getOutputStream();
103: writeMessageBody(stream);
104: stream.flush();
105: stream.close();
106: }
107:
108: protected String getContentType() {
109: return getMessageBody().getContentType();
110: }
111:
112: //============================= class InputStreamMessageBody ======================================
113:
114: /**
115: * A method request message body read directly from an input stream.
116: **/
117: public static class InputStreamMessageBody extends MessageBody {
118:
119: public InputStreamMessageBody(MessageBodyWebRequest request,
120: InputStream source, String contentType) {
121: super (request);
122: _source = source;
123: _contentType = contentType;
124: }
125:
126: /**
127: * Returns the content type of this message body.
128: **/
129: String getContentType() {
130: return _contentType;
131: }
132:
133: /**
134: * Transmits the body of this request as a sequence of bytes.
135: **/
136: void writeTo(OutputStream outputStream) throws IOException {
137: byte[] buffer = new byte[8 * 1024];
138: int count = 0;
139: do {
140: outputStream.write(buffer, 0, count);
141: count = _source.read(buffer, 0, buffer.length);
142: } while (count != -1);
143:
144: _source.close();
145: }
146:
147: private InputStream _source;
148: private String _contentType;
149: }
150: }
|