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

swim.http.HttpRequest Maven / Gradle / Ivy

// 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.uri.Uri;
import swim.util.Murmur3;

public final class HttpRequest extends HttpMessage implements Debug {
  final HttpMethod method;
  final Uri uri;
  final HttpVersion version;
  final FingerTrieSeq headers;
  final HttpEntity entity;

  HttpRequest(HttpMethod method, Uri uri, HttpVersion version,
              FingerTrieSeq headers, HttpEntity entity) {
    this.method = method;
    this.uri = uri;
    this.version = version;
    this.headers = headers;
    this.entity = entity;
  }

  HttpRequest(HttpMethod method, Uri uri, HttpVersion version,
              FingerTrieSeq headers) {
    this(method, uri, version, headers, HttpEntity.empty());
  }

  public HttpMethod method() {
    return this.method;
  }

  public HttpRequest method(HttpMethod method) {
    return from(method, this.uri, this.version, this.headers, this.entity);
  }

  public Uri uri() {
    return this.uri;
  }

  public HttpRequest uri(Uri uri) {
    return from(this.method, uri, this.version, this.headers, this.entity);
  }

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

  public HttpRequest version(HttpVersion version) {
    return from(this.method, this.uri, version, this.headers, this.entity);
  }

  @Override
  public FingerTrieSeq headers() {
    return this.headers;
  }

  @Override
  public HttpRequest headers(FingerTrieSeq headers) {
    return from(this.method, this.uri, this.version, headers, this.entity);
  }

  @Override
  public HttpRequest headers(HttpHeader... headers) {
    return headers(FingerTrieSeq.of(headers));
  }

  @Override
  public HttpRequest appendedHeaders(FingerTrieSeq newHeaders) {
    final FingerTrieSeq oldHeaders = this.headers;
    final FingerTrieSeq headers = oldHeaders.appended(newHeaders);
    if (oldHeaders != headers) {
      return from(this.method, this.uri, this.version, headers, this.entity);
    } else {
      return this;
    }
  }

  @Override
  public HttpRequest appendedHeaders(HttpHeader... newHeaders) {
    return appendedHeaders(FingerTrieSeq.of(newHeaders));
  }

  @Override
  public HttpRequest appendedHeader(HttpHeader newHeader) {
    final FingerTrieSeq oldHeaders = this.headers;
    final FingerTrieSeq headers = oldHeaders.appended(newHeader);
    if (oldHeaders != headers) {
      return from(this.method, this.uri, this.version, headers, this.entity);
    } else {
      return this;
    }
  }

  @Override
  public HttpRequest updatedHeaders(FingerTrieSeq newHeaders) {
    final FingerTrieSeq oldHeaders = this.headers;
    final FingerTrieSeq headers = updatedHeaders(oldHeaders, newHeaders);
    if (oldHeaders != headers) {
      return from(this.method, this.uri, this.version, headers, this.entity);
    } else {
      return this;
    }
  }

  @Override
  public HttpRequest updatedHeaders(HttpHeader... newHeaders) {
    return updatedHeaders(FingerTrieSeq.of(newHeaders));
  }

  @Override
  public HttpRequest updatedHeader(HttpHeader newHeader) {
    final FingerTrieSeq oldHeaders = this.headers;
    final FingerTrieSeq headers = updatedHeaders(oldHeaders, newHeader);
    if (oldHeaders != headers) {
      return from(this.method, this.uri, this.version, headers, this.entity);
    } else {
      return this;
    }
  }

  @Override
  public HttpEntity entity() {
    return this.entity;
  }

  @Override
  public  HttpRequest entity(HttpEntity entity) {
    return from(this.method, this.uri, this.version, this.headers, entity);
  }

  @Override
  public  HttpRequest content(HttpEntity entity) {
    final FingerTrieSeq headers = updatedHeaders(this.headers, entity.headers());
    return from(this.method, this.uri, this.version, headers, entity);
  }

  @Override
  public HttpRequest body(String content, MediaType mediaType) {
    return content(HttpBody.from(content, mediaType));
  }

  @Override
  public HttpRequest 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> httpEncoder(HttpWriter http) {
    return (Encoder>) super.httpEncoder(http);
  }

  @SuppressWarnings("unchecked")
  @Override
  public Encoder> httpEncoder() {
    return (Encoder>) super.httpEncoder();
  }

  @SuppressWarnings("unchecked")
  @Override
  public Encoder> encodeHttp(OutputBuffer output, HttpWriter http) {
    return (Encoder>) super.encodeHttp(output, http);
  }

  @SuppressWarnings("unchecked")
  @Override
  public Encoder> encodeHttp(OutputBuffer output) {
    return (Encoder>) super.encodeHttp(output);
  }

  @Override
  public Writer> httpWriter(HttpWriter http) {
    return http.requestWriter(this);
  }

  @Override
  public Writer> httpWriter() {
    return httpWriter(Http.standardWriter());
  }

  @Override
  public Writer> writeHttp(Output output, HttpWriter http) {
    return http.writeRequest(this, output);
  }

  @Override
  public Writer> writeHttp(Output output) {
    return writeHttp(output, Http.standardWriter());
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    } else if (other instanceof HttpRequest) {
      final HttpRequest that = (HttpRequest) other;
      return this.method.equals(that.method) && this.uri.equals(that.uri)
          && this.version.equals(that.version) && this.headers.equals(that.headers)
          && this.entity.equals(that.entity);
    }
    return false;
  }

  @Override
  public int hashCode() {
    if (hashSeed == 0) {
      hashSeed = Murmur3.seed(HttpRequest.class);
    }
    return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
        this.method.hashCode()), this.uri.hashCode()), this.version.hashCode()),
        this.headers.hashCode()), this.entity.hashCode()));
  }

  @Override
  public void debug(Output output) {
    output = output.write("HttpRequest").write('.').write("from").write('(')
        .debug(this.method).write(", ").debug(this.uri).write(", ").debug(this.version);
    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  HttpRequest from(HttpMethod method, Uri uri, HttpVersion version,
                                        FingerTrieSeq headers, HttpEntity entity) {
    return new HttpRequest(method, uri, version, headers, entity);
  }

  public static  HttpRequest from(HttpMethod method, Uri uri, HttpVersion version,
                                        FingerTrieSeq headers) {
    return new HttpRequest(method, uri, version, headers);
  }

  public static  HttpRequest from(HttpMethod method, Uri uri, HttpVersion version,
                                        HttpHeader... headers) {
    return new HttpRequest(method, uri, version, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest from(HttpMethod method, Uri uri, HttpVersion version) {
    return new HttpRequest(method, uri, version, FingerTrieSeq.empty());
  }

  public static  HttpRequest get(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.GET, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest get(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.GET, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest head(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.HEAD, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest head(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.HEAD, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest post(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.POST, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest post(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.POST, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest put(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.PUT, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest put(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.PUT, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest delete(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.DELETE, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest delete(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.DELETE, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest connect(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.CONNECT, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest connect(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.CONNECT, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest options(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.OPTIONS, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest options(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.OPTIONS, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest trace(Uri uri, FingerTrieSeq headers) {
    return new HttpRequest(HttpMethod.TRACE, uri, HttpVersion.HTTP_1_1, headers);
  }

  public static  HttpRequest trace(Uri uri, HttpHeader... headers) {
    return new HttpRequest(HttpMethod.TRACE, uri, HttpVersion.HTTP_1_1, FingerTrieSeq.of(headers));
  }

  public static  HttpRequest parseHttp(String string) {
    return Http.standardParser().parseRequestString(string);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy