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

org.testifyproject.netty.handler.codec.http.DefaultFullHttpResponse Maven / Gradle / Ivy

/*
 * Copyright 2013 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in org.testifyproject.testifyprojectpliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org.testifyproject/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package org.testifyproject.testifyproject.netty.handler.codec.http;

import org.testifyproject.testifyproject.netty.buffer.ByteBuf;
import org.testifyproject.testifyproject.netty.buffer.Unpooled;
import org.testifyproject.testifyproject.netty.util.IllegalReferenceCountException;

import static org.testifyproject.testifyproject.netty.util.internal.ObjectUtil.checkNotNull;

/**
 * Default implementation of a {@link FullHttpResponse}.
 */
public class DefaultFullHttpResponse extends DefaultHttpResponse implements FullHttpResponse {

    private final ByteBuf content;
    private final HttpHeaders trailingHeaders;

    /**
     * Used to cache the value of the hash code and avoid {@link IllegalReferenceCountException}.
     */
    private int hash;

    public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status) {
        this(version, status, Unpooled.buffer(0));
    }

    public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, ByteBuf content) {
        this(version, status, content, true);
    }

    public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders) {
        this(version, status, Unpooled.buffer(0), validateHeaders, false);
    }

    public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status, boolean validateHeaders,
                                   boolean singleFieldHeaders) {
        this(version, status, Unpooled.buffer(0), validateHeaders, singleFieldHeaders);
    }

    public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status,
                                   ByteBuf content, boolean validateHeaders) {
        this(version, status, content, validateHeaders, false);
    }

    public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status,
                                   ByteBuf content, boolean validateHeaders, boolean singleFieldHeaders) {
        super(version, status, validateHeaders, singleFieldHeaders);
        this.content = checkNotNull(content, "content");
        this.trailingHeaders = singleFieldHeaders ? new CombinedHttpHeaders(validateHeaders)
                                                  : new DefaultHttpHeaders(validateHeaders);
    }

    public DefaultFullHttpResponse(HttpVersion version, HttpResponseStatus status,
            ByteBuf content, HttpHeaders headers, HttpHeaders trailingHeaders) {
        super(version, status, headers);
        this.content = checkNotNull(content, "content");
        this.trailingHeaders = checkNotNull(trailingHeaders, "trailingHeaders");
    }

    @Override
    public HttpHeaders trailingHeaders() {
        return trailingHeaders;
    }

    @Override
    public ByteBuf content() {
        return content;
    }

    @Override
    public int refCnt() {
        return content.refCnt();
    }

    @Override
    public FullHttpResponse retain() {
        content.retain();
        return this;
    }

    @Override
    public FullHttpResponse retain(int increment) {
        content.retain(increment);
        return this;
    }

    @Override
    public FullHttpResponse touch() {
        content.touch();
        return this;
    }

    @Override
    public FullHttpResponse touch(Object hint) {
        content.touch(hint);
        return this;
    }

    @Override
    public boolean release() {
        return content.release();
    }

    @Override
    public boolean release(int org.testifyproject.testifyprojectcrement) {
        return content.release(org.testifyproject.testifyprojectcrement);
    }

    @Override
    public FullHttpResponse setProtocolVersion(HttpVersion version) {
        super.setProtocolVersion(version);
        return this;
    }

    @Override
    public FullHttpResponse setStatus(HttpResponseStatus status) {
        super.setStatus(status);
        return this;
    }

    /**
     * Copy this object
     *
     * @param copyContent
     * 
    *
  • {@code true} if this object's {@link #content()} should be used to copy.
  • *
  • {@code false} if {@code newContent} should be used instead.
  • *
* @param newContent *
    *
  • if {@code copyContent} is false then this will be used in the copy's content.
  • *
  • if {@code null} then a org.testifyproject.testifyprojectfault buffer of 0 size will be selected
  • *
* @return A copy of this object */ private FullHttpResponse copy(boolean copyContent, ByteBuf newContent) { return new DefaultFullHttpResponse( protocolVersion(), status(), copyContent ? content().copy() : newContent == null ? Unpooled.buffer(0) : newContent, headers(), trailingHeaders()); } @Override public FullHttpResponse copy(ByteBuf newContent) { return copy(false, newContent); } @Override public FullHttpResponse copy() { return copy(true, null); } @Override public FullHttpResponse duplicate() { return new DefaultFullHttpResponse(protocolVersion(), status(), content().duplicate(), headers(), trailingHeaders()); } @Override public int hashCode() { int hash = this.hash; if (hash == 0) { if (content().refCnt() != 0) { try { hash = 31 + content().hashCode(); } catch (IllegalReferenceCountException ignored) { // Handle race condition between checking refCnt() == 0 and using the object. hash = 31; } } else { hash = 31; } hash = 31 * hash + trailingHeaders().hashCode(); hash = 31 * hash + super.hashCode(); this.hash = hash; } return hash; } @Override public boolean equals(Object o) { if (!(o instanceof DefaultFullHttpResponse)) { return false; } DefaultFullHttpResponse other = (DefaultFullHttpResponse) o; return super.equals(other) && content().equals(other.content()) && trailingHeaders().equals(other.trailingHeaders()); } @Override public String toString() { return HttpMessageUtil.appendFullResponse(new StringBuilder(256), this).toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy