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