hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 35372] - The InputStreamRequestEntity doesnt make sense for chunked streams
Date Fri, 17 Jun 2005 17:36:10 GMT
DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG·
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=35372>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND·
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=35372





------- Additional Comments From boris.tabenkin@matrixone.com  2005-06-17 19:36 -------
Thanks,

Well here is the code I use for streaming to an output stream, maybe you'll find
it helpfull. Perhaps a special mode that does now allow a retry

package example;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

public class Example {
	static class SubmitWorker extends Thread {
		private final HttpClient _client;

		private final PostMethod _method;

		private Exception _ex;

		private InputStream _in;

		public SubmitWorker(HttpClient client, PostMethod method) {
			_client = client;
			_method = method;
		}
		public void setInputStream(InputStream in)
		{
			_in = in;
		}
		/**
		 * @return
		 */
		public Exception getException() {
			return _ex;
		}

		public void run() {
			try {
				_client.executeMethod(_method);
			} catch (Exception e) {
				_ex = e;
				try {
					// if an exception happens in the method, we need
					// to force the parent thread to abort it's write
					// the close forces the writer to get an error
					_in.close();
				} catch (IOException e1) {
				}

			} finally {
			}
		}
	}

	private static void checkStatus(PostMethod httppost) throws Exception {
		// We check for status, this can fail due to NPE, so we need
		// to ignore that and let the caller throw the real exception
		// that is set by the worker.
		try {
			if (httppost.getStatusCode() == HttpStatus.SC_OK) {
				return;
			}
		} catch (Exception ex) {
			return;
		}
		throw new Exception("Request failed - \n"
				+ httppost.getResponseBodyAsString());

	}

	public static OutputStream getOutputStream(PostMethod httppost, HttpClient
client, SubmitWorker worker)
			throws Exception

	{
		PipedOutputStream pipedOut = new PipedOutputStream();
		InputStream pipedIn = new PipedInputStream(pipedOut);
		httppost.setRequestBody(pipedIn);
		worker.setInputStream(pipedIn);
		worker.start();
		return pipedOut;
	}

	public static void main(String[] args) {
		try {
			if (args.length != 2)
			{
				System.err.println("Usage: url inReadFile");
				System.exit(1);
			}
			String url = args[0];
			String fileName = args[1];
			HttpClient client = new HttpClient();
			PostMethod method = new PostMethod(url);
			SubmitWorker worker = new SubmitWorker(client, method);

			try {
				BufferedInputStream in = new BufferedInputStream(
						new FileInputStream(fileName));
				BufferedOutputStream out = new BufferedOutputStream(
						getOutputStream(method, client, worker));
				worker.start();
				int count;
				int buffSize = 1024;
				byte[] buf = new byte[buffSize];
				boolean abort = false;
				while ((count = in.read(buf, 0, buffSize)) > 0 && !abort) {
					out.write(buf, 0, count);
				}
				in.close();
				worker.join();
			} catch (Exception e) {
				if (worker.getException() != null) {
					checkStatus(method);
					throw worker.getException();
				}
				throw e;
			} finally {
				method.releaseConnection();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Mime
View raw message