hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Chris Smith" <ch...@mindiq.com>
Subject Re: Put -method blocks
Date Wed, 29 Jan 2003 12:34:43 GMT
Maneesh,

This looks suspiciously like the same problem I had... that the proxy is not
returning the 100 response that HttpClient desires.  The HTTP RFC strongly
suggests that clients don't wait forever for this response, but HttpClient
does wait, at least until it times out (in my case, IIRC, that took about 10
minutes) and fails claiming an invalid response.

My solution was:

package com.mindiq.mindiqweb.devlite.upload;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.apache.commons.httpclient.ChunkedOutputStream;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.methods.PutMethod;

public class SimplePutMethod extends PutMethod
{
    private File file = null;
    private byte[] data = null;
    private URL url = null;

    public SimplePutMethod()
    {
        super();
    }

    public SimplePutMethod(String path)
    {
        super(path);
    }

    public void setRequestBody(byte[] arg0)
    {
        super.setRequestBody(arg0);
        this.data = arg0;
    }

    public void setRequestBody(File arg0) throws IOException
    {
        super.setRequestBody(arg0);
        this.file = arg0;
    }

    public void setRequestBody(InputStream arg0) throws IOException
    {
        byte[] buffer = new byte[4096];
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int nb = 0;
        while (true) {
            nb = arg0.read(buffer);
            if (nb == -1) {
                break;
            }
            os.write(buffer, 0, nb);
        }

        setRequestBody(os.toByteArray());
    }

    public void setRequestBody(String arg0)
    {
        super.setRequestBody(arg0);
    }

    public void setRequestBody(URL arg0) throws IOException
    {
        super.setRequestBody(arg0);
        this.url = arg0;
    }

    protected void addRequestHeaders(HttpState arg0, HttpConnection arg1)
        throws IOException, HttpException
    {
        super.addRequestHeaders(arg0, arg1);

        /*
         * Don't expect a 100-continue.  This has the side-effect of
         * clobbering other expectations, which is suitable for our
         * purposes.
         */
        if (getRequestHeader("expect") != null)
        {
            removeRequestHeader("expect");
        }
    }

    protected boolean writeRequestBody(HttpState state, HttpConnection conn)
        throws IOException, HttpException
    {
        OutputStream out = conn.getRequestOutputStream();
        if (isHttp11() && (null == getRequestHeader("Content-Length")))
        {
            out = new ChunkedOutputStream(out);
        }

        InputStream inputStream = null;
        if (file != null && file.exists())
        {
            inputStream = new FileInputStream(file);
        }
        else if (url != null)
        {
            inputStream = url.openConnection().getInputStream();
        }
        else if (data != null)
        {
            inputStream = new ByteArrayInputStream(data);
        }
        else
        {
            return true;
        }

        byte[] buffer = new byte[4096];
        int nb = 0;
        while (true)
        {
            nb = inputStream.read(buffer);
            if (nb == -1)
            {
                break;
            }
            out.write(buffer, 0, nb);
        }
        out.flush();
        return true;
    }
}

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Mime
View raw message