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.io.UnsupportedEncodingException; 027import java.net.URI; 028import java.net.URLEncoder; 029 030import org.apache.http.client.methods.HttpRequestBase; 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 @Deprecated 071 @Override 072 public PostBuilder digest(final String digest) { 073 return (PostBuilder) super.digest(digest); 074 } 075 076 @Override 077 public PostBuilder digest(final String digest, final String alg) { 078 return (PostBuilder) super.digest(digest, alg); 079 } 080 081 @Override 082 public PostBuilder digestMd5(final String digest) { 083 return (PostBuilder) super.digestMd5(digest); 084 } 085 086 @Override 087 public PostBuilder digestSha1(final String digest) { 088 return (PostBuilder) super.digestSha1(digest); 089 } 090 091 @Override 092 public PostBuilder digestSha256(final String digest) { 093 return (PostBuilder) super.digestSha256(digest); 094 } 095 096 /** 097 * Provide a content disposition header which will be used as the filename 098 * 099 * @param filename the name of the file being provided in the body of the request 100 * @return this builder 101 * @throws FcrepoOperationFailedException if unable to encode filename 102 */ 103 public PostBuilder filename(final String filename) throws FcrepoOperationFailedException { 104 try { 105 final String f = (filename != null) ? "; filename=\"" + URLEncoder.encode(filename, "utf-8") + "\"" : ""; 106 request.addHeader(CONTENT_DISPOSITION, "attachment" + f); 107 } catch (UnsupportedEncodingException e) { 108 throw new FcrepoOperationFailedException(request.getURI(), -1, e.getMessage()); 109 } 110 return this; 111 } 112 113 /** 114 * Provide a suggested name for the new child resource, which the repository may ignore. 115 * 116 * @param slug value to supply as the slug header 117 * @return this builder 118 */ 119 public PostBuilder slug(final String slug) { 120 if (slug != null) { 121 request.addHeader(SLUG, slug); 122 } 123 return this; 124 } 125}