Source code: com/meterware/httpunit/MessageBodyWebRequest.java
1 package com.meterware.httpunit;
2 /********************************************************************************************************************
3 * $Id: MessageBodyWebRequest.java,v 1.12 2004/09/29 17:15:24 russgold Exp $
4 *
5 * Copyright (c) 2001-2004, Russell Gold
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
16 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *******************************************************************************************************************/
22
23 import java.io.InputStream;
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 import java.net.HttpURLConnection;
28 import java.net.URL;
29 import java.net.URLConnection;
30
31
32 /**
33 * A web request which contains a non-empty message body. Note that such requests
34 * <em>must</em> use the <code>http</code> or <code>https</code> protocols.
35 **/
36 abstract
37 public class MessageBodyWebRequest extends WebRequest {
38
39
40 /**
41 * Constructs a web request using a specific absolute url string.
42 **/
43 protected MessageBodyWebRequest( String urlString ) {
44 super( urlString );
45 }
46
47
48 /**
49 * Constructs a web request with a specific target.
50 **/
51 protected MessageBodyWebRequest( URL urlBase, String urlString, String target ) {
52 super( urlBase, urlString, target );
53 }
54
55
56 /**
57 * Constructs a web request for a form submitted via a button.
58 **/
59 protected MessageBodyWebRequest( WebForm sourceForm, SubmitButton button, int x, int y ) {
60 this( sourceForm, WebRequest.newParameterHolder( sourceForm ), button, x, y );
61 }
62
63
64 /**
65 * Constructs a web request for a form submitted via a button.
66 *
67 * @since 1.6
68 **/
69 protected MessageBodyWebRequest( WebForm sourceForm, ParameterHolder parameterHolder, SubmitButton button, int x, int y ) {
70 super( sourceForm, parameterHolder, button, x, y );
71 }
72
73
74 /**
75 * Constructs a web request for a form submitted via script.
76 **/
77 protected MessageBodyWebRequest( WebForm sourceForm ) {
78 super( sourceForm, WebRequest.newParameterHolder( sourceForm ) );
79 }
80
81
82 /**
83 * Subclasses must override this method to provide a message body for the
84 * request.
85 **/
86 abstract
87 protected MessageBody getMessageBody();
88
89
90 //---------------------------------- WebRequest methods --------------------------------
91
92
93 protected void writeMessageBody( OutputStream stream ) throws IOException {
94 getMessageBody().writeTo( stream );
95 }
96
97
98 /**
99 * Performs any additional processing necessary to complete the request.
100 **/
101 protected void completeRequest( URLConnection connection ) throws IOException {
102 super.completeRequest( connection );
103 connection.setDoInput( true );
104 connection.setDoOutput( true );
105
106 OutputStream stream = connection.getOutputStream();
107 writeMessageBody( stream );
108 stream.flush();
109 stream.close();
110 }
111
112
113 protected String getContentType() {
114 return getMessageBody().getContentType();
115 }
116
117
118 //============================= class InputStreamMessageBody ======================================
119
120 /**
121 * A method request message body read directly from an input stream.
122 **/
123 public static class InputStreamMessageBody extends MessageBody {
124
125
126 public InputStreamMessageBody( MessageBodyWebRequest request, InputStream source, String contentType ) {
127 super( request );
128 _source = source;
129 _contentType = contentType;
130 }
131
132
133 /**
134 * Returns the content type of this message body.
135 **/
136 String getContentType() {
137 return _contentType;
138 }
139
140
141 /**
142 * Transmits the body of this request as a sequence of bytes.
143 **/
144 void writeTo( OutputStream outputStream ) throws IOException {
145 byte[] buffer = new byte[8 * 1024];
146 int count = 0;
147 do {
148 outputStream.write( buffer, 0, count );
149 count = _source.read( buffer, 0, buffer.length );
150 } while (count != -1);
151
152 _source.close();
153 }
154
155
156 private InputStream _source;
157 private String _contentType;
158 }
159 }