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.CONTENT_DISPOSITION;
009import static org.fcrepo.client.FedoraHeaderConstants.SLUG;
010import static org.fcrepo.client.HeaderHelpers.UTC_RFC_1123_FORMATTER;
011import static org.fcrepo.client.FedoraHeaderConstants.MEMENTO_DATETIME;
012
013import java.io.File;
014import java.io.IOException;
015import java.io.InputStream;
016import java.net.URI;
017import java.time.Instant;
018
019import org.apache.http.client.methods.HttpRequestBase;
020import org.springframework.http.ContentDisposition;
021
022/**
023 * Builds a POST request for creating a memento (LDPRm) with the state given in the request body
024 * and the datetime given in the Memento-Datetime request header.
025 *
026 * @author bbpennel
027 */
028public class HistoricMementoBuilder extends BodyRequestBuilder {
029
030    /**
031     * Instantiate builder
032     *
033     * @param uri uri of the resource this request is being made to
034     * @param client the client
035     * @param mementoInstant Instant to use for the memento-datetime
036     */
037    public HistoricMementoBuilder(final URI uri, final FcrepoClient client, final Instant mementoInstant) {
038        super(uri, client);
039        final String rfc1123Datetime = UTC_RFC_1123_FORMATTER.format(mementoInstant);
040        request.setHeader(MEMENTO_DATETIME, rfc1123Datetime);
041    }
042
043    /**
044     * Instantiate builder.
045     *
046     * @param uri uri of the resource this request is being made to
047     * @param client the client
048     * @param mementoDatetime RFC1123 formatted date to use for the memento-datetime
049     */
050    public HistoricMementoBuilder(final URI uri, final FcrepoClient client, final String mementoDatetime) {
051        super(uri, client);
052        // Parse the datetime to ensure that it is in RFC1123 format
053        UTC_RFC_1123_FORMATTER.parse(mementoDatetime);
054        request.setHeader(MEMENTO_DATETIME, mementoDatetime);
055    }
056
057    @Override
058    protected HttpRequestBase createRequest() {
059        return HttpMethods.POST.createRequest(targetUri);
060    }
061
062    @Override
063    public HistoricMementoBuilder body(final InputStream stream, final String contentType) {
064        return (HistoricMementoBuilder) super.body(stream, contentType);
065    }
066
067    @Override
068    public HistoricMementoBuilder body(final File file, final String contentType) throws IOException {
069        return (HistoricMementoBuilder) super.body(file, contentType);
070    }
071
072    @Override
073    public HistoricMementoBuilder body(final InputStream stream) {
074        return (HistoricMementoBuilder) super.body(stream);
075    }
076
077    @Override
078    public HistoricMementoBuilder externalContent(final URI contentURI,
079                                                  final String contentType,
080                                                  final String handling) {
081        return (HistoricMementoBuilder) super.externalContent(contentURI, contentType, handling);
082    }
083
084    @Override
085    public HistoricMementoBuilder digest(final String digest, final String alg) {
086        return (HistoricMementoBuilder) super.digest(digest, alg);
087    }
088
089    @Override
090    public HistoricMementoBuilder digestMd5(final String digest) {
091        return (HistoricMementoBuilder) super.digestMd5(digest);
092    }
093
094    @Override
095    public HistoricMementoBuilder digestSha1(final String digest) {
096        return (HistoricMementoBuilder) super.digestSha1(digest);
097    }
098
099    @Override
100    public HistoricMementoBuilder digestSha256(final String digest) {
101        return (HistoricMementoBuilder) super.digestSha256(digest);
102    }
103
104    @Override
105    public HistoricMementoBuilder addInteractionModel(final String interactionModelUri) {
106        return (HistoricMementoBuilder) super.addInteractionModel(interactionModelUri);
107    }
108
109    @Override
110    public HistoricMementoBuilder linkAcl(final String aclUri) {
111        return (HistoricMementoBuilder) super.linkAcl(aclUri);
112    }
113
114    @Override
115    public HistoricMementoBuilder addHeader(final String name, final String value) {
116        return (HistoricMementoBuilder) super.addHeader(name, value);
117    }
118
119    @Override
120    public HistoricMementoBuilder addLinkHeader(final FcrepoLink linkHeader) {
121        return (HistoricMementoBuilder) super.addLinkHeader(linkHeader);
122    }
123
124    /**
125     * Provide a content disposition header which will be used as the filename
126     *
127     * @param filename the name of the file being provided in the body of the request
128     * @return this builder
129     * @throws FcrepoOperationFailedException if unable to encode filename
130     */
131    public HistoricMementoBuilder filename(final String filename) throws FcrepoOperationFailedException {
132        final ContentDisposition.Builder builder = ContentDisposition.builder("attachment");
133        if (filename != null) {
134            builder.filename(filename);
135        }
136        request.addHeader(CONTENT_DISPOSITION, builder.build().toString());
137        return this;
138    }
139
140    /**
141     * Provide a suggested name for the new child resource, which the repository may ignore.
142     *
143     * @param slug value to supply as the slug header
144     * @return this builder
145     */
146    public HistoricMementoBuilder slug(final String slug) {
147        if (slug != null) {
148            request.addHeader(SLUG, slug);
149        }
150        return this;
151    }
152
153}