Esempio n. 1
0
 public void printHeaders() {
   if (this.request != null) {
     Header[] hdrs = this.request.getAllHeaders();
     if (hdrs == null) hdrs = new Header[0];
     System.err.println("Request Headers:");
     for (Header h : hdrs) System.err.println(h.toString());
   }
   if (this.response != null) {
     Header[] hdrs = this.response.getAllHeaders();
     if (hdrs == null) hdrs = new Header[0];
     System.err.println("Response Headers:");
     for (Header h : hdrs) System.err.println(h.toString());
   }
   System.err.flush();
 }
Esempio n. 2
0
 public boolean isApacheHttpd() throws Exception {
   GetMethod gm = new GetMethod(URL);
   try {
     httpResponseCode = httpClient.executeMethod(gm);
   } catch (HttpException e) {
     System.out.println("error: " + e);
     throw (e);
   }
   gm.releaseConnection();
   Header head = gm.getResponseHeader("Server");
   if (head != null) return head.toString().contains("Apache/2");
   return false;
 }
Esempio n. 3
0
  /** Log request headers and body. Consumes request body and returns identical replacement. */
  Request logAndReplaceRequest(String name, Request request, Object[] args) throws IOException {
    log.log(String.format("---> %s %s %s", name, request.getMethod(), request.getUrl()));

    if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal()) {
      for (Header header : request.getHeaders()) {
        log.log(header.toString());
      }

      String bodySize = "no";
      TypedOutput body = request.getBody();
      if (body != null) {
        String bodyMime = body.mimeType();
        if (bodyMime != null) {
          log.log("Content-Type: " + bodyMime);
        }

        long bodyLength = body.length();
        bodySize = bodyLength + "-byte";
        if (bodyLength != -1) {
          log.log("Content-Length: " + bodyLength);
        }

        if (logLevel.ordinal() >= LogLevel.FULL.ordinal()) {
          if (!request.getHeaders().isEmpty()) {
            log.log("");
          }
          if (!(body instanceof TypedByteArray)) {
            // Read the entire response body to we can log it and replace the original response
            request = Utils.readBodyToBytesIfNecessary(request);
            body = request.getBody();
          }

          byte[] bodyBytes = ((TypedByteArray) body).getBytes();
          String bodyCharset = MimeUtil.parseCharset(body.mimeType(), "UTF-8");
          log.log(new String(bodyBytes, bodyCharset));
        } else if (logLevel.ordinal() >= LogLevel.HEADERS_AND_ARGS.ordinal()) {
          if (!request.getHeaders().isEmpty()) {
            log.log("---> REQUEST:");
          }
          for (int i = 0; i < args.length; i++) {
            log.log("#" + i + ": " + args[i]);
          }
        }
      }

      log.log(String.format("---> END %s (%s body)", name, bodySize));
    }

    return request;
  }
Esempio n. 4
0
  /** Log response headers and body. Consumes response body and returns identical replacement. */
  private Response logAndReplaceResponse(String url, Response response, long elapsedTime)
      throws IOException {
    log.log(String.format("<--- HTTP %s %s (%sms)", response.getStatus(), url, elapsedTime));

    if (logLevel.ordinal() >= LogLevel.HEADERS.ordinal()) {
      for (Header header : response.getHeaders()) {
        log.log(header.toString());
      }

      long bodySize = 0;
      TypedInput body = response.getBody();
      if (body != null) {
        bodySize = body.length();

        if (logLevel.ordinal() >= LogLevel.FULL.ordinal()) {
          if (!response.getHeaders().isEmpty()) {
            log.log("");
          }

          if (!(body instanceof TypedByteArray)) {
            // Read the entire response body so we can log it and replace the original response
            response = Utils.readBodyToBytesIfNecessary(response);
            body = response.getBody();
          }

          byte[] bodyBytes = ((TypedByteArray) body).getBytes();
          bodySize = bodyBytes.length;
          String bodyMime = body.mimeType();
          String bodyCharset = MimeUtil.parseCharset(bodyMime, "UTF-8");
          log.log(new String(bodyBytes, bodyCharset));
        }
      }

      log.log(String.format("<--- END HTTP (%s-byte body)", bodySize));
    }

    return response;
  }
Esempio n. 5
0
  public void mainloop() {

    while (!client.isConnected()) {
      try {
        System.out.println("Connecting to " + hostname + ":" + port);
        client.connect(hostname, port);
      } catch (IOException e) {
      }
      if (!client.isConnected()) {
        System.out.println("Couldn't connect. waiting");
        try {
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          run = false;
          System.exit(-1);
        }
      }
    }

    // Load the header information in one go into a bytebuffer
    byte[] rawbytebuf = new byte[BUFFERSIZE];

    int n = 0;

    try {
      n = headerReader.read(rawbytebuf);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Byte-buffer used to parse the byte-stream. Force native ordering
    ByteBuffer hdrBuf = ByteBuffer.wrap(rawbytebuf, 0, n);
    hdrBuf.order(ByteOrder.nativeOrder());
    Header hdr = new Header(hdrBuf);
    if (VERB > 0) {
      System.out.println("Sending header: " + hdr.toString());
    }
    hdr.nSamples = 0; // reset number of samples to 0

    try {
      client.putHeader(hdr);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Interval between sending samples to the buffer
    int pktSamples = hdr.nChans * blockSize; // number data samples in each buffer packet
    int pktBytes = pktSamples * DataType.wordSize[hdr.dataType];
    int nsamp = 0; // sample counter
    int nblk = 0;
    int nevent = 0;
    byte[] samples = new byte[pktBytes];

    // Size of the event header: type,type_numel,val,val_numel,sample,offset,duration,bufsz
    int evtHdrSz = DataType.wordSize[DataType.INT32] * 8;
    byte[] evtRawBuf = new byte[BUFFERSIZE]; // buffer to hold complete event structure

    // Byte-buffer used to parse the byte-stream. Force native ordering
    ByteBuffer evtBuf = ByteBuffer.wrap(evtRawBuf);
    evtBuf.order(ByteOrder.nativeOrder());
    int payloadSz = 0;
    int evtSample = 0;
    int evtSz = 0;
    long sample_ms = 0;
    long starttime_ms = java.lang.System.currentTimeMillis();
    long elapsed_ms = 0;
    long print_ms = 0;

    // Now do the data forwarding
    boolean eof = false;

    while (!eof
        && run) { // The run switch allows control of stopping the thread and getting out of the
      // loop
      // Read one buffer packets worth of samples
      // increment the cursor position
      if (VERB > 0 && elapsed_ms > print_ms + 500) {
        print_ms = elapsed_ms;
        System.out.println(
            nblk
                + " "
                + nsamp
                + " "
                + nevent
                + " "
                + (elapsed_ms / 1000)
                + " (blk,samp,event,sec)\r");
      }

      // read and write the samples
      try {
        n = dataReader.read(samples);
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (n <= 0) {
        eof = true;
        break;
      } // stop if run out of samples

      try {
        client.putRawData(blockSize, hdr.nChans, hdr.dataType, samples);
      } catch (IOException e) {
        e.printStackTrace();
      }

      // update the sample count
      nsamp += blockSize;
      while (evtSample <= nsamp) {
        if (evtSample > 0) { // send the current event
          try {
            client.putRawEvent(evtRawBuf, 0, evtSz);
          } catch (IOException e) {
            e.printStackTrace();
          }
          nevent++;
        }

        // read the next event
        try {
          n = eventReader.read(evtRawBuf, 0, evtHdrSz); // read the fixed size header
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (n <= 0) {
          eof = true;
          break;
        }
        evtSample = ((ByteBuffer) evtBuf.position(4 * 4)).getInt(); // sample index for this event
        payloadSz = ((ByteBuffer) evtBuf.position(4 * 7)).getInt(); // payload size for this event
        evtSz = evtHdrSz + payloadSz;

        // read the variable part
        try {
          n = eventReader.read(evtRawBuf, evtHdrSz, payloadSz);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (n <= 0) {
          eof = true;
          break;
        }

        // print the event we just read
        if (VERB > 1) {
          ByteBuffer tmpev = ByteBuffer.wrap(evtRawBuf, 0, evtSz);
          tmpev.order(evtBuf.order());
          BufferEvent evt = new BufferEvent(tmpev);
          System.out.println("Read Event: " + evt);
        }
      }

      // sleep until the next packet should be send OR EOF
      /*when to send the next sample */
      sample_ms = (long) ((float) (nsamp * 1000) / hdr.fSample / (float) speedup);
      elapsed_ms = java.lang.System.currentTimeMillis() - starttime_ms; // current time
      if (sample_ms > elapsed_ms)
        try {
          Thread.sleep(sample_ms - elapsed_ms);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      nblk++;
    }

    stop();
  }
Esempio n. 6
0
 @Override
 public String toString() {
   return header.toString();
 }