1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/ExpectContinueMethod.java,v 1.5 2003/04/06 22:31:54 jsdever Exp $
3 * $Revision: 1.5 $
4 * $Date: 2003/04/06 22:31:54 $
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 * 'Expect: 100-continue' handshake should be used with caution,
81 * as it may cause problems with HTTP servers and proxies that
82 * do not support HTTP/1.1 protocol.
83 * </p>
84 *
85 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
86 *
87 * @since 2.0beta1
88 */
89
90 public abstract class ExpectContinueMethod extends GetMethod {
91
92 /*** This flag specifies whether "expect: 100-continue" handshake is
93 * to be used prior to sending the request body */
94 private boolean useExpectHeader = false;
95
96 /*** LOG object for this class. */
97 private static final Log LOG = LogFactory.getLog(ExpectContinueMethod.class);
98
99 /***
100 * No-arg constructor.
101 *
102 * @since 2.0
103 */
104 public ExpectContinueMethod() {
105 super();
106 }
107
108 /***
109 * Constructor specifying a URI.
110 *
111 * @param uri either an absolute or relative URI
112 *
113 * @since 2.0
114 */
115 public ExpectContinueMethod(String uri) {
116 super(uri);
117 }
118
119 /***
120 * Constructor specifying a URI and a tempDir.
121 *
122 * @param uri either an absolute or relative URI
123 * @param tempDir directory to store temp files in
124 *
125 * @deprecated the client is responsible for disk I/O
126 * @since 2.0
127 */
128 public ExpectContinueMethod(String uri, String tempDir) {
129 super(uri, tempDir);
130 }
131
132 /***
133 * Constructor specifying a URI, tempDir and tempFile.
134 *
135 * @param uri either an absolute or relative URI
136 * @param tempDir directory to store temp files in
137 * @param tempFile file to store temporary data in
138 *
139 * @deprecated the client is responsible for disk I/O
140 * @since 2.0
141 */
142 public ExpectContinueMethod(String uri, String tempDir, String tempFile) {
143 super(uri, tempDir, tempFile);
144 }
145
146 /***
147 * Returns the useExpectHeader.
148 *
149 * @return boolean
150 *
151 * @since 2.0beta1
152 */
153 public boolean getUseExpectHeader() {
154 return this.useExpectHeader;
155 }
156
157 /***
158 * Sets the useExpectHeader.
159 *
160 * <p>
161 * 'Expect: 100-continue' handshake should be used with
162 * caution, as it may cause problems with HTTP servers and
163 * proxies that do not support HTTP/1.1 protocol.
164 * </p>
165 *
166 * @param value The useExpectHeader to set
167 *
168 *
169 * @since 2.0beta1
170 */
171 public void setUseExpectHeader(boolean value) {
172 this.useExpectHeader = value;
173 }
174
175 /***
176 * Returns <tt>true</tt> if there is a request body to be sent.
177 * 'Expect: 100-continue' handshake may not be used if request
178 * body is not present
179 *
180 * @return boolean
181 *
182 * @since 2.0beta1
183 */
184 protected abstract boolean hasRequestContent();
185
186 /***
187 * Set the <tt>Expect</tt> header if it has not already been set,
188 * in addition to the "standard" set of headers.
189 *
190 * @param state the client state
191 * @param conn the connection to write to
192 *
193 * @throws HttpException when a protocol error occurs or state is invalid
194 * @throws IOException when i/o errors occur reading the response
195 */
196 protected void addRequestHeaders(HttpState state, HttpConnection conn)
197 throws IOException, HttpException {
198 LOG.trace("enter ExpectContinueMethod.addRequestHeaders(HttpState, HttpConnection)");
199
200 super.addRequestHeaders(state, conn);
201 // If the request is being retried, the header may already be present
202 boolean headerPresent = (getRequestHeader("Expect") != null);
203 // See if the expect header should be sent
204 // = HTTP/1.1 or higher
205 // = request body present
206
207 if (getUseExpectHeader() && isHttp11() && hasRequestContent()) {
208 if (!headerPresent) {
209 setRequestHeader("Expect", "100-continue");
210 }
211 } else {
212 if (headerPresent) {
213 removeRequestHeader("Expect");
214 }
215 }
216 }
217 }
This page was automatically generated by Maven