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.PREFER; 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 PUT request for interacting with the Fedora HTTP API in order to create a resource with a specified path, 034 * or replace the triples associated with a resource with the triples provided in the request body. 035 * 036 * @author bbpennel 037 */ 038public class PutBuilder 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 PutBuilder(final URI uri, final FcrepoClient client) { 047 super(uri, client); 048 } 049 050 @Override 051 protected HttpRequestBase createRequest() { 052 return HttpMethods.PUT.createRequest(targetUri); 053 } 054 055 @Override 056 public PutBuilder body(final InputStream stream, final String contentType) { 057 return (PutBuilder) super.body(stream, contentType); 058 } 059 060 @Override 061 public PutBuilder body(final File file, final String contentType) throws IOException { 062 return (PutBuilder) super.body(file, contentType); 063 } 064 065 @Override 066 public PutBuilder body(final InputStream stream) { 067 return (PutBuilder) super.body(stream); 068 } 069 070 @Override 071 public PutBuilder ifMatch(final String etag) { 072 return (PutBuilder) super.ifMatch(etag); 073 } 074 075 @Override 076 public PutBuilder ifUnmodifiedSince(final String modified) { 077 return (PutBuilder) super.ifUnmodifiedSince(modified); 078 } 079 080 @Deprecated 081 @Override 082 public PutBuilder digest(final String digest) { 083 return (PutBuilder) super.digest(digest); 084 } 085 086 @Override 087 public PutBuilder digest(final String digest, final String alg) { 088 return (PutBuilder) super.digest(digest, alg); 089 } 090 091 @Override 092 public PutBuilder digestMd5(final String digest) { 093 return (PutBuilder) super.digestMd5(digest); 094 } 095 096 @Override 097 public PutBuilder digestSha1(final String digest) { 098 return (PutBuilder) super.digestSha1(digest); 099 } 100 101 @Override 102 public PutBuilder digestSha256(final String digest) { 103 return (PutBuilder) super.digestSha256(digest); 104 } 105 106 /** 107 * Provide a content disposition header which will be used as the filename 108 * 109 * @param filename the name of the file being provided in the body of the request 110 * @return this builder 111 * @throws FcrepoOperationFailedException if unable to encode filename 112 */ 113 public PutBuilder filename(final String filename) throws FcrepoOperationFailedException { 114 try { 115 final String f = (filename != null) ? "; filename=\"" + URLEncoder.encode(filename, "utf-8") + "\"" : ""; 116 request.addHeader(CONTENT_DISPOSITION, "attachment" + f); 117 } catch (UnsupportedEncodingException e) { 118 throw new FcrepoOperationFailedException(request.getURI(), -1, e.getMessage()); 119 } 120 return this; 121 } 122 123 /** 124 * Set the prefer header for this request to lenient handling, to indicate that server-managed triples will not 125 * be included in the request body. 126 * 127 * @return this builder 128 */ 129 public BodyRequestBuilder preferLenient() { 130 request.setHeader(PREFER, "handling=lenient; received=\"minimal\""); 131 return this; 132 } 133}