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