All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.shaded.vertx.core.http.impl.VertxHttpResponseEncoder Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.http.impl;


import com.arangodb.shaded.netty.buffer.ByteBuf;
import com.arangodb.shaded.netty.buffer.Unpooled;
import com.arangodb.shaded.netty.channel.ChannelHandlerContext;
import com.arangodb.shaded.netty.channel.DefaultFileRegion;
import com.arangodb.shaded.netty.handler.codec.http.DefaultFullHttpResponse;
import com.arangodb.shaded.netty.handler.codec.http.DefaultHttpContent;
import com.arangodb.shaded.netty.handler.codec.http.HttpHeaders;
import com.arangodb.shaded.netty.handler.codec.http.HttpResponse;
import com.arangodb.shaded.netty.handler.codec.http.HttpResponseEncoder;
import com.arangodb.shaded.netty.handler.codec.http.LastHttpContent;
import com.arangodb.shaded.vertx.core.http.impl.headers.HeadersMultiMap;

/**
 * {@link com.arangodb.shaded.netty.handler.codec.http.HttpResponseEncoder} which forces the usage of direct buffers for max performance.
 *
 * @author Norman Maurer
 */
final class VertxHttpResponseEncoder extends HttpResponseEncoder {

  @Override
  protected void encodeHeaders(HttpHeaders headers, ByteBuf buf) {
    if (headers instanceof HeadersMultiMap) {
      HeadersMultiMap vertxHeaders = (HeadersMultiMap) headers;
      vertxHeaders.encode(buf);
    } else {
      super.encodeHeaders(headers, buf);
    }
  }

  @Override
  public boolean acceptOutboundMessage(Object msg) throws Exception {
    // fast-path singleton(s)
    if (msg == Unpooled.EMPTY_BUFFER || msg == LastHttpContent.EMPTY_LAST_CONTENT) {
      return true;
    }
    // fast-path exact class matches: we cannot use a (concrete) class type check
    // here because the contract of HttpResponseEncoder::acceptOutboundMessage
    // enforces msg to NOT implement HttpRequest and we don't know if users extends vertx/netty types to
    // implement it.
    final Class msgClazz = msg.getClass();
    if (msgClazz == AssembledFullHttpResponse.class ||
      msgClazz == DefaultFullHttpResponse.class ||
      msgClazz == AssembledHttpResponse.class ||
      msgClazz == DefaultHttpContent.class ||
      msgClazz == AssembledLastHttpContent.class ||
      msgClazz == DefaultFileRegion.class) {
      return true;
    }
    // Netty slow-path
    return super.acceptOutboundMessage(msg);
  }

  @Override
  public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    super.handlerAdded(ctx);
  }

  @Override
  protected boolean isContentAlwaysEmpty(HttpResponse msg) {
    // In HttpServerCodec this is tracked via a FIFO queue of HttpMethod
    // here we track it in the assembled response as we don't use HttpServerCodec
    return (msg instanceof AssembledHttpResponse && ((AssembledHttpResponse) msg).head()) || super.isContentAlwaysEmpty(msg);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy