Project

General

Profile

« Previous | Next » 

Revision 55163

View differences:

modules/dnet-openaireplus-workflows/trunk/src/main/java/eu/dnetlib/msro/openaireplus/workflows/nodes/vre/VREPostJobNode.java
1 1
package eu.dnetlib.msro.openaireplus.workflows.nodes.vre;
2 2

  
3
import java.io.IOException;
4
import java.net.HttpURLConnection;
3 5
import java.net.URI;
6
import java.net.URISyntaxException;
4 7

  
5 8
import com.googlecode.sarasvati.Arc;
6 9
import com.googlecode.sarasvati.NodeToken;
7 10
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
8 11
import org.apache.commons.logging.Log;
9 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;
10 16
import org.apache.http.client.methods.HttpPost;
11 17
import org.apache.http.client.utils.URIBuilder;
12 18
import org.apache.http.entity.ContentType;
13 19
import org.apache.http.entity.StringEntity;
14
import org.apache.http.impl.client.BasicResponseHandler;
15 20
import org.apache.http.impl.client.CloseableHttpClient;
16 21
import org.apache.http.impl.client.HttpClients;
22
import org.json.JSONObject;
17 23

  
18 24
/**
19 25
 * Created by alessia on 24/03/16.
......
24 30
	private String scheme;
25 31
	private String host;
26 32
	private String postPath;
33
	private int followRedirect = 10;
27 34

  
28 35
	//POST parameters
29 36
	private String appToken;
......
39 46
		try {
40 47
			URI postUri = new URIBuilder().setScheme(scheme).setHost(host).setPath(postPath).build();
41 48
			HttpPost postAction = new HttpPost(postUri);
42
			StringBuilder strBuilder = new StringBuilder();
43
			strBuilder.append("token=").append(appToken).append("&previewtitle=").append(previewTitle)
44
					.append("&previewdescription=").append(previewDescription).append("&text=").append(text)
45
					.append("&httpimageurl=").append(httpImageUrl).append("&enablenotification=").append(enableNotification);
46
			String postBody = strBuilder.toString();
47
			log.debug("POST body: " + postBody);
48
			StringEntity ent = new StringEntity(postBody, ContentType.APPLICATION_FORM_URLENCODED);
49 49

  
50
			postAction.setEntity(ent);
50
			JSONObject obj = createPostBody();
51 51

  
52
			BasicResponseHandler responseHandler = new BasicResponseHandler();
52
			postAction.addHeader("gcube-token", appToken);
53
			postAction.addHeader("Content-type", ContentType.APPLICATION_JSON.toString());
53 54

  
54
			httpclient.execute(postAction, responseHandler);
55
			return Arc.DEFAULT_ARC;
55
			StringEntity params = new StringEntity(obj.toString(), ContentType.APPLICATION_JSON);
56
			postAction.setEntity(params);
57

  
58
			return doPost(httpclient, postAction, followRedirect);
56 59
		} finally {
57 60
			httpclient.close();
58 61
		}
59 62

  
60 63
	}
61 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

  
62 102
	public String getScheme() {
63 103
		return scheme;
64 104
	}

Also available in: Unified diff