1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.vre;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.net.HttpURLConnection;
|
5
|
import java.net.URI;
|
6
|
import java.net.URISyntaxException;
|
7
|
|
8
|
import com.googlecode.sarasvati.Arc;
|
9
|
import com.googlecode.sarasvati.NodeToken;
|
10
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
11
|
import org.apache.commons.logging.Log;
|
12
|
import org.apache.commons.logging.LogFactory;
|
13
|
import org.apache.http.Header;
|
14
|
import org.apache.http.HttpResponse;
|
15
|
import org.apache.http.client.HttpClient;
|
16
|
import org.apache.http.client.methods.HttpPost;
|
17
|
import org.apache.http.client.utils.URIBuilder;
|
18
|
import org.apache.http.entity.ContentType;
|
19
|
import org.apache.http.entity.StringEntity;
|
20
|
import org.apache.http.impl.client.CloseableHttpClient;
|
21
|
import org.apache.http.impl.client.HttpClients;
|
22
|
import org.json.JSONObject;
|
23
|
|
24
|
/**
|
25
|
* Created by alessia on 24/03/16.
|
26
|
*/
|
27
|
public class VREPostJobNode extends SimpleJobNode {
|
28
|
|
29
|
private static final Log log = LogFactory.getLog(VREPostJobNode.class);
|
30
|
private String scheme;
|
31
|
private String host;
|
32
|
private String postPath;
|
33
|
private int followRedirect = 10;
|
34
|
|
35
|
//POST parameters
|
36
|
private String appToken;
|
37
|
private String previewTitle;
|
38
|
private String previewDescription;
|
39
|
private String text;
|
40
|
private String httpImageUrl;
|
41
|
private boolean enableNotification;
|
42
|
|
43
|
@Override
|
44
|
protected String execute(final NodeToken token) throws Exception {
|
45
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
46
|
try {
|
47
|
URI postUri = new URIBuilder().setScheme(scheme).setHost(host).setPath(postPath).build();
|
48
|
HttpPost postAction = new HttpPost(postUri);
|
49
|
|
50
|
JSONObject obj = createPostBody();
|
51
|
|
52
|
postAction.addHeader("gcube-token", appToken);
|
53
|
postAction.addHeader("Content-type", ContentType.APPLICATION_JSON.toString());
|
54
|
|
55
|
StringEntity params = new StringEntity(obj.toString(), ContentType.APPLICATION_JSON);
|
56
|
postAction.setEntity(params);
|
57
|
|
58
|
return doPost(httpclient, postAction, followRedirect);
|
59
|
} finally {
|
60
|
httpclient.close();
|
61
|
}
|
62
|
|
63
|
}
|
64
|
|
65
|
protected String doPost(HttpClient httpclient, HttpPost postAction, int followRedirect) throws IOException, URISyntaxException {
|
66
|
HttpResponse response = httpclient.execute(postAction);
|
67
|
log.debug(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
68
|
int status = response.getStatusLine().getStatusCode();
|
69
|
switch (status) {
|
70
|
case HttpURLConnection.HTTP_OK:
|
71
|
case HttpURLConnection.HTTP_CREATED:
|
72
|
return Arc.DEFAULT_ARC;
|
73
|
case HttpURLConnection.HTTP_MOVED_TEMP:
|
74
|
case HttpURLConnection.HTTP_MOVED_PERM:
|
75
|
case HttpURLConnection.HTTP_SEE_OTHER:
|
76
|
if (followRedirect > 0) {
|
77
|
Header[] locations = response.getHeaders("Location");
|
78
|
Header lastLocation = locations[locations.length - 1];
|
79
|
String realLocation = lastLocation.getValue();
|
80
|
log.info("New location after redirect for posting is " + realLocation);
|
81
|
|
82
|
// perform again the same request
|
83
|
postAction.setURI(new URI(realLocation));
|
84
|
return doPost(httpclient, postAction, followRedirect--);
|
85
|
} else
|
86
|
throw new RuntimeException("Too many redirects to post on the VRE");
|
87
|
}
|
88
|
throw new RuntimeException("HTTP error code " + status);
|
89
|
}
|
90
|
|
91
|
protected JSONObject createPostBody() {
|
92
|
JSONObject obj = new JSONObject();
|
93
|
obj.put("text", text);
|
94
|
obj.put("preview_title", previewTitle);
|
95
|
obj.put("preview_description", previewDescription);
|
96
|
obj.put("image_url", httpImageUrl);
|
97
|
obj.put("enable_notification", Boolean.toString(enableNotification));
|
98
|
log.debug("POST body: " + obj.toString());
|
99
|
return obj;
|
100
|
}
|
101
|
|
102
|
public String getScheme() {
|
103
|
return scheme;
|
104
|
}
|
105
|
|
106
|
public void setScheme(final String scheme) {
|
107
|
this.scheme = scheme;
|
108
|
}
|
109
|
|
110
|
public String getHost() {
|
111
|
return host;
|
112
|
}
|
113
|
|
114
|
public void setHost(final String host) {
|
115
|
this.host = host;
|
116
|
}
|
117
|
|
118
|
public String getPostPath() {
|
119
|
return postPath;
|
120
|
}
|
121
|
|
122
|
public void setPostPath(final String postPath) {
|
123
|
this.postPath = postPath;
|
124
|
}
|
125
|
|
126
|
public String getAppToken() {
|
127
|
return appToken;
|
128
|
}
|
129
|
|
130
|
public void setAppToken(final String token) {
|
131
|
this.appToken = token;
|
132
|
}
|
133
|
|
134
|
public String getPreviewTitle() {
|
135
|
return previewTitle;
|
136
|
}
|
137
|
|
138
|
public void setPreviewTitle(final String previewTitle) {
|
139
|
this.previewTitle = previewTitle;
|
140
|
}
|
141
|
|
142
|
public String getPreviewDescription() {
|
143
|
return previewDescription;
|
144
|
}
|
145
|
|
146
|
public void setPreviewDescription(final String previewDescription) {
|
147
|
this.previewDescription = previewDescription;
|
148
|
}
|
149
|
|
150
|
public String getText() {
|
151
|
return text;
|
152
|
}
|
153
|
|
154
|
public void setText(final String text) {
|
155
|
this.text = text;
|
156
|
}
|
157
|
|
158
|
public String getHttpImageUrl() {
|
159
|
return httpImageUrl;
|
160
|
}
|
161
|
|
162
|
public void setHttpImageUrl(final String httpImageUrl) {
|
163
|
this.httpImageUrl = httpImageUrl;
|
164
|
}
|
165
|
|
166
|
public boolean isEnableNotification() {
|
167
|
return enableNotification;
|
168
|
}
|
169
|
|
170
|
public void setEnableNotification(final boolean enablenotification) {
|
171
|
this.enableNotification = enablenotification;
|
172
|
}
|
173
|
}
|