Project

General

Profile

1
package eu.dnetlib.msro.workflows.nodes.vre;
2

    
3
import java.net.URI;
4

    
5
import eu.dnetlib.msro.workflows.graph.Arc;
6
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
7
import eu.dnetlib.msro.workflows.procs.Env;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.apache.http.client.methods.HttpPost;
11
import org.apache.http.client.utils.URIBuilder;
12
import org.apache.http.entity.ContentType;
13
import org.apache.http.entity.StringEntity;
14
import org.apache.http.impl.client.BasicResponseHandler;
15
import org.apache.http.impl.client.CloseableHttpClient;
16
import org.apache.http.impl.client.HttpClients;
17

    
18
/**
19
 * Created by alessia on 24/03/16.
20
 */
21
public class VREPostJobNode extends SimpleJobNode {
22

    
23
	private static final Log log = LogFactory.getLog(VREPostJobNode.class);
24
	private String scheme;
25
	private String host;
26
	private String postPath;
27

    
28
	// POST parameters
29
	private String appToken;
30
	private String previewTitle;
31
	private String previewDescription;
32
	private String text;
33
	private String httpImageUrl;
34
	private boolean enableNotification;
35

    
36
	@Override
37
	protected String execute(final Env env) throws Exception {
38
		final CloseableHttpClient httpclient = HttpClients.createDefault();
39
		try {
40
			final URI postUri = new URIBuilder().setScheme(this.scheme).setHost(this.host).setPath(this.postPath).build();
41
			final HttpPost postAction = new HttpPost(postUri);
42
			final StringBuilder strBuilder = new StringBuilder();
43
			strBuilder.append("token=").append(this.appToken).append("&previewtitle=").append(this.previewTitle)
44
					.append("&previewdescription=").append(this.previewDescription).append("&text=").append(this.text)
45
					.append("&httpimageurl=").append(this.httpImageUrl).append("&enablenotification=").append(this.enableNotification);
46
			final String postBody = strBuilder.toString();
47
			log.debug("POST body: " + postBody);
48
			final StringEntity ent = new StringEntity(postBody, ContentType.APPLICATION_FORM_URLENCODED);
49

    
50
			postAction.setEntity(ent);
51

    
52
			final BasicResponseHandler responseHandler = new BasicResponseHandler();
53

    
54
			httpclient.execute(postAction, responseHandler);
55
			return Arc.DEFAULT_ARC;
56
		} finally {
57
			httpclient.close();
58
		}
59

    
60
	}
61

    
62
	public String getScheme() {
63
		return this.scheme;
64
	}
65

    
66
	public void setScheme(final String scheme) {
67
		this.scheme = scheme;
68
	}
69

    
70
	public String getHost() {
71
		return this.host;
72
	}
73

    
74
	public void setHost(final String host) {
75
		this.host = host;
76
	}
77

    
78
	public String getPostPath() {
79
		return this.postPath;
80
	}
81

    
82
	public void setPostPath(final String postPath) {
83
		this.postPath = postPath;
84
	}
85

    
86
	public String getAppToken() {
87
		return this.appToken;
88
	}
89

    
90
	public void setAppToken(final String token) {
91
		this.appToken = token;
92
	}
93

    
94
	public String getPreviewTitle() {
95
		return this.previewTitle;
96
	}
97

    
98
	public void setPreviewTitle(final String previewTitle) {
99
		this.previewTitle = previewTitle;
100
	}
101

    
102
	public String getPreviewDescription() {
103
		return this.previewDescription;
104
	}
105

    
106
	public void setPreviewDescription(final String previewDescription) {
107
		this.previewDescription = previewDescription;
108
	}
109

    
110
	public String getText() {
111
		return this.text;
112
	}
113

    
114
	public void setText(final String text) {
115
		this.text = text;
116
	}
117

    
118
	public String getHttpImageUrl() {
119
		return this.httpImageUrl;
120
	}
121

    
122
	public void setHttpImageUrl(final String httpImageUrl) {
123
		this.httpImageUrl = httpImageUrl;
124
	}
125

    
126
	public boolean isEnableNotification() {
127
		return this.enableNotification;
128
	}
129

    
130
	public void setEnableNotification(final boolean enablenotification) {
131
		this.enableNotification = enablenotification;
132
	}
133
}
    (1-1/1)