/**
 * 
 */
package us.monoid.web;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URLConnection;

/**
 * Abstract base class for all resource handlers you want to use with Resty.
 * 
 * It gives access to the underlying URLConnection and the current inputStream
 *
 * @author beders
 * 
 */
public abstract class AbstractResource extends Resty {
	protected URLConnection urlConnection;
	protected InputStream inputStream;

	public AbstractResource(Option... options) {
		super(options);
	}

	abstract String getAcceptedTypes();
	
	void fill(URLConnection anUrlConnection) throws IOException {
		urlConnection = anUrlConnection;
		try{
			inputStream = anUrlConnection.getInputStream();
		} catch (IOException e){
			// Per http://docs.oracle.com/javase/1.5.0/docs/guide/net/http-keepalive.html
			// (comparable documentation exists for later java versions)
			// if an HttpURLConnection was used clear the errorStream and close it
			// so that keep alive can keep doing its work
			if(anUrlConnection instanceof HttpURLConnection){
				InputStream es = ((HttpURLConnection)anUrlConnection).getErrorStream();
				// read the response body
				byte[] buf = new byte[1024];
				while (es.read(buf) > 0);
				// close the errorstream
				es.close();
			}
			throw e;
		}
	}

	public URLConnection getUrlConnection() {
		return urlConnection;
	}

	public HttpURLConnection http() {
		return (HttpURLConnection)urlConnection;
	}

	public InputStream stream() {
		return inputStream;
	}
	
	/**
	 * Check if the URLConnection has returned the specified responseCode
	 * 
	 * @param responseCode
	 * @return
	 */
	public boolean status(int responseCode) {
		if (urlConnection instanceof HttpURLConnection) {
			HttpURLConnection http = (HttpURLConnection) urlConnection;
			try {
				return http.getResponseCode() == responseCode;
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		} else
			return false;
	}

	/** Get the location header as URI. Returns null if there is no location header.
	 * 
	 */
	public URI location() {
		String loc = http().getHeaderField("Location");
		if (loc != null) {
				return URI.create(loc);
		}
		return null;
	}
}

@@ -48,134 +48,28 @@
 .io.
-IOException;%0Aimport java.io.InputStream;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URI;%0Aimport java.net.URLConnection
+*;%0Aimport java.net.*
 ;%0A%0A/
@@ -1011,26 +1011,33 @@
 %09%09%09%09
-InputStream es
+HttpURLConnection conn
  = 
-(
 (Htt
@@ -1070,171 +1070,535 @@
 tion
-).getErrorStream();%0A%09%09%09%09// read the response body%0A%09%09%09%09byte%5B%5D buf = new byte%5B1024%5D;%0A%09%09%09%09while (es.read(buf) %3E 0);%0A%09%09%09%09// close the errorstream%0A%09%09%09%09es.close();%0A%09%09%09%7D%0A
+;%0A%09%09%09%09InputStream es = new BufferedInputStream(conn.getErrorStream());%0A%0A%09%09%09%09ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A%0A%09%09%09%09// read the response body%0A%09%09%09%09int read;%0A%09%09%09%09while ((read = es.read()) != -1) %7B%0A%09%09%09%09%09baos.write(read);%0A%09%09%09%09%7D%0A%0A%09%09%09%09// close the errorstream%0A%09%09%09%09es.close();%0A%0A%09%09%09%09throw new IOException(%0A%09%09%09%09%09%22Error while reading from %22 + conn.getRequestMethod()  + %22: %5B%22 + conn.getResponseCode() + %22%5D %22 + %0A%09%09%09%09%09conn.getResponseMessage() + %22%5Cn%22 + new String(baos.toByteArray(), %22UTF-8%22), e%0A%09%09%09%09);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09t
@@ -1605,16 +1605,21 @@
 hrow e;%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Print the error stream in the message (it often contains details about the problem).