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