Java Code Examples for io.undertow.server.HttpServerExchange#getResponseContentLength()

The following examples show how to use io.undertow.server.HttpServerExchange#getResponseContentLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AllowedContentEncodings.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public StreamSinkConduit wrap(final ConduitFactory<StreamSinkConduit> factory, final HttpServerExchange exchange) {
    if (exchange.getResponseHeaders().contains(Headers.CONTENT_ENCODING)) {
        //already encoded
        return factory.create();
    }
    //if this is a zero length response we don't want to encode
    if (exchange.getResponseContentLength() != 0
            && exchange.getStatusCode() != StatusCodes.NO_CONTENT
            && exchange.getStatusCode() != StatusCodes.NOT_MODIFIED) {
        EncodingMapping encoding = getEncoding();
        if (encoding != null) {
            exchange.getResponseHeaders().put(Headers.CONTENT_ENCODING, encoding.getName());
            if (exchange.getRequestMethod().equals(Methods.HEAD)) {
                //we don't create an actual encoder for HEAD requests, but we set the header
                return factory.create();
            } else {
                return encoding.getEncoding().getResponseWrapper().wrap(factory, exchange);
            }
        }
    }
    return factory.create();
}
 
Example 2
Source File: MetricsListener.java    From mangooio with Apache License 2.0 6 votes vote down vote up
@Override
public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
    String uri = Optional.ofNullable(exchange.getRequestURI())
            .orElse("")
            .toLowerCase(Locale.ENGLISH);
    
    if (StringUtils.isNotBlank(uri) && !uri.contains("@admin")) {
        int processTime = (int) (System.currentTimeMillis() - this.start);
        
        Metrics metrics = Application.getInstance(Metrics.class);
        metrics.update(processTime);
        metrics.addStatusCode(exchange.getStatusCode());
        
        long contentLengeh = exchange.getResponseContentLength();
        if (contentLengeh > 0) {
            metrics.incrementDataSend(contentLengeh);
        }
    }
    
    nextListener.proceed();
}
 
Example 3
Source File: JDBCLogHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public void logMessage(String pattern, HttpServerExchange exchange) {
    JDBCLogAttribute jdbcLogAttribute = new JDBCLogAttribute();

    if (pattern.equals("combined")) {
        jdbcLogAttribute.pattern = pattern;
    }
    jdbcLogAttribute.remoteHost = ((InetSocketAddress) exchange.getSourceAddress()).getAddress().getHostAddress();
    SecurityContext sc = exchange.getSecurityContext();
    if (sc == null || !sc.isAuthenticated()) {
        jdbcLogAttribute.user = null;
    } else {
        jdbcLogAttribute.user = sc.getAuthenticatedAccount().getPrincipal().getName();
    }
    jdbcLogAttribute.query = exchange.getQueryString();

    jdbcLogAttribute.bytes = exchange.getResponseContentLength();
    if (jdbcLogAttribute.bytes < 0) {
        jdbcLogAttribute.bytes = 0;
    }

    jdbcLogAttribute.status = exchange.getStatusCode();

    if (jdbcLogAttribute.pattern.equals("combined")) {
        jdbcLogAttribute.virtualHost = exchange.getRequestHeader(HttpHeaderNames.HOST);
        jdbcLogAttribute.method = exchange.getRequestMethod();
        jdbcLogAttribute.referer = exchange.getRequestHeader(HttpHeaderNames.REFERER);
        jdbcLogAttribute.userAgent = exchange.getRequestHeader(HttpHeaderNames.USER_AGENT);
    }

    this.pendingMessages.add(jdbcLogAttribute);
    int state = stateUpdater.get(this);
    if (state == 0) {
        if (stateUpdater.compareAndSet(this, 0, 1)) {
            this.executor = exchange.getWorker();
            this.executor.execute(this);
        }
    }
}
 
Example 4
Source File: JDBCLogHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void logMessage(String pattern, HttpServerExchange exchange) {
    JDBCLogAttribute jdbcLogAttribute = new JDBCLogAttribute();

    if (pattern.equals("combined")) {
        jdbcLogAttribute.pattern = pattern;
    }
    jdbcLogAttribute.remoteHost = ((InetSocketAddress) exchange.getConnection().getPeerAddress()).getAddress().getHostAddress();
    SecurityContext sc = exchange.getSecurityContext();
    if (sc == null || !sc.isAuthenticated()) {
        jdbcLogAttribute.user = null;
    } else {
        jdbcLogAttribute.user = sc.getAuthenticatedAccount().getPrincipal().getName();
    }
    jdbcLogAttribute.query = exchange.getQueryString();

    jdbcLogAttribute.bytes = exchange.getResponseContentLength();
    if (jdbcLogAttribute.bytes < 0) {
        jdbcLogAttribute.bytes = 0;
    }

    jdbcLogAttribute.status = exchange.getStatusCode();

    if (jdbcLogAttribute.pattern.equals("combined")) {
        jdbcLogAttribute.virtualHost = exchange.getRequestHeaders().getFirst(Headers.HOST);
        jdbcLogAttribute.method = exchange.getRequestMethod().toString();
        jdbcLogAttribute.referer = exchange.getRequestHeaders().getFirst(Headers.REFERER);
        jdbcLogAttribute.userAgent = exchange.getRequestHeaders().getFirst(Headers.USER_AGENT);
    }

    this.pendingMessages.add(jdbcLogAttribute);
    int state = stateUpdater.get(this);
    if (state == 0) {
        if (stateUpdater.compareAndSet(this, 0, 1)) {
            this.executor = exchange.getConnection().getWorker();
            this.executor.execute(this);
        }
    }
}
 
Example 5
Source File: StoredResponseStreamSinkConduit.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a new instance.
 *
 * @param next     the delegate conduit to set
 * @param exchange
 */
public StoredResponseStreamSinkConduit(StreamSinkConduit next, HttpServerExchange exchange) {
    super(next);
    this.exchange = exchange;
    long length = exchange.getResponseContentLength();
    if (length <= 0L) {
        outputStream = new ByteArrayOutputStream();
    } else {
        if (length > Integer.MAX_VALUE) {
            throw UndertowMessages.MESSAGES.responseTooLargeToBuffer(length);
        }
        outputStream = new ByteArrayOutputStream((int) length);
    }
}
 
Example 6
Source File: StoreResponseStreamSinkConduit.java    From light-4j with Apache License 2.0 5 votes vote down vote up
public StoreResponseStreamSinkConduit(StreamSinkConduit next, HttpServerExchange exchange) {
    super(next);
    this.exchange = exchange;
    long length = exchange.getResponseContentLength();
    if (length <= 0L) {
        outputStream = new ByteArrayOutputStream();
    } else {
        if (length > Integer.MAX_VALUE) {
            throw UndertowMessages.MESSAGES.responseTooLargeToBuffer(length);
        }
        outputStream = new ByteArrayOutputStream((int) length);
    }
}
 
Example 7
Source File: ServletOutputStreamImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a new instance.  No write timeout is configured.
 *
 * @param exchange The exchange
 */
public ServletOutputStreamImpl(HttpServerExchange exchange) {
    this.exchange = exchange;
    this.contentLength = exchange.getResponseContentLength();
    servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
}
 
Example 8
Source File: ServletOutputStreamImpl.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public ServletOutputStreamImpl(HttpServerExchange exchange, Integer bufferSize) {
    this.exchange = exchange;
    this.contentLength = exchange.getResponseContentLength();
    servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
}
 
Example 9
Source File: UndertowOutputStream.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Construct a new instance.  No write timeout is configured.
 *
 * @param exchange The exchange
 */
public UndertowOutputStream(HttpServerExchange exchange) {
    this.exchange = exchange;
    this.contentLength = exchange.getResponseContentLength();
}