Package freenet.support.api

Examples of freenet.support.api.Bucket


    if(logMINOR) Logger.minor(this, "Finalizing binary blob "+this, new Exception("debug"));
    if (!_isSingleBucket) {
      if (!mark && (_buckets.size()==1)) {
        return;
      }
      Bucket out = _bf.makeBucket(-1);
      getSnapshot(out, mark);
      for (int i=0,n=_buckets.size(); i<n;i++) {
        _buckets.get(i).free();
      }
      if (mark) {
        out.setReadOnly();
      }
      _buckets.clear();
      _buckets.add(0, out);
    } else if (mark){
      DataOutputStream out = new DataOutputStream(getOutputStream());
      try {
      BinaryBlob.writeEndBlob(out);
      } finally {
      out.close();
      }
    }
    if (mark) {
      _finalized = true;
    }
View Full Code Here


  }
 
  @Override
  public String getPartAsStringThrowing(String name, int maxLength) throws NoSuchElementException, SizeLimitExceededException {
    if(freedParts) throw new IllegalStateException("Already freed");
    Bucket part = this.parts.get(name);
   
    if(part == null)
      throw new NoSuchElementException(name);
   
    if(part.size() > maxLength)
      throw new SizeLimitExceededException();
   
    return getPartAsLimitedString(part, maxLength);
  }
View Full Code Here

  }
 
  @Override
  public String getPartAsStringFailsafe(String name, int maxLength) {
    if(freedParts) throw new IllegalStateException("Already freed");
    Bucket part = this.parts.get(name);
    return part == null ? "" : getPartAsLimitedString(part, maxLength);
  }
View Full Code Here

   */
  @Override
  @Deprecated
  public byte[] getPartAsBytes(String name, int maxlength) {
    if(freedParts) throw new IllegalStateException("Already freed");
    Bucket part = this.parts.get(name);
    if(part == null) return new byte[0];
   
    if (part.size() > maxlength) return new byte[0];
   
    InputStream is = null;
    DataInputStream dis = null;
    try {
      is = part.getInputStream();
      dis = new DataInputStream(is);
      byte[] buf = new byte[(int)Math.min(part.size(), maxlength)];
      dis.readFully(buf);
      return buf;
    } catch (IOException ioe) {
           Logger.error(this, "Caught IOE:" + ioe.getMessage());
    } finally {
View Full Code Here

  }
 
  @Override
  public byte[] getPartAsBytesThrowing(String name, int maxLength) throws NoSuchElementException, SizeLimitExceededException {
    if(freedParts) throw new IllegalStateException("Already freed");
    Bucket part = this.parts.get(name);
   
    if(part == null)
      throw new NoSuchElementException(name);
   
    if(part.size() > maxLength)
      throw new SizeLimitExceededException();
   
    return getPartAsLimitedBytes(part, maxLength);
  }
View Full Code Here

  }
 
  @Override
  public byte[] getPartAsBytesFailsafe(String name, int maxLength) {
    if(freedParts) throw new IllegalStateException("Already freed");
    Bucket part = this.parts.get(name);
    return part == null ? new byte[0] : getPartAsLimitedBytes(part, maxLength);
  }
View Full Code Here

        /*
         * copy the data into a bucket now,
         * before we go into the redirect loop
         */
       
        Bucket data;

        boolean methodIsConfigurable = true;

        String slen = headers.get("content-length");

        if (METHODS_MUST_HAVE_DATA.contains(method)) {
          // <method> must have data
          methodIsConfigurable = false;
          if (slen == null) {
            ctx.shouldDisconnect = true;
            ctx.sendReplyHeaders(400, "Bad Request", null, null, -1);
            return;
          }
        } else if (METHODS_CANNOT_HAVE_DATA.contains(method)) {
          // <method> can not have data
          methodIsConfigurable = false;
          if (slen != null) {
            ctx.shouldDisconnect = true;
            ctx.sendReplyHeaders(400, "Bad Request", null, null, -1);
            return;
          }
        }

        if (slen != null) {
          long len;
          try {
            len = Integer.parseInt(slen);
            if(len < 0) throw new NumberFormatException("content-length less than 0");
          } catch (NumberFormatException e) {
            ctx.shouldDisconnect = true;
            ctx.sendReplyHeaders(400, "Bad Request", null, null, -1);
            return;
          }
          if(allowPost && ((!container.publicGatewayMode()) || ctx.isAllowedFullAccess())) {
            data = bf.makeBucket(len);
            BucketTools.copyFrom(data, is, len);
          } else {
            FileUtil.skipFully(is, len);
            if (method.equals("POST")) {
              ctx.sendMethodNotAllowed("POST", true);
            } else {
              sendError(sock.getOutputStream(), 403, "Forbidden", "Content not allowed in this configuration", true, null);
            }
            ctx.close();
            return;
          }
        } else {
          // we're not doing to use it, but we have to keep
          // the compiler happy
          data = null;
        }

        if (!container.enableExtendedMethodHandling()) {
          if (!METHODS_RESTRICTED_MODE.contains(method)) {
            sendError(sock.getOutputStream(), 403, "Forbidden", "Method not allowed in this configuration", true, null);
            return;
          }
        }

        // Handle it.
        try {
          boolean redirect = true;
          while (redirect) {
            // don't go around the loop unless set explicitly
            redirect = false;
           
            Toadlet t;
            try {
              t = container.findToadlet(uri);
            } catch (PermanentRedirectException e) {
              Toadlet.writePermanentRedirect(ctx, "Found elsewhere", e.newuri.toASCIIString());
              break;
            }
         
            if(t == null) {
              ctx.sendNoToadletError(ctx.shouldDisconnect);
              break;
            }

            // if the Toadlet does not support the method, we don't need to parse the data
            // also due this pre check a 'NoSuchMethodException' should never appear
            if (!(t.findSupportedMethods().contains(method))) {
              ctx.sendMethodNotAllowed(method, ctx.shouldDisconnect);
              break;
            }

            HTTPRequestImpl req = new HTTPRequestImpl(uri, data, ctx, method);
           
            // require form password if it's a POST, unless the toadlet requests otherwise
            if (method.equals("POST") && !t.allowPOSTWithoutPassword()) {
              if (!ctx.checkFormPassword(req, t.path())) {
                break;
              }
            }
           
            if(ctx.isAllowedFullAccess()) {
              ctx.getPageMaker().parseMode(req, container);
            }
           
            try {
              callToadletMethod(t, method, uri, req, ctx, data, sock, redirect);
            } catch (RedirectException re) {
              uri = re.newuri;
              redirect = true;
            } finally {
              req.freeParts();
            }
          }
          if(ctx.shouldDisconnect) {
            sock.close();
            return;
          }
        } finally {
          if(data != null) data.free();
        }
      }
     
    } catch (ParseException e) {
      try {
View Full Code Here

  private boolean checkCache(ClientContext context) {
    // Fproxy uses lookupInstant() with mustCopy = false. I.e. it can reuse stuff unsafely. If the user frees it it's their fault.
    if(bogusUSK(context)) return false;
    CacheFetchResult result = context.downloadCache == null ? null : context.downloadCache.lookupInstant(uri, !fctx.filterData, false, null);
    if(result == null) return false;
    Bucket data = null;
    String mimeType = null;
    if((!fctx.filterData) && (!result.alreadyFiltered)) {
      if(fctx.overrideMIME == null || fctx.overrideMIME.equals(result.getMimeType())) {
        // Works as-is.
        // Any time we re-use old content we need to remove the tracker because it may not remain available.
        tracker.removeFetcher(this);
        onSuccess(result, null);
        return true;
      } else if(fctx.overrideMIME != null && !fctx.overrideMIME.equals(result.getMimeType())) {
        // Change the MIME type.
        tracker.removeFetcher(this);
        onSuccess(new FetchResult(new ClientMetadata(fctx.overrideMIME), result.asBucket()), null);
        return true;
      }
    } else if(result.alreadyFiltered) {
      if(refilterPolicy == REFILTER_POLICY.RE_FETCH || !fctx.filterData) {
        // Can't use it.
        return false;
      } else if(fctx.filterData) {
        if(shouldAcceptCachedFilteredData(fctx, result)) {
          if(refilterPolicy == REFILTER_POLICY.ACCEPT_OLD) {
            tracker.removeFetcher(this);
            onSuccess(result, null);
            return true;
          } // else re-filter
        } else
          return false;
      } else {
        return false;
      }
    }
    data = result.asBucket();
    mimeType = result.getMimeType();
    if(mimeType == null || mimeType.equals("")) mimeType = DefaultMIMETypes.DEFAULT_MIME_TYPE;
    if(fctx.overrideMIME != null && !result.alreadyFiltered)
      mimeType = fctx.overrideMIME;
    else if(fctx.overrideMIME != null && !mimeType.equals(fctx.overrideMIME)) {
      // Doesn't work.
      return false;
    }
    String fullMimeType = mimeType;
    mimeType = ContentFilter.stripMIMEType(mimeType);
    FilterMIMEType type = ContentFilter.getMIMEType(mimeType);
    if(type == null || ((!type.safeToRead) && type.readFilter == null)) {
      UnknownContentTypeException e = new UnknownContentTypeException(mimeType);
      data.free();
      onFailure(new FetchException(e.getFetchErrorCode(), data.size(), e, mimeType), null);
      return true;
    } else if(type.safeToRead) {
      tracker.removeFetcher(this);
      onSuccess(new FetchResult(new ClientMetadata(mimeType), data), null);
      return true;
    } else {
      // Try to filter it.
      Bucket output = null;
      InputStream is = null;
      OutputStream os = null;
      try {
        output = context.tempBucketFactory.makeBucket(-1);
        is = data.getInputStream();
        os = output.getOutputStream();
        ContentFilter.filter(is, os, fullMimeType, uri.toURI("/"), null, null, fctx.charset, context.linkFilterExceptionProvider);
        is.close();
        is = null;
        os.close();
        os = null;
View Full Code Here

    wakeWaiters(true);
  }

  @Override
  public void onSuccess(FetchResult result, ClientGetter state) {
    Bucket droppedData = null;
    synchronized(this) {
      if(cancelled)
        droppedData = result.asBucket();
      else
        this.data = result.asBucket();
      this.mimeType = result.getMimeType();
      this.finished = true;
    }
    wakeWaiters(true);
    if(droppedData != null)
      droppedData.free();
  }
View Full Code Here

      getter.cancel(tracker.context);
    } catch (Throwable t) {
      // Ensure we get to the next bit
      Logger.error(this, "Failed to cancel: "+t, t);
    }
    Bucket d;
    synchronized(this) {
      d = data;
      cancelled = true;
    }
    if(d != null) {
      try {
        d.free();
      } catch (Throwable t) {
        // Ensure we get to the next bit
        Logger.error(this, "Failed to free: "+t, t);
      }
    }
View Full Code Here

TOP

Related Classes of freenet.support.api.Bucket

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.