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(); }
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(); }
public void writeHeaders(final String iContentType, final boolean iKeepAlive) throws IOException { if (headers != null) writeLine(headers); writeLine("Date: " + new Date()); writeLine("Content-Type: " + iContentType + "; charset=" + characterSet); writeLine("Server: " + serverInfo); writeLine("Connection: " + (iKeepAlive ? "Keep-Alive" : "close")); // INCLUDE COMMON CUSTOM HEADERS if (additionalHeaders != null) for (String h : additionalHeaders) writeLine(h); }
public void writeStatus(final int iStatus, final String iReason) throws IOException { writeLine(httpVersion + " " + iStatus + " " + iReason); }