Package org.xlightweb

Examples of org.xlightweb.NonBlockingBodyDataSource


          // does request contain a body?
          if (response.hasBody()) {
           
            // get the body
            NonBlockingBodyDataSource orgDataSource = response.getNonBlockingBody();
           
            // ... and replace it 
            final BodyDataSink inBodyChannel = exchange.send(response.getResponseHeader());
           
            //... by a body forward handler
            BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
             
              @Override
              public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
                ByteBuffer[] bufs = bodyDataSource.readByteBufferByLength(bodyDataSource.available());
                   
                for (ByteBuffer byteBuffer : bufs) {
                  // WARNING DataConverter call could fail!
                  requestString.append(DataConverter.toString(byteBuffer.duplicate()));
                }
                   
                bodyDataSink.write(bufs);
                bodyDataSink.flush();
              }
            };
            orgDataSource.setDataHandler(bodyForwardHandler);
           
          } else {
            exchange.send(response);
          }
        }
       
        public void onException(IOException ioe) {
         
        }
      };
     
     
      // does request contain a body?
      if (req.hasBody()) {
       
        // get the body
        NonBlockingBodyDataSource orgDataSource = req.getNonBlockingBody();
       
        // ... and replace it 
        final BodyDataSink inBodyChannel = exchange.forward(req.getRequestHeader(), respHdl);
       
        //... by a body forward handler
        BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
         
          @Override
          public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
            ByteBuffer[] bufs = bodyDataSource.readByteBufferByLength(bodyDataSource.available());
               
            for (ByteBuffer byteBuffer : bufs) {
              // WARNING DataConverter call could fail!
              requestString.append(DataConverter.toString(byteBuffer.duplicate()));
            }
               
            bodyDataSink.write(bufs);
            bodyDataSink.flush();
          }
        };
        orgDataSource.setDataHandler(bodyForwardHandler);
       
      } else {
        exchange.forward(req, respHdl);
      }
    }
View Full Code Here


        public void onResponse(IHttpResponse response) throws IOException {

          if (response.hasBody()) {

            // get the body
            NonBlockingBodyDataSource orgDataSource = response.getNonBlockingBody();

            // ... and replace it 
            final BodyDataSink inBodyChannel = exchange.send(response.getResponseHeader());
           
            //... by a body forward handler
            BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
             
              @Override
              public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
                byte[] data = bodyDataSource.readBytesByLength(bodyDataSource.available());
                   
                for (int i = 0; i < data.length; i++) {
                  data[i] = (byte) ((int) data[i] - shift);
                }
                                       
                inBodyChannel.write(data);
                inBodyChannel.flush();
              }
            };
            orgDataSource.setDataHandler(bodyForwardHandler);
           
          } else {
            exchange.send(response);
          }
        };
       
        public void onException(IOException ioe) {
         
        };
      };
     
      // does request contain a body?
      if (exchange.getRequest().hasBody()) {
       
        // get the body
        NonBlockingBodyDataSource orgDataSource = exchange.getRequest().getNonBlockingBody();
       
        // ... and replace it ...
        final BodyDataSink inBodyChannel = exchange.forward(exchange.getRequest().getRequestHeader(), responseInterceptor);
       
        //... by a body forward handler
        BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
         
          @Override
          public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
            byte[] data = bodyDataSource.readBytesByLength(bodyDataSource.available());
               
            for (int i = 0; i < data.length; i++) {
              data[i] = (byte) ((int) data[i] + shift);
            }
               
               
            inBodyChannel.write(data);
            inBodyChannel.flush();
          }
        };
        orgDataSource.setDataHandler(bodyForwardHandler);
       
      } else {
        exchange.forward(exchange.getRequest(), responseInterceptor);
      }
    }
View Full Code Here

  private static final class ContentEchoHandler implements IHttpRequestHandler {

    public void onRequest(IHttpExchange exchange) throws IOException {

      IHttpRequest request = exchange.getRequest();
      NonBlockingBodyDataSource bodyChannel = request.getNonBlockingBody();
      exchange.send(new HttpResponse(200, request.getContentType(), bodyChannel));     
    }
View Full Code Here

             
                public void onResponse(IHttpResponse response) throws IOException {
                   
                    BodyDataSink ds = exchange.send(response.getResponseHeader());
                   
                    NonBlockingBodyDataSource source = response.getNonBlockingBody();
                    BodyForwarder bf = new BodyForwarder(source, ds) {
                       
                        @Override
                        public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
                            int available = bodyDataSource.available();
View Full Code Here

          // does request contain a body?
          if (response.hasBody()) {
           
            // get the body
            NonBlockingBodyDataSource orgDataSource = response.getNonBlockingBody();
           
            // ... and replace it 
            final BodyDataSink inBodyChannel = exchange.send(response.getResponseHeader());
           
            //... by a body forward handler
            BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
             
              @Override
              public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
                ByteBuffer[] bufs = bodyDataSource.readByteBufferByLength(bodyDataSource.available());
                   
                for (ByteBuffer byteBuffer : bufs) {
                  // WARNING DataConverter call could fail!
                  requestString.append(DataConverter.toString(byteBuffer.duplicate()));
                }
                   
                bodyDataSink.write(bufs);
                bodyDataSink.flush();
              }
            };
            orgDataSource.setDataHandler(bodyForwardHandler);
           
          } else {
            exchange.send(response);
          }
        }
       
        public void onException(IOException ioe) {
         
        }
      };
     
     
      // does request contain a body?
      if (req.hasBody()) {
       
        // get the body
        NonBlockingBodyDataSource orgDataSource = req.getNonBlockingBody();
       
        // ... and replace it 
        final BodyDataSink inBodyChannel = exchange.forward(req.getRequestHeader(), respHdl);
       
        //... by a body forward handler
        BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
         
          @Override
          public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
            ByteBuffer[] bufs = bodyDataSource.readByteBufferByLength(bodyDataSource.available());
               
            for (ByteBuffer byteBuffer : bufs) {
              // WARNING DataConverter call could fail!
              requestString.append(DataConverter.toString(byteBuffer.duplicate()));
            }
               
            bodyDataSink.write(bufs);
            bodyDataSink.flush();
          }
        };
        orgDataSource.setDataHandler(bodyForwardHandler);
       
      } else {
        exchange.forward(req, respHdl);
      }
    }
View Full Code Here

        public void onResponse(IHttpResponse response) throws IOException {

          if (response.hasBody()) {

            // get the body
            NonBlockingBodyDataSource orgDataSource = response.getNonBlockingBody();

            // ... and replace it 
            final BodyDataSink inBodyChannel = exchange.send(response.getResponseHeader());
           
            //... by a body forward handler
            BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
             
              @Override
              public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
                byte[] data = bodyDataSource.readBytesByLength(bodyDataSource.available());
                   
                for (int i = 0; i < data.length; i++) {
                  data[i] = (byte) ((int) data[i] - shift);
                }
                                       
                inBodyChannel.write(data);
                inBodyChannel.flush();
              }
            };
            orgDataSource.setDataHandler(bodyForwardHandler);
           
          } else {
            exchange.send(response);
          }
        };
       
        public void onException(IOException ioe) {
         
        };
      };
     
      // does request contain a body?
      if (exchange.getRequest().hasBody()) {
       
        // get the body
        NonBlockingBodyDataSource orgDataSource = exchange.getRequest().getNonBlockingBody();
       
        // ... and replace it ...
        final BodyDataSink inBodyChannel = exchange.forward(exchange.getRequest().getRequestHeader(), responseInterceptor);
       
        //... by a body forward handler
        BodyForwarder bodyForwardHandler = new BodyForwarder(orgDataSource, inBodyChannel) {
         
          @Override
          public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
            byte[] data = bodyDataSource.readBytesByLength(bodyDataSource.available());
               
            for (int i = 0; i < data.length; i++) {
              data[i] = (byte) ((int) data[i] + shift);
            }
               
               
            inBodyChannel.write(data);
            inBodyChannel.flush();
          }
        };
        orgDataSource.setDataHandler(bodyForwardHandler);
       
      } else {
        exchange.forward(exchange.getRequest(), responseInterceptor);
      }
    }
View Full Code Here

  private static final class ContentEchoHandler implements IHttpRequestHandler {

    public void onRequest(IHttpExchange exchange) throws IOException {

      IHttpRequest request = exchange.getRequest();
      NonBlockingBodyDataSource bodyChannel = request.getNonBlockingBody();
      exchange.send(new HttpResponse(200, request.getContentType(), bodyChannel));     
    }
View Full Code Here

             
                public void onResponse(IHttpResponse response) throws IOException {
                   
                    BodyDataSink ds = exchange.send(response.getResponseHeader());
                   
                    NonBlockingBodyDataSource source = response.getNonBlockingBody();
                    BodyForwarder bf = new BodyForwarder(source, ds) {
                       
                        @Override
                        public void onData(NonBlockingBodyDataSource bodyDataSource, BodyDataSink bodyDataSink) throws BufferUnderflowException, IOException {
                            int available = bodyDataSource.available();
View Full Code Here

                header.setProtocol(getRequest().getProtocol());
                header.setHeader("Connection", "close");
               
                BodyDataSink bodyDataSink = HttpServerConnection.this.sendResponseHeader(header, false);
               
                NonBlockingBodyDataSource bodyDataSource = response.getNonBlockingBody();
                BodyForwarder forwarder = new BodyForwarder(bodyDataSource, bodyDataSink);
                bodyDataSource.setDataHandler(forwarder);
                return;
              }
             
              // http protocol version downgrade necessary?
              if (isLargerOrEquals(response.getResponseHeader().getProtocolVersion(), getRequest().getRequestHeader().getProtocolVersion())) {
                response.getResponseHeader().setProtocol(getRequest().getProtocol());
                response.getResponseHeader().setHeader("Connection", "close");
              }
            }
          } catch (Exception e) {
            HttpResponse errorResponse = null;
            if (HttpUtils.isShowDetailedError()) {
              errorResponse = new HttpResponse(400, "text/html", generateErrorMessageHtml(400, DataConverter.toString(e), getId()));
             
            } else {
              errorResponse = new HttpResponse(400, "text/html", generateErrorMessageHtml(400, HttpUtils.getReason(400), getId()));
            }
            setResponseCommited(true);
            HttpServerConnection.this.sendResponseMessage(errorResponse, false);
            throw new IOException(e.toString());
          }
   
          if (response.getStatus() != 100) {
              setResponseCommited(true);
          }
         
                addLengthHeaderIfRequired(response);
         
          BodyDataSink dataSink = HttpServerConnection.this.sendResponseMessage(response, isResponseCompressable(response.getResponseHeader()));
         
          if (transactionMonitor != null) {
              transactionMonitor.registerMessageHeaderSent(request, response.getResponseHeader(), dataSink);
             
              if (response.hasBody()) {
                 
                  BodyListener bodyListener = new BodyListener();
                  NonBlockingBodyDataSource ds = response.getNonBlockingBody();
                  ds.addCompleteListener(bodyListener);
                  ds.addDestroyListener(bodyListener);
                 
              } else {
                  transactionMonitor.registerMessageSent(request);
              }
          }
View Full Code Here

                header.setProtocol(getRequest().getProtocol());
                header.setHeader("Connection", "close");
               
                BodyDataSink bodyDataSink = HttpServerConnection.this.sendResponseHeader(header, false);
               
                NonBlockingBodyDataSource bodyDataSource = response.getNonBlockingBody();
                BodyForwarder forwarder = new BodyForwarder(bodyDataSource, bodyDataSink);
                bodyDataSource.setDataHandler(forwarder);
                return;
              }
             
              // http protocol version downgrade necessary?
              if (isLargerOrEquals(response.getResponseHeader().getProtocolVersion(), getRequest().getRequestHeader().getProtocolVersion())) {
                response.getResponseHeader().setProtocol(getRequest().getProtocol());
                response.getResponseHeader().setHeader("Connection", "close");
              }
            }
          } catch (Exception e) {
            HttpResponse errorResponse = null;
            if (HttpUtils.isShowDetailedError()) {
              errorResponse = new HttpResponse(400, "text/html", generateErrorMessageHtml(400, DataConverter.toString(e), getId()));
             
            } else {
              errorResponse = new HttpResponse(400, "text/html", generateErrorMessageHtml(400, HttpUtils.getReason(400), getId()));
            }
            setResponseCommited(true);
            HttpServerConnection.this.sendResponseMessage(errorResponse, false);
            throw new IOException(e.toString());
          }
   
          if (response.getStatus() != 100) {
              setResponseCommited(true);
          }
         
          BodyDataSink dataSink = HttpServerConnection.this.sendResponseMessage(response, isResponseCompressable(response.getResponseHeader()));
         
          if (transactionMonitor != null) {
              transactionMonitor.registerMessageHeaderSent(request, response.getResponseHeader(), dataSink);
             
              if (response.hasBody()) {
                 
                  BodyListener bodyListener = new BodyListener();
                  NonBlockingBodyDataSource ds = response.getNonBlockingBody();
                  ds.addCompleteListener(bodyListener);
                  ds.addDestroyListener(bodyListener);
                 
              } else {
                  transactionMonitor.registerMessageSent(request);
              }
          }
View Full Code Here

TOP

Related Classes of org.xlightweb.NonBlockingBodyDataSource

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.