avatar

964

Fix the 'Buy' operation. [#742 state:resolved tagged:committed] by mrs, 06 Jul, 2009 10:07 AM
Diff this changeset:
Http.java
cyberfox 1   package com.jbidwatcher.util.http;
cyberfox 2   /*
cyberfox 3    * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved.
cyberfox 4    *
cyberfox 5    * Developed by mrs (Morgan Schweers)
cyberfox 6    */
cyberfox 7   
mrs      8   import com.jbidwatcher.util.config.JConfig;
mrs      9   import com.jbidwatcher.util.config.Base64;
cyberfox 10  import com.jbidwatcher.util.ByteBuffer;
mrs      11  import com.jbidwatcher.util.Constants;
mrs      12  import com.jbidwatcher.util.Parameters;
mrs      13  import com.jbidwatcher.util.StringTools;
cyberfox 14  
cyberfox 15  import java.net.*;
cyberfox 16  import java.io.*;
mrs      17  import java.util.Map;
cyberfox 18  
cyberfox 19  public class Http {
mrs      20    private String mUsername = null;
mrs      21    private String mPassword = null;
mrs      22  
mrs      23    private static Http sInstance = new Http();
mrs      24    public static Http net() { return sInstance; }
mrs      25  
mrs      26    public void setAuthInfo(String user, String pass) {
mrs      27      mUsername = user;
mrs      28      mPassword = pass;
mrs      29    }
mrs      30  
mrs      31    private void setConnectionInfo(URLConnection huc) {
mrs      32      if(mUsername != null && mPassword != null) {
mrs      33        huc.setRequestProperty("Authorization", "Basic " + Base64.encodeString(mUsername + ':' + mPassword));
mrs      34      }
mrs      35      setConnectionProxyInfo(huc);
mrs      36    }
mrs      37  
mrs      38    private void setConnectionProxyInfo(URLConnection huc) {
cyberfox 39      if(JConfig.queryConfiguration("proxyfirewall", "none").equals("proxy")) {
cyberfox 40        String proxyHost = JConfig.queryConfiguration("proxy.host", null);
cyberfox 41  
cyberfox 42        if(proxyHost != null) {
cyberfox 43          String user = JConfig.queryConfiguration("proxy.user", null);
cyberfox 44          String pass = JConfig.queryConfiguration("proxy.pass", null);
cyberfox 45  
cyberfox 46          System.setProperty("http.proxyUser", user);
cyberfox 47          System.setProperty("http.proxyPassword", pass);
cyberfox 48          if (user != null && pass != null) {
cyberfox 49            String str = user + ':' + pass;
mrs      50            String encoded = "Basic " + Base64.encodeString(str);
cyberfox 51            huc.setRequestProperty("Proxy-Authorization", encoded);
cyberfox 52          }
cyberfox 53        }
cyberfox 54      }
cyberfox 55    }
cyberfox 56  
mrs      57    public URLConnection postFormPage(String urlToPost, String cgiData, String cookie, String referer, boolean follow_redirects) {
cyberfox 58      URLConnection huc;
cyberfox 59  
cyberfox 60      try {
mrs      61        if(JConfig.queryConfiguration("debug.urls", "false").equals("true")) {
mrs      62          JConfig.log().logDebug("postFormPage: " + urlToPost);
mrs      63        }
mrs      64        URL authURL = new URL(urlToPost);
cyberfox 65  
cyberfox 66        huc = authURL.openConnection();
mrs      67        setConnectionInfo(huc);
cyberfox 68        huc.setDoOutput(true);
cyberfox 69  
cyberfox 70        if(huc instanceof HttpURLConnection) {
mrs      71          HttpURLConnection conn = (HttpURLConnection)huc;
mrs      72          conn.setRequestMethod("POST");
mrs      73          if(!follow_redirects) conn.setInstanceFollowRedirects(false);
cyberfox 74        }
mrs      75        if(JConfig.queryConfiguration("debug.uber", "false").equals("true") && JConfig.debugging) {
mrs      76          dumpFormHeaders(System.err, cgiData, cookie);
cyberfox 77        }
cyberfox 78  
cyberfox 79        huc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
mrs      80        huc.setRequestProperty("Content-Length", Integer.toString(cgiData.length()));
mrs      81        huc.setRequestProperty("User-Agent", Constants.FAKE_BROWSER);
cyberfox 82        if(referer != null) huc.setRequestProperty("Referer", referer);
cyberfox 83        if(cookie != null) {
cyberfox 84          huc.setRequestProperty("Cookie", cookie);
cyberfox 85        }
mrs      86        PrintStream obw = new PrintStream(huc.getOutputStream());
mrs      87        obw.print(cgiData);
cyberfox 88        obw.close();
cyberfox 89      } catch(ConnectException ce) {
mrs      90        JConfig.log().logMessage("postFormPage: " + ce);
cyberfox 91        huc = null;
cyberfox 92      } catch(Exception e) {
mrs      93        JConfig.log().handleException("postFormPage: " + e, e);
cyberfox 94        huc = null;
cyberfox 95      }
cyberfox 96      return(huc);
cyberfox 97    }
cyberfox 98  
mrs      99    private static void dumpFormHeaders(PrintStream out, String cgiData, String cookie) {
mrs      100     if(cgiData != null) {
mrs      101       out.println("Content-Type: application/x-www-form-urlencoded");
mrs      102       out.println("Content-Length: " + Integer.toString(cgiData.length()));
mrs      103       out.println("User-Agent: " + Constants.FAKE_BROWSER);
mrs      104       out.println("Cookie: " + cookie);
mrs      105     } else {
mrs      106       out.println("CGI Data is null!");
mrs      107     }
mrs      108   }
cyberfox 109 
mrs      110   public URLConnection makeRequest(URL source, String cookie) throws java.io.IOException {
mrs      111     if(JConfig.queryConfiguration("debug.urls", "false").equals("true")) {
mrs      112       JConfig.log().logDebug("makeRequest: " + source.toString());
mrs      113     }
mrs      114     URLConnection uc = source.openConnection();
mrs      115     setConnectionInfo(uc);
cyberfox 116     if(cookie != null) {
cyberfox 117       uc.setRequestProperty("Cookie", cookie);
cyberfox 118     }
cyberfox 119 
cyberfox 120     //  We fake our user-agent, since some auction servers only let
cyberfox 121     //  you bid/read if we are a 'supported' browser.
mrs      122     uc.setRequestProperty("User-Agent", Constants.FAKE_BROWSER);
cyberfox 123 
cyberfox 124     return uc;
cyberfox 125   }
cyberfox 126 
mrs      127   public ByteBuffer getURL(URL dataURL) {
cyberfox 128     return getURL(dataURL, null);
cyberfox 129   }
cyberfox 130 
cyberfox 131   /** 
cyberfox 132    * @brief Retrieve data from HTTP in raw byte form.
cyberfox 133    * 
cyberfox 134    * @param dataURL - The URL of the raw data to retrieve.
cyberfox 135    * @param inCookie - Any cookie needed to be passed along.
cyberfox 136    * 
cyberfox 137    * @return - A result with raw data and the length.
cyberfox 138    */
mrs      139   private ByteBuffer getURL(URL dataURL, String inCookie) {
cyberfox 140     ByteBuffer rval;
cyberfox 141 
cyberfox 142     try {
cyberfox 143       rval = receiveData(makeRequest(dataURL, inCookie));
cyberfox 144     } catch(FileNotFoundException fnfe) {
cyberfox 145       //  It'd be great if we could pass along something that said, 'not here, never will be'.
cyberfox 146       rval = null;
cyberfox 147     } catch(IOException e) {
cyberfox 148       //  Mostly ignore HTTP 504 error, it's just a temporary 'gateway down' error.
cyberfox 149       if(e.getMessage().indexOf("HTTP response code: 504")==-1) {
mrs      150         JConfig.log().handleException("Error loading data URL (" + dataURL.toString() + ')', e);
cyberfox 151       } else {
mrs      152         JConfig.log().logMessage("HTTP 504 error loading URL (" + dataURL.toString() + ')');
cyberfox 153       }
cyberfox 154       rval = null;
cyberfox 155     }
cyberfox 156     return rval;
cyberfox 157   }
cyberfox 158 
mrs      159   /**
cyberfox 160    * @brief Retrieve raw data from an already existing URL connection.
mrs      161    *
cyberfox 162    * @param uc - The URLConnection to pull the data from.
mrs      163    *
cyberfox 164    * @return - A structure containing the raw data and the length.
mrs      165    * @throws java.io.IOException if an error occurs while reading the data.
cyberfox 166    */
mrs      167   private ByteBuffer receiveData(URLConnection uc) throws IOException {
cyberfox 168     InputStream is = uc.getInputStream();
mrs      169     return receiveStream(is);
mrs      170   }
mrs      171 
mrs      172   private ByteBuffer receiveStream(InputStream is) throws IOException {
mrs      173     int curMax = 32768;
cyberfox 174     byte[] mainBuf = new byte[curMax];
mrs      175 
mrs      176     int count = is.read(mainBuf, 0, curMax);
cyberfox 177     int offset = 0;
cyberfox 178 
cyberfox 179     while(count != -1) {
cyberfox 180       if(offset+count == curMax) {
cyberfox 181         curMax *= 3;
cyberfox 182         byte[] tmp = new byte[curMax];
cyberfox 183         System.arraycopy(mainBuf, 0, tmp, 0, offset+count);
cyberfox 184         mainBuf = tmp;
cyberfox 185       }
cyberfox 186       offset += count;
cyberfox 187       count = is.read(mainBuf, offset, curMax-offset);
cyberfox 188     }
mrs      189     is.close();
cyberfox 190     return new ByteBuffer(mainBuf, offset);
cyberfox 191   }
cyberfox 192 
mrs      193   public StringBuffer get(String url) {
mrs      194     try {
mrs      195       HttpURLConnection huc = (HttpURLConnection)getPage(url);
mrs      196       InputStream is = getStream(huc);
mrs      197       ByteBuffer results = receiveStream(is);
mrs      198       StringBuffer sb = convertByteBufferToStringBuffer(huc, results);
mrs      199       if((huc.getResponseCode() / 100) > 3) {
mrs      200         JConfig.log().logMessage("Failed to get " + url + ": " + sb);
mrs      201         return null;
mrs      202       }
mrs      203       return sb;
mrs      204     } catch (IOException ioe) {
mrs      205       JConfig.log().logDebug("Got an exception reading " + url + ": " + ioe.getMessage());
mrs      206       return null;
mrs      207     }
mrs      208   }
mrs      209 
mrs      210   public StringBuffer receivePage(URLConnection uc) throws IOException {
mrs      211     if(uc == null) return null;
mrs      212     ByteBuffer buff = receiveData(uc);
mrs      213 
mrs      214     return convertByteBufferToStringBuffer(uc, buff);
mrs      215   }
mrs      216 
mrs      217   private StringBuffer convertByteBufferToStringBuffer(URLConnection uc, ByteBuffer buff) throws UnsupportedEncodingException {
mrs      218     if(buff == null) return null;
mrs      219     String charset = uc.getContentType();
mrs      220     if(charset != null && charset.matches(".*charset=([^;]*).*")) {
mrs      221       charset = charset.replaceFirst(".*charset=([^;]*).*", "$1");
mrs      222       return new StringBuffer(new String(buff.getData(), 0, buff.getLength(), charset));
mrs      223     }
mrs      224     return new StringBuffer(new String(buff.getData(), 0, buff.getLength()));
mrs      225   }
mrs      226 
cyberfox 227   /**
cyberfox 228    * Simplest request, load a URL, no cookie, no referer, follow redirects blindly.
cyberfox 229    *
cyberfox 230    * @param urlToGet - The URL to load.
cyberfox 231    * @return - A URLConnection usable to retrieve the page requested.
cyberfox 232    */
mrs      233   public URLConnection getPage(String urlToGet) {
cyberfox 234     return(getPage(urlToGet, null, null, true));
cyberfox 235   }
cyberfox 236 
mrs      237   public URLConnection getPage(String urlToGet, String cookie, String referer, boolean redirect) {
cyberfox 238     HttpURLConnection huc;
cyberfox 239 
cyberfox 240     try {
mrs      241       if(JConfig.queryConfiguration("debug.urls", "false").equals("true")) {
mrs      242         JConfig.log().logDebug("getPage: " + urlToGet);
mrs      243       }
cyberfox 244       URL authURL = new URL(urlToGet);
cyberfox 245       URLConnection uc = authURL.openConnection();
cyberfox 246       if(!(uc instanceof HttpURLConnection)) {
cyberfox 247         return uc;
cyberfox 248       }
cyberfox 249       huc = (HttpURLConnection)uc;
cyberfox 250       huc.setInstanceFollowRedirects(redirect);
mrs      251       setConnectionInfo(huc);
cyberfox 252 
mrs      253       huc.setRequestProperty("User-Agent", Constants.FAKE_BROWSER);
cyberfox 254       if(referer != null) huc.setRequestProperty("Referer", referer);
cyberfox 255       if(cookie != null) huc.setRequestProperty("Cookie", cookie);
cyberfox 256     } catch(Exception e) {
mrs      257       JConfig.log().handleException("getPage: " + e, e);
cyberfox 258       huc = null;
cyberfox 259     }
cyberfox 260     return(huc);
cyberfox 261   }
mrs      262 
mrs      263   public String putTo(String url, String sb) {
mrs      264     HttpURLConnection huc = null;
mrs      265     String result = null;
mrs      266     try {
mrs      267       huc = (HttpURLConnection) new URL(url).openConnection();
mrs      268       setConnectionInfo(huc);
mrs      269       huc.setRequestProperty("Content-Type", "application/octet-stream");
mrs      270       huc.setRequestProperty("Content-Length", Integer.toString(sb.length() - 1));
mrs      271       huc.setRequestProperty("User-Agent", Constants.FAKE_BROWSER);
mrs      272 
mrs      273       huc.setRequestMethod("PUT");
mrs      274       huc.setDoOutput(true);
mrs      275       huc.getOutputStream().write(sb.getBytes());
mrs      276       result = StringTools.cat(huc.getInputStream());
mrs      277     } catch (MalformedURLException murle) {
mrs      278       JConfig.log().logMessage("Invalid URL!? (" + url + "): " + murle.getMessage());
mrs      279     } catch (IOException ioe) {
mrs      280       try { if(huc != null) result = StringTools.cat(huc.getErrorStream()); } catch (Exception ignored) { }
mrs      281     }
mrs      282     return result;
mrs      283   }
mrs      284 
mrs      285   public String postTo(String url, Parameters params) {
mrs      286     StringBuffer postData = null;
mrs      287     try {
mrs      288       postData = createCGIData(params);
mrs      289       URLConnection uc = postFormPage(url, postData.toString(), null, null, false);
mrs      290       StringBuffer sb = receivePage(uc);
mrs      291       return sb == null ? null : sb.toString();
mrs      292     } catch (IOException e) {
mrs      293       int length = 0;
mrs      294       if (postData != null) length = postData.length();
mrs      295       JConfig.log().logDebug("Couldn't send params (length: " + length + ") to " + url);
mrs      296       JConfig.log().logDebug(e.getMessage());
mrs      297       return null;
mrs      298     }
mrs      299   }
mrs      300 
mrs      301   private static StringBuffer createCGIData(Parameters data) throws UnsupportedEncodingException {
mrs      302     StringBuffer postData = new StringBuffer();
mrs      303     boolean first = true;
mrs      304     for (Map.Entry<Object, Object> param : data.entrySet()) {
mrs      305       Object key = param.getKey();
mrs      306       Object value = param.getValue();
mrs      307 
mrs      308       if (value != null) {
mrs      309         if (!first)
mrs      310           postData.append('&');
mrs      311         else
mrs      312           first = false;
mrs      313 
mrs      314         postData.append(key.toString());
mrs      315         postData.append('=');
mrs      316         postData.append(URLEncoder.encode(value.toString(), "UTF-8"));
mrs      317       }
mrs      318     }
mrs      319     return postData;
mrs      320   }
mrs      321 
mrs      322   public InputStream getStream(HttpURLConnection huc) {
mrs      323     if(huc == null) return null;
mrs      324     InputStream rval;
mrs      325 
mrs      326     try {
mrs      327       int status = huc.getResponseCode();
mrs      328       if (status / 100 == 4) {
mrs      329         rval = huc.getErrorStream();
mrs      330       } else if (status / 100 == 3) {
mrs      331         String location = huc.getHeaderField("Location");
mrs      332         huc = (HttpURLConnection)getPage(location);
mrs      333         rval = huc.getInputStream();
mrs      334       } else {
mrs      335         rval = huc.getInputStream();
mrs      336       }
mrs      337     } catch (IOException e) {
mrs      338       JConfig.log().logDebug("Error getting the stream from " + huc.getURL() + ": " + e.getMessage());
mrs      339       rval = null;
mrs      340     }
mrs      341     return rval;
mrs      342   }
cyberfox 343 }

Check out the code: svn co jbidwatcher/trunk/src/com/jbidwatcher/util/http/Http.java