io.vertx.ext.web.client.HttpResponse Maven / Gradle / Ivy
/*
* Copyright (c) 2011-2013 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.web.client;
import io.vertx.codegen.annotations.CacheReturn;
import io.vertx.codegen.annotations.Nullable;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpResponseHead;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.codec.BodyCodec;
import io.vertx.ext.web.codec.impl.BodyCodecImpl;
import java.util.List;
/**
* An HTTP response.
*
* The usual HTTP response attributes are available:
*
* - {@link #statusCode()} the HTTP status code
* - {@link #statusMessage()} the HTTP status message
* - {@link #headers()} the HTTP headers
* - {@link #version()} the HTTP version
*
*
* The body of the response is returned by {@link #body()} decoded as the format specified by the {@link io.vertx.ext.web.codec.BodyCodec} that
* built the response.
*
* Keep in mind that using this {@code HttpResponse} impose to fully buffer the response body and should be used for payload
* that can fit in memory.
*
* @author Julien Viet
*/
@VertxGen
public interface HttpResponse extends HttpResponseHead {
/**
* @return the trailers
*/
@CacheReturn
MultiMap trailers();
/**
* Return the first trailer value with the specified name
*
* @param trailerName the trailer name
* @return the trailer value
*/
@Nullable String getTrailer(String trailerName);
/**
* @return the response body in the format it was decoded.
*/
@CacheReturn
@Nullable
T body();
/**
* @return the response body decoded as a {@link Buffer}, or {@code null} if a codec other than {@link BodyCodec#buffer()} was used
*/
@CacheReturn
@Nullable
Buffer bodyAsBuffer();
/**
* @return the list of all followed redirects, including the final location.
*/
@CacheReturn
List followedRedirects();
/**
* @return the response body decoded as a {@code String}, or {@code null} if a codec other than {@link BodyCodec#buffer()} was used
*/
@CacheReturn
@Nullable
default String bodyAsString() {
Buffer b = bodyAsBuffer();
return b != null ? BodyCodecImpl.UTF8_DECODER.apply(b) : null;
}
/**
* @return the response body decoded as a {@code String} given a specific {@code encoding}, or {@code null} if a codec other than {@link BodyCodec#buffer()} was used
*/
@Nullable
default String bodyAsString(String encoding) {
Buffer b = bodyAsBuffer();
return b != null ? b.toString(encoding) : null;
}
/**
* @return the response body decoded as {@link JsonObject}, or {@code null} if a codec other than {@link BodyCodec#buffer()} was used
*/
@CacheReturn
@Nullable
default JsonObject bodyAsJsonObject() {
Buffer b = bodyAsBuffer();
return b != null ? BodyCodecImpl.JSON_OBJECT_DECODER.apply(b) : null;
}
/**
* @return the response body decoded as a {@link JsonArray}, or {@code null} if a codec other than {@link BodyCodec#buffer()} was used
*/
@CacheReturn
@Nullable
JsonArray bodyAsJsonArray();
/**
* @return the response body decoded as the specified {@code type} with the Jackson mapper, or {@code null} if a codec other than {@link BodyCodec#buffer()} was used
*/
@Nullable
default R bodyAsJson(Class type) {
Buffer b = bodyAsBuffer();
return b != null ? BodyCodecImpl.jsonDecoder(type).apply(b) : null;
}
}