import com.allaire.cfx.*;
import java.io.*;
import java.net.*;

public class HTTP implements CustomTag {
      public void processRequest(Request request, Response response) throws Exception {
        if(!request.attributeExists("var")){
            throw new Exception("You have not specified a variable attribute to store the returned http request");
        }
        if(!request.attributeExists("url")){
            throw new Exception("You have not specified a url attribute to pass to the http call.");         
         }
         if(!request.attributeExists("method")) {
            throw new Exception("You have not specified a method attribute for the http call.");       
         }
	// reqired vars       
	String strVar = request.getAttribute("var");
	String strURL = request.getAttribute("url");
	String strMethod = request.getAttribute("method");
	// optional atrributes  
	String strPort = "";
	String strProxy = "";
	String strProxyport = "";
	String strUsername = "";
	String strPassword = "";
	String strName = "";
	String strColumns = "";
	String strStartrow = "";
	String strFilepath = "";
	String strFilename = "";
	String strDelimeter = "";
	String strTextqualifier = "";
	String strResolveURL = "";
	String strUserAgent = "";
	if(request.attributeExists("port")) {
	strPort =  request.getAttribute("port"); 
	}
	if(request.attributeExists("proxy")) {
	strProxy =  request.getAttribute("proxy"); 
	}
	if(request.attributeExists("proxyport")) {
	strProxyport =  request.getAttribute("proxyport"); 
	}
	if(request.attributeExists("username")) { 
	strUsername =  request.getAttribute("username"); 
	}
	if(request.attributeExists("password")) { 
	strPassword = request.getAttribute("password");  
	}
 	if(request.attributeExists("name")) { 
	strName = request.getAttribute("name"); 
	}
	if(request.attributeExists("columns")) { 
	strColumns  = request.getAttribute("columns"); 
	}
	if(request.attributeExists("startrow")) { 
	strStartrow  = request.getAttribute("startrow"); 
	}
	if(request.attributeExists("path")) { 
	strFilepath  = request.getAttribute("filepath"); 
	}
	if(request.attributeExists("name")) { 
	strFilename  = request.getAttribute("filename"); 
	}
	if(request.attributeExists("delimeter")) { 
	strDelimeter  = request.getAttribute("delimeter"); 
	}
	if(request.attributeExists("textqualifier")) { 
	strTextqualifier  = request.getAttribute("textqualifier"); 
	}
	if(request.attributeExists("resolveURL")) { 
	strResolveURL  = request.getAttribute("resolveURL"); 
	}
	/* Set the proxy server host and port */
    System.getProperties().setProperty("http.proxyHost", strProxy);
	System.getProperties().setProperty("http.proxyPort", strProxyport);
    /* Set up and encode the proxy credentials */
    String s = strUsername + ":" + strPassword;
    String se = new sun.misc.BASE64Encoder().encode(s.getBytes()); 
    try {
    /* Create a HttpURLConnection Object and set the properties */
    URL u = new URL(strURL);
    HttpURLConnection connection = (HttpURLConnection) u.openConnection();
	connection.setRequestProperty( "Accept: ","*/*,text/html,text/plain");
	connection.setRequestProperty( "Accept-Language: ","en-au");
	connection.setRequestProperty( "Accept-Encoding: ","gzip, deflate");
	//connection.setRequestProperty( "Connection: ","Keep-Alive");
		if(request.attributeExists("useragent")) { 
			connection.setRequestProperty( "User-Agent: ", request.getAttribute("UserAgent"));
		} else {
			connection.setRequestProperty( "User-Agent: ", "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		}
    connection.setRequestProperty("Proxy-authorization: ", "Basic " + se);
    connection.connect();
	InputStream i = connection.getInputStream();
	InputStreamReader isr = new InputStreamReader(i);
	BufferedReader br = new BufferedReader(isr);
	String line;
	String returnContent = "";
	File file = new File(strFilepath, strFilename);
	PrintWriter printwriter = new PrintWriter(new FileWriter(file));
		while ((line = br.readLine()) != null) {
		returnContent = returnContent + "\n" + line;
		printwriter.println(line);
		}
		response.setVariable(strVar, returnContent);
	} catch (IOException e) {
      e.printStackTrace();
    }
  }
}



