2020-04-27 14:52:31 +02:00
|
|
|
|
2019-10-03 12:22:44 +02:00
|
|
|
package eu.dnetlib.dhp.application;
|
|
|
|
|
2021-02-04 14:06:02 +01:00
|
|
|
import java.io.*;
|
2019-12-11 15:43:24 +01:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.zip.GZIPInputStream;
|
|
|
|
import java.util.zip.GZIPOutputStream;
|
2020-04-28 11:23:29 +02:00
|
|
|
|
2020-04-18 12:42:58 +02:00
|
|
|
import org.apache.commons.cli.*;
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
2021-02-04 14:06:02 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-10-03 12:22:44 +02:00
|
|
|
|
2020-04-28 11:23:29 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
2019-10-10 11:33:51 +02:00
|
|
|
public class ArgumentApplicationParser implements Serializable {
|
2019-10-03 12:22:44 +02:00
|
|
|
|
2021-02-04 14:06:02 +01:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(ArgumentApplicationParser.class);
|
|
|
|
|
2020-04-27 14:52:31 +02:00
|
|
|
private final Options options = new Options();
|
|
|
|
private final Map<String, String> objectMap = new HashMap<>();
|
|
|
|
|
|
|
|
private final List<String> compressedValues = new ArrayList<>();
|
|
|
|
|
2021-02-04 14:06:02 +01:00
|
|
|
public ArgumentApplicationParser(final String json_configuration) throws IOException {
|
2020-04-27 14:52:31 +02:00
|
|
|
final ObjectMapper mapper = new ObjectMapper();
|
|
|
|
final OptionsParameter[] configuration = mapper.readValue(json_configuration, OptionsParameter[].class);
|
|
|
|
createOptionMap(configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArgumentApplicationParser(final OptionsParameter[] configuration) {
|
|
|
|
createOptionMap(configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createOptionMap(final OptionsParameter[] configuration) {
|
|
|
|
Arrays
|
|
|
|
.stream(configuration)
|
|
|
|
.map(
|
|
|
|
conf -> {
|
|
|
|
final Option o = new Option(conf.getParamName(), true, conf.getParamDescription());
|
|
|
|
o.setLongOpt(conf.getParamLongName());
|
|
|
|
o.setRequired(conf.isParamRequired());
|
|
|
|
if (conf.isCompressed()) {
|
|
|
|
compressedValues.add(conf.getParamLongName());
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
})
|
|
|
|
.forEach(options::addOption);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String decompressValue(final String abstractCompressed) {
|
|
|
|
try {
|
|
|
|
byte[] byteArray = Base64.decodeBase64(abstractCompressed.getBytes());
|
|
|
|
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(byteArray));
|
|
|
|
final StringWriter stringWriter = new StringWriter();
|
|
|
|
IOUtils.copy(gis, stringWriter);
|
|
|
|
return stringWriter.toString();
|
2021-08-11 12:13:22 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Wrong value to decompress: {}", abstractCompressed);
|
|
|
|
throw new IllegalArgumentException(e);
|
2020-04-27 14:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 12:13:22 +02:00
|
|
|
public static String compressArgument(final String value) throws IOException {
|
2020-04-27 14:52:31 +02:00
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
|
GZIPOutputStream gzip = new GZIPOutputStream(out);
|
|
|
|
gzip.write(value.getBytes());
|
|
|
|
gzip.close();
|
|
|
|
return java.util.Base64.getEncoder().encodeToString(out.toByteArray());
|
|
|
|
}
|
|
|
|
|
2021-02-04 14:06:02 +01:00
|
|
|
public void parseArgument(final String[] args) throws ParseException {
|
2020-04-27 14:52:31 +02:00
|
|
|
CommandLineParser parser = new BasicParser();
|
|
|
|
CommandLine cmd = parser.parse(options, args);
|
|
|
|
Arrays
|
|
|
|
.stream(cmd.getOptions())
|
|
|
|
.forEach(
|
|
|
|
it -> objectMap
|
|
|
|
.put(
|
|
|
|
it.getLongOpt(),
|
|
|
|
compressedValues.contains(it.getLongOpt())
|
|
|
|
? decompressValue(it.getValue())
|
|
|
|
: it.getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public String get(final String key) {
|
|
|
|
return objectMap.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, String> getObjectMap() {
|
|
|
|
return objectMap;
|
|
|
|
}
|
2019-10-03 12:22:44 +02:00
|
|
|
}
|