📄 entityenclosingmethod.java
字号:
/* * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v 1.39 2004/07/03 14:27:03 olegk Exp $ * $Revision: 480424 $ * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $ * * ==================================================================== * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */package org.apache.commons.httpclient.methods;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import org.apache.commons.httpclient.ChunkedOutputStream;import org.apache.commons.httpclient.Header;import org.apache.commons.httpclient.HttpConnection;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpState;import org.apache.commons.httpclient.HttpVersion;import org.apache.commons.httpclient.ProtocolException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;/** * This abstract class serves as a foundation for all HTTP methods * that can enclose an entity within requests * * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> * * @since 2.0beta1 * @version $Revision: 480424 $ */public abstract class EntityEnclosingMethod extends ExpectContinueMethod { // ----------------------------------------- Static variables/initializers /** * The content length will be calculated automatically. This implies * buffering of the content. * @deprecated Use {@link InputStreamRequestEntity#CONTENT_LENGTH_AUTO}. */ public static final long CONTENT_LENGTH_AUTO = InputStreamRequestEntity.CONTENT_LENGTH_AUTO; /** * The request will use chunked transfer encoding. Content length is not * calculated and the content is not buffered.<br> * @deprecated Use {@link #setContentChunked(boolean)}. */ public static final long CONTENT_LENGTH_CHUNKED = -1; /** LOG object for this class. */ private static final Log LOG = LogFactory.getLog(EntityEnclosingMethod.class); /** The unbuffered request body, if any. */ private InputStream requestStream = null; /** The request body as string, if any. */ private String requestString = null; private RequestEntity requestEntity; /** Counts how often the request was sent to the server. */ private int repeatCount = 0; /** The content length of the <code>requestBodyStream</code> or one of * <code>CONTENT_LENGTH_AUTO</code> and <code>CONTENT_LENGTH_CHUNKED</code>. * * @deprecated */ private long requestContentLength = InputStreamRequestEntity.CONTENT_LENGTH_AUTO; private boolean chunked = false; // ----------------------------------------------------------- Constructors /** * No-arg constructor. * * @since 2.0 */ public EntityEnclosingMethod() { super(); setFollowRedirects(false); } /** * Constructor specifying a URI. * * @param uri either an absolute or relative URI * * @since 2.0 */ public EntityEnclosingMethod(String uri) { super(uri); setFollowRedirects(false); } /** * Returns <tt>true</tt> if there is a request body to be sent. * * <P>This method must be overridden by sub-classes that implement * alternative request content input methods * </p> * * @return boolean * * @since 2.0beta1 */ protected boolean hasRequestContent() { LOG.trace("enter EntityEnclosingMethod.hasRequestContent()"); return (this.requestEntity != null) || (this.requestStream != null) || (this.requestString != null); } /** * Clears the request body. * * <p>This method must be overridden by sub-classes that implement * alternative request content input methods.</p> * * @since 2.0beta1 */ protected void clearRequestBody() { LOG.trace("enter EntityEnclosingMethod.clearRequestBody()"); this.requestStream = null; this.requestString = null; this.requestEntity = null; } /** * Generates the request body. * * <p>This method must be overridden by sub-classes that implement * alternative request content input methods.</p> * * @return request body as an array of bytes. If the request content * has not been set, returns <tt>null</tt>. * * @since 2.0beta1 */ protected byte[] generateRequestBody() { LOG.trace("enter EntityEnclosingMethod.renerateRequestBody()"); return null; } protected RequestEntity generateRequestEntity() { byte[] requestBody = generateRequestBody(); if (requestBody != null) { // use the request body, if it exists. // this is just for backwards compatability this.requestEntity = new ByteArrayRequestEntity(requestBody); } else if (this.requestStream != null) { this.requestEntity = new InputStreamRequestEntity( requestStream, requestContentLength); this.requestStream = null; } else if (this.requestString != null) { String charset = getRequestCharSet(); try { this.requestEntity = new StringRequestEntity( requestString, null, charset); } catch (UnsupportedEncodingException e) { if (LOG.isWarnEnabled()) { LOG.warn(charset + " not supported"); } try { this.requestEntity = new StringRequestEntity( requestString, null, null); } catch (UnsupportedEncodingException ignore) { } } } return this.requestEntity; } /** * Entity enclosing requests cannot be redirected without user intervention * according to RFC 2616. * * @return <code>false</code>. * * @since 2.0 */ public boolean getFollowRedirects() { return false; } /** * Entity enclosing requests cannot be redirected without user intervention * according to RFC 2616. * * @param followRedirects must always be <code>false</code> */ public void setFollowRedirects(boolean followRedirects) { if (followRedirects == true) { throw new IllegalArgumentException("Entity enclosing requests cannot be redirected without user intervention"); } super.setFollowRedirects(false); } /** * Sets length information about the request body. * * <p> * Note: If you specify a content length the request is unbuffered. This * prevents redirection and automatic retry if a request fails the first * time. This means that the HttpClient can not perform authorization * automatically but will throw an Exception. You will have to set the * necessary 'Authorization' or 'Proxy-Authorization' headers manually. * </p> * * @param length size in bytes or any of CONTENT_LENGTH_AUTO, * CONTENT_LENGTH_CHUNKED. If number of bytes or CONTENT_LENGTH_CHUNKED * is specified the content will not be buffered internally and the * Content-Length header of the request will be used. In this case * the user is responsible to supply the correct content length. * If CONTENT_LENGTH_AUTO is specified the request will be buffered * before it is sent over the network. * * @deprecated Use {@link #setContentChunked(boolean)} or * {@link #setRequestEntity(RequestEntity)} */ public void setRequestContentLength(int length) { LOG.trace("enter EntityEnclosingMethod.setRequestContentLength(int)"); this.requestContentLength = length; } /** * Returns the request's charset. The charset is parsed from the request entity's * content type, unless the content type header has been set manually. * * @see RequestEntity#getContentType() * * @since 3.0 */ public String getRequestCharSet() { if (getRequestHeader("Content-Type") == null) { // check the content type from request entity // We can't call getRequestEntity() since it will probably call // this method. if (this.requestEntity != null) { return getContentCharSet( new Header("Content-Type", requestEntity.getContentType())); } else { return super.getRequestCharSet(); } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -