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
|
|
46
|
log.debug("Posting text: "+getText());
|
47
|
|
48
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
49
|
try {
|
50
|
URI postUri = new URIBuilder().setScheme(scheme).setHost(host).setPath(postPath).build();
|
51
|
HttpPost postAction = new HttpPost(postUri);
|
52
|
|
53
|
JSONObject obj = createPostBody();
|
54
|
|
55
|
postAction.addHeader("gcube-token", appToken);
|
56
|
postAction.addHeader("Content-type", ContentType.APPLICATION_JSON.toString());
|
57
|
|
58
|
StringEntity params = new StringEntity(obj.toString(), ContentType.APPLICATION_JSON);
|
59
|
postAction.setEntity(params);
|
60
|
|
61
|
return doPost(httpclient, postAction, followRedirect);
|
62
|
} finally {
|
63
|
httpclient.close();
|
64
|
}
|
65
|
|
66
|
}
|
67
|
|
68
|
protected String doPost(HttpClient httpclient, HttpPost postAction, int followRedirect) throws IOException, URISyntaxException {
|
69
|
HttpResponse response = httpclient.execute(postAction);
|
70
|
log.debug(" " + response.getStatusLine().getStatusCode() + " and response message is " + response.getStatusLine().getReasonPhrase());
|
71
|
int status = response.getStatusLine().getStatusCode();
|
72
|
switch (status) {
|
73
|
case HttpURLConnection.HTTP_OK:
|
74
|
case HttpURLConnection.HTTP_CREATED:
|
75
|
return Arc.DEFAULT_ARC;
|
76
|
case HttpURLConnection.HTTP_MOVED_TEMP:
|
77
|
case HttpURLConnection.HTTP_MOVED_PERM:
|
78
|
case HttpURLConnection.HTTP_SEE_OTHER:
|
79
|
if (followRedirect > 0) {
|
80
|
Header[] locations = response.getHeaders("Location");
|
81
|
Header lastLocation = locations[locations.length - 1];
|
82
|
String realLocation = lastLocation.getValue();
|
83
|
log.info("New location after redirect for posting is " + realLocation);
|
84
|
|
85
|
// perform again the same request
|
86
|
postAction.setURI(new URI(realLocation));
|
87
|
return doPost(httpclient, postAction, followRedirect--);
|
88
|
} else
|
89
|
throw new RuntimeException("Too many redirects to post on the VRE");
|
90
|
}
|
91
|
throw new RuntimeException("HTTP error code " + status);
|
92
|
}
|
93
|
|
94
|
protected JSONObject createPostBody() {
|
95
|
JSONObject obj = new JSONObject();
|
96
|
obj.put("text", text.replace("\\n", "\n"));
|
97
|
obj.put("preview_title", previewTitle);
|
98
|
obj.put("preview_description", previewDescription);
|
99
|
obj.put("image_url", httpImageUrl);
|
100
|
obj.put("enable_notification", Boolean.toString(enableNotification));
|
101
|
log.debug("POST body: " + obj.toString());
|
102
|
return obj;
|
103
|
}
|
104
|
|
105
|
public String getScheme() {
|
106
|
return scheme;
|
107
|
}
|
108
|
|
109
|
public void setScheme(final String scheme) {
|
110
|
this.scheme = scheme;
|
111
|
}
|
112
|
|
113
|
public String getHost() {
|
114
|
return host;
|
115
|
}
|
116
|
|
117
|
public void setHost(final String host) {
|
118
|
this.host = host;
|
119
|
}
|
120
|
|
121
|
public String getPostPath() {
|
122
|
return postPath;
|
123
|
}
|
124
|
|
125
|
public void setPostPath(final String postPath) {
|
126
|
this.postPath = postPath;
|
127
|
}
|
128
|
|
129
|
public String getAppToken() {
|
130
|
return appToken;
|
131
|
}
|
132
|
|
133
|
public void setAppToken(final String token) {
|
134
|
this.appToken = token;
|
135
|
}
|
136
|
|
137
|
public String getPreviewTitle() {
|
138
|
return previewTitle;
|
139
|
}
|
140
|
|
141
|
public void setPreviewTitle(final String previewTitle) {
|
142
|
this.previewTitle = previewTitle;
|
143
|
}
|
144
|
|
145
|
public String getPreviewDescription() {
|
146
|
return previewDescription;
|
147
|
}
|
148
|
|
149
|
public void setPreviewDescription(final String previewDescription) {
|
150
|
this.previewDescription = previewDescription;
|
151
|
}
|
152
|
|
153
|
public String getText() {
|
154
|
return text;
|
155
|
}
|
156
|
|
157
|
public void setText(final String text) {
|
158
|
this.text = text;
|
159
|
}
|
160
|
|
161
|
public String getHttpImageUrl() {
|
162
|
return httpImageUrl;
|
163
|
}
|
164
|
|
165
|
public void setHttpImageUrl(final String httpImageUrl) {
|
166
|
this.httpImageUrl = httpImageUrl;
|
167
|
}
|
168
|
|
169
|
public boolean isEnableNotification() {
|
170
|
return enableNotification;
|
171
|
}
|
172
|
|
173
|
public void setEnableNotification(final boolean enablenotification) {
|
174
|
this.enableNotification = enablenotification;
|
175
|
}
|
176
|
}
|