Package com.planet_ink.coffee_web.interfaces

Examples of com.planet_ink.coffee_web.interfaces.DataBuffers


    {
      return buffers;
    }
   
    String compressorName;
    DataBuffers compressedBytes;
    if((deflatePreference>0.0)&&(deflatePreference>gzipPreference))
    {
      // deflate between ie & everyone else appears to be a problem
      // so, all else being equal, prefer gzip below
      compressorName="deflate";
View Full Code Here


    {
      pageFile = pathFile;
    }
   
    final MIMEType mimeType = MIMEType.getMIMEType(pageFile.getName());
    DataBuffers buffers = null;
    try
    {
      buffers = new CWDataBuffers(); // before forming output, process range request
      final Class<? extends HTTPOutputConverter> converterClass=config.getConverters().findConverter(mimeType);
      if(converterClass != null)
      {
        buffers=config.getFileCache().getFileData(pageFile, null);
        //checkIfModifiedSince(request,buffers); this is RETARDED!!!
        HTTPOutputConverter converter;
        try {
          converter = converterClass.newInstance();
          return new CWDataBuffers(converter.convertOutput(config, request, pathFile, HTTPStatus.S200_OK, buffers.flushToBuffer()), System.currentTimeMillis(), true);
        }
        catch (final Exception e) { }
        return buffers;
      }
      else
      {
        return config.getFileCache().getFileData(pageFile, null);
      }
    }
    catch(final HTTPException e)
    {
      buffers.close();
      throw e;
    }
  }
View Full Code Here

    final Map<HTTPHeader,String> extraHeaders=new TreeMap<HTTPHeader, String>();
    extraHeaders.put(HTTPHeader.CONTENT_TYPE, mimeType.getType());
    confirmMimeType(request,mimeType);
   
    HTTPStatus responseStatus = HTTPStatus.S200_OK;
    DataBuffers buffers = null;
    try
    {
      buffers = new CWDataBuffers(); // before forming output, process range request
      switch(request.getMethod())
      {
      case HEAD:
      case GET:
      case POST:
      {
        final Class<? extends HTTPOutputConverter> converterClass=config.getConverters().findConverter(mimeType);
        if(converterClass != null)
        {
          buffers=config.getFileCache().getFileData(pageFile, null);
          //checkIfModifiedSince(request,buffers); this is RETARDED!!!
          try
          {
            HTTPOutputConverter converter;
            converter = converterClass.newInstance();
            extraHeaders.put(HTTPHeader.CACHE_CONTROL, "no-cache");
            final long dateTime=System.currentTimeMillis();
            extraHeaders.put(HTTPHeader.EXPIRES, HTTPIOHandler.DATE_FORMAT.format(Long.valueOf(dateTime)));
            buffers=new CWDataBuffers(converter.convertOutput(config, request, pathFile, HTTPStatus.S200_OK, buffers.flushToBuffer()), dateTime, true);
            buffers = handleEncodingRequest(request, null, buffers, extraHeaders);
          }
          catch (final Exception e)
          {
            config.getLogger().throwing("", "", e);
            throw HTTPException.standardException(HTTPStatus.S500_INTERNAL_ERROR);
          }
        }
        else
        {
          final String[] eTagMarker =generateETagMarker(request);
          buffers=config.getFileCache().getFileData(pageFile, eTagMarker);
          if((eTagMarker[0]!=null)&&(eTagMarker[0].length()>0))
            extraHeaders.put(HTTPHeader.ETAG, eTagMarker[0]);
          checkIfModifiedSince(request,buffers);
          buffers = handleEncodingRequest(request, pageFile, buffers, extraHeaders);
        }
        if(buffers == null)
        {
          throw HTTPException.standardException(HTTPStatus.S500_INTERNAL_ERROR);
        }
        final long fullSize = buffers.getLength();
        final long[] fullRange = setRangeRequests(request, buffers);
        if(fullRange != null)
        {
          responseStatus = HTTPStatus.S206_PARTIAL_CONTENT;
          extraHeaders.put(HTTPHeader.CONTENT_RANGE, "bytes "+fullRange[0]+"-"+fullRange[1]+"/"+fullSize);
        }
        break;
      }
      default:
        break;
      }

      lastHttpStatusCode=responseStatus.getStatusCode();

      String specifiedHost = request.getHost(); // check for chunking
      int chunkedSize = 0;
      if(specifiedHost != null)
      {
        int portIndex=specifiedHost.indexOf(':');
        if(portIndex > 0)
          specifiedHost=specifiedHost.substring(0,portIndex);
        final ChunkSpec chunkSpec = config.getChunkSpec(specifiedHost, request.getClientPort(), request.getUrlPath());
        if((chunkSpec != null) && (buffers.getLength() >= chunkSpec.getMinFileSize()))
        {
          chunkedSize = chunkSpec.getChunkSize(); // set chunking flag
          extraHeaders.put(HTTPHeader.TRANSFER_ENCODING, "chunked");
        }
      }
     
      // finally, generate the response headers and body
      switch(request.getMethod())
      {
      case HEAD:
        final DataBuffers header=new CWDataBuffers(generateStandardHeaderResponse(request, responseStatus, extraHeaders, buffers), buffers.getLastModified().getTime(), false);
        buffers.close();
        return header;
      case GET:
      case POST:
        final DataBuffers buf = generateStandardResponse(request, responseStatus, extraHeaders, buffers);
        buf.setChunked(chunkedSize);
        return buf;
      default:
      {
        final HTTPException exception = new HTTPException(HTTPStatus.S405_METHOD_NOT_ALLOWED);
        exception.getErrorHeaders().put(HTTPHeader.ALLOW, HTTPMethod.getAllowedList());
View Full Code Here

  {
    synchronized(writeables)
    {
      while(writeables.size()>0)
      {
        final DataBuffers bufs=writeables.getFirst();
        while(bufs.hasNext())
        {
          final ByteBuffer buf=bufs.next();
          if(buf.remaining()>0)
          {
            webServerChannel.write(buf);
            if(buf.remaining()>0)
            {
View Full Code Here

                  if((forward != null) && (state != ParseState.CHUNKED_TRAILER_INLINE))
                  {
                    final String requestLine=startPortForwarding(forward.second, forward.first);
                    if(forwarder!=null)
                    {
                      final DataBuffers out=new CWDataBuffers();
                      out.add(ByteBuffer.wrap(requestLine.getBytes()),0,false);
                      final ByteBuffer writeableBuf=ByteBuffer.allocate(buffer.remaining());
                      writeableBuf.put(buffer);
                      writeableBuf.flip();
                      out.add(writeableBuf,0,false);
                      forwarder.writeBytesToChannel(out);
                      buffer.clear();
                      state=ParseState.FORWARD;
                    }
                  }
View Full Code Here

   */
  @Override
  public int writeBlockingBytesToChannel(final DataBuffers buffers) throws IOException
  {
    int written=0;
    DataBuffers bufs=null;
    synchronized(writeables)
    {
      writeables.addLast(buffers);
      while(writeables.size()>0)
      {
        bufs=writeables.getFirst();
        while(bufs.hasNext())
        {
          ByteBuffer buffer=buffers.next();
          while(buffer.remaining()>0)
          {
            long maxToWrite = buffer.remaining();
            if(outputThrottle != null)
            {
              maxToWrite=outputThrottle.request(maxToWrite);
              if(maxToWrite < buffer.remaining())
                buffer = bufs.splitTopBuffer((int)maxToWrite);
            }
            final int bytesWritten=chan.write(buffer);
            if(bytesWritten>=0)
            {
              written+=bytesWritten;
View Full Code Here

   * interest.
   * @throws IOException
   */
  protected void handleWrites() throws IOException
  {
    DataBuffers bufs = null;
    synchronized(writeables)
    {
      while(writeables.size()>0)
      {
        bufs=writeables.getFirst();
        while(bufs.hasNext())
        {
          ByteBuffer buf=bufs.next();
          if(outputThrottle != null)
          {
            final long maxToWrite=outputThrottle.request(buf.remaining());
            if(maxToWrite < buf.remaining())
              buf = bufs.splitTopBuffer((int)maxToWrite);
          }
          final int written = chan.write(buf);
          if(outputThrottle != null)
          {
            outputThrottle.registerWritten(written);
          }
          if(buf.remaining()>0)
          {
            server.registerChannelInterest(chan, SelectionKey.OP_WRITE | SelectionKey.OP_READ);
            return;
          }
        }
        bufs.close();
        writeables.removeFirst();
      }
    }
  }
View Full Code Here

            }
            processBuffer(currentReq.getBuffer()); // process any data received
            if(currentReq.isFinished()) // if the request was completed, generate output!
            {
              final HTTPReqProcessor processor = new HTTPReqProcessor(config);
              final DataBuffers bufs = processor.generateOutput(currentReq);
             
              if(accessLog != null)
                accessLog.append(Log.makeLogEntry(Log.Type.access, Thread.currentThread().getName(),
                  currentReq.getClientAddress().getHostAddress()
                  +" "+currentReq.getHost()
                  +" \""+currentReq.getFullRequest()+" \" "+processor.getLastHttpStatusCode()+" "+bufs.getLength())).append("\n");
              writeBytesToChannel(bufs);
              // after output, prepare for a second request on this channel
              final String closeHeader = currentReq.getHeader(HTTPHeader.CONNECTION.lowerCaseName());
              if((closeHeader != null) && (closeHeader.trim().equalsIgnoreCase("close")))
                closeRequested = true;
              else
              {
                currentReq=new CWHTTPRequest(currentReq);
                currentState=ParseState.REQ_INLINE;
              }
            }
          }
        }
        catch(final HTTPException me) // if an exception is generated, go ahead and send it out
        {
          final DataBuffers bufs=me.generateOutput(currentReq);
          writeBytesToChannel(bufs);
          if(accessLog != null)
            accessLog.append(Log.makeLogEntry(Log.Type.access, Thread.currentThread().getName(),
              currentReq.getClientAddress().getHostAddress()
              +" "+currentReq.getHost()
              +" \""+currentReq.getFullRequest()+"\" "+me.getStatus().getStatusCode()+" "+bufs.getLength())).append("\n");
          // have to assume any exception caused
          // before a finish is malformed and needs a closed connection.
          if(currentReq.isFinished())
          {
            final String closeHeader = currentReq.getHeader(HTTPHeader.CONNECTION.lowerCaseName());
View Full Code Here

    str.append(HTTPIOHandler.SERVER_HEADER);
    str.append(HTTPIOHandler.CONN_HEADER);
    str.append(HTTPHeader.getKeepAliveHeader());
    str.append(HTTPHeader.DATE.makeLine(HTTPIOHandler.DATE_FORMAT.format(new Date(System.currentTimeMillis()))));
   
    DataBuffers finalBody=null;
    if((body.length()==0)
    &&(status.isAnError())
    &&(config!=null)
    &&(config.getErrorPage().length()>0))
    {
      final File errorFile=config.getFileManager().createFileFromPath(config.getErrorPage());
      DataBuffers fileBytes=null;
      try
      {
        fileBytes=config.getFileCache().getFileData(errorFile, null);
        final MIMEType mimeType=MIMEType.getMIMEType(config.getErrorPage());
        if(mimeType!=null)
        {
          headers.put(HTTPHeader.CONTENT_TYPE, mimeType.getType());
          final Class<? extends HTTPOutputConverter> converterClass=config.getConverters().findConverter(mimeType);
          if(converterClass != null)
          {
            final HTTPOutputConverter converter=converterClass.newInstance();
            finalBody=new CWDataBuffers(converter.convertOutput(config, request, errorFile, status, fileBytes.flushToBuffer()),0,true);
          }
          else
            finalBody=fileBytes;
        }
        else
          finalBody=fileBytes;
      }
      catch(final Exception e)
      {
        if(fileBytes!=null)
          fileBytes.close();
      }
    }
    if(finalBody==null)
    {
      finalBody=new CWDataBuffers(body.getBytes(), 0, true);
View Full Code Here

TOP

Related Classes of com.planet_ink.coffee_web.interfaces.DataBuffers

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.