예제 #1
0
  public void send(
      final int iCode,
      final String iReason,
      final String iContentType,
      final Object iContent,
      final String iHeaders,
      final boolean iKeepAlive)
      throws IOException {
    if (sendStarted)
      // AVOID TO SEND RESPONSE TWICE
      return;
    sendStarted = true;

    final String content;
    final String contentType;

    if (callbackFunction != null) {
      content = callbackFunction + "(" + iContent + ")";
      contentType = "text/javascript";
    } else {
      content = iContent != null ? iContent.toString() : null;
      contentType = iContentType;
    }

    final boolean empty = content == null || content.length() == 0;

    writeStatus(empty && iCode == 200 ? 204 : iCode, iReason);
    writeHeaders(contentType, iKeepAlive);

    if (additionalHeaders != null) for (String h : additionalHeaders) writeLine(h);

    if (iHeaders != null) writeLine(iHeaders);

    final String sessId = sessionId != null ? sessionId : "-";

    writeLine("Set-Cookie: " + OHttpUtils.OSESSIONID + "=" + sessId + "; Path=/; HttpOnly");

    final byte[] binaryContent = empty ? null : OBinaryProtocol.string2bytes(content);

    writeLine(OHttpUtils.HEADER_CONTENT_LENGTH + (empty ? 0 : binaryContent.length));

    writeLine(null);

    if (binaryContent != null) out.write(binaryContent);
    out.flush();
  }
예제 #2
0
  public void sendStream(
      final int iCode,
      final String iReason,
      final String iContentType,
      final InputStream iContent,
      final long iSize)
      throws IOException {
    writeStatus(iCode, iReason);
    writeHeaders(iContentType);
    writeLine(OHttpUtils.HEADER_CONTENT_LENGTH + (iSize));
    writeLine(null);

    if (iContent != null) {
      int b;
      while ((b = iContent.read()) > -1) out.write(b);
    }

    out.flush();
  }