-
Notifications
You must be signed in to change notification settings - Fork 19
/
RequestBuilder.java
99 lines (83 loc) · 2.83 KB
/
RequestBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree.
*/
package org.fcrepo.client;
import static org.slf4j.LoggerFactory.getLogger;
import static org.fcrepo.client.FedoraHeaderConstants.LINK;
import java.net.URI;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.util.Args;
import org.slf4j.Logger;
/**
* Base RequestBuilder class for constructing requests to the Fedora API
*
* @author bbpennel
*/
public abstract class RequestBuilder {
private static final Logger LOGGER = getLogger(RequestBuilder.class);
// Fedora client which will make this request
protected FcrepoClient client;
// URL this request will be executed against
protected URI targetUri;
// The request being built
protected HttpRequestBase request;
/**
* Instantiate builder. Throws an IllegalArgumentException if either the uri or client are null.
*
* @param uri uri of the resource this request is being made to
* @param client the client
*/
protected RequestBuilder(final URI uri, final FcrepoClient client) {
Args.notNull(uri, "uri");
Args.notNull(client, "client");
this.targetUri = uri;
this.client = client;
this.request = createRequest();
}
/**
* Creates the HTTP request object for this builder
*
* @return HTTP request object for this builder
*/
protected abstract HttpRequestBase createRequest();
/**
* Performs the request constructed in this builder and returns the response
*
* @return the repository response
* @throws FcrepoOperationFailedException when the underlying HTTP request results in an error
*/
public FcrepoResponse perform() throws FcrepoOperationFailedException {
LOGGER.debug("Fcrepo {} request to {} with headers: {}", request.getMethod(), targetUri,
request.getAllHeaders());
return client.executeRequest(targetUri, request);
}
/**
* Add a header with the given name and value to the request.
*
* @param name name of the header
* @param value value of the header
* @return this builder
*/
protected RequestBuilder addHeader(final String name, final String value) {
request.addHeader(name, value);
return this;
}
/**
* Add a link header to the request
*
* @param linkHeader link header value represented as a FcrepoLink
* @return this builder
*/
protected RequestBuilder addLinkHeader(final FcrepoLink linkHeader) {
request.addHeader(LINK, linkHeader.toString());
return this;
}
/**
* Add a transaction atomic id header to the request
*
* @param transaction transaction atomic id
* @return this builder
*/
}