001/*
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 */
006package org.fcrepo.client;
007
008import static org.fcrepo.client.FedoraHeaderConstants.CACHE_CONTROL;
009import static org.fcrepo.client.FedoraHeaderConstants.WANT_DIGEST;
010import static org.fcrepo.client.HeaderHelpers.UTC_RFC_1123_FORMATTER;
011import static org.fcrepo.client.FedoraHeaderConstants.ACCEPT_DATETIME;
012
013import java.net.URI;
014import java.time.Instant;
015
016import org.apache.http.client.config.RequestConfig;
017
018/**
019 * Abstract builder for requests to retrieve information from the server
020 *
021 * @author bbpennel
022 */
023public abstract class RetrieveRequestBuilder extends RequestBuilder {
024
025    protected RetrieveRequestBuilder(final URI uri, final FcrepoClient client) {
026        super(uri, client);
027    }
028
029    /**
030     * Disable following redirects.
031     *
032     * @return this builder
033     */
034    public RetrieveRequestBuilder disableRedirects() {
035        request.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
036        return this;
037    }
038
039    /**
040     * Provide a Want-Digest header for this request
041     *
042     * @param value header value, following the syntax defined in: https://tools.ietf.org/html/rfc3230#section-4.3.1
043     * @return this builder
044     */
045    public RetrieveRequestBuilder wantDigest(final String value) {
046        if (value != null) {
047            request.setHeader(WANT_DIGEST, value);
048        }
049        return this;
050    }
051
052    /**
053     * Provide a Cache-Control header with value "no-cache"
054     *
055     * @return this builder
056     */
057    public RetrieveRequestBuilder noCache() {
058        request.setHeader(CACHE_CONTROL, "no-cache");
059        return this;
060    }
061
062    /**
063     * Provide an Accept-Datetime header in RFC1123 format from the given instant for memento datetime negotiation.
064     *
065     * @param acceptInstant the accept datetime represented as an Instant.
066     * @return this builder
067     */
068    public RetrieveRequestBuilder acceptDatetime(final Instant acceptInstant) {
069        if (acceptInstant != null) {
070            final String rfc1123Datetime = UTC_RFC_1123_FORMATTER.format(acceptInstant);
071            request.setHeader(ACCEPT_DATETIME, rfc1123Datetime);
072        }
073        return this;
074    }
075
076    /**
077     * Provide an Accept-Datetime from the given RFC1123 formatted string.
078     *
079     * @param acceptDatetime the accept datetime as a string, must be in RFC1123 format.
080     * @return this builder
081     */
082    public RetrieveRequestBuilder acceptDatetime(final String acceptDatetime) {
083        if (acceptDatetime != null) {
084            // Parse the datetime to ensure that it is in RFC1123 format
085            UTC_RFC_1123_FORMATTER.parse(acceptDatetime);
086            request.setHeader(ACCEPT_DATETIME, acceptDatetime);
087        }
088        return this;
089    }
090}