-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAbstractOpsImplV1.java
More file actions
110 lines (91 loc) · 3.19 KB
/
AbstractOpsImplV1.java
File metadata and controls
110 lines (91 loc) · 3.19 KB
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
/*
*
*/
package com.researchspace.dataverse.http;
import com.researchspace.dataverse.api.v1.DataverseConfig;
import com.researchspace.dataverse.entities.DataverseResponse;
import com.researchspace.springrest.ext.LoggingResponseErrorHandler;
import com.researchspace.springrest.ext.RestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
/** <pre>
Copyright 2016 ResearchSpace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
</pre>
*/
@Slf4j
public abstract class AbstractOpsImplV1 {
String apiKey = "";
String serverURL = "";
String serverAPIURL = serverURL +"/api";
String serverAPIv1URL = serverAPIURL +"/v1";
protected RestTemplate template;
public AbstractOpsImplV1() {
super();
this.template = createTemplate();
}
protected void setTemplate(RestTemplate template) {
this.template = template;
}
public final static String apiHeader = "X-Dataverse-key";
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public void setServerURL(String serverURL) {
this.serverURL = serverURL;
this.serverAPIURL = serverURL + "/api";
this.serverAPIv1URL = this.serverAPIURL +"/v1";
}
public void configure(DataverseConfig config) {
setApiKey(config.getApiKey());
setServerURL(config.getServerURL().toString());
}
protected <T> void handleError(ResponseEntity<DataverseResponse<T>> resp) {
log.debug("{}", resp.getBody());
if (RestUtil.isError(resp.getStatusCode())) {
String msg = String.format("Error code returned %d with message [%s]", resp.getStatusCodeValue(),
resp.getBody().getMessage());
log.error(msg);
throw new RestClientException(msg);
}
}
RestTemplate createTemplate() {
RestTemplate template = new RestTemplate();
template.setErrorHandler(new LoggingResponseErrorHandler());
return template;
}
protected String createV1Url(String ... pathComponents) {
String url = serverAPIv1URL + "/" + StringUtils.join(pathComponents, "/") ;
log.info("URL is {}", url);
return url;
}
String createAdminUrl(String ... pathComponents) {
String url = serverAPIURL + "/" + StringUtils.join(pathComponents, "/") ;
log.info("URL is {}", url);
return url;
}
HttpHeaders addAPIKeyToHeader() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
addApiKey(headers);
return headers;
}
void addApiKey(HttpHeaders headers) {
headers.add(apiHeader, apiKey);
}
}