1 7 package com.inversoft.savant; 8 9 10 import java.io.BufferedReader ; 11 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.net.HttpURLConnection ; 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 import java.net.URLConnection ; 19 20 import com.inversoft.savant.log.Log; 21 22 23 30 public abstract class InternetProcess implements Process { 31 32 private String defaultDomain; 33 private File mapping; 34 private boolean failonmd5; 35 36 37 public String getDefaultdomain() { 38 return defaultDomain; 39 } 40 41 public void setDefaultdomain(String defaultDomain) { 42 this.defaultDomain = defaultDomain; 43 } 44 45 public String getUrl() { 46 return defaultDomain; 47 } 48 49 public void setUrl(String defaultDomain) { 50 this.defaultDomain = defaultDomain; 51 } 52 53 public File getMapping() { 54 return mapping; 55 } 56 57 public void setMapping(File mapping) { 58 this.mapping = mapping; 59 } 60 61 public boolean isFailonmd5() { 62 return failonmd5; 63 } 64 65 public void setFailonmd5(boolean failonmd5) { 66 this.failonmd5 = failonmd5; 67 } 68 69 74 public abstract String getProcessName(); 75 76 82 public void validate() throws SavantException { 83 if (defaultDomain == null && mapping == null) { 84 throw new SavantException("The " + getProcessName() + " requires " + 85 "either a default domain or a mapping file"); 86 } 87 } 88 89 99 protected File findFile(URLBuilder builder, Artifact artifact, 100 LocalCacheStore localCache) 101 throws SavantException { 102 URL artifactURL = builder.buildURL(defaultDomain, mapping, artifact); 103 if (artifactURL == null) { 104 throw new SavantException("Unable to build URL for artifact [" + 105 artifact.toString() + "]"); 106 } 107 108 URLConnection uc = openConnection(artifactURL); 109 if (uc == null) { 110 return null; 111 } 112 113 InputStream is = null; 114 try { 115 if (!isValid(uc)) { 116 return null; 117 } 118 119 byte[] md5 = getMD5Hash(builder, artifact); 120 is = uc.getInputStream(); 121 122 return localCache.store(artifact, is, md5, failonmd5); 123 } catch (IOException ioe) { 124 Log.log("Unable to download artifact [" + artifact + "]", Log.ERROR); 125 throw new SavantException(ioe); 126 } catch (SavantException se) { 127 Log.log(se.getMessage(), Log.ERROR); 128 throw new SavantException(se); 129 } finally { 130 if (is != null) { 131 try { 132 is.close(); 133 } catch (IOException ioe) { 134 throw new SavantException(ioe); 135 } 136 } 137 } 138 } 139 140 150 protected byte[] getMD5Hash(URLBuilder builder, Artifact artifact) 151 throws SavantException { 152 byte[] bytes = null; 153 URL url = builder.buildMD5URL(defaultDomain, mapping, artifact); 154 if (url != null) { 155 URLConnection uc = openConnection(url); 156 if (isValid(uc)) { 157 StringBuffer buf = new StringBuffer (); 158 try { 159 InputStream is = uc.getInputStream(); 160 InputStreamReader isr = new InputStreamReader (is); 161 BufferedReader br = new BufferedReader (isr); 162 char[] c = new char[1024]; 163 int count; 164 while ((count = br.read(c, 0, 1024)) != -1) { 165 for (int i = 0; i < count; i++) { 166 if (Character.isWhitespace(c[i])) { 167 continue; 168 } 169 170 buf.append(c[i]); 171 } 172 } 173 174 br.close(); 175 isr.close(); 176 is.close(); 177 178 if (buf.length() > 0) { 179 bytes = StringTools.fromHex(buf.toString()); 180 } 181 } catch (IOException ioe) { 182 Log.log("Unable to download MD5 (skipping) for artifact [" + 183 artifact + "]", Log.DEBUG); 184 } catch (IllegalArgumentException iae) { 185 Log.log("Unable to download MD5 (skipping) for artifact [" + 186 artifact + "]", Log.DEBUG); 187 } 188 } 189 } 190 191 return bytes; 192 } 193 194 205 protected boolean resolveArtifactDependencies(URLBuilder builder, Artifact artifact, 206 LocalCacheStore localCache) 207 throws SavantException { 208 URL url = builder.buildDepsURL(defaultDomain, mapping, artifact); 209 if (url != null) { 210 URLConnection uc = openConnection(url); 211 if (isValid(uc)) { 212 InputStream is = null; 213 File tmp = null; 214 try { 215 is = uc.getInputStream(); 216 tmp = File.createTempFile("savant", "deps"); 217 218 FileTools.output(is, tmp); 219 ArtifactTools.resolveArtifactDependencies(artifact, tmp); 220 localCache.storeDeps(artifact, tmp); 221 222 return true; 223 } catch (IOException e) { 224 Log.log("Unable to locate artifact dependency XML for artifact [" + 226 artifact + "]", Log.DEBUG); 227 } finally { 228 try { 229 if (is != null) { 230 is.close(); 231 } 232 233 if (tmp != null) { 234 tmp.delete(); 235 } 236 } catch (IOException ioe) { 237 throw new SavantException(ioe); 238 } 239 } 240 } 241 } 242 243 return false; 244 } 245 246 254 protected URLConnection openConnection(URL url) throws SavantException { 255 Log.log("Opening connection to [" + url + "]", Log.DEBUG); 256 URLConnection uc = null; 257 try { 258 uc = url.openConnection(); 259 uc.connect(); 260 } catch (MalformedURLException mue) { 261 throw new SavantException(mue); 262 } catch (IOException ioe) { 263 Log.log("Unable to open connection to [" + url + "] because [" + 265 ioe.getMessage() + "]", Log.INFO); 266 } 267 268 return uc; 269 } 270 271 281 protected boolean isValid(URLConnection uc) throws SavantException { 282 boolean valid = true; 283 if (uc instanceof HttpURLConnection ) { 284 try { 285 int code = ((HttpURLConnection ) uc).getResponseCode(); 286 Log.log("Got code from URL of [" + code + "]", Log.DEBUG); 287 valid = (code == HttpURLConnection.HTTP_ACCEPTED || 288 code == HttpURLConnection.HTTP_OK); 289 } catch (IOException ioe) { 290 Log.log("Unable to verify the status of HTTP connection to the URL [" + 291 uc.getURL() + "]", Log.ERROR); 292 throw new SavantException(ioe); 293 } 294 } 295 296 return valid; 297 } 298 }
| Popular Tags
|