public boolean post(
     HTTPResponse httpRes, long contentOffset, long contentLength, boolean isOnlyHeader) {
   // TODO Close if Connection != keep-alive
   if (httpRes.hasContentInputStream() == true)
     return post(
         httpRes, httpRes.getContentInputStream(), contentOffset, contentLength, isOnlyHeader);
   return post(httpRes, httpRes.getContent(), contentOffset, contentLength, isOnlyHeader);
 }
  private boolean post(
      HTTPResponse httpRes,
      InputStream in,
      long contentOffset,
      long contentLength,
      boolean isOnlyHeader) {
    httpRes.setDate(Calendar.getInstance());
    OutputStream out = getOutputStream();

    try {
      httpRes.setContentLength(contentLength);

      out.write(httpRes.getHeader().getBytes());
      out.write(HTTP.CRLF.getBytes());

      if (isOnlyHeader == true) {
        out.flush();
        return true;
      }

      boolean isChunkedResponse = httpRes.isChunked();

      if (0 < contentOffset) in.skip(contentOffset);

      int chunkSize = HTTP.getChunkSize();
      byte readBuf[] = new byte[chunkSize];
      long readCnt = 0;
      long readSize = (chunkSize < contentLength) ? chunkSize : contentLength;
      int readLen = in.read(readBuf, 0, (int) readSize);
      while (0 < readLen && readCnt < contentLength) {
        if (isChunkedResponse == true) {
          String chunSizeBuf = Long.toString(readLen);
          out.write(chunSizeBuf.getBytes());
          out.write(HTTP.CRLF.getBytes());
        }
        out.write(readBuf, 0, readLen);
        if (isChunkedResponse == true) out.write(HTTP.CRLF.getBytes());
        readCnt += readLen;
        readSize = (chunkSize < (contentLength - readCnt)) ? chunkSize : (contentLength - readCnt);
        readLen = in.read(readBuf, 0, (int) readSize);
      }

      if (isChunkedResponse == true) {
        out.write("0".getBytes());
        out.write(HTTP.CRLF.getBytes());
      }

      out.flush();
    } catch (Exception e) {
      // Debug.warning(e);
      return false;
    }
    return true;
  }
Example #3
0
  private boolean notify(Subscriber sub, StateVariable stateVar) {
    String varName = stateVar.getName();
    String value = stateVar.getValue();

    String host = sub.getDeliveryHost();
    int port = sub.getDeliveryPort();

    NotifyRequest notifyReq = new NotifyRequest();
    notifyReq.setRequest(sub, varName, value);

    HTTPResponse res = notifyReq.post(host, port);
    if (res.isSuccessful() == false) return false;

    sub.incrementNotifyCount();

    return true;
  }
  private boolean post(
      HTTPResponse httpRes,
      byte content[],
      long contentOffset,
      long contentLength,
      boolean isOnlyHeader) {
    httpRes.setDate(Calendar.getInstance());
    OutputStream out = getOutputStream();

    try {
      httpRes.setContentLength(contentLength);

      out.write(httpRes.getHeader().getBytes());
      out.write(HTTP.CRLF.getBytes());
      if (isOnlyHeader == true) {
        out.flush();
        return true;
      }

      boolean isChunkedResponse = httpRes.isChunked();

      if (isChunkedResponse == true) {
        String chunSizeBuf = Long.toString(contentLength);
        out.write(chunSizeBuf.getBytes());
        out.write(HTTP.CRLF.getBytes());
      }

      out.write(content, (int) contentOffset, (int) contentLength);

      if (isChunkedResponse == true) {
        out.write(HTTP.CRLF.getBytes());
        out.write("0".getBytes());
        out.write(HTTP.CRLF.getBytes());
      }

      out.flush();
    } catch (Exception e) {
      // Debug.warning(e);
      return false;
    }

    return true;
  }