00001 package org.hfbk.util;
00002
00003 import java.io.BufferedReader;
00004 import java.io.DataInputStream;
00005 import java.io.DataOutputStream;
00006 import java.io.File;
00007 import java.io.FileInputStream;
00008 import java.io.FileOutputStream;
00009 import java.io.IOException;
00010 import java.io.InputStream;
00011 import java.io.InputStreamReader;
00012 import java.io.OutputStream;
00013 import java.io.UnsupportedEncodingException;
00014 import java.net.HttpURLConnection;
00015 import java.net.MalformedURLException;
00016 import java.net.URL;
00017 import java.net.URLConnection;
00018 import java.net.URLDecoder;
00019 import java.net.URLEncoder;
00020 import java.nio.charset.Charset;
00021 import java.util.regex.Matcher;
00022 import java.util.regex.Pattern;
00023
00024 import org.hfbk.vis.Prefs;
00025
00029 public class HTTPUtils {
00030
00036 public static BufferedReader connect(URL url, boolean silent) throws IOException{
00037 if(!silent) System.out.print("Connecting "+url);
00038 URLConnection con=url.openConnection();
00039 con.setConnectTimeout(Prefs.current.timeout);
00040 con.setRequestProperty("Accept-Language", Prefs.current.language);
00041 con.setReadTimeout(Prefs.current.timeout);
00042
00043 BufferedReader r=new BufferedReader(
00044 new InputStreamReader(con.getInputStream(),Charset.forName("utf8"))
00045 );
00046 if(!silent) System.out.print(", reading ");
00047
00048 return r;
00049 }
00050
00055 public static BufferedReader connectAsMozilla(URL url, boolean silent) throws IOException{
00056 if(!silent) System.out.print("Connecting "+url);
00057 URLConnection con=url.openConnection();
00058 con.setConnectTimeout(Prefs.current.timeout);
00059 con.setReadTimeout(Prefs.current.timeout);
00060
00061 con.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
00062 con.setRequestProperty("Accept-Language", Prefs.current.language);
00063 con.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
00064 con.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5");
00065
00066 BufferedReader r=new BufferedReader(
00067 new InputStreamReader(con.getInputStream(),Charset.forName("utf8"))
00068 );
00069 if(!silent) System.out.print(", reading ");
00070
00071 return r;
00072 }
00073
00074
00075 static void download(URL url, File target) throws IOException{
00076 System.out.print("Downloading "+url+"...");
00077 URLConnection con=url.openConnection();
00078 con.setConnectTimeout(Prefs.current.timeout);
00079 con.setReadTimeout(Prefs.current.timeout);
00080 InputStream is=con.getInputStream();
00081
00082 OutputStream os=new FileOutputStream(target);
00083
00084 byte[] buffer= new byte[4096];
00085 int bytesRead=0;
00086 while ((bytesRead=is.read(buffer))>0)
00087 os.write(buffer,0,bytesRead);
00088
00089 os.close();
00090 is.close();
00091 System.out.println("OK.");
00092 }
00093
00094
00100 public static String fetch(String url, boolean silent) {
00101 try{
00102 BufferedReader r=HTTPUtils.connectAsMozilla(new URL(url),silent);
00103
00104 String line;
00105 StringBuilder s=new StringBuilder(1024);
00106
00107 while ((line=r.readLine())!=null)
00108 s.append(line);
00109 r.close();
00110 if(!silent) System.out.println(s.length()+" chars finished.");
00111 return s.toString();
00112 }catch (Exception e) {
00113 if (!silent) throw new RuntimeException(e);
00114 return "";
00115 }
00116 }
00117
00127 public static String fetchUntil(String url, Pattern pattern, boolean silent) {
00128 try{
00129 BufferedReader r=HTTPUtils.connectAsMozilla(new URL(url),silent);
00130
00131 StringBuilder s=new StringBuilder(1024);
00132
00133 Matcher endMatcher=pattern.matcher("");
00134
00135 String line;
00136
00137 while ((line=r.readLine())!=null){
00138 s.append(line);
00139 endMatcher.reset(line);
00140 if (endMatcher.find())
00141 break;
00142 }
00143 r.close();
00144 if(!silent) System.out.println(s.length()+" chars finished.");
00145 return s.toString();
00146 }catch (Exception e) {
00147 if (!silent) throw new RuntimeException(e);
00148 return "";
00149 }
00150 }
00151
00155 public static String decode(String encoded){
00156 try {
00157 return URLDecoder.decode(encoded, "utf8");
00158 } catch (UnsupportedEncodingException e) {
00159 throw new RuntimeException (e);
00160 }
00161 }
00162
00166 public static String encode(String encode){
00167 try {
00168 return URLEncoder.encode(encode, "utf8");
00169 } catch (UnsupportedEncodingException e) {
00170 throw new RuntimeException (e);
00171 }
00172 }
00173
00174 public static void SubmitPicture(String exsistingFileName, String ScriptSource)
00175 {
00176 if (Prefs.current.verbose) System.out.println("uploading :"+exsistingFileName );
00177
00178 HttpURLConnection conn = null;
00179 DataOutputStream dos = null;
00180 DataInputStream inStream = null;
00181
00182 String lineEnd = "\r\n";
00183 String twoHyphens = "--";
00184 String boundary = "*****";
00185
00186 int bytesRead, bytesAvailable, bufferSize;
00187
00188 byte[] buffer;
00189
00190 int maxBufferSize = 1*1024*1024;
00191
00192 String urlString = Prefs.current.baseURL+"upload.php";
00193
00194 try
00195 {
00196 File file = new File(exsistingFileName);
00197
00198 FileInputStream fileInputStream = new FileInputStream(file);
00199
00200
00201 URL url = new URL(urlString);
00202
00203
00204 conn = (HttpURLConnection) url.openConnection();
00205
00206
00207 conn.setDoInput(true);
00208
00209
00210 conn.setDoOutput(true);
00211 conn.setUseCaches(false);
00212
00213
00214 conn.setRequestMethod("POST");
00215 conn.setRequestProperty("Connection", "Keep-Alive");
00216 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
00217
00218 dos = new DataOutputStream( conn.getOutputStream() );
00219 dos.writeBytes(twoHyphens + boundary + lineEnd);
00220 dos.writeBytes("Content-Disposition: form-data; name=\"bild\";"
00221 + " filename=\"" + exsistingFileName +"\"" + lineEnd);
00222 dos.writeBytes(lineEnd);
00223
00224
00225 bytesAvailable = fileInputStream.available();
00226 bufferSize = Math.min(bytesAvailable, maxBufferSize);
00227 buffer = new byte[bufferSize];
00228
00229
00230 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
00231
00232 while (bytesRead > 0)
00233 {
00234 dos.write(buffer, 0, bufferSize);
00235 bytesAvailable = fileInputStream.available();
00236 bufferSize = Math.min(bytesAvailable, maxBufferSize);
00237 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
00238 }
00239
00240 dos.writeBytes(lineEnd);
00241 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
00242
00243
00244 fileInputStream.close();
00245 dos.flush();
00246 dos.close();
00247
00248 }
00249 catch (MalformedURLException ex)
00250 {
00251 System.out.println("Fehler beim Verbindungsaufbau mit Script-Dateien.");
00252 }
00253 catch (IOException ioe)
00254 {
00255 System.out.println("Fehler beim Laden des Bildes.");
00256 }
00257
00258 try
00259 {
00260 inStream = new DataInputStream ( conn.getInputStream() );
00261
00262
00263 String str;
00264 String output = "";
00265
00266 while (( str = inStream.readUTF()) != null)
00267 {
00268 output = output+str;
00269 }
00270 if(output != "")
00271 {
00272
00273
00274 }
00275 inStream.close();
00276 new File(exsistingFileName).delete();
00277
00278 }
00279 catch (IOException ioex)
00280 {
00281 System.out.println("Fehler beim Empfangen der Serverantwort.");
00282 }
00283 }
00284
00285 }