Produced by Araxis Merge on 7/6/2017 8:22:51 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | OSCIF_CTT_v4_build 1.zip\ISAAC-file-transfer-utils\src\main\java\gov\vha\isaac\utils\file_transfer | FileTransferUtils.java | Wed May 31 03:58:42 2017 UTC |
| 2 | OSCIF_CTT_v4_build 1.zip\ISAAC-file-transfer-utils\src\main\java\gov\vha\isaac\utils\file_transfer | FileTransferUtils.java | Mon Jul 3 21:53:27 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 4 | 780 |
| Changed | 3 | 8 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | /** | |
| 2 | * Copyrig ht Notice | |
| 3 | * | |
| 4 | * This is a work of the U.S. Government and is no t subject to copyrig ht | |
| 5 | * protect ion in the United St ates. Fore ign copyri ghts may a pply. | |
| 6 | * | |
| 7 | * License d under th e Apache L icense, Ve rsion 2.0 (the "Lice nse"); | |
| 8 | * you may not use t his file e xcept in c ompliance with the L icense. | |
| 9 | * You may obtain a copy of th e License at | |
| 10 | * | |
| 11 | * http:// www.apache .org/licen ses/LICENS E-2.0 | |
| 12 | * | |
| 13 | * Unless required b y applicab le law or agreed to in writing , software | |
| 14 | * distrib uted under the Licen se is dist ributed on an "AS IS " BASIS, | |
| 15 | * WITHOUT WARRANTIE S OR CONDI TIONS OF A NY KIND, e ither expr ess or imp lied. | |
| 16 | * See the License f or the spe cific lang uage gover ning permi ssions and | |
| 17 | * limitat ions under the Licen se. | |
| 18 | */ | |
| 19 | ||
| 20 | package go v.vha.isaa c.utils.fi le_transfe r; | |
| 21 | ||
| 22 | import jav a.io.File; | |
| 23 | import jav a.io.FileO utputStrea m; | |
| 24 | import jav a.io.IOExc eption; | |
| 25 | import jav a.io.Input Stream; | |
| 26 | import jav a.net.Http URLConnect ion; | |
| 27 | import jav a.net.URL; | |
| 28 | import jav a.nio.chan nels.Close dByInterru ptExceptio n; | |
| 29 | import jav a.util.Arr ays; | |
| 30 | import jav a.util.Col lection; | |
| 31 | import jav a.util.Col lections; | |
| 32 | import jav a.util.Has hSet; | |
| 33 | import jav a.util.Set ; | |
| 34 | ||
| 35 | import org .slf4j.Log ger; | |
| 36 | import org .slf4j.Log gerFactory ; | |
| 37 | import org .w3c.dom.N odeList; | |
| 38 | ||
| 39 | import gov .vha.isaac .utils.fil e_transfer .FileTrans fer.FileAc tionResult ; | |
| 40 | ||
| 41 | /** | |
| 42 | * | |
| 43 | * {@link FileTransf erUtils} | |
| 44 | * | |
| 45 | * @author <a href=" mailto:joe l.kniaz.li st@gmail.c om">Joel K niaz</a> | |
| 46 | * | |
| 47 | */ | |
| 48 | public cla ss FileTra nsferUtils { | |
| 49 | st atic class Paths { | |
| 50 | priv ate final String rem ote; | |
| 51 | priv ate final String loc al; | |
| 52 | ||
| 53 | /** | |
| 54 | * @ param remo te | |
| 55 | * @ param loca l | |
| 56 | */ | |
| 57 | publ ic Paths(S tring remo te, String local) { | |
| 58 | super( ); | |
| 59 | this.r emote = re mote; | |
| 60 | this.l ocal = loc al; | |
| 61 | } | |
| 62 | ||
| 63 | /** | |
| 64 | * @ return the remote | |
| 65 | */ | |
| 66 | publ ic String getRemote( ) { | |
| 67 | return remote; | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * @ return the local | |
| 72 | */ | |
| 73 | publ ic String getLocal() { | |
| 74 | return local; | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | pr ivate stat ic Logger log = Logg erFactory. getLogger( FileTransf erUtils.cl ass); | |
| 79 | ||
| 80 | pr ivate File TransferUt ils() {} | |
| 81 | ||
| 82 | st atic Strin g basename (String pa th) { | |
| 83 | retu rn path.su bstring(pa th.lastInd exOf('/') + 1, path. length()); | |
| 84 | } | |
| 85 | ||
| 86 | st atic Strin g getDirec toryListin gHtmlPage( String dir ectoryUri, String us ername, St ring passw ord) throw s IOExcept ion { | |
| 87 | URL url = new URL(direct oryUri); | |
| 88 | Http URLConnect ion con = NetworkUti ls.getConn ection(url , username , password ); // url. openConnec tion(); | |
| 89 | con. connect(); | |
| 90 | ||
| 91 | java .io.Buffer edReader i n = new ja va.io.Buff eredReader | |
| 92 | (new jav a.io.Input StreamRead er(con.get InputStrea m())); | |
| 93 | ||
| 94 | Stri ngBuilder sb = new S tringBuild er(); | |
| 95 | Stri ng line = null; | |
| 96 | for (; (line = in.readLi ne()) != n ull; ) { | |
| 97 | if (! line.trim( ).startsWi th("<link ") | |
| 98 | && ! line. trim().equ als("  ;")) { | |
| 99 | // Web p ages somet imes have <link> tag s without terminator s | |
| 100 | // and & nbsp;, whi ch the par ser can't handle | |
| 101 | sb.appen d(line + " \n"); | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | retu rn sb.toSt ring(); | |
| 106 | } | |
| 107 | ||
| 108 | st atic Set<S tring> get FileNamesF romDirecto ryListingH tmlPage(St ring html) { | |
| 109 | Set< String> fi leNames = new HashSe t<>(); | |
| 110 | ||
| 111 | Node List nodeL ist = XMLU tils.getNo deList(htm l, "/html/ body/table /tr/td/a") ; | |
| 112 | ||
| 113 | for (int i = 0 ; i < node List.getLe ngth(); ++ i) { | |
| 114 | String fileOrDir Name = nod eList.item (i).getTex tContent() ; | |
| 115 | if (! fileOrDirN ame.trim() .endsWith( "/") | |
| 116 | && ! fileO rDirName.t rim().equa ls("Parent Directory ") | |
| 117 | && ! fileO rDirName.t rim().ends With(".md5 ") | |
| 118 | && ! fileO rDirName.t rim().ends With(".sha 1")) { | |
| 119 | fileName s.add(file OrDirName) ; | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | retu rn Collect ions.unmod ifiableSet (fileNames ); | |
| 124 | } | |
| 125 | ||
| 126 | ||
| 127 | st atic boole an validat eChecksum( File file, File sha1 File) { | |
| 128 | try { | |
| 129 | String expectedS ha1Value = java.nio. file.Files .readAllLi nes(sha1Fi le.toPath( )).get(0); | |
| 130 | String calculate dSha1Value = Checksu mGenerator .calculate Checksum(" SHA1", fil e); | |
| 131 | if (ca lculatedSh a1Value != null && ! expectedSh a1Value.eq uals(calcu latedSha1V alue)) { | |
| 132 | return f alse; | |
| 133 | } else { | |
| 134 | return t rue; | |
| 135 | } | |
| 136 | } ca tch (Excep tion e) { | |
| 137 | if (e instanceof ClosedByI nterruptEx ception) { | |
| 138 | log.warn ("Failed c alculating SHA1 chec ksum for " + file.ge tAbsoluteP ath()); | |
| 139 | } else { | |
| 140 | log.warn ("Failed c alculating SHA1 chec ksum for " + file.ge tAbsoluteP ath(), e); | |
| 141 | } | |
| 142 | ||
| 143 | return false; | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | st atic boole an downloa dChecksumF ilesAndVal idateCheck sum(File f ile, URL r emoteFileU rl, String username, String pa ssword) { | |
| 148 | retu rn downloa dChecksumF ilesAndVal idateCheck sum(file, remoteFile Url, usern ame, passw ord, true) ; | |
| 149 | } | |
| 150 | ||
| 151 | st atic boole an downloa dChecksumF ilesAndVal idateCheck sum(File f ile, URL r emoteFileU rl, String username, String pa ssword, bo olean keep ChecksumFi les) { | |
| 152 | try { | |
| 153 | URL sh a1Url = ne w URL(remo teFileUrl. toString() + ".sha1" ); | |
| 154 | URL md 5Url = new URL(remot eFileUrl.t oString() + ".md5"); | |
| 155 | ||
| 156 | boolea n sha1File Exists = r emoteFileE xists(sha1 Url, usern ame, passw ord); | |
| 157 | boolea n md5FileE xists = re moteFileEx ists(md5Ur l, usernam e, passwor d); | |
| 158 | ||
| 159 | if (! sha1FileEx ists && ! md5FileExi sts) { | |
| 160 | return t rue; | |
| 161 | } | |
| 162 | ||
| 163 | File c hecksumFil eDir = kee pChecksumF iles ? fil e.getParen tFile() : java.nio.f ile.Files. createTemp Directory( "ISAAC").t oFile(); | |
| 164 | if (! keepChecks umFiles) { | |
| 165 | checksum FileDir.de leteOnExit (); | |
| 166 | } | |
| 167 | ||
| 168 | File s ha1File = null; | |
| 169 | if (sh a1FileExis ts) { | |
| 170 | sha1File = downloa d(sha1Url, username, password, checksumF ileDir); | |
| 171 | if (! ke epChecksum Files) { | |
| 172 | sha1File.d eleteOnExi t(); | |
| 173 | } | |
| 174 | } | |
| 175 | ||
| 176 | File m d5File = n ull; | |
| 177 | if (md 5FileExist s) { | |
| 178 | md5File = download (md5Url, u sername, p assword, c hecksumFil eDir); | |
| 179 | if (! ke epChecksum Files) { | |
| 180 | md5File.de leteOnExit (); | |
| 181 | } | |
| 182 | } | |
| 183 | ||
| 184 | if (! file.exist s()) { | |
| 185 | return f alse; | |
| 186 | } | |
| 187 | ||
| 188 | boolea n validati onSucceede d = false; | |
| 189 | ||
| 190 | // If file exist s and SHA1 file exis ts and fil e exists v alidate ch ecksum | |
| 191 | if (sh a1FileExis ts) { | |
| 192 | validati onSucceede d = valida teChecksum (file, sha 1File); | |
| 193 | } else { | |
| 194 | // If fi le exists and SHA1 f ile DOES N OT exist t hen automa tically pa ss validat ion | |
| 195 | validati onSucceede d = true; | |
| 196 | } | |
| 197 | ||
| 198 | if (! keepChecks umFiles) { | |
| 199 | if (sha1 File != nu ll) { | |
| 200 | sha1File.d elete(); | |
| 201 | } | |
| 202 | if (md5F ile != nul l) { | |
| 203 | md5File.de lete(); | |
| 204 | } | |
| 205 | checksum FileDir.de lete(); | |
| 206 | } | |
| 207 | ||
| 208 | return validatio nSucceeded ; | |
| 209 | } ca tch (Excep tion e) { | |
| 210 | log.wa rn("Failed validatin g SHA1 che cksum for " + file.g etAbsolute Path(), e) ; | |
| 211 | ||
| 212 | return false; | |
| 213 | } | |
| 214 | } | |
| 215 | ||
| 216 | st atic boole an remoteF ileExists( URL URLNam e, String username, String pas sword) { | |
| 217 | try { | |
| 218 | HttpUR LConnectio n con = Ne tworkUtils .getConnec tion(URLNa me, userna me, passwo rd); | |
| 219 | HttpUR LConnectio n.setFollo wRedirects (false); | |
| 220 | con.se tInstanceF ollowRedir ects(false ); | |
| 221 | con.se tRequestMe thod("HEAD "); | |
| 222 | return (con.getR esponseCod e() == Htt pURLConnec tion.HTTP_ OK); | |
| 223 | } | |
| 224 | catc h (Excepti on e) { | |
| 225 | e.prin tStackTrac e(); | |
| 226 | return false; | |
| 227 | } | |
| 228 | } | |
| 229 | ||
| 230 | st atic Set<S tring> lis tFilesInDi rectory(St ring direc toryUri, S tring user name, Stri ng passwor d) throws IOExceptio n { | |
| 231 | Set< String> fi leNames = new HashSe t<>(); | |
| 232 | if ( directoryU ri.contain s(":")) { | |
| 233 | return getFileNa mesFromDir ectoryList ingHtmlPag e(getDirec toryListin gHtmlPage( directoryU ri, userna me, passwo rd)); | |
| 234 | } el se { | |
| 235 | File d irectory = new File( directoryU ri); | |
| 236 | if (di rectory.ex ists() && directory. canRead() && directo ry.canExec ute()) { | |
| 237 | for (Fil e file : d irectory.l istFiles() ) { | |
| 238 | if (! file .isDirecto ry()) { | |
| 239 | fi leNames.ad d(file.get Name()); | |
| 240 | } | |
| 241 | } | |
| 242 | } | |
| 243 | } | |
| 244 | ||
| 245 | retu rn Collect ions.unmod ifiableSet (fileNames ); | |
| 246 | } | |
| 247 | ||
| 248 | /* * | |
| 249 | * @param ur l remote f ile url | |
| 250 | * @param us ername log in usernam e | |
| 251 | * @param pa ssword log in passwor d | |
| 252 | * @param ta rgetDirect ory target directory | |
| 253 | * @return | |
| 254 | * @throws E xception | |
| 255 | * | |
| 256 | * This meth od does no t offer an y way to c ancel down load | |
| 257 | * / | |
| 258 | st atic File download(U RL url, St ring usern ame, Strin g password , File tar getDirecto ry) throws Exception { | |
| 259 | retu rn downloa d(url, use rname, pas sword, tar getDirecto ry, true); | |
| 260 | } | |
| 261 | ||
| 262 | /* * | |
| 263 | * @param ur l remote f ile url | |
| 264 | * @param us ername log in usernam e | |
| 265 | * @param pa ssword log in passwor d | |
| 266 | * @param ta rgetDirect ory target directory | |
| 267 | * @param us eFileResul tsRegistry boolean s pecifying whether to use stati c results registry | |
| 268 | * @return | |
| 269 | * @throws E xception | |
| 270 | * | |
| 271 | * This meth od does no t offer an y way to c ancel down load | |
| 272 | * / | |
| 273 | st atic File download(U RL url, St ring usern ame, Strin g password , File tar getDirecto ry, boolea n useFileR esultsRegi stry) thro ws Excepti on | |
| 274 | { | |
| 275 | Http URLConnect ion httpCo n = null; | |
| 276 | File OutputStre am fos = n ull; | |
| 277 | Inpu tStream in = null; | |
| 278 | ||
| 279 | Stri ng fileNam e = url.to String(); | |
| 280 | file Name = fil eName.subs tring(file Name.lastI ndexOf('/' ) + 1, fil eName.leng th()); | |
| 281 | ||
| 282 | File file = ne w File(tar getDirecto ry, fileNa me); | |
| 283 | ||
| 284 | if ( useFileRes ultsRegist ry && File Transfer.h asBeenSucc essfullyHa ndled(file .getAbsolu teFile().t oString()) ) { | |
| 285 | return file; | |
| 286 | } | |
| 287 | ||
| 288 | if ( useFileRes ultsRegist ry) { | |
| 289 | FileTr ansfer.set FileAction Result(fil e, FileAct ionResult. FAILED); | |
| 290 | } | |
| 291 | ||
| 292 | log. debug("Beg inning dow nload from " + url); | |
| 293 | if ( ! targetDi rectory.ex ists()) { | |
| 294 | target Directory. mkdirs(); | |
| 295 | } | |
| 296 | try { | |
| 297 | httpCo n = Networ kUtils.get Connection (url, user name, pass word); | |
| 298 | httpCo n.setDoInp ut(true); | |
| 299 | httpCo n.setReque stMethod(" GET"); | |
| 300 | httpCo n.setConne ctTimeout( 30 * 1000) ; | |
| 301 | httpCo n.setReadT imeout(60 * 60 * 100 0); | |
| 302 | in = h ttpCon.get InputStrea m(); | |
| 303 | ||
| 304 | int by tesReadSin ceShowingP rogress = 0; | |
| 305 | byte[] buf = new byte[1048 576]; | |
| 306 | fos = new FileOu tputStream (file); | |
| 307 | int re ad = 0; | |
| 308 | while ((read = i n.read(buf , 0, buf.l ength)) > 0) | |
| 309 | { | |
| 310 | fos.writ e(buf, 0, read); | |
| 311 | ||
| 312 | bytesRea dSinceShow ingProgres s += buf.l ength; | |
| 313 | if (byte sReadSince ShowingPro gress >= F ileTransfe r.IO_SHOW_ PROGRESS_B YTES_THRES HOLD) { | |
| 314 | ConsoleUti l.showProg ress(); // Show prog ress for b uffered I/ O | |
| 315 | bytesReadS inceShowin gProgress = 0; | |
| 316 | } | |
| 317 | ||
| 318 | if (File Transfer.D EBUG_FAIL_ PROCESSING _FILE_NAME != null & & fileName .equals(Fi leTransfer .DEBUG_FAI L_PROCESSI NG_FILE_NA ME)) { | |
| 319 | throw new IOExceptio n("intenti onally fai ling downl oad of \"" + fileNam e + "\" fo r debuggin g purposes "); | |
| 320 | } | |
| 321 | } | |
| 322 | fos.fl ush(); | |
| 323 | ||
| 324 | if (us eFileResul tsRegistry ) { | |
| 325 | FileTran sfer.setFi leActionRe sult(file, FileActio nResult.SU CCEEDED); | |
| 326 | } | |
| 327 | log.de bug("Downl oad comple te from " + url); | |
| 328 | ||
| 329 | return file; | |
| 330 | } ca tch (Excep tion e) { | |
| 331 | // Reg ister file as unsucc essfully h andled | |
| 332 | FileTr ansfer.set FileAction Result(fil e, FileAct ionResult. FAILED); | |
| 333 | ||
| 334 | log.er ror("Faile d writing to file " + file.get AbsolutePa th(), e); | |
| 335 | file.d elete(); | |
| 336 | throw e; | |
| 337 | } fi nally { | |
| 338 | if (ht tpCon != n ull) { | |
| 339 | httpCon. disconnect (); | |
| 340 | } | |
| 341 | if (fo s != null) { | |
| 342 | fos.clos e(); | |
| 343 | } | |
| 344 | if (in != null) { | |
| 345 | in.close (); | |
| 346 | } | |
| 347 | } | |
| 348 | } | |
| 349 | ||
| 350 | st atic Strin g toString (Collectio n<?> objs) { | |
| 351 | Stri ng[] names = new Str ing[objs.s ize()]; | |
| 352 | int index = 0; | |
| 353 | for (Object ob j : objs) { | |
| 354 | if (ob j == null) { | |
| 355 | names[in dex++] = n ull; | |
| 356 | } else if (obj i nstanceof File) { | |
| 357 | names[in dex++] = ( (File)obj) .getPath() ; | |
| 358 | } else if (obj i nstanceof URL) { | |
| 359 | names[in dex++] = ( (URL)obj). getPath(); | |
| 360 | } else { | |
| 361 | names[in dex++] = o bj.toStrin g(); | |
| 362 | } | |
| 363 | } | |
| 364 | ||
| 365 | retu rn Arrays. toString(n ames); | |
| 366 | } | |
| 367 | ||
| 368 | pu blic stati c void mai n(String.. .argv) thr ows Except ion { | |
| 369 | //St ring uri = "http://U RL.DNS:POR T | |
| 370 | Stri ng uri = " http://URL .DNS:PORT" ; | |
| 371 | ||
| 372 | Set< String> fi leNames = null; | |
| 373 | ||
| 374 | // Set< String> fi leNames = listFilesI nDirectory (uri, "dev test", "de vtest"); | |
| 375 | // for (String fi leName : f ileNames) { | |
| 376 | // System .out.print ln(fileNam e + ": typ e=" + getT ypeFromFil eName(file Name) + ", classifie r=" + getC lassifierF romFileNam e(fileName , "isaac-r est", "1.8 ")); | |
| 377 | // } | |
| 378 | file Names = Ma venFileUti ls.listMos tRecentMav enMetadata VersionOfE achFileInD irectory(u ri, "devte st", "devt est"); | |
| 379 | for (String fi leName : f ileNames) { | |
| 380 | System .out.print ln(fileNam e + " (mos t recent): type=" + MavenFileU tils.getTy peFromFile Name(fileN ame, "meta data", "3. 07") + (Ma venFileUti ls.getClas sifierFrom FileName(f ileName, " metadata", "3.07") ! = null ? " , classifi er=" + Mav enFileUtil s.getClass ifierFromF ileName(fi leName, "m etadata", "3.07") : "")); | |
| 381 | } | |
| 382 | ||
| 383 | uri = "http:// URL.DNS:PO RT"; | |
| 384 | // file Names = li stFilesInD irectory(u ri, "devte st", "devt est"); | |
| 385 | // for (String fi leName : f ileNames) { | |
| 386 | // System .out.print ln(fileNam e + ": typ e=" + getT ypeFromFil eName(file Name, "met adata", "3 .07-SNAPSH OT") + (ge tClassifie rFromFileN ame(fileNa me, "metad ata", "3.0 7-SNAPSHOT ") != null ? ", clas sifier=" + getClassi fierFromFi leName(fil eName, "me tadata", " 3.07-SNAPS HOT") : "" )); | |
| 387 | // } | |
| 388 | //ur i = "http: //URL.DNS: PORT"; | |
| 389 | file Names = Ma venFileUti ls.listMos tRecentMav enMetadata VersionOfE achFileInD irectory(u ri, "devte st", "devt est"); | |
| 390 | for (String fi leName : f ileNames) { | |
| 391 | System .out.print ln(fileNam e + " (mos t recent): type=" + MavenFileU tils.getTy peFromFile Name(fileN ame, "meta data", "3. 07-SNAPSHO T") + (Mav enFileUtil s.getClass ifierFromF ileName(fi leName, "m etadata", "3.07-SNAP SHOT") != null ? ", classifier =" + Maven FileUtils. getClassif ierFromFil eName(file Name, "met adata", "3 .07-SNAPSH OT") : "") ); | |
| 392 | } | |
| 393 | } | |
| 394 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.