Commit 7c77564e authored by Matija Obreza's avatar Matija Obreza

Source code formatted

parent d278bd04
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.client.oauth;
......@@ -29,13 +29,6 @@ import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.scribe.exceptions.OAuthConnectionException;
import org.scribe.model.Verb;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -46,30 +39,37 @@ import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.scribe.exceptions.OAuthConnectionException;
import org.scribe.model.Verb;
/**
* Simple command line interface to Genesys.
*
* @author matijaobreza
*/
public class CLI {
/** The Constant _log. */
private static final Logger _log = LogManager.getLogger(CLI.class);
/** The properties file. */
private File propertiesFile;
/** The properties. */
private Properties properties;
/** The in. */
private Scanner in = new Scanner(System.in);
private final Scanner in = new Scanner(System.in);
/** The mapper. */
private static ObjectMapper mapper = new ObjectMapper();
/** The genesys client. */
private GenesysClient genesysClient = new GenesysClient();
private final GenesysClient genesysClient = new GenesysClient();
/** The ignored fields. */
private static Set<String> ignoredFields;
......@@ -89,10 +89,10 @@ public class CLI {
*
* @param args the arguments
*/
public static void main(String[] args) {
public static void main(final String[] args) {
_log.info("Hello World!");
CLI cli = new CLI();
final CLI cli = new CLI();
cli.loadProperties("client.properties");
cli.run();
}
......@@ -109,24 +109,24 @@ public class CLI {
this.properties.put("access.token", genesysClient.getTokens().getAccessToken());
saveProperties();
} catch (OAuthAuthenticationException e) {
} catch (final OAuthAuthenticationException e) {
_log.error("Failed to fetch /me", e);
authenticate();
} catch (OAuthConnectionException e) {
} catch (final OAuthConnectionException e) {
if (e.getCause() != null && e.getCause() instanceof SocketException) {
_log.error("Could not connect to host");
return;
} else {
_log.error(e.getMessage(), e);
}
} catch (Throwable e) {
} catch (final Throwable e) {
_log.error(e.getMessage(), e);
return;
}
try {
doWork();
} catch (Throwable e) {
} catch (final Throwable e) {
_log.error(e.getMessage(), e);
}
}
......@@ -148,14 +148,15 @@ public class CLI {
System.out.println("Q QUIT");
line = in.nextLine();
if ("1".equals(line))
if ("1".equals(line)) {
doDatasets();
else if ("2".equals(line))
} else if ("2".equals(line)) {
doTraits();
else if ("3".equals(line))
} else if ("3".equals(line)) {
doCrops();
else if ("0".equals(line))
} else if ("0".equals(line)) {
doCustom();
}
} while (!"Q".equalsIgnoreCase(line));
}
......@@ -165,7 +166,7 @@ public class CLI {
* @param label the label
* @param n the n
*/
private void updateJsonData(String label, JsonNode n) {
private void updateJsonData(final String label, final JsonNode n) {
if (n.isArray()) {
updateJsonArray(label, (ArrayNode) n);
} else if (n.isObject()) {
......@@ -179,11 +180,11 @@ public class CLI {
* @param label the label
* @param n the n
*/
private void updateJsonObject(String label, JsonNode n) {
Iterator<Entry<String, JsonNode>> f = n.fields();
private void updateJsonObject(final String label, final JsonNode n) {
final Iterator<Entry<String, JsonNode>> f = n.fields();
while (f.hasNext()) {
Entry<String, JsonNode> field = f.next();
final Entry<String, JsonNode> field = f.next();
System.out.print(label + "." + StringUtils.capitalize(field.getKey()) + ":");
System.out.println(field.getValue());
......@@ -199,7 +200,7 @@ public class CLI {
continue;
}
String val = in.nextLine();
final String val = in.nextLine();
if (!field.getValue().isNull() && val.length() == 0) {
continue;
}
......@@ -223,9 +224,9 @@ public class CLI {
* @param label the label
* @param n the n
*/
private void updateJsonArray(String label, ArrayNode n) {
private void updateJsonArray(final String label, final ArrayNode n) {
System.out.println("Array: " + n);
ArrayNode na = n.arrayNode();
final ArrayNode na = n.arrayNode();
String val;
do {
val = in.nextLine().trim();
......@@ -235,20 +236,20 @@ public class CLI {
na.add(Double.parseDouble(val.substring(2)));
} else if (val.startsWith("o ")) {
try {
Object o = Class.forName("org.genesys2.client.rest.model." + val.substring(2)).newInstance();
JsonNode newNode = mapper.readTree(mapper.writeValueAsString(o));
final Object o = Class.forName("org.genesys2.client.rest.model." + val.substring(2)).newInstance();
final JsonNode newNode = mapper.readTree(mapper.writeValueAsString(o));
System.out.println(newNode);
updateJsonObject(label + "." + val.substring(2), newNode);
na.add(newNode);
} catch (ClassNotFoundException e) {
} catch (final ClassNotFoundException e) {
System.err.println(e.getMessage());
} catch (JsonProcessingException e) {
} catch (final JsonProcessingException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
} catch (final IOException e) {
System.err.println(e.getMessage());
} catch (InstantiationException e) {
} catch (final InstantiationException e) {
System.err.println(e.getMessage());
} catch (IllegalAccessException e) {
} catch (final IllegalAccessException e) {
System.err.println(e.getMessage());
}
} else if (StringUtils.isBlank(val)) {
......@@ -269,11 +270,11 @@ public class CLI {
*/
private void doCustom() throws GenesysApiException {
System.out.print("URL: ");
String url = in.nextLine();
final String url = in.nextLine();
System.out.print("Method [GET]: ");
String method = StringUtils.defaultIfBlank(in.nextLine(), "GET");
final String method = StringUtils.defaultIfBlank(in.nextLine(), "GET");
System.out.println("JSON: ");
String data = StringUtils.defaultIfBlank(in.nextLine(), null);
final String data = StringUtils.defaultIfBlank(in.nextLine(), null);
// Exec
System.out.println(genesysClient.query(Verb.valueOf(method), url, null, data));
......@@ -293,15 +294,15 @@ public class CLI {
System.out.println("0 Back");
line = in.nextLine();
if ("1".equals(line))
if ("1".equals(line)) {
System.out.println("/crops: " + genesysClient.query("/crops"));
else if ("2".equals(line))
} else if ("2".equals(line)) {
updateCrop();
else if ("3".equals(line))
} else if ("3".equals(line)) {
updateCropRules();
else if ("9".equals(line))
} else if ("9".equals(line)) {
System.out.println("/methods: " + genesysClient.query("/methods"));
else if ("0".equalsIgnoreCase(line))
} else if ("0".equalsIgnoreCase(line))
return;
} while (!("0".equalsIgnoreCase(line)));
}
......@@ -313,12 +314,12 @@ public class CLI {
*/
private void updateCropRules() throws GenesysApiException {
System.out.println("Crop shortName: ");
String shortName = in.nextLine().trim();
final String shortName = in.nextLine().trim();
String rules = genesysClient.query("/crops/" + shortName + "/rules");
final String rules = genesysClient.query("/crops/" + shortName + "/rules");
System.out.println("Crop rules: " + rules);
String newRules = StringUtils.defaultIfBlank(in.nextLine().trim(), rules);
final String newRules = StringUtils.defaultIfBlank(in.nextLine().trim(), rules);
if (StringUtils.equals(newRules, rules)) {
System.out.println("No change.");
......@@ -328,15 +329,14 @@ public class CLI {
ArrayNode arr;
try {
arr = (ArrayNode) mapper.readTree(newRules);
} catch (Throwable e) {
} catch (final Throwable e) {
System.err.println(e.getMessage());
return;
}
System.out.println(arr.toString());
// make a crop
System.out.println("New rules: "
+ genesysClient.query(Verb.POST, "/crops/" + shortName + "/rules", null, arr.toString()));
System.out.println("New rules: " + genesysClient.query(Verb.POST, "/crops/" + shortName + "/rules", null, arr.toString()));
}
/**
......@@ -346,13 +346,13 @@ public class CLI {
*/
private void updateCrop() throws GenesysApiException {
System.out.println("Crop shortName: ");
String shortName = in.nextLine().trim();
final String shortName = in.nextLine().trim();
ObjectNode cropJson = null;
try {
cropJson = (ObjectNode) mapper.readTree(genesysClient.getCrop(shortName));
cropJson.remove("rules");
} catch (IOException e) {
} catch (final IOException e) {
System.err.println(e.getMessage());
}
if (cropJson == null) {
......@@ -389,15 +389,15 @@ public class CLI {
System.out.println("0 Back");
line = in.nextLine();
if ("1".equals(line))
if ("1".equals(line)) {
System.out.println("/parameters: " + genesysClient.query("/descriptors"));
else if ("2".equals(line))
} else if ("2".equals(line)) {
System.out.println("/mymethods: " + genesysClient.query("/mymethods"));
else if ("3".equals(line))
} else if ("3".equals(line)) {
addMethod();
else if ("9".equals(line))
} else if ("9".equals(line)) {
System.out.println("/methods: " + genesysClient.query("/methods"));
else if ("0".equalsIgnoreCase(line))
} else if ("0".equalsIgnoreCase(line))
return;
} while (!("0".equalsIgnoreCase(line)));
}
......@@ -417,15 +417,15 @@ public class CLI {
System.out.println("0 Back");
line = in.nextLine();
if ("1".equals(line))
if ("1".equals(line)) {
System.out.println("/datasets: " + genesysClient.query("/datasets"));
else if ("2".equals(line))
} else if ("2".equals(line)) {
addDataset();
else if ("3".equals(line))
} else if ("3".equals(line)) {
addDatasetData();
else if ("4".equals(line))
} else if ("4".equals(line)) {
addDatasetRaw();
else if ("0".equalsIgnoreCase(line))
} else if ("0".equalsIgnoreCase(line))
return;
} while (!("0".equalsIgnoreCase(line)));
}
......@@ -436,7 +436,7 @@ public class CLI {
* @throws GenesysApiException the genesys api exception
*/
private void addMethod() throws GenesysApiException {
ObjectNode datasetJson = mapper.createObjectNode();
final ObjectNode datasetJson = mapper.createObjectNode();
System.out.println("Method description: ");
datasetJson.put("description", in.nextLine());
......@@ -447,7 +447,7 @@ public class CLI {
datasetJson.put("fieldName", StringUtils.defaultIfBlank(in.nextLine(), null));
System.out.println("Field type: (0=String, 1=Double, 2=Long)");
int fieldType = Integer.parseInt(in.nextLine());
final int fieldType = Integer.parseInt(in.nextLine());
datasetJson.put("fieldType", fieldType);
if (fieldType == 0) {
......@@ -474,7 +474,7 @@ public class CLI {
* @throws GenesysApiException the genesys api exception
*/
private void addDataset() throws GenesysApiException {
ObjectNode datasetJson = mapper.createObjectNode();
final ObjectNode datasetJson = mapper.createObjectNode();
System.out.println("WIEWS Code: ");
datasetJson.put("institute", in.nextLine());
......@@ -497,10 +497,10 @@ public class CLI {
* @throws GenesysApiException the genesys api exception
*/
private void addDatasetData() throws GenesysApiException {
ObjectNode datasetJson = mapper.createObjectNode();
final ObjectNode datasetJson = mapper.createObjectNode();
System.out.println("Dataset ID: ");
long datasetId = Long.parseLong(in.nextLine());
final long datasetId = Long.parseLong(in.nextLine());
System.out.println("WIEWS Code: ");
datasetJson.put("instCode", in.nextLine());
......@@ -510,8 +510,7 @@ public class CLI {
System.err.println(datasetJson.toString());
// make a dataset
System.out.println("Put dataset: "
+ genesysClient.query(Verb.PUT, "/datasets/" + datasetId + "/data", null, datasetJson.toString()));
System.out.println("Put dataset: " + genesysClient.query(Verb.PUT, "/datasets/" + datasetId + "/data", null, datasetJson.toString()));
}
/**
......@@ -521,16 +520,15 @@ public class CLI {
*/
private void addDatasetRaw() throws GenesysApiException {
System.out.println("Dataset ID: ");
long datasetId = Long.parseLong(in.nextLine());
final long datasetId = Long.parseLong(in.nextLine());
System.out.println("JSON: ");
String json = in.nextLine();
final String json = in.nextLine();
System.err.println(json);
// make a dataset
System.out.println("Put dataset: "
+ genesysClient.query(Verb.PUT, "/datasets/" + datasetId + "/data", null, json));
System.out.println("Put dataset: " + genesysClient.query(Verb.PUT, "/datasets/" + datasetId + "/data", null, json));
}
/**
......@@ -567,11 +565,11 @@ public class CLI {
* Authenticate.
*/
private void authenticate() {
String authorizationUrl = genesysClient.getAuthorizationUrl();
final String authorizationUrl = genesysClient.getAuthorizationUrl();
System.out.println("Authorization URL: \n" + authorizationUrl);
System.out.print("\nVerifier: ");
String verifierCode = in.nextLine();
final String verifierCode = in.nextLine();
System.out.println();
// Trade the Request Token and Verifier for the Access Token
......@@ -592,7 +590,7 @@ public class CLI {
try {
fis = new FileOutputStream(propertiesFile);
this.properties.store(fis, "OAuth client properties");
} catch (IOException e) {
} catch (final IOException e) {
_log.error(e);
} finally {
IOUtils.closeQuietly(fis);
......@@ -604,11 +602,11 @@ public class CLI {
*
* @param propertiesFileName the properties file name
*/
private void loadProperties(String propertiesFileName) {
private void loadProperties(final String propertiesFileName) {
// .properties file location
propertiesFile = new File(propertiesFileName);
Properties properties = new Properties();
final Properties properties = new Properties();
FileInputStream fis = null;
try {
......@@ -617,9 +615,9 @@ public class CLI {
properties.load(fis);
// Keep properties
this.properties = properties;
} catch (FileNotFoundException e) {
} catch (final FileNotFoundException e) {
_log.warn(e, e);
} catch (IOException e) {
} catch (final IOException e) {
_log.error(e, e);
} finally {
IOUtils.closeQuietly(fis);
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.client.oauth;
......@@ -20,7 +20,7 @@ package org.genesys2.client.oauth;
* General GenesysApiException.
*/
public class GenesysApiException extends Exception {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -6008425446284829953L;
......@@ -35,7 +35,7 @@ public class GenesysApiException extends Exception {
*
* @param string the string
*/
public GenesysApiException(String string) {
public GenesysApiException(final String string) {
super(string);
}
......
/**
* Copyright 2015 Global Crop Diversity Trust
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.client.oauth;
......@@ -25,13 +25,13 @@ import org.scribe.model.Token;
* A place to keep the tokens for the session.
*/
public class GenesysTokens implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 3022586353518887500L;
/** The access token. */
private String accessToken = null;
/** The refresh token. */
private String refreshToken = null;
......@@ -52,7 +52,7 @@ public class GenesysTokens implements Serializable {
*
* @param accessToken the new access token
*/
public void setAccessToken(String accessToken) {
public void setAccessToken(final String accessToken) {
this.accessToken = accessToken;
this._accessToken = new Token(accessToken, "");
}
......@@ -71,7 +71,7 @@ public class GenesysTokens implements Serializable {
*
* @param refreshToken the new refresh token
*/
public void setRefreshToken(String refreshToken) {
public void setRefreshToken(final String refreshToken) {
this.refreshToken = refreshToken;
}
......
/**
* Copyright 2015 Global Crop Diversity Trust
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,26 +12,26 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.client.oauth;
/**
* The Class HttpRedirectException.
*/
public class HttpRedirectException extends GenesysApiException {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The redirect url. */
private String redirectUrl;
private final String redirectUrl;
/**
* Instantiates a new http redirect exception.
*
* @param redirectUrl the redirect url
*/
public HttpRedirectException(String redirectUrl) {
public HttpRedirectException(final String redirectUrl) {
this.redirectUrl = redirectUrl;
}
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,15 +12,15 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.client.oauth;
/**
* Exception for authentication exceptions.
* Exception for authentication exceptions.
*/
public class OAuthAuthenticationException extends GenesysApiException {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = -6008425446284829953L;
......@@ -35,7 +35,7 @@ public class OAuthAuthenticationException extends GenesysApiException {
*