001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.client;
019
020import static org.fcrepo.client.FedoraHeaderConstants.CACHE_CONTROL;
021import static org.fcrepo.client.FedoraHeaderConstants.WANT_DIGEST;
022import static org.fcrepo.client.HeaderHelpers.UTC_RFC_1123_FORMATTER;
023import static org.fcrepo.client.FedoraHeaderConstants.ACCEPT_DATETIME;
024
025import java.net.URI;
026import java.time.Instant;
027
028import org.apache.http.client.config.RequestConfig;
029
030/**
031 * Abstract builder for requests to retrieve information from the server
032 *
033 * @author bbpennel
034 */
035public abstract class RetrieveRequestBuilder extends RequestBuilder {
036
037    protected RetrieveRequestBuilder(final URI uri, final FcrepoClient client) {
038        super(uri, client);
039    }
040
041    /**
042     * Disable following redirects.
043     *
044     * @return this builder
045     */
046    public RetrieveRequestBuilder disableRedirects() {
047        request.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build());
048        return this;
049    }
050
051    /**
052     * Provide a Want-Digest header for this request
053     *
054     * @param value header value, following the syntax defined in: https://tools.ietf.org/html/rfc3230#section-4.3.1
055     * @return this builder
056     */
057    public RetrieveRequestBuilder wantDigest(final String value) {
058        if (value != null) {
059            request.setHeader(WANT_DIGEST, value);
060        }
061        return this;
062    }
063
064    /**
065     * Provide a Cache-Control header with value "no-cache"
066     *
067     * @return this builder
068     */
069    public RetrieveRequestBuilder noCache() {
070        request.setHeader(CACHE_CONTROL, "no-cache");
071        return this;
072    }
073
074    /**
075     * Provide an Accept-Datetime header in RFC1123 format from the given instant for memento datetime negotiation.
076     *
077     * @param acceptInstant the accept datetime represented as an Instant.
078     * @return this builder
079     */
080    public RetrieveRequestBuilder acceptDatetime(final Instant acceptInstant) {
081        if (acceptInstant != null) {
082            final String rfc1123Datetime = UTC_RFC_1123_FORMATTER.format(acceptInstant);
083            request.setHeader(ACCEPT_DATETIME, rfc1123Datetime);
084        }
085        return this;
086    }
087
088    /**
089     * Provide an Accept-Datetime from the given RFC1123 formatted string.
090     *
091     * @param acceptDatetime the accept datetime as a string, must be in RFC1123 format.
092     * @return this builder
093     */
094    public RetrieveRequestBuilder acceptDatetime(final String acceptDatetime) {
095        if (acceptDatetime != null) {
096            // Parse the datetime to ensure that it is in RFC1123 format
097            UTC_RFC_1123_FORMATTER.parse(acceptDatetime);
098            request.setHeader(ACCEPT_DATETIME, acceptDatetime);
099        }
100        return this;
101    }
102}