Exemple #1
0
  /**
   * Construct a cookie from the URI and header fields
   *
   * @param uri URI for cookie
   * @param header Set of attributes in header
   */
  public Cookie(URI uri, String header) {
    String[] attributes = header.split(";");
    String nameValue = attributes[0].trim();
    this.uri = uri;
    this.name = nameValue.substring(0, nameValue.indexOf('='));
    this.value = nameValue.substring(nameValue.indexOf('=') + 1);
    this.path = "/";
    this.domain = uri.getHost();

    for (int i = 1; i < attributes.length; i++) {
      nameValue = attributes[i].trim();
      int equals = nameValue.indexOf('=');
      if (equals == -1) {
        continue;
      }
      String name = nameValue.substring(0, equals);
      String value = nameValue.substring(equals + 1);
      if (name.equalsIgnoreCase("domain")) {
        String uriDomain = uri.getHost();
        if (uriDomain.equals(value)) {
          this.domain = value;
        } else {
          if (!value.startsWith(".")) {
            value = '.' + value;
          }
          uriDomain = uriDomain.substring(uriDomain.indexOf('.'));
          if (!uriDomain.equals(value)
              && !uriDomain.endsWith(value)
              && !value.endsWith(uriDomain)) {
            throw new IllegalArgumentException("Trying to set foreign cookie");
          }
          this.domain = value;
        }
      } else if (name.equalsIgnoreCase("path")) {
        this.path = value;
      } else if (name.equalsIgnoreCase("expires")) {
        try {
          this.expires = whiteSpaceFormat.parse(value);
        } catch (ParseException e) {
          try {
            this.expires = hyphenFormat.parse(value);
          } catch (ParseException e2) {
            throw new IllegalArgumentException("Bad date format in header: " + value);
          }
        }
      }
    }
  }
  public void setLibrary(URI url) throws Exception {
    if (url == null) url = new URI("http://repo.jpm4j.org/");

    this.host = new URLClient(url.toString());
    host.setReporter(reporter);
    library = JSONRPCProxy.createRPC(JpmRepo.class, host, "jpm");
  }
 void put(final URI uri, ArtifactData data) throws Exception {
   reporter.trace("put %s %s", uri, data);
   File tmp = createTempFile(repoDir, "mtp", ".whatever");
   tmp.deleteOnExit();
   try {
     copy(uri.toURL(), tmp);
     byte[] sha = SHA1.digest(tmp).digest();
     reporter.trace("SHA %s %s", uri, Hex.toHexString(sha));
     ArtifactData existing = get(sha);
     if (existing != null) {
       reporter.trace("existing");
       xcopy(existing, data);
       return;
     }
     File meta = new File(repoDir, Hex.toHexString(sha) + ".json");
     File file = new File(repoDir, Hex.toHexString(sha));
     rename(tmp, file);
     reporter.trace("file %s", file);
     data.file = file.getAbsolutePath();
     data.sha = sha;
     data.busy = false;
     CommandData cmddata = parseCommandData(data);
     if (cmddata.bsn != null) {
       data.name = cmddata.bsn + "-" + cmddata.version;
     } else data.name = Strings.display(cmddata.title, cmddata.bsn, cmddata.name, uri);
     codec.enc().to(meta).put(data);
     reporter.trace("TD = " + data);
   } finally {
     tmp.delete();
     reporter.trace("puted %s %s", uri, data);
   }
 }
Exemple #4
0
  /**
   * Check if cookie isn't expired and if URI matches, should cookie be included in response.
   *
   * @param uri URI to check against
   * @return true if match, false otherwise
   */
  public boolean matches(URI uri) {

    if (hasExpired()) {
      return false;
    }

    String path = uri.getPath();
    if (path == null) {
      path = "/";
    }

    return path.startsWith(this.path);
  }