package com.chiorichan.framework;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.chiorichan.Loader;
import com.chiorichan.http.PersistentSession;

public class HttpUtilsWrapper extends WebUtils
{
	PersistentSession sess;
	
	public HttpUtilsWrapper(PersistentSession _sess)
	{
		sess = _sess;
	}
	
	protected String findPackagePath( String pack )
	{
		try
		{
			Site site = sess.getSite();
			return findPackagePathWithException( pack, site );
		}
		catch ( FileNotFoundException e )
		{
			try
			{
				Site site = Loader.getSiteManager().getSiteById( "framework" );
				return findPackagePathWithException( pack, site );
			}
			catch ( FileNotFoundException e1 )
			{
				Loader.getLogger().warning( e1.getMessage() );
				return "";
			}
		}
	}
	
	public String readPackage( String pack ) throws IOException
	{
		return readPackage( pack, sess.getSite() );
	}
	
	public String evalPackage( String pack ) throws IOException, CodeParsingException
	{
		Evaling eval = sess.getEvaling();
		
		return evalPackage( eval, pack, sess.getSite() );
	}
	
	public String evalFile( File file ) throws IOException, CodeParsingException
	{
		return evalFile( file.getAbsolutePath() );
	}
	
	public String evalFile( String absoluteFile ) throws IOException, CodeParsingException
	{
		Evaling eval = sess.getEvaling();
		return evalFile( eval, absoluteFile );
	}
	
	public String evalGroovy( String source ) throws IOException, CodeParsingException
	{
		return evalGroovy( source, "" );
	}
	
	public String evalGroovy( String source, String filePath ) throws IOException, CodeParsingException
	{
		Evaling eval = sess.getEvaling();
		
		return evalGroovy( eval, source, filePath );
	}
}

@@ -28,16 +28,54 @@
 ework;%0A%0A
+import java.io.ByteArrayOutputStream;%0A
 import j
@@ -152,16 +152,96 @@
 ception;
+%0Aimport java.io.InputStream;%0Aimport java.net.URL;%0Aimport java.net.URLConnection;
 %0A%0Aimport
@@ -309,16 +309,56 @@
 Session;
+%0Aimport com.sun.jersey.core.util.Base64;
 %0A%0Apublic
@@ -1876,10 +1876,795 @@
 h );%0A%09%7D%0A
+%09%0A%09public byte%5B%5D readUrl( String url ) throws IOException%0A%09%7B%0A%09%09return readUrl( url, null, null );%0A%09%7D%0A%09%0A%09public byte%5B%5D readUrl( String surl, String user, String pass ) throws IOException%0A%09%7B%0A%09%09ByteArrayOutputStream out = new ByteArrayOutputStream();%0A%09%09%0A%09%09URL url = new URL( surl );%0A%09%09URLConnection uc = url.openConnection();%0A%09%09if ( user != null %7C%7C pass != null )%0A%09%09%7B%0A%09%09%09String userpass = user + %22:%22 + pass;%0A%09%09%09String basicAuth = %22Basic %22 + new String( new Base64().encode( userpass.getBytes() ) );%0A%09%09%09uc.setRequestProperty( %22Authorization%22, basicAuth );%0A%09%09%7D%0A%09%09InputStream is = uc.getInputStream();%0A%09%09%0A%09%09byte%5B%5D byteChunk = new byte%5B4096%5D;%0A%09%09int n;%0A%09%09%0A%09%09while ( ( n = is.read( byteChunk ) ) %3E 0 )%0A%09%09%7B%0A%09%09%09out.write( byteChunk, 0, n );%0A%09%09%7D%0A%09%09%0A%09%09is.close();%0A%09%09%0A%09%09return out.toByteArray();%0A%09%7D%0A
 %7D%0A

Implemented the readUrl method. getFramework().getHttpUtils().readUrl( (url), [user], [pass] );