/**
 * 
 */
package org.javarosa.j2me.reference;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import org.javarosa.core.reference.Reference;

/**
 * @author ctsims
 *
 */
public class HttpReference implements Reference {

	String URI;
	
	public HttpReference(String URI) {
		this.URI = URI;
	}

	/* (non-Javadoc)
	 * @see org.commcare.reference.Reference#doesBinaryExist()
	 */
	public boolean doesBinaryExist() throws IOException {
		//Do HTTP connection stuff? Look for a 404? 
		return true;
	}
	
	public InputStream getStream() throws IOException {
		HttpConnection connection = (HttpConnection)Connector.open(URI);
		connection.setRequestMethod(HttpConnection.GET);
		return connection.openInputStream();
	}
	
	public String getURI() {
		return URI;
	}

	/* (non-Javadoc)
	 * @see org.commcare.reference.Reference#isReadOnly()
	 */
	public boolean isReadOnly() {
		return true;
	}

	public OutputStream getOutputStream() throws IOException {
		//TODO: Support writing here?
		throw new IOException("JavaRosa HTTP References are readonly. Please use the transport manager for this op.");
	}

	public void remove() throws IOException {
		throw new IOException("JavaRosa HTTP References are readonly. Please use the transport manager for this op.");
	}

	public String getLocalURI() {
		return URI;
	}
	
	public Reference[] probeAlternativeReferences() {
		return new Reference [0];
	}
}

@@ -830,22 +830,40 @@
 ET);%0D%0A%09%09
-return
+InputStream httpStream =
  connect
@@ -882,24 +882,256 @@
 tStream();%0D%0A
+%09%09%0D%0A%09%09//This actually signals the connection to close as soon as the input stream%0D%0A%09%09//does, which we need, since after we pass it out, we have no way to manage%0D%0A%09%09//the connection.%0D%0A%09%09connection.close();%0D%0A%09%09%0D%0A%09%09return httpStream;%0D%0A
 %09%7D%0D%0A%09%0D%0A%09publ

Fix for http references not cleaning up after themselves when loading in files. Remote update should be working now, regardless of the number of external resources