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