Record Class SafeFetchRequest

java.lang.Object
java.lang.Record
io.meeds.commons.http.SafeFetchRequest
Record Components:
uri - the URL to read
accept - the Accept header sent, null for none
ifNoneMatch - the If-None-Match header sent, null for none
ifModifiedSince - the If-Modified-Since header sent, null for none
acceptedContentTypes - the media types this read accepts, within the policy's when the policy names some; null keeps the policy's, and an empty set is refused
maxBytes - the most bytes this read takes, capped at the policy's; zero or less keeps the policy's
truncateAtLimit - whether a body past the limit is cut at it and returned, instead of refused: for a page whose head comes first, never for an image, useless cut

public record SafeFetchRequest(URI uri, String accept, String ifNoneMatch, String ifModifiedSince, Set<String> acceptedContentTypes, long maxBytes, boolean truncateAtLimit) extends Record
One read of a URL by SafeHttpFetcher: a GET, with the headers it sends and how it narrows the fetcher's SafeFetchPolicy. A request never widens the policy: its body limit is capped at the policy's, and its media types are kept only where the policy accepts them too. Built from get(URI) and the with methods; immutable. A header value carrying a line break or another control character than a horizontal tab is refused, so that no value can add a header of its own.
  • Constructor Details

    • SafeFetchRequest

      public SafeFetchRequest(URI uri, String accept, String ifNoneMatch, String ifModifiedSince, Set<String> acceptedContentTypes, long maxBytes, boolean truncateAtLimit)
      Checks the request and normalizes its media types.
      Parameters:
      uri - the URL to read
      accept - the Accept header sent
      ifNoneMatch - the If-None-Match header sent
      ifModifiedSince - the If-Modified-Since header sent
      acceptedContentTypes - the media types accepted
      maxBytes - the most bytes read
      truncateAtLimit - whether a longer body is cut rather than refused
      Throws:
      IllegalArgumentException - when a header value carries a control character, or the media types name none
  • Method Details

    • get

      public static SafeFetchRequest get(URI uri)
      A plain GET under the fetcher's policy.
      Parameters:
      uri - the URL to read
      Returns:
      the request
    • withAccept

      public SafeFetchRequest withAccept(String acceptHeader)
      The same read with an Accept header.
      Parameters:
      acceptHeader - the header value
      Returns:
      the request
    • withValidators

      public SafeFetchRequest withValidators(String etag, String lastModified)
      The same read made conditional on the validators of a previous one; a server answering 304 gives a not-modified response.
      Parameters:
      etag - the entity tag of the previous read, or null
      lastModified - the Last-Modified of the previous read, or null
      Returns:
      the request
      Throws:
      IllegalArgumentException - when a value carries a control character
    • withAcceptedContentTypes

      public SafeFetchRequest withAcceptedContentTypes(Collection<String> contentTypes)
      The same read accepting these media types only, within the policy's when the policy names some: a type the policy refuses stays refused.
      Parameters:
      contentTypes - the media types, without parameters, case ignored; at least one
      Returns:
      the request
      Throws:
      IllegalArgumentException - when the set names no media type
    • withMaxBytes

      public SafeFetchRequest withMaxBytes(long limit)
      The same read taking at most this many bytes, or the policy's limit when that is lower.
      Parameters:
      limit - the limit, positive
      Returns:
      the request
    • truncatedAtLimit

      public SafeFetchRequest truncatedAtLimit()
      The same read, a body past the limit cut at it and returned rather than refused.
      Returns:
      the request
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • uri

      public URI uri()
      Returns the value of the uri record component.
      Returns:
      the value of the uri record component
    • accept

      public String accept()
      Returns the value of the accept record component.
      Returns:
      the value of the accept record component
    • ifNoneMatch

      public String ifNoneMatch()
      Returns the value of the ifNoneMatch record component.
      Returns:
      the value of the ifNoneMatch record component
    • ifModifiedSince

      public String ifModifiedSince()
      Returns the value of the ifModifiedSince record component.
      Returns:
      the value of the ifModifiedSince record component
    • acceptedContentTypes

      public Set<String> acceptedContentTypes()
      Returns the value of the acceptedContentTypes record component.
      Returns:
      the value of the acceptedContentTypes record component
    • maxBytes

      public long maxBytes()
      Returns the value of the maxBytes record component.
      Returns:
      the value of the maxBytes record component
    • truncateAtLimit

      public boolean truncateAtLimit()
      Returns the value of the truncateAtLimit record component.
      Returns:
      the value of the truncateAtLimit record component