1
|
package eu.dnetlib.parthenos.workflows.nodes;
|
2
|
|
3
|
import java.net.URI;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
import java.util.concurrent.ExecutorService;
|
7
|
import java.util.concurrent.Executors;
|
8
|
import java.util.concurrent.Future;
|
9
|
|
10
|
import com.google.common.collect.Lists;
|
11
|
import com.google.common.collect.Maps;
|
12
|
import com.google.gson.Gson;
|
13
|
import com.google.gson.GsonBuilder;
|
14
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
15
|
import eu.dnetlib.msro.workflows.nodes.AsyncJobNode;
|
16
|
import eu.dnetlib.msro.workflows.procs.Env;
|
17
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
18
|
import org.apache.commons.logging.Log;
|
19
|
import org.apache.commons.logging.LogFactory;
|
20
|
import org.apache.http.client.utils.URIBuilder;
|
21
|
import org.springframework.http.HttpEntity;
|
22
|
import org.springframework.http.HttpMethod;
|
23
|
import org.springframework.http.ResponseEntity;
|
24
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
25
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
26
|
import org.springframework.web.client.RestTemplate;
|
27
|
|
28
|
public class PublishJRRJobNode extends AsyncJobNode {
|
29
|
|
30
|
private static final Log log = LogFactory.getLog(PublishJRRJobNode.class);
|
31
|
|
32
|
private String publisherEndpoint;
|
33
|
private String typeNamespace;
|
34
|
private String typeName;
|
35
|
private String datasourceName;
|
36
|
private String datasourceInterface;
|
37
|
|
38
|
private RestTemplate restTemplate;
|
39
|
|
40
|
|
41
|
@Override
|
42
|
protected String execute(final Env env) throws Exception {
|
43
|
Map<String, String> requestMap;
|
44
|
requestMap = Maps.newHashMap();
|
45
|
requestMap.put("typeNamespace", typeNamespace);
|
46
|
requestMap.put("typeName", typeName);
|
47
|
requestMap.put("datasourceApi", datasourceInterface);
|
48
|
requestMap.put("typeName", datasourceName);
|
49
|
|
50
|
Gson gson = new GsonBuilder().create();
|
51
|
String jsonBody = gson.toJson(requestMap);
|
52
|
HttpEntity<String> entity = new HttpEntity<String>(jsonBody);
|
53
|
URI uri = new URIBuilder(getPublisherEndpoint()+"/publishJRR").build();
|
54
|
log.debug("Request goes to "+uri.toString()+" post body:\n"+jsonBody);
|
55
|
restTemplate = new RestTemplate(getClientHttpRequestFactory());
|
56
|
ResponseEntity<Integer> res = restTemplate.exchange(uri, HttpMethod.POST, entity, Integer.class);
|
57
|
if(res.getStatusCode().is2xxSuccessful()){
|
58
|
int resolved = res.getBody();
|
59
|
log.info(String.format("%d entries of type %s published on JRR for API %s", resolved, typeName, datasourceInterface));
|
60
|
env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX+typeName+"_published", resolved);
|
61
|
return Arc.DEFAULT_ARC;
|
62
|
}
|
63
|
else{
|
64
|
log.warn(String.format("Entries of type %s could not be published on JRR for API %s", typeName, datasourceInterface));
|
65
|
env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX+"error", res.getStatusCode().getReasonPhrase());
|
66
|
return Arc.DEFAULT_ARC;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
protected ClientHttpRequestFactory getClientHttpRequestFactory() {
|
71
|
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
|
72
|
return clientHttpRequestFactory;
|
73
|
}
|
74
|
|
75
|
public String getPublisherEndpoint() {
|
76
|
return publisherEndpoint;
|
77
|
}
|
78
|
|
79
|
public void setPublisherEndpoint(final String publisherEndpoint) {
|
80
|
this.publisherEndpoint = publisherEndpoint;
|
81
|
}
|
82
|
|
83
|
public String getTypeNamespace() {
|
84
|
return typeNamespace;
|
85
|
}
|
86
|
|
87
|
public void setTypeNamespace(final String typeNamespace) {
|
88
|
this.typeNamespace = typeNamespace;
|
89
|
}
|
90
|
|
91
|
public String getTypeName() {
|
92
|
return typeName;
|
93
|
}
|
94
|
|
95
|
public void setTypeName(final String typeName) {
|
96
|
this.typeName = typeName;
|
97
|
}
|
98
|
|
99
|
public RestTemplate getRestTemplate() {
|
100
|
return restTemplate;
|
101
|
}
|
102
|
|
103
|
public void setRestTemplate(final RestTemplate restTemplate) {
|
104
|
this.restTemplate = restTemplate;
|
105
|
}
|
106
|
|
107
|
public String getDatasourceName() {
|
108
|
return datasourceName;
|
109
|
}
|
110
|
|
111
|
public void setDatasourceName(final String datasourceName) {
|
112
|
this.datasourceName = datasourceName;
|
113
|
}
|
114
|
|
115
|
public String getDatasourceInterface() {
|
116
|
return datasourceInterface;
|
117
|
}
|
118
|
|
119
|
public void setDatasourceInterface(final String datasourceInterface) {
|
120
|
this.datasourceInterface = datasourceInterface;
|
121
|
}
|
122
|
}
|