public StorageErrorReason checkStorageOnAdd(
     Request request, CollectionResource nearestParent, Path parentPath, String host) {
   if (nearestParent instanceof QuotaResource) {
     QuotaResource qr = (QuotaResource) nearestParent;
     Long llAvail = qr.getQuotaAvailable();
     if (llAvail == null) {
       Logger.debug(this, "no quota data available");
       return null;
     }
     if (llAvail <= 0) {
       Logger.debug(this, "no quota available, reject");
       return StorageErrorReason.SER_QUOTA_EXCEEDED;
     } else {
       // new content must be less then that available
       Long newContentLength = request.getContentLengthHeader();
       if (newContentLength == null) {
         Logger.debug(this, "new content length is not available, cant check quota, allow");
         return null;
       }
       if (newContentLength < llAvail) {
         return null;
       } else {
         Logger.debug(this, "new content length is greater then available storage, reject");
         return StorageErrorReason.SER_QUOTA_EXCEEDED;
       }
     }
   } else {
     return null;
   }
 }
Exemple #2
0
 public void sample(InputStream in) {
   Logger.debug(this, "outputting sample");
   try {
     ByteArrayOutputStream out = FileUtils.readIn(in);
     writer.write(out.toString());
   } catch (FileNotFoundException ex) {
     Logger.error(this, "", ex);
   } catch (IOException ex) {
     Logger.error(this, "", ex);
   } finally {
     FileUtils.close(in);
   }
 }
Exemple #3
0
 /** Read the binary form of a key, preceded by a short for its length. */
 public static FreenetURI readFullBinaryKeyWithLength(DataInputStream dis) throws IOException {
   int len = dis.readShort();
   byte[] buf = new byte[len];
   dis.readFully(buf);
   Logger.minor(FreenetURI.class, "Read " + len + " bytes for key");
   return fromFullBinaryKey(buf);
 }
Exemple #4
0
  /**
   * Get the FreenetURI as a string.
   *
   * @param prefix Whether to include the freenet: prefix.
   * @param pureAscii If true, encode any non-english characters. If false, only encode dangerous
   *     characters (slashes e.g.).
   */
  public String toString(boolean prefix, boolean pureAscii) {
    if (keyType == null) {
      // Not activated or something...
      Logger.minor(this, "Not activated?? in toString(" + prefix + "," + pureAscii + ")");
      return null;
    }
    StringBuilder b;
    if (prefix) b = new StringBuilder("freenet:");
    else b = new StringBuilder();

    b.append(keyType).append('@');

    if (!"KSK".equals(keyType)) {
      if (routingKey != null) b.append(Base64.encode(routingKey));
      if (cryptoKey != null) b.append(',').append(Base64.encode(cryptoKey));
      if (extra != null) b.append(',').append(Base64.encode(extra));
      if (docName != null) b.append('/');
    }

    if (docName != null) b.append(URLEncoder.encode(docName, "/", pureAscii));
    if (keyType.equals("USK")) {
      b.append('/');
      b.append(suggestedEdition);
    }
    if (metaStr != null)
      for (int i = 0; i < metaStr.length; i++) {
        b.append('/').append(URLEncoder.encode(metaStr[i], "/", pureAscii));
      }
    return b.toString();
  }
Exemple #5
0
 /**
  * Write a binary representation of this URI, with a short length, so it can be passed over if
  * necessary.
  *
  * @param dos The stream to write to.
  * @throws MalformedURLException If the key could not be written because of inconsistencies or
  *     other problems in the key itself.
  * @throws IOException If an error occurred while writing the key.
  */
 public void writeFullBinaryKeyWithLength(DataOutputStream dos) throws IOException {
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   DataOutputStream ndos = new DataOutputStream(baos);
   writeFullBinaryKey(ndos);
   ndos.close();
   byte[] data = baos.toByteArray();
   if (data.length > Short.MAX_VALUE)
     throw new MalformedURLException("Full key too long: " + data.length + " - " + this);
   dos.writeShort((short) data.length);
   Logger.minor(this, "Written " + data.length + " bytes");
   dos.write(data);
 }
Exemple #6
0
 /** USK constructor from components. */
 public FreenetURI(
     byte[] pubKeyHash, byte[] cryptoKey, byte[] extra, String siteName, long suggestedEdition2) {
   //		this.uniqueHashCode = super.hashCode();
   this.keyType = "USK";
   this.routingKey = pubKeyHash;
   this.cryptoKey = cryptoKey;
   this.extra = extra;
   this.docName = siteName;
   this.suggestedEdition = suggestedEdition2;
   metaStr = null;
   Logger.minor(this, "Created from components (USK): " + toString(), new Exception("debug"));
 }
Exemple #7
0
 public static String findContentTypes(File file) {
   Collection mimeTypes = null;
   try {
     mimeTypes = MimeUtil.getMimeTypes(file);
   } catch (MimeException e) {
     Logger.warning(
         ContentTypeUtils.class,
         "exception retrieving content type for file: " + file.getAbsolutePath(),
         e);
     return "application/binary";
   }
   return buildContentTypeText(mimeTypes);
 }
Exemple #8
0
 public FreenetURI(
     String keyType,
     String docName,
     String[] metaStr,
     byte[] routingKey,
     byte[] cryptoKey,
     byte[] extra2) {
   //		this.uniqueHashCode = super.hashCode();
   this.keyType = keyType.trim().toUpperCase().intern();
   this.docName = docName;
   this.metaStr = metaStr;
   this.routingKey = routingKey;
   this.cryptoKey = cryptoKey;
   this.extra = extra2;
   this.suggestedEdition = -1;
   Logger.minor(this, "Created from components: " + toString(), new Exception("debug"));
 }
Exemple #9
0
 /**
  * Generate a suggested filename for the URI. This may be constructed from more than one part of
  * the URI e.g. SSK@blah,blah,blah/sitename/ might return sitename.
  */
 public String getPreferredFilename() {
   Logger.minor(this, "Getting preferred filename for " + this);
   ArrayList<String> names = new ArrayList<String>();
   if (keyType != null
       && (keyType.equals("KSK") || keyType.equals("SSK") || keyType.equals("USK"))) {
     Logger.minor(this, "Adding docName: " + docName);
     names.add(docName);
     if (keyType.equals("USK")) names.add(Long.toString(suggestedEdition));
   }
   if (metaStr != null)
     for (int i = 0; i < metaStr.length; i++) {
       if (metaStr[i] == null || metaStr[i].equals("")) {
         Logger.minor(this, "metaString " + i + ": was null or empty");
         continue;
       }
       Logger.minor(this, "Adding metaString " + i + ": " + metaStr[i]);
       names.add(metaStr[i]);
     }
   StringBuilder out = new StringBuilder();
   for (int i = 0; i < names.size(); i++) {
     String s = names.get(i);
     Logger.minor(this, "name " + i + " = " + s);
     s = FileUtil.sanitize(s);
     Logger.minor(this, "Sanitized name " + i + " = " + s);
     if (s.length() > 0) {
       if (out.length() > 0) out.append('-');
       out.append(s);
     }
   }
   Logger.minor(this, "out = " + out.toString());
   if (out.length() == 0) {
     if (routingKey != null) {
       Logger.minor(this, "Returning base64 encoded routing key");
       return Base64.encode(routingKey);
     }
     return "unknown";
   }
   return out.toString();
 }
Exemple #10
0
 public FreenetURI(FreenetURI uri) {
   //		this.uniqueHashCode = super.hashCode();
   keyType = uri.keyType;
   docName = uri.docName;
   if (uri.metaStr != null) {
     metaStr = new String[uri.metaStr.length];
     System.arraycopy(uri.metaStr, 0, metaStr, 0, metaStr.length);
   } else metaStr = null;
   if (uri.routingKey != null) {
     routingKey = new byte[uri.routingKey.length];
     System.arraycopy(uri.routingKey, 0, routingKey, 0, routingKey.length);
   } else routingKey = null;
   if (uri.cryptoKey != null) {
     cryptoKey = new byte[uri.cryptoKey.length];
     System.arraycopy(uri.cryptoKey, 0, cryptoKey, 0, cryptoKey.length);
   } else cryptoKey = null;
   if (uri.extra != null) {
     extra = new byte[uri.extra.length];
     System.arraycopy(uri.extra, 0, extra, 0, extra.length);
   } else extra = null;
   this.suggestedEdition = uri.suggestedEdition;
   Logger.minor(this, "Copied: " + toString() + " from " + uri.toString(), new Exception("debug"));
 }
Exemple #11
0
  /**
   * Create a FreenetURI from its string form. May or may not have a freenet: prefix.
   *
   * @throws MalformedURLException If the string could not be parsed.
   */
  public FreenetURI(String URI, boolean noTrim) throws MalformedURLException {
    //		this.uniqueHashCode = super.hashCode();
    if (URI == null) throw new MalformedURLException("No URI specified");

    if (!noTrim) URI = URI.trim();
    if (URI.indexOf('@') < 0 || URI.indexOf('/') < 0)
      // Encoded URL?
      try {
        URI = URLDecoder.decode(URI, false);
      } catch (URLEncodedFormatException e) {
        throw new MalformedURLException(
            "Invalid URI: no @ or /, or @ or / is escaped but there are invalid escapes");
      }

    URI = URI_PREFIX.matcher(URI).replaceFirst("");

    // decode keyType
    int atchar = URI.indexOf('@');
    if (atchar == -1) throw new MalformedURLException("There is no @ in that URI! (" + URI + ')');

    String _keyType = URI.substring(0, atchar).toUpperCase();
    URI = URI.substring(atchar + 1);

    boolean validKeyType = false;
    for (int i = 0; i < VALID_KEY_TYPES.length; i++) {
      if (_keyType.equals(VALID_KEY_TYPES[i])) {
        validKeyType = true;
        _keyType = VALID_KEY_TYPES[i];
        break;
      }
    }
    keyType = _keyType;
    if (!validKeyType) throw new MalformedURLException("Invalid key type: " + keyType);

    boolean isSSK = "SSK".equals(keyType);
    boolean isUSK = "USK".equals(keyType);
    boolean isKSK = "KSK".equals(keyType);

    // decode metaString
    ArrayList<String> sv = null;
    int slash2;
    sv = new ArrayList<String>();
    if (isKSK) URI = "/" + URI; // ensure that KSK docNames are decoded
    while ((slash2 = URI.lastIndexOf('/')) != -1) {
      String s;
      try {
        s = URLDecoder.decode(URI.substring(slash2 + 1 /* "/".length() */), true);
      } catch (URLEncodedFormatException e) {
        throw (MalformedURLException) new MalformedURLException(e.toString()).initCause(e);
      }
      if (s != null) sv.add(s);
      URI = URI.substring(0, slash2);
    }

    // sv is *backwards*
    // this makes for more efficient handling

    if (isSSK || isUSK || isKSK) {

      if (sv.isEmpty()) throw new MalformedURLException("No docname for " + keyType);
      docName = sv.remove(sv.size() - 1);
      if (isUSK) {
        if (sv.isEmpty()) throw new MalformedURLException("No suggested edition number for USK");
        try {
          suggestedEdition = Long.parseLong(sv.remove(sv.size() - 1));
        } catch (NumberFormatException e) {
          throw (MalformedURLException)
              new MalformedURLException("Invalid suggested edition: " + e).initCause(e);
        }
      } else suggestedEdition = -1;
    } else {
      // docName not necessary, nor is it supported, for CHKs.
      docName = null;
      suggestedEdition = -1;
    }

    if (!sv.isEmpty()) {
      metaStr = new String[sv.size()];
      for (int i = 0; i < metaStr.length; i++) {
        metaStr[i] = sv.get(metaStr.length - 1 - i).intern();
        if (metaStr[i] == null) throw new NullPointerException();
      }
    } else metaStr = null;

    if (isKSK) {
      routingKey = extra = cryptoKey = null;
      return;
    }

    // strip 'file extensions' from CHKs
    // added by aum ([email protected])
    if ("CHK".equals(keyType)) URI = URI.split("[.]")[0];

    // URI now contains: routingKey[,cryptoKey][,metaInfo]
    StringTokenizer st = new StringTokenizer(URI, ",");
    try {
      if (st.hasMoreTokens()) routingKey = Base64.decode(st.nextToken());
      else {
        routingKey = cryptoKey = extra = null;
        return;
      }
      if (!st.hasMoreTokens()) {
        cryptoKey = extra = null;
        return;
      }

      // Can be cryptokey or name-value pair.
      String t = st.nextToken();
      cryptoKey = Base64.decode(t);
      if (!st.hasMoreTokens()) {
        extra = null;
        return;
      }
      extra = Base64.decode(st.nextToken());

    } catch (IllegalBase64Exception e) {
      throw new MalformedURLException("Invalid Base64 quantity: " + e);
    }
    Logger.debug(
        this, "Created from parse: " + toString() + " from " + URI, new Exception("debug"));
  }
  public StorageErrorReason checkStorageOnReplace(
      Request request, CollectionResource parent, Resource replaced, String host) {
    if (parent instanceof QuotaResource) {
      QuotaResource qr = (QuotaResource) parent;
      Long llAvail = qr.getQuotaAvailable();
      if (llAvail == null) {
        Logger.debug(this, "no quota data available");
        return null;
      }
      if (llAvail <= 0) {
        // new content length must be less then existing
        Long newContentLength = request.getContentLengthHeader();
        if (newContentLength == null) {
          Logger.debug(this, "new content length is not available, cant check quota, reject");
          return StorageErrorReason.SER_QUOTA_EXCEEDED;
        }
        if (replaced instanceof GetableResource) {
          GetableResource gr = (GetableResource) replaced;
          Long existingLength = gr.getContentLength();
          if (existingLength == null) {
            Logger.debug(
                this, "existing content length cant be determined, cant check quota, reject");
            return StorageErrorReason.SER_QUOTA_EXCEEDED;
          } else {
            long diff = existingLength - newContentLength;
            if (diff > 0) {
              return null;
            } else {
              Logger.debug(
                  this,
                  "new content is larger then existing content, but no quota is available, reject");
              return StorageErrorReason.SER_QUOTA_EXCEEDED;
            }
          }
        } else {
          Logger.debug(
              this, "existing content length cant be determined, cant check quota, reject");
          return StorageErrorReason.SER_QUOTA_EXCEEDED;
        }
      } else {
        // difference of new content to existing must be less then available, but if in doubt allow
        Long newContentLength = request.getContentLengthHeader();
        if (newContentLength == null) {
          Logger.debug(this, "new content length is not available, cant check quota, allow");
          return null;
        }
        if (replaced instanceof GetableResource) {
          GetableResource gr = (GetableResource) replaced;
          Long existingLength = gr.getContentLength();
          if (existingLength == null) {
            Logger.debug(
                this, "existing content length cant be determined, cant check quota, allow");
            return null;
          } else {
            long diff = newContentLength - existingLength; // this is the amount extra needed
            if (diff <= llAvail) {
              return null;
            } else {
              Logger.debug(
                  this,
                  "new content is larger then existing content, but no quota is available, reject");
              return StorageErrorReason.SER_QUOTA_EXCEEDED;
            }
          }
        } else {
          Logger.debug(this, "existing content length cant be determined, cant check quota, allow");
          return null;
        }
      }
      // if difference between new content and existing is less then available, then ok

    } else {
      return null;
    }
  }