ResourceRetriever.java
Go to the documentation of this file.
00001 /*******************************************************************************
00002  * Copyright (c) 2012 Stefan Profanter.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the GNU Public License v3.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.gnu.org/licenses/gpl.html
00007  * 
00008  * Contributors:
00009  *     Stefan Profanter - initial API and implementation, Year: 2012
00010  ******************************************************************************/
00011 package edu.tum.cs.ias.knowrob.utils;
00012 
00013 import java.io.BufferedOutputStream;
00014 import java.io.BufferedReader;
00015 import java.io.File;
00016 import java.io.FileOutputStream;
00017 import java.io.IOException;
00018 import java.io.InputStream;
00019 import java.io.InputStreamReader;
00020 import java.io.OutputStream;
00021 import java.io.UnsupportedEncodingException;
00022 import java.net.URL;
00023 import java.net.URLConnection;
00024 
00025 import org.apache.commons.net.ftp.FTPClient;
00026 
00039 public class ResourceRetriever {
00040 
00044         private static final String     tmpPrefix       = "knowrob_resource_";
00045 
00051         public static String createTempDirectory() {
00052 
00053                 String tmpDir = System.getProperty("java.io.tmpdir");
00054 
00055                 File temp = new File(tmpDir, "temp" + Long.toString(System.nanoTime()));
00056 
00057                 if (!(temp.mkdir())) {
00058                         System.err.println("Could not create temp directory: " + temp.getAbsolutePath());
00059                 }
00060 
00061                 return temp.getAbsolutePath();
00062         }
00063 
00071         public static String findPackage(String pkgname) {
00072 
00073                 try {
00074                         String line;
00075                         String cmd = "rospack find " + pkgname;
00076                         Process p = Runtime.getRuntime().exec(cmd);
00077                         BufferedReader errReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
00078                         while ((line = errReader.readLine()) != null) {
00079                                 System.err.println(line);
00080                         }
00081                         BufferedReader pathreader = new BufferedReader(new InputStreamReader(
00082                                         p.getInputStream(), "UTF-8"));
00083                         if ((line = pathreader.readLine()) != null) {
00084                                 return line;
00085                         }
00086                 } catch (IOException e) {
00087                         e.printStackTrace(System.err);
00088                 }
00089                 return null;
00090         }
00091 
00104         private static File getTmpName(String url, String filename) {
00105                 String hashName = MD5(url);
00106                 if (hashName == null)
00107                         return null;
00108 
00109                 int idx = filename.lastIndexOf('.');
00110                 String ext = "";
00111                 if (idx > 0)
00112                         ext = filename.substring(idx);
00113 
00114                 String tmpDir = System.getProperty("java.io.tmpdir");
00115 
00116                 File f = new File(tmpDir, tmpPrefix + hashName + ext);
00117 
00118                 return f;
00119         }
00120 
00128         private static String MD5(String md5) {
00129                 try {
00130                         java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
00131                         byte[] array = md.digest(md5.getBytes("UTF-8"));
00132                         StringBuffer sb = new StringBuffer();
00133                         for (int i = 0; i < array.length; ++i) {
00134                                 sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1, 3));
00135                         }
00136                         return sb.toString();
00137                 } catch (java.security.NoSuchAlgorithmException e) {
00138                         System.err.println("ResourceRetriever NoSuchAlgorithmException Error: "
00139                                         + e.getMessage());
00140                 } catch (UnsupportedEncodingException e) {
00141                         System.err.println("ResourceRetriever UnsupportedEncodingException Error: "
00142                                         + e.getMessage());
00143                 }
00144                 return null;
00145         }
00146 
00157         public static File retrieve(String url) {
00158                 return retrieve(url, true);
00159         }
00160 
00173         public static File retrieve(String url, boolean checkAlreadyRetrieved) {
00174                 if (url.indexOf("://") <= 0) {
00175                         // Is local file
00176                         return new File(url);
00177                 }
00178                 int start = url.indexOf('/') + 2;
00179                 int end = url.indexOf('/', start);
00180                 String serverName = url.substring(start, end);
00181                 if (url.startsWith("package://")) {
00182                         String filePath = url.substring(end + 1);
00183                         String pkgPath = findPackage(serverName);
00184                         if (pkgPath == null)
00185                                 return null;
00186                         return new File(pkgPath, filePath);
00187                 } else if (url.startsWith("http://")) {
00188 
00189                         OutputStream out = null;
00190                         URLConnection conn = null;
00191                         InputStream in = null;
00192                         String filename = url.substring(url.lastIndexOf('/') + 1);
00193 
00194                         File tmpPath = getTmpName(url, filename);
00195 
00196                         if (checkAlreadyRetrieved && tmpPath.exists()) {
00197                                 return tmpPath;
00198                         }
00199 
00200                         try {
00201                                 // Get the URL
00202                                 URL urlClass = new URL(url);
00203                                 // Open an output stream to the destination file on our local filesystem
00204                                 out = new BufferedOutputStream(new FileOutputStream(tmpPath));
00205                                 conn = urlClass.openConnection();
00206                                 in = conn.getInputStream();
00207 
00208                                 // Get the data
00209                                 byte[] buffer = new byte[1024];
00210                                 int numRead;
00211                                 while ((numRead = in.read(buffer)) != -1) {
00212                                         out.write(buffer, 0, numRead);
00213                                 }
00214                                 // Done! Just clean up and get out
00215                         } catch (Exception exception) {
00216                                 exception.printStackTrace();
00217                         } finally {
00218                                 try {
00219                                         if (in != null) {
00220                                                 in.close();
00221                                         }
00222                                         if (out != null) {
00223                                                 out.close();
00224                                         }
00225                                 } catch (IOException ioe) {
00226                                         // Ignore if stream not possible to close
00227                                 }
00228                         }
00229                         return tmpPath;
00230                 } else if (url.startsWith("ftp://")) {
00231                         FTPClient client = new FTPClient();
00232                         OutputStream outStream = null;
00233                         String filename = url.substring(url.lastIndexOf('/') + 1);
00234 
00235                         File tmpPath = getTmpName(url, filename);
00236 
00237                         if (checkAlreadyRetrieved && tmpPath.exists()) {
00238                                 System.out
00239                                                 .println("Already retrieved: " + url + " to " + tmpPath.getAbsolutePath());
00240                                 return tmpPath;
00241                         }
00242 
00243                         try {
00244                                 // Connect to the FTP server as anonymous
00245                                 client.connect(serverName);
00246                                 client.login("anonymous", "knowrob@example.com");
00247                                 String remoteFile = url.substring(end);
00248                                 // Write the contents of the remote file to a FileOutputStream
00249                                 outStream = new FileOutputStream(tmpPath);
00250                                 client.retrieveFile(remoteFile, outStream);
00251 
00252                         } catch (IOException ioe) {
00253                                 System.out.println("ResourceRetriever: Error communicating with FTP server: "
00254                                                 + serverName + "\n" + ioe.getMessage());
00255                         } finally {
00256                                 try {
00257                                         outStream.close();
00258                                 } catch (IOException e1) {
00259                                         // Ignore if stream not possible to close
00260                                 }
00261                                 try {
00262                                         client.disconnect();
00263                                 } catch (IOException e) {
00264                                         System.out.println("ResourceRetriever: Problem disconnecting from FTP server: "
00265                                                         + serverName + "\n" + e.getMessage());
00266                                 }
00267                         }
00268                         return tmpPath;
00269                 }
00270                 return null;
00271         }
00272 }


knowrob_common
Author(s): Moritz Tenorth
autogenerated on Mon Oct 6 2014 01:29:31