示例#1
0
  private boolean isUpToDate(URL u, File dest) throws IOException {
    if (!dest.isFile()) {
      return false;
    }

    HttpURLConnection con = null;

    try {
      con = URLSupport.openURL(u, options.getProxyServer());
      con.setConnectTimeout(CONNECTION_TIMEOUT);
      con.setRequestMethod("HEAD");
      con.connect();

      String serverLen = con.getHeaderField("Content-Length");
      if (serverLen != null) {
        long contentLength = Long.parseLong(serverLen);
        if (dest.length() != contentLength) {
          return false;
        }
      }

      return true;
    } finally {
      Closer.close(con);
    }
  }
 private List<String> getProviders() {
   List<String> providers = URLSupport.getProtocolHandlerProviders();
   providers.addAll(packagesAdded);
   return providers;
 }
示例#3
0
  @Override
  public void run() {
    File destinationFile =
        new File(
            destination,
            artifact.getArtifactId()
                + ((options.isStripVersions()) ? "" : "-" + artifact.getVersion())
                + (artifact.getClassifier().isEmpty() ? "" : ("-" + artifact.getClassifier()))
                + ".jar");
    for (String server : options.getServers()) {
      URL u = artifact.toURL(server);
      HttpURLConnection con = null;
      BufferedInputStream bis = null;
      BufferedOutputStream bos = null;

      try {
        if (!isUpToDate(u, destinationFile)) {
          con = URLSupport.openURL(u, options.getProxyServer());
          con.setConnectTimeout(CONNECTION_TIMEOUT);
          con.connect();

          bis = new BufferedInputStream(con.getInputStream());
          bos = new BufferedOutputStream(new FileOutputStream(destinationFile));
          Deque<TransferBuffer> dq = new ArrayDeque<TransferBuffer>();

          ArtifactReader r =
              new ArtifactReader(
                  project,
                  bis,
                  dq,
                  BUFFER_SIZE,
                  options.isCheckSHADigests() && !artifact.getDigest().isEmpty());
          Thread rt = new Thread(r);
          rt.start();

          ArtifactWriter w = new ArtifactWriter(project, bos, dq);
          Thread wt = new Thread(w);
          wt.start();

          rt.join();
          wt.join();

          if (r.wasSuccessful() && w.wasSuccessful()) {
            if (options.isCheckSHADigests() && !artifact.getDigest().isEmpty()) {
              if (!digestEquals(artifact, r.getDigest())) {
                artifact.setStatus(Artifact.Status.DIGEST_MISMATCH);
                destinationFile.deleteOnExit();
                project.log(
                    "download failed with incorrect digest: "
                        + artifact
                        + " - actual: "
                        + byteArrayDigestToHexString(r.getDigest()),
                    Project.MSG_ERR);
                return;
              }
            }
            artifact.setStatus(Artifact.Status.DOWNLOADED);
          }
        } else {
          artifact.setStatus(Artifact.Status.UPTODATE);
        }

        project.log(
            "download successful: " + artifact,
            (artifact.getStatus() != Artifact.Status.UPTODATE)
                ? Project.MSG_ERR
                : Project.MSG_VERBOSE);
        return;
      } catch (Exception e) {
        if (!YankTask.SOURCE_CLASSIFIER.equals(artifact.getClassifier())) {
          project.log(e.getMessage(), e, Project.MSG_VERBOSE);
          project.log("download failed: " + artifact, Project.MSG_ERR);
        }
        artifact.setStatus(Artifact.Status.FAILED);
      } finally {
        Closer.close(bis);
        Closer.close(bos);
        Closer.close(con);
      }
    }
  }