-
Notifications
You must be signed in to change notification settings - Fork 19
/
GetBuilder.java
192 lines (169 loc) · 6.11 KB
/
GetBuilder.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* 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.fcrepo.client.FedoraHeaderConstants.ACCEPT;
import static org.fcrepo.client.FedoraHeaderConstants.ATOMIC_ID;
import static org.fcrepo.client.FedoraHeaderConstants.IF_MODIFIED_SINCE;
import static org.fcrepo.client.FedoraHeaderConstants.IF_NONE_MATCH;
import static org.fcrepo.client.FedoraHeaderConstants.PREFER;
import static org.fcrepo.client.FedoraHeaderConstants.RANGE;
import java.net.URI;
import java.time.Instant;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import org.apache.http.client.methods.HttpRequestBase;
import org.fcrepo.client.FcrepoResponse.TransactionURI;
/**
* Builds a GET request to retrieve the content of a resource from the Fedora HTTP API
*
* @author bbpennel
*/
public class GetBuilder extends RetrieveRequestBuilder {
/**
* Construct a GetBuilder
*
* @param uri the target
* @param client the client for this request
*/
public GetBuilder(final URI uri, final FcrepoClient client) {
super(uri, client);
}
@Override
protected HttpRequestBase createRequest() {
return HttpMethods.GET.createRequest(targetUri);
}
/**
* Add the accept header to this request to negotiate the response format.
*
* @param mediaType media type to set as the accept header. It should be a value from one of the allowed RDF
* source formats supported by Fedora.
* @return this builder
*/
public GetBuilder accept(final String mediaType) {
if (mediaType != null) {
request.setHeader(ACCEPT, mediaType);
}
return this;
}
/**
* Set the byte range of content to retrieve
*
* @param rangeStart beginning byte index
* @param rangeEnd ending byte index
* @return this builder
*/
public GetBuilder range(final Long rangeStart, final Long rangeEnd) {
if (rangeStart != null || rangeEnd != null) {
String range = "bytes=";
if (rangeStart != null && rangeStart.longValue() > -1L) {
range += rangeStart.toString();
}
range += "-";
if (rangeEnd != null && rangeEnd.longValue() > -1L) {
range += rangeEnd.toString();
}
request.setHeader(RANGE, range);
}
return this;
}
/**
* Set the prefer header for this request to representation, to indicate that links to other resources and their
* properties should also be included.
*
* @return this builder
*/
public GetBuilder preferRepresentation() {
request.setHeader(PREFER, buildPrefer("representation", null, null));
return this;
}
/**
* Set the prefer header for this request to representation, to indicate that links to other resources and their
* properties should also be included. The set of properties returned can be further specified by providing lists
* of LDP defined preferences to omit or include.
*
* @param includeUris URIs of LDP defined preferences to include
* @param omitUris URIs of LDP defined preferences to omit
* @return this builder
*/
public GetBuilder preferRepresentation(final List<URI> includeUris, final List<URI> omitUris) {
request.setHeader(PREFER, buildPrefer("representation", includeUris, omitUris));
return this;
}
private String buildPrefer(final String prefer, final List<URI> includeUris, final List<URI> omitUris) {
final StringJoiner preferJoin = new StringJoiner("; ");
preferJoin.add("return=" + prefer);
if (includeUris != null) {
final String include = includeUris.stream().map(URI::toString).collect(Collectors.joining(" "));
if (include.length() > 0) {
preferJoin.add("include=\"" + include + "\"");
}
}
if (omitUris != null) {
final String omit = omitUris.stream().map(URI::toString).collect(Collectors.joining(" "));
if (omit.length() > 0) {
preferJoin.add("omit=\"" + omit + "\"");
}
}
return preferJoin.toString();
}
/**
* Provide an etag for the if-none-match header for this request
*
* @param etag etag to provide as the if-none-match header
* @return this builder
*/
public GetBuilder ifNoneMatch(final String etag) {
if (etag != null) {
request.setHeader(IF_NONE_MATCH, etag);
}
return this;
}
/**
* Provide a if-last-modified header for this request
*
* @param lastModified date to provided as the if-modified-since header
* @return this builder
*/
public GetBuilder ifModifiedSince(final String lastModified) {
if (lastModified != null) {
request.setHeader(IF_MODIFIED_SINCE, lastModified);
}
return this;
}
@Override
public GetBuilder disableRedirects() {
return (GetBuilder) super.disableRedirects();
}
@Override
public GetBuilder wantDigest(final String value) {
return (GetBuilder) super.wantDigest(value);
}
@Override
public GetBuilder noCache() {
return (GetBuilder) super.noCache();
}
@Override
public GetBuilder acceptDatetime(final Instant acceptInstant) {
return (GetBuilder) super.acceptDatetime(acceptInstant);
}
@Override
public GetBuilder acceptDatetime(final String acceptDatetime) {
return (GetBuilder) super.acceptDatetime(acceptDatetime);
}
@Override
public GetBuilder addHeader(final String name, final String value) {
return (GetBuilder) super.addHeader(name, value);
}
@Override
public GetBuilder addLinkHeader(final FcrepoLink linkHeader) {
return (GetBuilder) super.addLinkHeader(linkHeader);
}
public GetBuilder addTransaction(final TransactionURI transaction) {
request.addHeader(ATOMIC_ID, transaction.asString());
return this;
}
}