| 1 7 package org.mmbase.applications.packaging.providerhandlers; 8 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.BufferedReader ; 12 import java.io.DataOutputStream ; 13 import java.io.File ; 14 import java.io.FileOutputStream ; 15 import java.io.InputStreamReader ; 16 import java.net.HttpURLConnection ; 17 import java.net.URL ; 18 import java.util.Iterator ; 19 import java.util.jar.JarEntry ; 20 import java.util.jar.JarFile ; 21 22 import org.mmbase.applications.packaging.BundleManager; 23 import org.mmbase.applications.packaging.PackageManager; 24 import org.mmbase.applications.packaging.ShareManager; 25 import org.mmbase.applications.packaging.bundlehandlers.BundleInterface; 26 import org.mmbase.applications.packaging.packagehandlers.PackageInterface; 27 import org.mmbase.applications.packaging.projects.creators.CreatorInterface; 28 import org.mmbase.applications.packaging.util.ExtendedDocumentReader; 29 import org.mmbase.util.Encode; 30 import org.mmbase.util.XMLEntityResolver; 31 import org.mmbase.util.logging.Logger; 32 import org.mmbase.util.logging.Logging; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.xml.sax.InputSource ; 36 37 43 public class HttpProvider extends BasicProvider implements ProviderInterface { 44 private static Logger log = Logging.getLoggerInstance(HttpProvider.class); 45 46 private String name; 47 private String method; 48 private String maintainer; 49 private String account = "guest"; 50 private String password = "guest"; 51 52 55 public final static String DTD_SHAREDPACKAGES_1_0 = "sharedpackages_1_0.dtd"; 56 57 60 public final static String PUBLIC_ID_SHAREDPACKAGES_1_0 = "-//MMBase//DTD sharedpackages config 1.0//EN"; 61 62 63 67 public static void registerPublicIDs() { 68 XMLEntityResolver.registerPublicID(PUBLIC_ID_SHAREDPACKAGES_1_0, DTD_SHAREDPACKAGES_1_0, HttpProvider.class); 69 } 70 71 72 73 76 public HttpProvider() { } 77 78 79 87 public void init(org.w3c.dom.Node n, String name, String method, String maintainer) { 88 super.init(n, name, method, maintainer); 89 org.w3c.dom.Node n2 = xmlnode.getFirstChild(); 90 while (n2 != null) { 91 if (n2.getNodeName().equals("path")) { 92 org.w3c.dom.Node n3 = n2.getFirstChild(); 93 path = n3.getNodeValue(); 94 } 95 if (n2.getNodeName().equals("description")) { 96 org.w3c.dom.Node n3 = n2.getFirstChild(); 97 if (n3 != null) { 98 description = n3.getNodeValue(); 99 } 100 } 101 if (n2.getNodeName().equals("account")) { 102 org.w3c.dom.Node n3 = n2.getFirstChild(); 103 account = n3.getNodeValue(); 104 } 105 if (n2.getNodeName().equals("password")) { 106 org.w3c.dom.Node n3 = n2.getFirstChild(); 107 password = n3.getNodeValue(); 108 } 109 n2 = n2.getNextSibling(); 110 } 111 baseScore = 2000; 112 } 113 114 115 123 public void init(String name, String method, String maintainer, String path) { 124 super.init(name, method, maintainer, path); 125 baseScore = 2000; 128 } 129 130 131 136 public void setAccount(String account) { 137 this.account = account; 138 } 139 140 141 146 public String getAccount() { 147 return account; 148 } 149 150 151 156 public void setPassword(String password) { 157 this.password = password; 158 } 159 160 161 166 public String getPassword() { 167 return password; 168 } 169 170 171 174 public void getPackages() { 175 signalUpdate(); 176 177 String url = path + "?user=" + account + "&password=" + password; 178 if (ShareManager.getCallbackUrl() != null) { 179 url += "&callbackurl=" + Encode.encode("ESCAPE_URL_PARAM",ShareManager.getCallbackUrl()); 180 } 181 try { 182 URL includeURL = new URL (url); 183 HttpURLConnection connection = (HttpURLConnection ) includeURL.openConnection(); 184 BufferedInputStream input = new BufferedInputStream (connection.getInputStream()); 185 ExtendedDocumentReader reader = new ExtendedDocumentReader(new InputSource (input), HttpProvider.class); 186 if (reader != null) { 187 try { 188 for (Iterator ns = reader.getChildElements("sharedpackages", "package"); ns.hasNext(); ) { 189 Element e = (Element ) ns.next(); 190 191 NamedNodeMap nm = e.getAttributes(); 192 if (nm != null) { 193 String name = null; 194 String type = null; 195 String version = null; 196 String date = null; 197 198 org.w3c.dom.Node n2 = nm.getNamedItem("name"); 200 if (n2 != null) { 201 name = n2.getNodeValue(); 202 } 203 204 n2 = nm.getNamedItem("type"); 206 if (n2 != null) { 207 type = n2.getNodeValue(); 208 } 209 210 n2 = nm.getNamedItem("maintainer"); 212 if (n2 != null) { 213 maintainer = n2.getNodeValue(); 214 } 215 216 n2 = nm.getNamedItem("version"); 218 if (n2 != null) { 219 version = n2.getNodeValue(); 220 } 221 222 n2 = nm.getNamedItem("creation-date"); 224 if (n2 != null) { 225 date = n2.getNodeValue(); 226 } 227 228 Element e2 = reader.getElementByPath(e, "package.path"); 229 org.w3c.dom.Node pathnode = e2.getFirstChild(); 230 String pkgpath = pathnode.getNodeValue(); 231 if (type.indexOf("bundle/") == 0) { 232 BundleInterface bun = BundleManager.foundBundle(this, e, name, type, maintainer, version, date, pkgpath); 233 findIncludedPackages(bun, e, pkgpath, date); 235 } else { 236 PackageManager.foundPackage(this, e, name, type, maintainer, version, date, pkgpath); 237 } 238 } 239 } 240 } catch (Exception f) { 241 log.error("something went wrong while decoding sharedpackagefile : " + url); 242 f.printStackTrace(); 243 } 244 } else { 245 log.error("can't get a valid reader for sharedpackagefile : " + url); 246 } 247 setState("up"); 248 } catch (Exception e) { 249 log.error("can't get sharedpackagefile : " + url); 252 setState("down"); 254 } 255 } 256 257 258 268 public JarFile getIncludedPackageJarFile(String path, String id, String version, String packageid, String packageversion) { 269 getJarFile(path, id, version); 271 272 try { 274 String localname = getImportPath() + id + "_" + version + ".mmb"; 275 JarFile jarFile = new JarFile (localname); 276 JarEntry je = jarFile.getJarEntry(packageid + "_" + packageversion + ".mmp"); 277 try { 278 BufferedInputStream in = new BufferedInputStream (jarFile.getInputStream(je)); 279 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (getImportPath() + ".temp_" + packageid + "_" + packageversion + ".mmp")); 280 int val; 281 while ((val = in.read()) != -1) { 282 out.write(val); 283 } 284 out.close(); 285 } catch (Exception e) { 286 log.error("can't load : " + path); 287 e.printStackTrace(); 288 } 289 JarFile tmpjarfile = new JarFile (getImportPath() + ".temp_" + packageid + "_" + packageversion + ".mmp"); 290 return tmpjarfile; 291 } catch (Exception e) { 292 log.error("can't load : " + path); 293 e.printStackTrace(); 294 } 295 return null; 296 } 297 298 299 307 public JarFile getJarFile(String path, String id, String version) { 308 try { 314 String localname = getImportPath() + id + "_" + version + ".mmp"; 316 if (id.indexOf("_bundle_") != -1) { 318 localname = getImportPath() + id + "_" + version + ".mmb"; 319 } 320 321 File checkfile = new File (localname); 322 if (!checkfile.exists()) { 323 324 URL includeURL = new URL (path); 325 HttpURLConnection connection = (HttpURLConnection ) includeURL.openConnection(); 326 BufferedInputStream in = new BufferedInputStream (connection.getInputStream()); 327 int buffersize = 10240; 328 byte[] buffer = new byte[buffersize]; 329 330 331 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (localname)); 332 StringBuffer string = new StringBuffer (); 333 int len; 334 int totallen = 0; 335 while ((len = in.read(buffer, 0, buffersize)) != -1) { 336 out.write(buffer, 0, len); 337 if (getInstallStep()!=null) { 338 totallen+=len; 339 String lenp=" received "; 340 if (totallen<1024) { 341 lenp+=""+totallen+" bytes"; 342 } else if (totallen<(1024*1024)) { 343 lenp+=""+((float)totallen/1024)+" KB"; 344 } else { 345 lenp+=""+((float)totallen/(1024*1024))+" MB"; 346 } 347 getInstallStep().setUserFeedBack("getting the mmb bundle... "+lenp); 348 } 349 } 350 out.close(); 351 } 352 353 JarFile jarFile = new JarFile (localname); 354 return jarFile; 355 } catch (Exception e) { 356 log.error("can't load : " + path + " because " + e); 357 } 358 return null; 359 } 360 361 362 367 public boolean close() { 368 return super.close(); 369 } 370 371 372 380 private void findIncludedPackages(BundleInterface bun, org.w3c.dom.Node n, String realpath, String date) { 381 382 org.w3c.dom.Node n2 = n.getFirstChild(); 383 while (n2 != null) { 384 String name = n2.getNodeName(); 385 if (name.equals("includedpackages")) { 387 org.w3c.dom.Node n3 = n2.getFirstChild(); 388 while (n3 != null) { 389 name = n3.getNodeName(); 390 NamedNodeMap nm = n3.getAttributes(); 391 if (nm != null) { 392 String maintainer = null; 393 String type = null; 394 String version = null; 395 boolean packed = false; 396 397 org.w3c.dom.Node n5 = nm.getNamedItem("name"); 399 if (n5 != null) { 400 name = n5.getNodeValue(); 401 } 402 403 n5 = nm.getNamedItem("type"); 405 if (n5 != null) { 406 type = n5.getNodeValue(); 407 } 408 409 n5 = nm.getNamedItem("maintainer"); 411 if (n5 != null) { 412 maintainer = n5.getNodeValue(); 413 } 414 415 n5 = nm.getNamedItem("version"); 417 if (n5 != null) { 418 version = n5.getNodeValue(); 419 } 420 421 n5 = nm.getNamedItem("packed"); 423 if (n5 != null) { 424 if (n5.getNodeValue().equals("true")) { 425 packed = true; 426 } 427 } 428 429 if (packed) { 431 PackageInterface pack = PackageManager.foundPackage(this, (Element ) n3, name, type, maintainer, version, date, realpath); 432 if (pack != null) { 434 pack.setParentBundle(bun); 435 } 436 } 437 } 438 n3 = n3.getNextSibling(); 439 } 440 } 441 n2 = n2.getNextSibling(); 442 } 443 } 444 445 446 451 public String getImportPath() { 452 String path = PackageManager.getConfigPath() + File.separator + "packaging" + File.separator + "import" + File.separator; 453 File dir = new File (path); 454 if (!dir.exists()) { 455 dir.mkdir(); 456 } 457 return path; 458 } 459 460 461 public boolean publish(CreatorInterface creator,PackageInterface pack,String sharepassword) { 462 String filename=pack.getId()+"_"+pack.getVersion()+".mmp"; 464 465 String posturl = getPath(); 466 if (posturl.startsWith("http://")) { 467 posturl = posturl.substring(7); 468 } 469 int pos=posturl.indexOf("/"); 470 if (pos!=-1) { 471 posturl = posturl.substring(0,pos); 472 } 473 posturl = "http://"+posturl + "/mmbase/packagemanager/upload/package.mmp"; 474 try { 475 String boundary = "*5433***3243"; 476 477 URL url = new URL (posturl); 479 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 480 conn.setDoInput(true); 481 conn.setDoOutput(true); 482 conn.setUseCaches(false); 483 conn.setRequestMethod("POST"); 484 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 485 486 DataOutputStream out = new DataOutputStream (conn.getOutputStream()); 487 488 out.writeBytes("--"+boundary+"\r\n"); 489 out.writeBytes("Content-Disposition: form-data; name=\"filename\"\r\n\r\n"); 490 out.writeBytes(filename+"\r\n"); 491 492 out.writeBytes("--"+boundary+"\r\n"); 493 out.writeBytes("Content-Disposition: form-data; name=\"account\"\r\n\r\n"); 494 out.writeBytes(account+"\r\n"); 495 496 out.writeBytes("--"+boundary+"\r\n"); 497 out.writeBytes("Content-Disposition: form-data; name=\"password\"\r\n\r\n"); 498 out.writeBytes(password+"\r\n"); 499 500 out.writeBytes("--"+boundary+"\r\n"); 501 out.writeBytes("Content-Disposition: form-data; name=\"sharepassword\"\r\n\r\n"); 502 out.writeBytes(sharepassword+"\r\n"); 503 out.writeBytes("--"+boundary+"\r\n"); 504 out.writeBytes("Content-Disposition: form-data; name=\"handle\"; filename=\"" +"testname" +"\"\r\n\r\n"); 505 String oldline=""; 506 if (getPackageStep()!=null) { oldline = getPackageStep().getUserFeedBack(); } 507 508 try { 509 BufferedInputStream in = new BufferedInputStream (pack.getJarStream()); 510 int val; 511 int totallen=0; 512 while ((val = in.read()) != -1) { 513 out.write(val); 514 totallen++; 515 if (getPackageStep()!=null) { 516 String lenp=" send "; 517 if (totallen<1024) { 518 lenp+=""+totallen+" bytes"; 519 } else if (totallen<(1024*1024)) { 520 lenp+=""+((float)totallen/1024)+" KB"; 521 } else { 522 lenp+=""+((float)totallen/(1024*1024))+" MB"; 523 } 524 getPackageStep().setUserFeedBack(oldline+lenp); 525 } 526 } 527 } catch (Exception e) { 528 log.error("can't load : " + path); 529 e.printStackTrace(); 530 } 531 out.writeBytes("\r\n"); 532 out.writeBytes("--"+boundary+"--\r\n"); 533 out.flush(); 534 out.close(); 535 536 if (getPackageStep()!=null) getPackageStep().setUserFeedBack(oldline+" server processing"); 537 538 BufferedReader rd = new BufferedReader (new InputStreamReader (conn.getInputStream())); 540 String line; 541 while ((line = rd.readLine()) != null) { 542 } 544 rd.close(); 545 } catch (Exception e) { 546 log.error("Publish upload problem to : "+posturl); 547 } 548 return true; 549 } 550 551 public boolean publish(CreatorInterface creator,BundleInterface bundle,String sharepassword) { 552 String filename=bundle.getId()+"_"+bundle.getVersion()+".mmb"; 554 555 String posturl = getPath(); 556 if (posturl.startsWith("http://")) { 557 posturl = posturl.substring(7); 558 } 559 int pos=posturl.indexOf("/"); 560 if (pos!=-1) { 561 posturl = posturl.substring(0,pos); 562 } 563 posturl = "http://"+posturl + "/mmbase/packagemanager/upload/package.mmp"; 564 try { 565 String boundary = "*5433***3243"; 566 567 URL url = new URL (posturl); 569 HttpURLConnection conn = (HttpURLConnection ) url.openConnection(); 570 conn.setDoInput(true); 571 conn.setDoOutput(true); 572 conn.setUseCaches(false); 573 conn.setRequestMethod("POST"); 574 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 575 576 DataOutputStream out = new DataOutputStream (conn.getOutputStream()); 577 578 out.writeBytes("--"+boundary+"\r\n"); 579 out.writeBytes("Content-Disposition: form-data; name=\"filename\"\r\n\r\n"); 580 out.writeBytes(filename+"\r\n"); 581 582 out.writeBytes("--"+boundary+"\r\n"); 583 out.writeBytes("Content-Disposition: form-data; name=\"account\"\r\n\r\n"); 584 out.writeBytes(account+"\r\n"); 585 586 out.writeBytes("--"+boundary+"\r\n"); 587 out.writeBytes("Content-Disposition: form-data; name=\"password\"\r\n\r\n"); 588 out.writeBytes(password+"\r\n"); 589 590 out.writeBytes("--"+boundary+"\r\n"); 591 out.writeBytes("Content-Disposition: form-data; name=\"sharepassword\"\r\n\r\n"); 592 out.writeBytes(sharepassword+"\r\n"); 593 out.writeBytes("--"+boundary+"\r\n"); 594 out.writeBytes("Content-Disposition: form-data; name=\"handle\"; filename=\"" +"testname" +"\"\r\n\r\n"); 595 String oldline=""; 596 if (getPackageStep()!=null) { oldline = getPackageStep().getUserFeedBack(); } 597 598 try { 599 BufferedInputStream in = new BufferedInputStream (bundle.getJarStream()); 600 int val; 601 int totallen=0; 602 while ((val = in.read()) != -1) { 603 out.write(val); 604 totallen++; 605 if (getPackageStep()!=null) { 606 String lenp=" send "; 607 if (totallen<1024) { 608 lenp+=""+totallen+" bytes"; 609 } else if (totallen<(1024*1024)) { 610 lenp+=""+((float)totallen/1024)+" KB"; 611 } else { 612 lenp+=""+((float)totallen/(1024*1024))+" MB"; 613 } 614 getPackageStep().setUserFeedBack(oldline+lenp); 615 } 616 } 617 } catch (Exception e) { 618 log.error("can't load : " + path); 619 e.printStackTrace(); 620 } 621 out.writeBytes("\r\n"); 622 out.writeBytes("--"+boundary+"--\r\n"); 623 out.flush(); 624 out.close(); 625 626 if (getPackageStep()!=null) getPackageStep().setUserFeedBack(oldline+" server processing"); 627 628 BufferedReader rd = new BufferedReader (new InputStreamReader (conn.getInputStream())); 630 String line; 631 while ((line = rd.readLine()) != null) { 632 } 634 rd.close(); 635 } catch (Exception e) { 636 log.error("Publish upload problem to : "+posturl); 637 } 638 return true; 639 } 640 } 641 642 | Popular Tags |