Package org.xlightweb

Examples of org.xlightweb.IHttpRequest


    public void onRequest(IHttpExchange exchange) throws IOException {

      // send response
      HttpResponseHeader responseHeader = new HttpResponseHeader(200, "text/plain");

      IHttpRequest request = exchange.getRequest();
     
      BodyDataSink bodyDataSink = exchange.send(responseHeader);
      bodyDataSink.write("requestUri=" + request.getRequestURI() + "\r\n");
      bodyDataSink.write("queryString=" + request.getQueryString() + "\r\n");

      List<String> paramNames = sort(request.getParameterNameSet());
      for (String key : paramNames) {
        String[] values = request.getParameterValues(key);
        for (String value : values) {
          bodyDataSink.write("[param] " + key  + "=" + value + "\r\n")
        }
      }
View Full Code Here


   
   
 
    public void onRequest(IHttpExchange exchange) throws IOException {
     
      IHttpRequest request = exchange.getRequest();
     
      // only POST is supported
      if (request.getMethod().equalsIgnoreCase("POST")) {
       
        if (request.getRequestURI().endsWith("/RegisterClient")) {
          BodyDataSource inChannel = request.getBody();
          inChannel.setReceiveTimeoutSec(1);
         
          BodyDataSink outChannel = exchange.send(new HttpResponseHeader(200, "text/plain"));
          outChannel.setFlushmode(FlushMode.ASYNC);
          outChannel.flush();
View Full Code Here

     
     
      public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {

         
          IHttpRequest req = exchange.getRequest();
         
          int port;
          if (req.isSecure()) {
              port = sslServer.getLocalPort();
          } else {
              port = server.getLocalPort();
          }
         
          URL url = req.getRequestUrl();
         
          req.setRequestUrl(new URL(url.getProtocol(), url.getHost(), port, url.getFile()));
         
          try {
              httpClient.send(req, new ResponseHandler(exchange));
          }catch (ConnectException ce) {
              exchange.sendError(502, ce.getMessage());
View Full Code Here

    private StringBuilder requestString = new StringBuilder();
   
   
    public void onRequest(final IHttpExchange exchange) throws IOException {
     
      IHttpRequest req = exchange.getRequest();

      requestString.append(req.getRequestHeader().toString());
     
     
      IHttpResponseHandler respHdl = new IHttpResponseHandler() {
       
        public void onResponse(IHttpResponse response) throws IOException {

          // 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
View Full Code Here

     
      public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
         
          lastConRef.set((AbstractHttpConnection) exchange.getConnection());
         
          IHttpRequest request = exchange.getRequest();
         
          if (request.getBooleanParameter("isWaitForBody", false)) {
              request.getBlockingBody().readString();
         
         
          IHttpResponse response = new HttpResponse(200, "text/plain", "1234567890");
          response.setHeader("Connection", "close");
           
View Full Code Here

  public static final class TimestampHandler implements IHttpRequestHandler {
   
   
    public void onRequest(final IHttpExchange exchange) throws IOException {
     
      IHttpRequest req = exchange.getRequest();
      req.addHeader("X-Received", new Date().toString());
     
     
      IHttpResponseHandler responseInterceptor = new IHttpResponseHandler() {

        public void onResponse(IHttpResponse response) throws IOException {
View Full Code Here

   
   
    public void onRequest(final IHttpExchange exchange) throws IOException {
     
     
      IHttpRequest request = exchange.getRequest();
      String authorization = request.getHeader("Authorization");
      if (authorization != null) {
        String[] s = authorization.split(" ");
        if (!s[0].equalsIgnoreCase("BASIC")) {
          exchange.sendError(401);
        }
       
        String decoded = new String(Base64.decodeBase64(s[1].getBytes()));
        String[] userPasswordPair = decoded.split(":");
       
        String authtoken = authenticator.login(userPasswordPair[0], userPasswordPair[1]);

        request.removeHeader("Authorization");
        request.setHeader("X-Authentication", authtoken);
       
        IHttpResponseHandler respHdl = new IHttpResponseHandler() {

          public void onResponse(IHttpResponse response) throws IOException {
            exchange.send(response);
           
          }
         
          public void onException(IOException ioe) {
          }
        };
       
        exchange.forward(exchange.getRequest(), respHdl);
      }
     
         
      String authentication = request.getHeader("Authentication");
      if (authentication == null) {
       
        HttpResponse resp = new HttpResponse(401);
        resp.setHeader("WWW-Authenticate", "basic");
        exchange.send(resp);
View Full Code Here

     
        IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
               
                IHttpRequest request = exchange.getRequest();
                String ifNoneMatch = request.getHeader("If-None-Match");
                if ((ifNoneMatch != null) && (ifNoneMatch.equals("\"23\""))) {
                    exchange.send(new HttpResponse(304));
                    return;
                }
               
View Full Code Here

       
        IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
               
                IHttpRequest request = exchange.getRequest();
                String ifNoneMatch = request.getHeader("If-None-Match");
                if ((ifNoneMatch != null) && (ifNoneMatch.equals("\"23\""))) {
                    exchange.send(new HttpResponse(304));
                    return;
                }
               
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("ETag", "\"23\"");
                exchange.send(resp);
            }
        };
       
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpRequest request = new PostRequest("http://localhost:" + server.getLocalPort() + "/", "text/plain", "test");
        IHttpResponse resp = httpClient.call(request);
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBlockingBody().readString());
       
        QAUtil.sleep(1000);
View Full Code Here

       
        IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
               
                IHttpRequest request = exchange.getRequest();
                String ifNoneMatch = request.getHeader("If-None-Match");
                if ((ifNoneMatch != null) && (ifNoneMatch.equals("\"24\""))) {
                    exchange.send(new HttpResponse(304));
                    return;
                }
               
View Full Code Here

TOP

Related Classes of org.xlightweb.IHttpRequest

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.