001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.client;
029
030import java.io.IOException;
031import java.io.InputStream;
032import java.io.OutputStream;
033
034import org.apache.http.Header;
035import org.apache.http.HttpEntity;
036import org.apache.http.HttpEntityEnclosingRequest;
037import org.apache.http.ProtocolException;
038import org.apache.http.entity.HttpEntityWrapper;
039import org.apache.http.protocol.HTTP;
040
041/**
042 * A wrapper class for {@link HttpEntityEnclosingRequest}s that can
043 * be used to change properties of the current request without
044 * modifying the original object.
045 * <p>
046 * This class is also capable of resetting the request headers to
047 * the state of the original request.
048 * </p>
049 *
050 * @since 4.0
051 *
052 * @deprecated (4.3) do not use.
053 */
054@Deprecated
055public class EntityEnclosingRequestWrapper extends RequestWrapper
056    implements HttpEntityEnclosingRequest {
057
058    private HttpEntity entity;
059    private boolean consumed;
060
061    public EntityEnclosingRequestWrapper(final HttpEntityEnclosingRequest request)
062        throws ProtocolException {
063        super(request);
064        setEntity(request.getEntity());
065    }
066
067    @Override
068    public HttpEntity getEntity() {
069        return this.entity;
070    }
071
072    @Override
073    public void setEntity(final HttpEntity entity) {
074        this.entity = entity != null ? new EntityWrapper(entity) : null;
075        this.consumed = false;
076    }
077
078    @Override
079    public boolean expectContinue() {
080        final Header expect = getFirstHeader(HTTP.EXPECT_DIRECTIVE);
081        return expect != null && HTTP.EXPECT_CONTINUE.equalsIgnoreCase(expect.getValue());
082    }
083
084    @Override
085    public boolean isRepeatable() {
086        return this.entity == null || this.entity.isRepeatable() || !this.consumed;
087    }
088
089    class EntityWrapper extends HttpEntityWrapper {
090
091        EntityWrapper(final HttpEntity entity) {
092            super(entity);
093        }
094
095        @Override
096        public void consumeContent() throws IOException {
097            consumed = true;
098            super.consumeContent();
099        }
100
101        @Override
102        public InputStream getContent() throws IOException {
103            consumed = true;
104            return super.getContent();
105        }
106
107        @Override
108        public void writeTo(final OutputStream outstream) throws IOException {
109            consumed = true;
110            super.writeTo(outstream);
111        }
112
113    }
114
115}