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
  /**
   * 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;
    }
  }