001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.fcrepo.client;
018
019import static org.fcrepo.client.FedoraHeaderConstants.CONTENT_DISPOSITION;
020import static org.fcrepo.client.FedoraHeaderConstants.SLUG;
021
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 digest(final String digest) {
071        return (PostBuilder) super.digest(digest);
072    }
073
074    /**
075     * Provide a content disposition header which will be used as the filename
076     * 
077     * @param filename the name of the file being provided in the body of the request
078     * @return this builder
079     * @throws FcrepoOperationFailedException if unable to encode filename
080     */
081    public PostBuilder filename(final String filename) throws FcrepoOperationFailedException {
082        if (filename != null) {
083            try {
084                final String encodedFilename = URLEncoder.encode(filename, "utf-8");
085                final String disposition = "attachment; filename=\"" + encodedFilename + "\"";
086                request.addHeader(CONTENT_DISPOSITION, disposition);
087            } catch (UnsupportedEncodingException e) {
088                throw new FcrepoOperationFailedException(request.getURI(), -1, e.getMessage());
089            }
090
091        }
092        return this;
093    }
094
095    /**
096     * Provide a suggested name for the new child resource, which the repository may ignore.
097     * 
098     * @param slug value to supply as the slug header
099     * @return this builder
100     */
101    public PostBuilder slug(final String slug) {
102        if (slug != null) {
103            request.addHeader(SLUG, slug);
104        }
105        return this;
106    }
107}