Search in sources :

Example 1 with ServerHttpFuture

use of com.generallycloud.baseio.codec.http11.future.ServerHttpFuture in project baseio by generallycloud.

the class HttpExceptionCaughtHandle method exceptionCaught.

@Override
public void exceptionCaught(SocketSession session, Future future, Exception ex) {
    logger.error(ex.getMessage(), ex);
    ServerHttpFuture f = new ServerHttpFuture(session.getContext());
    StringBuilder builder = new StringBuilder(HtmlUtil.HTML_HEADER);
    builder.append("        <div style=\"margin-left:20px;\">\n");
    builder.append("            <div>oops, server threw an inner exception, the stack trace is :</div>\n");
    builder.append("            <div style=\"font-family:serif;color:#5c5c5c;\">\n");
    builder.append("            -------------------------------------------------------</BR>\n");
    builder.append("            ");
    builder.append(ex.toString());
    builder.append("</BR>\n");
    StackTraceElement[] es = ex.getStackTrace();
    for (StackTraceElement e : es) {
        builder.append("                &emsp;at ");
        builder.append(e.toString());
        builder.append("</BR>\n");
    }
    builder.append("            </div>\n");
    builder.append("        </div>\n");
    builder.append(HtmlUtil.HTML_POWER_BY);
    builder.append(HtmlUtil.HTML_BOTTOM);
    f.write(builder.toString());
    f.setStatus(HttpStatus.C500);
    f.setResponseHeader("Content-Type", "text/html");
    session.flush(f);
}
Also used : ServerHttpFuture(com.generallycloud.baseio.codec.http11.future.ServerHttpFuture)

Example 2 with ServerHttpFuture

use of com.generallycloud.baseio.codec.http11.future.ServerHttpFuture in project baseio by generallycloud.

the class FutureAcceptorHttpFilter method accept404.

@Override
protected void accept404(SocketSession session, NamedFuture future, String serviceName) throws IOException {
    HttpEntity entity = html_cache.get(serviceName);
    HttpStatus status = HttpStatus.C200;
    ServerHttpFuture f = (ServerHttpFuture) future;
    if (entity == null) {
        f.setStatus(HttpStatus.C404);
        entity = html_cache.get("/404.html");
        if (entity == null) {
            super.accept404(session, f, serviceName);
            return;
        }
    }
    File file = entity.getFile();
    if (file != null && file.lastModified() > entity.getLastModify()) {
        synchronized (entity) {
            reloadEntity(entity, session.getContext(), status);
        }
        flush(session, f, entity);
        return;
    }
    String ims = f.getRequestHeader(HttpHeader.IF_MODIFIED_SINCE);
    long imsTime = -1;
    if (!StringUtil.isNullOrBlank(ims)) {
        imsTime = HttpHeaderDateFormat.getFormat().parse(ims).getTime();
    }
    if (imsTime < entity.getLastModifyGTMTime()) {
        flush(session, f, entity);
        return;
    }
    f.setStatus(HttpStatus.C304);
    session.flush(f);
}
Also used : HttpStatus(com.generallycloud.baseio.codec.http11.future.HttpStatus) ServerHttpFuture(com.generallycloud.baseio.codec.http11.future.ServerHttpFuture) File(java.io.File)

Example 3 with ServerHttpFuture

use of com.generallycloud.baseio.codec.http11.future.ServerHttpFuture in project baseio by generallycloud.

the class ServerHTTPProtocolEncoder method encode.

@Override
public void encode(SocketChannel channel, ChannelFuture readFuture) throws IOException {
    ByteBufAllocator allocator = channel.getByteBufAllocator();
    ServerHttpFuture f = (ServerHttpFuture) readFuture;
    if (f.isUpdateWebSocketProtocol()) {
        channel.setProtocolDecoder(WebSocketProtocolFactory.WS_PROTOCOL_DECODER);
        channel.setProtocolEncoder(WebSocketProtocolFactory.WS_PROTOCOL_ENCODER);
        channel.setProtocolFactory(WebSocketProtocolFactory.WS_PROTOCOL_FACTORY);
        channel.getSession().setAttribute(WebSocketFuture.SESSION_KEY_SERVICE_NAME, f.getFutureName());
    }
    f.setResponseHeader("Date", HttpHeaderDateFormat.getFormat().format(System.currentTimeMillis()));
    ByteArrayBuffer os = f.getBinaryBuffer();
    if (os != null) {
        encode(allocator, f, os.size(), os.array());
        return;
    }
    int writeSize = f.getWriteSize();
    if (writeSize == 0) {
        encode(allocator, f, 0, null);
        return;
    }
    encode(allocator, f, writeSize, f.getWriteBuffer());
}
Also used : ByteBufAllocator(com.generallycloud.baseio.buffer.ByteBufAllocator) ServerHttpFuture(com.generallycloud.baseio.codec.http11.future.ServerHttpFuture) ByteArrayBuffer(com.generallycloud.baseio.component.ByteArrayBuffer)

Example 4 with ServerHttpFuture

use of com.generallycloud.baseio.codec.http11.future.ServerHttpFuture in project baseio by generallycloud.

the class ServerHTTPProtocolEncoder method encode.

private void encode(ByteBufAllocator allocator, ServerHttpFuture f, int length, byte[] array) throws IOException {
    ByteBuf buf = allocator.allocate(256);
    try {
        buf.put(PROTOCOL);
        buf.put(f.getStatus().getHeaderBinary());
        buf.put(SERVER_CL);
        buf.put(String.valueOf(length).getBytes());
        buf.put(RN);
        writeHeaders(f, buf);
        List<Cookie> cookieList = f.getCookieList();
        if (cookieList != null) {
            for (Cookie c : cookieList) {
                writeBuf(buf, SET_COOKIE);
                writeBuf(buf, c.toString().getBytes());
                writeBuf(buf, RN);
            }
        }
        writeBuf(buf, RN);
        if (length != 0) {
            writeBuf(buf, array, 0, length);
        }
    } catch (Exception e) {
        ReleaseUtil.release(buf);
        throw e;
    }
    f.setByteBuf(buf.flip());
}
Also used : Cookie(com.generallycloud.baseio.codec.http11.future.Cookie) ByteBuf(com.generallycloud.baseio.buffer.ByteBuf) IOException(java.io.IOException)

Aggregations

ServerHttpFuture (com.generallycloud.baseio.codec.http11.future.ServerHttpFuture)3 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)1 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 Cookie (com.generallycloud.baseio.codec.http11.future.Cookie)1 HttpStatus (com.generallycloud.baseio.codec.http11.future.HttpStatus)1 ByteArrayBuffer (com.generallycloud.baseio.component.ByteArrayBuffer)1 File (java.io.File)1 IOException (java.io.IOException)1