1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/ExpectContinueMethod.java,v 1.5.2.2 2003/08/09 19:36:39 olegk Exp $
3 * $Revision: 1.5.2.2 $
4 * $Date: 2003/08/09 19:36:39 $
5 *
6 * ====================================================================
7 *
8 * The Apache Software License, Version 1.1
9 *
10 * Copyright (c) 2003 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
23 * distribution.
24 *
25 * 3. The end-user documentation included with the redistribution, if
26 * any, must include the following acknowlegement:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowlegement may appear in the software itself,
30 * if and wherever such third-party acknowlegements normally appear.
31 *
32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33 * Foundation" must not be used to endorse or promote products derived
34 * from this software without prior written permission. For written
35 * permission, please contact apache@apache.org.
36 *
37 * 5. Products derived from this software may not be called "Apache"
38 * nor may "Apache" appear in their names without prior written
39 * permission of the Apache Group.
40 *
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 * ====================================================================
54 *
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
59 *
60 * [Additional notices, if required by prior licensing conditions]
61 *
62 */
63
64 package org.apache.commons.httpclient.methods;
65
66 import java.io.IOException;
67 import org.apache.commons.httpclient.HttpConnection;
68 import org.apache.commons.httpclient.HttpException;
69 import org.apache.commons.httpclient.HttpState;
70 import org.apache.commons.logging.Log;
71 import org.apache.commons.logging.LogFactory;
72
73 /***
74 * <p>
75 * This abstract class serves as a foundation for all HTTP methods
76 * that support 'Expect: 100-continue' handshake.
77 * </p>
78 *
79 * <p>
80 * The purpose of the 100 (Continue) status (refer to section 10.1.1
81 * of the RFC 2616 for more details) is to allow a client that is
82 * sending a request message with a request body to determine if the
83 * origin server is willing to accept the request (based on the request
84 * headers) before the client sends the request body. In some cases,
85 * it might either be inappropriate or highly inefficient for the
86 * client to send the body if the server will reject the message
87 * without looking at the body.
88 * </p>
89 *
90 * <p>
91 * 'Expect: 100-continue' handshake should be used with caution,
92 * as it may cause problems with HTTP servers and proxies that
93 * do not support HTTP/1.1 protocol.
94 * </p>
95 *
96 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
97 *
98 * @since 2.0beta1
99 */
100
101 public abstract class ExpectContinueMethod extends GetMethod {
102
103 /*** This flag specifies whether "expect: 100-continue" handshake is
104 * to be used prior to sending the request body */
105 private boolean useExpectHeader = false;
106
107 /*** LOG object for this class. */
108 private static final Log LOG = LogFactory.getLog(ExpectContinueMethod.class);
109
110 /***
111 * No-arg constructor.
112 *
113 * @since 2.0
114 */
115 public ExpectContinueMethod() {
116 super();
117 }
118
119 /***
120 * Constructor specifying a URI.
121 *
122 * @param uri either an absolute or relative URI
123 *
124 * @since 2.0
125 */
126 public ExpectContinueMethod(String uri) {
127 super(uri);
128 }
129
130 /***
131 * Constructor specifying a URI and a tempDir.
132 *
133 * @param uri either an absolute or relative URI
134 * @param tempDir directory to store temp files in
135 *
136 * @deprecated the client is responsible for disk I/O
137 * @since 2.0
138 */
139 public ExpectContinueMethod(String uri, String tempDir) {
140 super(uri, tempDir);
141 }
142
143 /***
144 * Constructor specifying a URI, tempDir and tempFile.
145 *
146 * @param uri either an absolute or relative URI
147 * @param tempDir directory to store temp files in
148 * @param tempFile file to store temporary data in
149 *
150 * @deprecated the client is responsible for disk I/O
151 * @since 2.0
152 */
153 public ExpectContinueMethod(String uri, String tempDir, String tempFile) {
154 super(uri, tempDir, tempFile);
155 }
156
157 /***
158 * <p>
159 * Returns <tt>true</tt> if the 'Expect: 100-Continue' handshake
160 * is activated. The purpose of the 'Expect: 100-Continue'
161 * handshake to allow a client that is sending a request message
162 * with a request body to determine if the origin server is
163 * willing to accept the request (based on the request headers)
164 * before the client sends the request body.
165 * </p>
166 *
167 * @return <tt>true</tt> if 'Expect: 100-Continue' handshake is to
168 * be used, <tt>false</tt> otherwise.
169 *
170 * @since 2.0beta1
171 */
172 public boolean getUseExpectHeader() {
173 return this.useExpectHeader;
174 }
175
176 /***
177 * <p>
178 * Activates 'Expect: 100-Continue' handshake. The purpose of
179 * the 'Expect: 100-Continue' handshake to allow a client that is
180 * sending a request message with a request body to determine if
181 * the origin server is willing to accept the request (based on
182 * the request headers) before the client sends the request body.
183 * </p>
184 *
185 * <p>
186 * The use of the 'Expect: 100-continue' handshake can result in
187 * noticable peformance improvement for entity enclosing requests
188 * (such as POST and PUT) that require the target server's
189 * authentication.
190 * </p>
191 *
192 * <p>
193 * 'Expect: 100-continue' handshake should be used with
194 * caution, as it may cause problems with HTTP servers and
195 * proxies that do not support HTTP/1.1 protocol.
196 * </p>
197 *
198 * @param value boolean value
199 *
200 *
201 * @since 2.0beta1
202 */
203 public void setUseExpectHeader(boolean value) {
204 this.useExpectHeader = value;
205 }
206
207 /***
208 * Returns <tt>true</tt> if there is a request body to be sent.
209 * 'Expect: 100-continue' handshake may not be used if request
210 * body is not present
211 *
212 * @return boolean
213 *
214 * @since 2.0beta1
215 */
216 protected abstract boolean hasRequestContent();
217
218 /***
219 * Sets the <tt>Expect</tt> header if it has not already been set,
220 * in addition to the "standard" set of headers.
221 *
222 * @param state the {@link HttpState state} information associated with this method
223 * @param conn the {@link HttpConnection connection} used to execute
224 * this HTTP method
225 *
226 * @throws IOException if an I/O (transport) error occurs
227 * @throws HttpException if a protocol exception occurs.
228 * @throws HttpRecoverableException if a recoverable transport error occurs.
229 * Usually this kind of exceptions can be recovered from by
230 * retrying the HTTP method
231 */
232 protected void addRequestHeaders(HttpState state, HttpConnection conn)
233 throws IOException, HttpException {
234 LOG.trace("enter ExpectContinueMethod.addRequestHeaders(HttpState, HttpConnection)");
235
236 super.addRequestHeaders(state, conn);
237 // If the request is being retried, the header may already be present
238 boolean headerPresent = (getRequestHeader("Expect") != null);
239 // See if the expect header should be sent
240 // = HTTP/1.1 or higher
241 // = request body present
242
243 if (getUseExpectHeader() && isHttp11() && hasRequestContent()) {
244 if (!headerPresent) {
245 setRequestHeader("Expect", "100-continue");
246 }
247 } else {
248 if (headerPresent) {
249 removeRequestHeader("Expect");
250 }
251 }
252 }
253 }
This page was automatically generated by Maven