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