listSubscriptions

This commit is contained in:
Michele Artini 2021-03-22 11:24:35 +01:00
parent bb06882318
commit b5ae4b92ab
3 changed files with 78 additions and 9 deletions

View File

@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>eu.dnetlib.dhp</groupId>
@ -17,7 +19,11 @@
<description>D-Net Broker - Command Line Client</description>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
<build>

View File

@ -1,5 +1,11 @@
package eu.dnetlib.broker;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
@ -7,14 +13,22 @@ import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
@SpringBootApplication
public class BrokerClientApp implements CommandLineRunner {
@ -77,7 +91,7 @@ public class BrokerClientApp implements CommandLineRunner {
try {
final CommandLine cmd = cmdLineParser.parse(options, args, true);
final CommandLine cmd = cmdLineParser.parse(options, args, false);
if (cmd.hasOption("v")) {
SpringApplication.run(BrokerClientApp.class, ArrayUtils.add(args, "--logging.level.root=INFO"));
@ -94,23 +108,71 @@ public class BrokerClientApp implements CommandLineRunner {
@Override
public void run(final String... args) throws Exception {
System.out.println();
log.info("**** EXECUTING - BrokerClientApp ***");
final CommandLine cmd = cmdLineParser.parse(options, args, false);
final CommandLine cmd = cmdLineParser.parse(options, args, true);
final String user = cmd.getOptionValue("u");
final String baseUrl = cmd.getOptionValue("bu", defaultBrokerApiBaseUrl);
final URL baseUrl = new URL(cmd.getOptionValue("bu", defaultBrokerApiBaseUrl));
final String outputDir = cmd.getOptionValue("o");
log.info("USER: " + user);
log.info("BASE_URL: " + baseUrl);
log.info("OUPUT DIR: " + outputDir);
log.info("* PARAMS: USER: " + user);
log.info("* PARAMS: BASE_URL: " + baseUrl);
log.info("* PARAMS: OUPUT DIR: " + outputDir);
listSubscriptions(baseUrl, user);
log.info("**** DONE ***");
System.out.println();
}
private List<String> listSubscriptions(final URL baseUrl, final String email) throws Exception {
final String url = baseUrl + "/subscriptions?email=" + URLEncoder.encode(email, StandardCharsets.UTF_8.name());
log.info("Performing HTTP GET for subscriptions: " + url);
final HttpClient client = HttpClientBuilder.create().build();
final HttpGet request = new HttpGet(url);
request.addHeader("accept", "application/json");
final HttpResponse response = client.execute(request);
final String json = IOUtils.toString(response.getEntity().getContent());
log.info("Found subscriptions: " + json);
final JSONArray array = new JSONArray(json);
System.out.println(String.format("Found %d subscription(s):", array.length()));
final List<String> res = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
final JSONObject object = array.getJSONObject(i);
final String suscrId = object.getString("subscriptionId");
final String topic = object.getString("topic");
final String ds = extractDsName(object.getJSONArray("conditionsAsList"));
System.out.println(String.format(" - %s (TOPIC: %s, DS: %s)", suscrId, topic, ds));
res.add(suscrId);
}
return res;
}
private String extractDsName(final JSONArray conds) {
try {
for (int i = 0; i < conds.length(); i++) {
final JSONObject object = conds.getJSONObject(i);
if (object.getString("field").equals("targetDatasourceName")) { return object.getJSONArray("listParams").getJSONObject(0).getString("value"); }
}
} catch (final JSONException e) {
log.warn(e.getMessage());
}
return "";
}
private static void printHelpAndExit(final Options options) {
final String ln = StringUtils.repeat("=", APPLICATION_TITLE.length());
System.out.println(String.format("\n%s\n%s\n%s\n", ln, APPLICATION_TITLE, ln));

View File

@ -2,4 +2,5 @@ spring.main.banner-mode=off
logging.level.root=off
dhp.broker.api.base-url + http://test.it/test
dhp.broker.api.base-url = http://api.openaire.eu/broker