1 17 18 19 20 package org.apache.lenya.net; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.HttpURLConnection ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 34 import org.apache.log4j.Category; 35 36 37 40 public class WGet { 41 static Category log = Category.getInstance(WGet.class); 42 String directory_prefix = null; 43 44 47 public WGet() { 48 directory_prefix = System.getProperty("user.dir"); 49 } 50 51 56 public static void main(String [] args) { 57 if (args.length == 0) { 58 System.out.println("Usage: org.apache.lenya.net.WGet [URL] -P/home/lenya/download"); 59 60 return; 61 } 62 63 try { 64 WGet wget = new WGet(); 65 66 for (int i = 0; i < args.length; i++) { 67 if (args[i].indexOf("-P") == 0) { 68 wget.setDirectoryPrefix(args[i].substring(2)); } 70 } 71 72 byte[] response = wget.download(new URL (args[0]), "s/\\/lenya\\/oscom//g", ""); 73 } catch (MalformedURLException e) { 74 System.err.println(e); 75 } catch (Exception e) { 76 System.err.println(e); 77 } 78 } 79 80 85 public void setDirectoryPrefix(String directory_prefix) { 86 this.directory_prefix = directory_prefix; 87 } 88 89 98 public byte[] download(URL url, String prefixSubstitute, String substituteReplacement) 99 throws IOException { 100 log.debug(".download(): " + url + " " + prefixSubstitute + " " + substituteReplacement); 101 102 return downloadUsingHttpClient(url, prefixSubstitute, substituteReplacement); 103 } 104 105 113 public byte[] downloadUsingHttpClient(URL url, String prefixSubstitute, 114 String substituteReplacement) { 115 log.debug(".downloadUsingHttpClient(): " + url); 116 117 byte[] sresponse = null; 118 119 try { 120 sresponse = getResource(url); 121 122 File file = new File (createFileName(url, prefixSubstitute, substituteReplacement)); 123 124 saveToFile(file.getAbsolutePath(), sresponse); 125 126 substitutePrefix(file.getAbsolutePath(), prefixSubstitute, substituteReplacement); 127 } catch (MalformedURLException e) { 128 log.error(".downloadUsingHttpClient(): ", e); 129 } catch (FileNotFoundException e) { 130 log.error(".downloadUsingHttpClient(): ", e); 131 } catch (IOException e) { 132 log.error(".downloadUsingHttpClient(): ", e); 133 } 134 135 List links = null; 136 137 try { 138 links = getLinks(url); 139 } catch (IOException ioe) { 140 log.error(".downloadUsingHttpClient(): ", ioe); 141 } 142 143 if (links != null) { 144 Iterator iterator = links.iterator(); 145 146 while (iterator.hasNext()) { 147 String link = (String ) iterator.next(); 148 149 try { 150 URL child_url = new URL (org.apache.lenya.util.URLUtil.complete(url.toString(), 151 link)); 152 153 byte[] child_sresponse = getResource(child_url); 154 saveToFile(createFileName(child_url, prefixSubstitute, substituteReplacement), 155 child_sresponse); 156 } catch (Exception e) { 157 log.error(".downloadUsingHttpClient(): ", e); 158 } 159 } 160 } 161 162 return sresponse; 163 } 164 165 168 public byte[] getResource(URL url) throws IOException { 169 log.debug(".getResource(): " + url); 170 171 HttpURLConnection httpConnection = (HttpURLConnection ) url.openConnection(); 172 InputStream in = httpConnection.getInputStream(); 173 byte[] buffer = new byte[1024]; 174 int bytes_read; 175 ByteArrayOutputStream bufferOut = new ByteArrayOutputStream (); 176 177 while ((bytes_read = in.read(buffer)) != -1) { 178 bufferOut.write(buffer, 0, bytes_read); 179 } 180 181 byte[] sresponse = bufferOut.toByteArray(); 182 httpConnection.disconnect(); 183 184 return sresponse; 185 } 186 187 190 public List getLinks(URL url) throws IOException { 191 log.debug(".getLinks(): Get links from " + url); 192 193 List links = null; 194 195 try { 196 org.apache.lenya.util.HTML html = new org.apache.lenya.util.HTML(url.toString()); 197 links = html.getImageSrcs(false); 198 links.addAll(html.getLinkHRefs(false)); 199 } catch (Exception e) { 200 log.error(".getLinks() Exception 423432: ", e); 201 } 202 203 if (links != null) { 204 log.debug(".getLinks(): Number of links found: " + links.size()); 205 } 206 207 return links; 208 } 209 210 219 public void substitutePrefix(String filename, String prefixSubstitute, String substituteReplacement) throws IOException { 220 log.debug("Replace " + prefixSubstitute + " by " + substituteReplacement); 221 222 org.apache.lenya.util.SED.replaceAll(new File (filename), escapeSlashes(prefixSubstitute), escapeSlashes(substituteReplacement)); 223 } 224 225 230 public String escapeSlashes(String string) { 231 StringBuffer buffer = new StringBuffer (""); 232 233 for (int i = 0; i < string.length(); i++) { 234 if (string.charAt(i) == '/') { 235 buffer.append("\\/"); 236 } else { 237 buffer.append(string.charAt(i)); 238 } 239 } 240 241 return buffer.toString(); 242 } 243 244 249 public String toString() { 250 return "-P: " + directory_prefix; 251 } 252 253 256 public void saveToFile(String filename, byte[] bytes) 257 throws FileNotFoundException , IOException { 258 File file = new File (filename); 259 File parent = new File (file.getParent()); 260 261 if (!parent.exists()) { 262 log.warn(".saveToFile(): Directory will be created: " + parent.getAbsolutePath()); 263 parent.mkdirs(); 264 } 265 266 FileOutputStream out = new FileOutputStream (file.getAbsolutePath()); 267 out.write(bytes); 268 out.close(); 269 } 270 271 275 public String createFileName(URL url, String prefixSubstitute, String substituteReplacement) { 276 File file = new File (directory_prefix + File.separator + url.getFile()); 277 278 return file.getAbsolutePath().replaceAll(prefixSubstitute, substituteReplacement); 279 } 280 281 284 public byte[] runProcess(String command) throws Exception { 285 Process process = Runtime.getRuntime().exec(command); 286 287 java.io.InputStream in = process.getInputStream(); 288 byte[] buffer = new byte[1024]; 289 int bytes_read = 0; 290 java.io.ByteArrayOutputStream baout = new java.io.ByteArrayOutputStream (); 291 292 while ((bytes_read = in.read(buffer)) != -1) { 293 baout.write(buffer, 0, bytes_read); 294 } 295 296 if (baout.toString().length() > 0) { 297 log.debug(".runProcess(): %%%InputStream:START" + baout.toString() + 298 "END:InputStream%%%"); 299 } 300 301 java.io.InputStream in_e = process.getErrorStream(); 302 java.io.ByteArrayOutputStream baout_e = new java.io.ByteArrayOutputStream (); 303 304 while ((bytes_read = in_e.read(buffer)) != -1) { 305 baout_e.write(buffer, 0, bytes_read); 306 } 307 308 if (baout_e.toString().length() > 0) { 309 log.error(".runProcess(): ###ErrorStream:START" + baout_e.toString() + 310 "END:ErrorStream###"); 311 } 312 313 return baout.toByteArray(); 314 } 315 } 316 | Popular Tags |