
swim.http.HttpResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swim-http Show documentation
Show all versions of swim-http Show documentation
Uploads all artifacts belonging to configuration ':swim-http:archives'
The newest version!
// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/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 swim.http;
import swim.codec.Debug;
import swim.codec.Decoder;
import swim.codec.Encoder;
import swim.codec.Format;
import swim.codec.Output;
import swim.codec.OutputBuffer;
import swim.codec.Writer;
import swim.collections.FingerTrieSeq;
import swim.util.Murmur3;
public final class HttpResponse extends HttpMessage implements Debug {
final HttpVersion version;
final HttpStatus status;
final FingerTrieSeq headers;
final HttpEntity entity;
HttpResponse(HttpVersion version, HttpStatus status,
FingerTrieSeq headers, HttpEntity entity) {
this.version = version;
this.status = status;
this.headers = headers;
this.entity = entity;
}
HttpResponse(HttpVersion version, HttpStatus status,
FingerTrieSeq headers) {
this(version, status, headers, HttpEntity.empty());
}
@Override
public HttpVersion version() {
return this.version;
}
public HttpResponse version(HttpVersion version) {
return from(version, this.status, this.headers, this.entity);
}
public HttpStatus status() {
return this.status;
}
public HttpResponse status(HttpStatus status) {
return from(this.version, status, this.headers, this.entity);
}
@Override
public FingerTrieSeq headers() {
return this.headers;
}
@Override
public HttpResponse headers(FingerTrieSeq headers) {
return from(this.version, this.status, headers, this.entity);
}
@Override
public HttpResponse headers(HttpHeader... headers) {
return headers(FingerTrieSeq.of(headers));
}
@Override
public HttpResponse appendedHeaders(FingerTrieSeq newHeaders) {
final FingerTrieSeq oldHeaders = this.headers;
final FingerTrieSeq headers = oldHeaders.appended(newHeaders);
if (oldHeaders != headers) {
return from(this.version, this.status, headers, this.entity);
} else {
return this;
}
}
@Override
public HttpResponse appendedHeaders(HttpHeader... newHeaders) {
return appendedHeaders(FingerTrieSeq.of(newHeaders));
}
@Override
public HttpResponse appendedHeader(HttpHeader newHeader) {
final FingerTrieSeq oldHeaders = this.headers;
final FingerTrieSeq headers = oldHeaders.appended(newHeader);
if (oldHeaders != headers) {
return from(this.version, this.status, headers, this.entity);
} else {
return this;
}
}
@Override
public HttpResponse updatedHeaders(FingerTrieSeq newHeaders) {
final FingerTrieSeq oldHeaders = this.headers;
final FingerTrieSeq headers = updatedHeaders(oldHeaders, newHeaders);
if (oldHeaders != headers) {
return from(this.version, this.status, headers, this.entity);
} else {
return this;
}
}
@Override
public HttpResponse updatedHeaders(HttpHeader... newHeaders) {
return updatedHeaders(FingerTrieSeq.of(newHeaders));
}
@Override
public HttpResponse updatedHeader(HttpHeader newHeader) {
final FingerTrieSeq oldHeaders = this.headers;
final FingerTrieSeq headers = updatedHeaders(oldHeaders, newHeader);
if (oldHeaders != headers) {
return from(this.version, this.status, headers, this.entity);
} else {
return this;
}
}
@Override
public HttpEntity entity() {
return this.entity;
}
@Override
public HttpResponse entity(HttpEntity entity) {
return from(this.version, this.status, this.headers, entity);
}
@Override
public HttpResponse content(HttpEntity entity) {
final FingerTrieSeq headers = updatedHeaders(this.headers, entity.headers());
return from(this.version, this.status, headers, entity);
}
@Override
public HttpResponse body(String content, MediaType mediaType) {
return content(HttpBody.from(content, mediaType));
}
@Override
public HttpResponse body(String content) {
return content(HttpBody.from(content));
}
@SuppressWarnings("unchecked")
@Override
public Decoder> entityDecoder(Decoder contentDecoder) {
return (Decoder>) super.entityDecoder(contentDecoder);
}
@SuppressWarnings("unchecked")
@Override
public Encoder, HttpResponse> httpEncoder(HttpWriter http) {
return (Encoder, HttpResponse>) super.httpEncoder(http);
}
@SuppressWarnings("unchecked")
@Override
public Encoder, HttpResponse> httpEncoder() {
return (Encoder, HttpResponse>) super.httpEncoder();
}
@SuppressWarnings("unchecked")
@Override
public Encoder, HttpResponse> encodeHttp(OutputBuffer> output, HttpWriter http) {
return (Encoder, HttpResponse>) super.encodeHttp(output, http);
}
@SuppressWarnings("unchecked")
@Override
public Encoder, HttpResponse> encodeHttp(OutputBuffer> output) {
return (Encoder, HttpResponse>) super.encodeHttp(output);
}
@Override
public Writer, HttpResponse> httpWriter(HttpWriter http) {
return http.responseWriter(this);
}
@Override
public Writer, HttpResponse> httpWriter() {
return httpWriter(Http.standardWriter());
}
@Override
public Writer, HttpResponse> writeHttp(Output> output, HttpWriter http) {
return http.writeResponse(this, output);
}
@Override
public Writer, HttpResponse> writeHttp(Output> output) {
return writeHttp(output, Http.standardWriter());
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof HttpResponse>) {
final HttpResponse> that = (HttpResponse>) other;
return this.version.equals(that.version) && this.status.equals(that.status)
&& this.headers.equals(that.headers) && this.entity.equals(that.entity);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(HttpResponse.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
this.version.hashCode()), this.status.hashCode()),
this.headers.hashCode()), this.entity.hashCode()));
}
@Override
public void debug(Output> output) {
output = output.write("HttpResponse").write('.').write("from").write('(')
.debug(this.version).write(", ").debug(this.status);
for (HttpHeader header : this.headers) {
output = output.write(", ").debug(header);
}
output = output.write(')');
if (this.entity.isDefined()) {
output = output.write('.').write("entity").write('(').debug(this.entity).write(')');
}
}
@Override
public String toString() {
return Format.debug(this);
}
private static int hashSeed;
public static HttpResponse from(HttpVersion version, HttpStatus status,
FingerTrieSeq headers, HttpEntity entity) {
return new HttpResponse(version, status, headers, entity);
}
public static HttpResponse from(HttpVersion version, HttpStatus status,
FingerTrieSeq headers) {
return new HttpResponse(version, status, headers);
}
public static HttpResponse from(HttpVersion version, HttpStatus status,
HttpHeader... headers) {
return new HttpResponse(version, status, FingerTrieSeq.of(headers));
}
public static HttpResponse from(HttpVersion version, HttpStatus status) {
return new HttpResponse(version, status, FingerTrieSeq.empty());
}
public static HttpResponse from(HttpStatus status, FingerTrieSeq headers) {
return new HttpResponse(HttpVersion.HTTP_1_1, status, headers);
}
public static HttpResponse from(HttpStatus status, HttpHeader... headers) {
return new HttpResponse(HttpVersion.HTTP_1_1, status, FingerTrieSeq.of(headers));
}
public static HttpResponse from(HttpStatus status) {
return new HttpResponse(HttpVersion.HTTP_1_1, status, FingerTrieSeq.empty());
}
public static HttpResponse parseHttp(String string) {
return Http.standardParser().parseResponseString(string);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy