Project

General

Profile

« Previous | Next » 

Revision 58315

Added by Andreas Czerniak about 4 years ago

add AuthMethod Bearer to RestIterator class, changed version to 1.3.32-SNAPSHOT

View differences:

modules/dnet-collector-plugins/trunk/src/main/java/eu/dnetlib/data/collector/plugins/rest/RestCollectorPlugin.java
26 26
		final String resultFormatValue = ifDescriptor.getParams().get("resultFormatValue");
27 27
		final String resultSizeParam = ifDescriptor.getParams().get("resultSizeParam");
28 28
		final String resultSizeValue = (StringUtils.isBlank(ifDescriptor.getParams().get("resultSizeValue"))) ? "100" : ifDescriptor.getParams().get("resultSizeValue");
29
                final String queryParams = ifDescriptor.getParams().get("queryParams");
29
        final String queryParams = ifDescriptor.getParams().get("queryParams");
30 30
		final String entityXpath = ifDescriptor.getParams().get("entityXpath");
31
		final String authMethod = ifDescriptor.getParams().get("authMethod");
32
		final String authToken = ifDescriptor.getParams().get("authToken");
31 33
		
32 34
		if (StringUtils.isBlank(baseUrl)) {throw new CollectorServiceException("Param 'baseUrl' is null or empty");}
33 35
		if (StringUtils.isBlank(resumptionType)) {throw new CollectorServiceException("Param 'resumptionType' is null or empty");}
......
51 53
				resultFormatParam,
52 54
				resultFormatValue,
53 55
				resultSizeParam,
54
                                resultSizeValue,
56
                resultSizeValue,
55 57
				queryParams,
56 58
				entityXpath);
57 59
	}
modules/dnet-collector-plugins/trunk/src/main/java/eu/dnetlib/data/collector/plugins/rest/RestIterator.java
8 8
import java.io.InputStream;
9 9
import java.io.StringWriter;
10 10
import java.net.URL;
11
import java.net.HttpURLConnection;
11 12
import java.util.Iterator;
12 13
import java.util.Queue;
13 14
import java.util.concurrent.PriorityBlockingQueue;
......
31 32

  
32 33
/**
33 34
 * @author Jochen Schirrwagen, Aenne Loehden, Andreas Czerniak
34
 * @date 2018-09-03
35
 * @date 2020-03-20
35 36
 *
36 37
 */
37 38
public class RestIterator implements Iterator<String> {
......
58 59
	private XPathExpression xprEntity;
59 60
	private String queryFormat;
60 61
	private String querySize;
62
	private String authMethod;
63
	private String authToken;
61 64
	private Queue<String> recordQueue = new PriorityBlockingQueue<String>();
62 65
	private int discoverResultSize = 0;
63 66
	private int pagination = 1;
......
75 78
			final String queryParams,
76 79
			final String entityXpath
77 80
	) {
81
//		this.baseUrl = baseUrl;
82
//		this.resumptionType = resumptionType;
83
//		this.resumptionParam = resumptionParam;
84
//		this.resultFormatValue = resultFormatValue;
85
//		this.queryParams = queryParams;
86
//		this.resultSizeValue = Integer.valueOf(resultSizeValueStr);
87
//
88
//		queryFormat = StringUtils.isNotBlank(resultFormatParam) ? "&" + resultFormatParam + "=" + resultFormatValue : "";
89
//		querySize = StringUtils.isNotBlank(resultSizeParam) ? "&" + resultSizeParam + "=" + resultSizeValueStr : "";
90
//
91
//		try {
92
//			initXmlTransformation(resultTotalXpath, resumptionXpath, entityXpath);
93
//		} catch (Exception e) {
94
//			throw new IllegalStateException("xml transformation init failed: " + e.getMessage());
95
//		}
96
//		initQueue();
97
		this(baseUrl,resumptionType,resumptionParam,resumptionXpath,resultTotalXpath,resultFormatParam,resultFormatValue,resultSizeParam,resultSizeValueStr,queryParams,entityXpath,"", "");
98
	}
99

  
100
	public RestIterator(
101
			final String baseUrl,
102
			final String resumptionType,
103
			final String resumptionParam,
104
			final String resumptionXpath,
105
			final String resultTotalXpath,
106
			final String resultFormatParam,
107
			final String resultFormatValue,
108
			final String resultSizeParam,
109
			final String resultSizeValueStr,
110
			final String queryParams,
111
			final String entityXpath,
112
			final String authMethod,
113
			final String authToken
114
	) {
78 115
		this.baseUrl = baseUrl;
79 116
		this.resumptionType = resumptionType;
80 117
		this.resumptionParam = resumptionParam;
81 118
		this.resultFormatValue = resultFormatValue;
82 119
		this.queryParams = queryParams;
83 120
		this.resultSizeValue = Integer.valueOf(resultSizeValueStr);
121
		this.authMethod = authMethod;
122
		this.authToken = authToken;
84 123

  
85 124
		queryFormat = StringUtils.isNotBlank(resultFormatParam) ? "&" + resultFormatParam + "=" + resultFormatValue : "";
86 125
		querySize = StringUtils.isNotBlank(resultSizeParam) ? "&" + resultSizeParam + "=" + resultSizeValueStr : "";
......
92 131
		}
93 132
		initQueue();
94 133
	}
95

  
134
	
135
	
96 136
	private void initXmlTransformation(String resultTotalXpath, String resumptionXpath, String entityXpath)
97 137
			throws TransformerConfigurationException, XPathExpressionException {
98 138
		transformer = TransformerFactory.newInstance().newTransformer();
......
157 197
		NodeList nodeList = null;
158 198
		String qUrlArgument = "";
159 199
		int urlOldResumptionSize = 0;
200
		InputStream theHttpInputStream;
160 201

  
161 202
		try {
162 203
			URL qUrl = new URL(query);
163

  
164
			resultStream = qUrl.openStream();
204
			
205
			if (this.authMethod == "bearer") {
206
				HttpURLConnection conn = (HttpURLConnection) qUrl.openConnection();
207
	        	conn.setRequestProperty("Authorization","Bearer "+authToken);
208
	        	conn.setRequestProperty("Content-Type","application/json");
209
	        	conn.setRequestMethod("GET");   
210
	        	theHttpInputStream = conn.getInputStream();
211
			} else {
212
				theHttpInputStream = qUrl.openStream();
213
			}
214
			
215
			resultStream = theHttpInputStream;
165 216
			if ("json".equals(resultFormatValue.toLowerCase())) {
166

  
167 217
				resultJson = IOUtils.toString(resultStream, "UTF-8");
168 218
				resultJson = syntaxConvertJsonKeyNamens(resultJson);
169 219
				org.json.JSONObject jsonObject = new org.json.JSONObject(resultJson);
......
247 297
			}
248 298
			log.info("resultTotal: " + resultTotal);
249 299
			log.info("resInt: " + resumptionInt);
250
			if (resumptionInt < resultTotal) {
300
			if (resumptionInt <= resultTotal) {
251 301
				nextQuery = baseUrl + "?" + queryParams + querySize + "&" + resumptionParam + "=" + resumptionStr + queryFormat;
252 302
			} else
253 303
				nextQuery = "";

Also available in: Unified diff