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.slf4j.LoggerFactory.getLogger; 021import static org.fcrepo.client.FedoraHeaderConstants.LINK; 022 023import java.net.URI; 024 025import org.apache.http.client.methods.HttpRequestBase; 026import org.apache.http.util.Args; 027import org.slf4j.Logger; 028 029/** 030 * Base RequestBuilder class for constructing requests to the Fedora API 031 * 032 * @author bbpennel 033 */ 034public abstract class RequestBuilder { 035 036 private static final Logger LOGGER = getLogger(RequestBuilder.class); 037 038 // Fedora client which will make this request 039 protected FcrepoClient client; 040 041 // URL this request will be executed against 042 protected URI targetUri; 043 044 // The request being built 045 protected HttpRequestBase request; 046 047 /** 048 * Instantiate builder. Throws an IllegalArgumentException if either the uri or client are null. 049 * 050 * @param uri uri of the resource this request is being made to 051 * @param client the client 052 */ 053 protected RequestBuilder(final URI uri, final FcrepoClient client) { 054 Args.notNull(uri, "uri"); 055 Args.notNull(client, "client"); 056 057 this.targetUri = uri; 058 this.client = client; 059 this.request = createRequest(); 060 } 061 062 /** 063 * Creates the HTTP request object for this builder 064 * 065 * @return HTTP request object for this builder 066 */ 067 protected abstract HttpRequestBase createRequest(); 068 069 /** 070 * Performs the request constructed in this builder and returns the response 071 * 072 * @return the repository response 073 * @throws FcrepoOperationFailedException when the underlying HTTP request results in an error 074 */ 075 public FcrepoResponse perform() throws FcrepoOperationFailedException { 076 LOGGER.debug("Fcrepo {} request to {} with headers: {}", request.getMethod(), targetUri, 077 request.getAllHeaders()); 078 079 return client.executeRequest(targetUri, request); 080 } 081 082 /** 083 * Add a header with the given name and value to the request. 084 * 085 * @param name name of the header 086 * @param value value of the header 087 * @return this builder 088 */ 089 protected RequestBuilder addHeader(final String name, final String value) { 090 request.addHeader(name, value); 091 return this; 092 } 093 094 /** 095 * Add a link header to the request 096 * 097 * @param linkHeader link header value represented as a FcrepoLink 098 * @return this builder 099 */ 100 protected RequestBuilder addLinkHeader(final FcrepoLink linkHeader) { 101 request.addHeader(LINK, linkHeader.toString()); 102 return this; 103 } 104}