Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

AbstractPost.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc ; 
00002 
00003 /*
00004  * Copyright 2003 Shawn Deleurme
00005  * Copyright 2003 Kevin O'Donnell
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 import java.io.BufferedReader;
00022 import java.io.PrintWriter;
00023 import java.io.BufferedWriter;
00024 import java.io.OutputStreamWriter; 
00025 import java.io.InputStreamReader; 
00026 import java.util.logging.Logger; 
00027 import java.util.logging.FileHandler; 
00028 import java.util.logging.SimpleFormatter; 
00029 import java.net.*;
00030 import java.util.*;
00031 import javax.net.ssl.SSLSocketFactory; 
00032 import javax.net.ssl.SSLSocket;  
00033 
00048 public abstract class AbstractPost {
00049 
00051         private int httpversion = 1; 
00053         private String server = ""; 
00055         private String script = ""; 
00057         private boolean debug = false; 
00058 
00062         public AbstractPost (javax.servlet.jsp.JspWriter out, String server, String script ){
00063                 this.server = server; 
00064                 this.script = script;   
00065         }
00066         
00067         public AbstractPost ( String server, String script ) throws Exception {
00068                 this.server = server; 
00069                 this.script = script;   
00070                 debug = true ;
00071         }
00072 
00073         public AbstractPost ( String server, String script, int httpversion ) throws Exception {
00074                 this.httpversion = httpversion; 
00075                 this.server = server; 
00076                 this.script = script;   
00077                 debug = true ;
00078         }
00079 
00080 
00081         //
00082         //
00083         // these methods make up a provider.  They are protected to avoid accidental
00084         // execution by evil spirits. 
00085         //
00086         //
00087 
00091         protected void setServer( String server ) {
00092                 this.server = server ; 
00093         } 
00094 
00098         protected void setScript( String script ) {
00099                 this.script = script ; 
00100         } 
00101 
00112         protected Map executeRequest (Map input) throws Exception {
00113                 System.out.println ( "IN THE POST" ) ; 
00114                 System.out.println ( server ) ; 
00115                 Map map = encode ( input ) ; // this call allows processor specific map-munging.
00116                 
00117                 // get a SSL connection to the server
00118                 SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
00119                 SSLSocket s = (SSLSocket)sf.createSocket(server, 443);
00120 
00121                 // create a PrintWriter for sending request to the server
00122                 PrintWriter o = new PrintWriter(
00123                         new BufferedWriter(
00124                                 new OutputStreamWriter(
00125                                         s.getOutputStream())));
00126 
00127                 // send the HTTP header name/value pairs
00128                 o.println("POST "+script+" HTTP/1."+httpversion);
00129                 o.println("Content-Type: application/x-www-form-urlencoded");
00130                 o.println("User-Agent: threebit");
00131                 o.println("Host: "+server);
00132                 o.println("Connection: close"); // o.println("Connection:Keep-Alive"); 
00133 
00134                 // build the body by iterating through the specifed HashMap 
00135                 String body = new String();
00136                 for (Iterator i = map.keySet().iterator(); i.hasNext() ; ) {
00137                         try {
00138                                 String key = (String) i.next();
00139                                 // getParameterMap returns an ARRAY of strings, not strings. 
00140                                 // i always get fucked by this one. 
00141                                 if(debug)System.out.print ( "key = " + key  + "\t\t\t" );  
00142                                 Object obj = map.get(key);  
00143                                 // Fix maps called above fixes this. 
00144                                 if (obj instanceof String) {
00145                                         String value = (String) obj; 
00146                                         if(debug)System.out.println ( "value = "+  value );  
00147                                         body += URLEncoder.encode(key,"UTF-8") 
00148                                                 + "=" + URLEncoder.encode(value,"UTF-8") + "&"; //i want to be a stringbuffer 
00149                                 }
00150                                 // This is for when you call me with the getParameterMap.  Of course, i don't 
00151                                 // deal with *actual* arrays yet. 
00152                                 else if (obj instanceof String[] ){
00153                                         String[] values = (String[]) obj; 
00154                                         if(debug) System.out.println ( "value = "+  values[0] );  
00155                                         body += URLEncoder.encode(key,"UTF-8") 
00156                                                 + "=" + URLEncoder.encode(values[0],"UTF-8") + "&"; //i want to be a stringbuffer 
00157                                 }
00158                         } 
00159                         catch ( Exception e ) {
00160                                 // do not die here. 
00161                                 e.printStackTrace(System.out);  
00162                         }
00163                 }
00164                 // System.out.println ( body ) ; 
00165 
00166                 // specify the content length and the blank line following the HTTP heades
00167                 o.println("Content-Length: "+body.length());
00168                 o.println();
00169 
00170                 // send the body and flush the output
00171                 o.print(body);
00172                 o.flush();
00173 
00174                 // create a buffered reader for reading the server's response
00175                 BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
00176 
00177                 // NOTE: this method is not yet validating the HTTP response codes.
00178                 Map decoded = decode ( in ) ; 
00179 
00180                 // This is a fix for any keys that go missing from the input. 
00181                 decoded.putAll ( input ) ; 
00182                         
00183                 // close the socket, this closes the output stream
00184                 s.close();
00185                 in.close(); 
00186 
00187                 // return the response.
00188                 return decoded;
00189         }
00190 
00196         protected abstract Map encode ( Map unencoded ) throws Exception ;
00197 
00205         protected abstract Map decode ( BufferedReader in ) throws Exception; 
00206 
00210         private void setLogger ( String log ) {
00211                 try { 
00212                         /*
00213                         FileHandler handler = new FileHandler(log , true);
00214                         handler.setFormatter(new net.threebit.utils.sosc.TerseLoggingFormatter());
00215                         logger.addHandler(handler);
00216                         */ 
00217                 }       
00218                 catch ( Exception e ) {
00219                         // so bad. 
00220                         e.printStackTrace( System.out ) ; 
00221                 } 
00222         }
00223 }

Generated on Mon Jul 14 17:19:19 2003 for SOSC by doxygen1.2.15