From cbc658b2ba61d9b9275a6c47a11041fc6a44f83a Mon Sep 17 00:00:00 2001 From: Ryan Amari Date: Fri, 22 Nov 2024 14:38:24 -0500 Subject: [PATCH 1/2] ALS-7883: Fix issues from checkmarx report --- .../dbmi/avillach/data/entity/Query.java | 8 +- .../data/repository/BaseRepository.java | 12 +- .../dbmi/avillach/service/SystemService.java | 2 +- pic-sure-initializer/README.md | 33 ------ pic-sure-initializer/pom.xml | 58 ---------- .../hms/dbmi/avillach/PicSureInitializer.java | 103 ------------------ .../src/main/resources/log4j.properties | 8 -- .../hms/dbmi/avillach/HttpClientUtil.java | 51 ++------- .../service/DataProcessingService.java | 1 - .../IllegalArgumentExceptionMapper.java | 6 +- .../mapper/NullPointerExceptionMapper.java | 7 +- .../mapper/PersistenceExceptionMapper.java | 6 +- 12 files changed, 34 insertions(+), 261 deletions(-) delete mode 100644 pic-sure-initializer/README.md delete mode 100644 pic-sure-initializer/pom.xml delete mode 100644 pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java delete mode 100644 pic-sure-initializer/src/main/resources/log4j.properties diff --git a/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java b/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java index 9b06fa14..1b6dd855 100644 --- a/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java +++ b/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java @@ -8,9 +8,13 @@ import javax.persistence.*; import edu.harvard.dbmi.avillach.util.PicSureStatus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @Entity(name = "query") public class Query extends BaseEntity { + + private static final Logger logger = LoggerFactory.getLogger(Query.class); //TODO may not need these two things private Date startTime; @@ -89,7 +93,7 @@ public String getQuery() { outStr += line; } } catch (IOException e) { - e.printStackTrace(); + throw new UncheckedIOException(e); } return outStr; } @@ -106,7 +110,7 @@ public void setQuery(String queryStr) { gzip.close(); this.query = obj.toByteArray(); } catch (IOException e) { - e.printStackTrace(); + throw new UncheckedIOException(e); } } diff --git a/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/repository/BaseRepository.java b/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/repository/BaseRepository.java index 61aae96a..4325b3cd 100644 --- a/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/repository/BaseRepository.java +++ b/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/repository/BaseRepository.java @@ -142,13 +142,8 @@ public List getByColumns(CriteriaQuery query, Root root, Predicate... predica query.select(root); if (predicates != null && predicates.length > 0) query.where(predicates); - try{ - return em().createQuery(query) - .getResultList(); - } catch (PersistenceException e) { - e.printStackTrace(); - return null; - } + return em().createQuery(query) + .getResultList(); } @@ -168,9 +163,6 @@ public T getUniqueResultByColumns(CriteriaQuery query, Root root, Predicate... p .getSingleResult(); } catch (NoResultException e){ return null; - } catch (PersistenceException e){ - e.printStackTrace(); - return null; } } diff --git a/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/SystemService.java b/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/SystemService.java index a9c8fcb2..64895b26 100755 --- a/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/SystemService.java +++ b/pic-sure-api-war/src/main/java/edu/harvard/dbmi/avillach/service/SystemService.java @@ -102,7 +102,7 @@ public String status() { lastStatus = ONE_OR_MORE_COMPONENTS_DEGRADED; } } catch (Exception e) { - e.printStackTrace(); + logger.error("One or more components degraded", e); lastStatus = ONE_OR_MORE_COMPONENTS_DEGRADED; } return lastStatus; diff --git a/pic-sure-initializer/README.md b/pic-sure-initializer/README.md deleted file mode 100644 index b4110638..00000000 --- a/pic-sure-initializer/README.md +++ /dev/null @@ -1,33 +0,0 @@ -This project creates a resource and user initialization command line application. - -Getting started: - -mvn clean install assembly:single -cd target -java -jar pic-sure-initializer-2.0.0-SNAPSHOT-jar-with-dependencies.jar - - -Follow the instructions given in the usage output. - -Example users.json: - -[ - { - "userId":"foobar@bar.com", - "subject":"foobar@bar.com", - "roles":"SYSTEM_USER" - } -] - -Example resources.json: - -[ - { - "name": "Foo Resource", - "description": "This is the Foo resource. The data in this resource was loaded using the Foo loader from the base files obtained from http://foo.project/foodata after curation by Foo-cleaner.", - "baseUrl": "http://localhost:8081/i-pity-da-foo", - "token":"foo" - } -] - - diff --git a/pic-sure-initializer/pom.xml b/pic-sure-initializer/pom.xml deleted file mode 100644 index 24162e53..00000000 --- a/pic-sure-initializer/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - 4.0.0 - - pic-sure-api - edu.harvard.hms.dbmi.avillach - 2.0.0-SNAPSHOT - - pic-sure-initializer - pic-sure-initializer - - - commons-io - commons-io - - - edu.harvard.hms.dbmi.avillach - pic-sure-api-data - ${project.version} - - - junit - junit - test - - - org.apache.httpcomponents - httpclient - compile - - - commons-cli - commons-cli - - - - - ${artifactId} - - - maven-assembly-plugin - 3.1.0 - - - jar-with-dependencies - - - - edu.harvard.hms.dbmi.avillach.PicSureInitializer - - - - - - - - diff --git a/pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java b/pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java deleted file mode 100644 index 61be7f17..00000000 --- a/pic-sure-initializer/src/main/java/edu/harvard/hms/dbmi/avillach/PicSureInitializer.java +++ /dev/null @@ -1,103 +0,0 @@ -package edu.harvard.hms.dbmi.avillach; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; - -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.DefaultParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.HttpClientBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PicSureInitializer -{ - private static String url, users, resources, token; - private static Options options; - private static Logger logger = LoggerFactory.getLogger(PicSureInitializer.class); - - public static void main( String[] args ) throws UnsupportedEncodingException, ClientProtocolException, IOException, ParseException - { - createOptions(); - try { - parseCommandLine(args); - populateUsers(); - populateResources(); - }catch(Exception e) { - e.printStackTrace(); - printHelpAndExit(); - } - } - - private static void populateUsers() throws UnsupportedEncodingException, ClientProtocolException, IOException, URISyntaxException { - logger.error("setting users"); - post("/user", users); - } - - private static void populateResources() throws UnsupportedEncodingException, ClientProtocolException, IOException, URISyntaxException { - logger.error("setting resources"); - post("/resource", resources); - } - - private static void post(String resourcePath, String body) throws UnsupportedEncodingException, IOException, ClientProtocolException, URISyntaxException { - URI baseUri = new URI(url + resourcePath); - HttpClient client = HttpClientBuilder.create().build(); - HttpPost post = new HttpPost(baseUri); - post.setEntity(new StringEntity(body)); - post.addHeader("Authorization", "Bearer " + token); - post.addHeader("Content-type","application/json"); - HttpResponse response = client.execute(post); - if(response.getStatusLine().getStatusCode() != 200) { - logger.error("Response status code not 200: " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase()); - }else { - logger.info("Success"); - } - } - - private static void parseCommandLine(String[] args) throws ParseException, FileNotFoundException, IOException { - CommandLine cmd = new DefaultParser().parse(options, args); - if(cmd.hasOption("h")) { - printHelpAndExit(); - } - if(cmd.hasOption("h") || ( ! (cmd.hasOption("t") && cmd.hasOption("p") && cmd.hasOption("u") && cmd.hasOption("r")))) { - printHelpAndExit(); - } - url = cmd.getOptionValue("p"); - users = EntityUtils.toString(new FileInputStream(cmd.getOptionValue("u")), "UTF-8"); - resources = EntityUtils.toString(new FileInputStream(cmd.getOptionValue("r")), "UTF-8"); - token = cmd.getOptionValue("t"); - } - - private static void createOptions() { - options = new Options(); - options.addOption("p", true, "REQUIRED : The PIC-SURE 2 base URL, this typically ends with \"PICSURE\" and should not have a trailing slash"); - options.addOption("t", true, "REQUIRED : An authorized ROLE_SYSTEM user's token for the PIC-SURE 2 instance pointed at by the base URL"); - options.addOption("r", true, - "REQUIRED : The resources initialization file, the contents of this file should be a JSON array " - + "where each entry is a hash of string keys(name, description, baseUrl, token) and " - + "string values for each of those keys"); - options.addOption("u", true, - "REQUIRED : The users initialization file, the contents of this file should be a JSON array where " - + "each entry is a hash of string keys(userId, subject, roles) and string values for " - + "each of those keys"); - options.addOption("h", false, "Prints this help documentation"); - } - - private static void printHelpAndExit() { - new HelpFormatter().printHelp("java -jar PIC-SURE-2_initializer.jar -h http://pic-sure-2-api/PICSURE -u users.json -r resources.json", options); - System.exit(-1); - } - -} diff --git a/pic-sure-initializer/src/main/resources/log4j.properties b/pic-sure-initializer/src/main/resources/log4j.properties deleted file mode 100644 index 393e0877..00000000 --- a/pic-sure-initializer/src/main/resources/log4j.properties +++ /dev/null @@ -1,8 +0,0 @@ -# Root logger option -log4j.rootLogger=INFO, stdout - -# Direct log messages to stdout -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.Target=System.out -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java b/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java index 2353ad26..5f5b6d04 100644 --- a/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java +++ b/pic-sure-resources/pic-sure-ga4gh-dos/src/main/java/edu/harvard/hms/dbmi/avillach/HttpClientUtil.java @@ -1,31 +1,26 @@ package edu.harvard.hms.dbmi.avillach; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; -import org.apache.commons.io.IOUtils; +import com.fasterxml.jackson.databind.ObjectMapper; +import edu.harvard.dbmi.avillach.util.exception.ResourceCommunicationException; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -import edu.harvard.dbmi.avillach.util.exception.ResourceCommunicationException; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + public class HttpClientUtil { private final static ObjectMapper json = new ObjectMapper(); + private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class); + public static HttpResponse retrieveGetResponse(String uri, String token) { try { HttpClient client = HttpClientBuilder.create().build(); @@ -39,32 +34,6 @@ public static HttpResponse retrieveGetResponse(String uri, String token) { } } - public static HttpResponse retrievePostResponse(String uri, String token) { - try { - HttpClient client = HttpClientBuilder.create().build(); - HttpPost post = new HttpPost(uri); - if (token != null) { - Map clientCredentials = new HashMap(); - clientCredentials.put("BEARER_TOKEN", token); - post.setEntity(new StringEntity(json.writeValueAsString(clientCredentials))); - } - post.setHeader("Content-type","application/json"); - return client.execute(post); - } catch (IOException e) { - throw new ResourceCommunicationException(uri, e); - } - } - - public static List readListFromResponse(HttpResponse response, Class expectedElementType) { - try { - String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); - return json.readValue(responseBody, new TypeReference>() {}); - } catch (IOException e) { - e.printStackTrace(); - return new ArrayList(); - } - } - public static List readDataObjectsFromResponse(HttpResponse response, Class expectedElementType) { try { String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8"); @@ -79,7 +48,7 @@ public static List readDataObjectsFromResponse(HttpResponse response, Cla return json.readValue(jn.get("data_objects").toString(), new TypeReference>() {}); } } catch (IOException e) { - e.printStackTrace(); + logger.error("Error reading object from response, returning empty list", e); return new ArrayList(); } } diff --git a/pic-sure-resources/pic-sure-visualization-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/DataProcessingService.java b/pic-sure-resources/pic-sure-visualization-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/DataProcessingService.java index 845909e4..0e8d656b 100644 --- a/pic-sure-resources/pic-sure-visualization-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/DataProcessingService.java +++ b/pic-sure-resources/pic-sure-visualization-resource/src/main/java/edu/harvard/hms/dbmi/avillach/resource/visualization/service/DataProcessingService.java @@ -283,7 +283,6 @@ private String createXAxisLabel(String title) { return title.substring(title.lastIndexOf(" ") + 1); } catch (IndexOutOfBoundsException e) { logger.error("Error getting cross counts: " + e.getMessage()); - e.printStackTrace(); return title; } } diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/IllegalArgumentExceptionMapper.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/IllegalArgumentExceptionMapper.java index 3cd01885..f7816674 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/IllegalArgumentExceptionMapper.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/IllegalArgumentExceptionMapper.java @@ -1,6 +1,8 @@ package edu.harvard.dbmi.avillach.util.exception.mapper; import edu.harvard.dbmi.avillach.util.response.PICSUREResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; @@ -9,9 +11,11 @@ @Provider public class IllegalArgumentExceptionMapper implements ExceptionMapper{ + private static final Logger logger = LoggerFactory.getLogger(IllegalArgumentExceptionMapper.class); + @Override public Response toResponse(IllegalArgumentException exception) { - exception.printStackTrace(); + logger.error("Uncaught exception", exception); return PICSUREResponse.protocolError(exception.getMessage()); } } diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/NullPointerExceptionMapper.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/NullPointerExceptionMapper.java index e4e8a4d1..92f9f90f 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/NullPointerExceptionMapper.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/NullPointerExceptionMapper.java @@ -1,8 +1,9 @@ package edu.harvard.dbmi.avillach.util.exception.mapper; import edu.harvard.dbmi.avillach.util.response.PICSUREResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @@ -10,9 +11,11 @@ @Provider public class NullPointerExceptionMapper implements ExceptionMapper{ + private static final Logger logger = LoggerFactory.getLogger(NullPointerExceptionMapper.class); + @Override public Response toResponse(NullPointerException exception) { - exception.printStackTrace(); + logger.error("Uncaught exception", exception); return PICSUREResponse.applicationError("An inner problem pops up, no worry, please contact your admin to see the logs in server"); } } diff --git a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/PersistenceExceptionMapper.java b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/PersistenceExceptionMapper.java index 78749ac9..77b94549 100644 --- a/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/PersistenceExceptionMapper.java +++ b/pic-sure-util/src/main/java/edu/harvard/dbmi/avillach/util/exception/mapper/PersistenceExceptionMapper.java @@ -1,6 +1,8 @@ package edu.harvard.dbmi.avillach.util.exception.mapper; import edu.harvard.dbmi.avillach.util.response.PICSUREResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.persistence.PersistenceException; import javax.ws.rs.core.Response; @@ -10,9 +12,11 @@ @Provider public class PersistenceExceptionMapper implements ExceptionMapper{ + private static final Logger logger = LoggerFactory.getLogger(PersistenceExceptionMapper.class); + @Override public Response toResponse(PersistenceException exception) { - exception.printStackTrace(); + logger.error("Uncaught exception", exception); return PICSUREResponse.applicationError(exception.getMessage()); } } From d52a28d2940ed3973506db5b2fc92822dd45c6cd Mon Sep 17 00:00:00 2001 From: Ryan Amari Date: Mon, 25 Nov 2024 08:12:48 -0500 Subject: [PATCH 2/2] ALS-7883: Remove unused logger --- .../java/edu/harvard/dbmi/avillach/data/entity/Query.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java b/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java index 1b6dd855..e937462b 100644 --- a/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java +++ b/pic-sure-api-data/src/main/java/edu/harvard/dbmi/avillach/data/entity/Query.java @@ -8,13 +8,9 @@ import javax.persistence.*; import edu.harvard.dbmi.avillach.util.PicSureStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @Entity(name = "query") public class Query extends BaseEntity { - - private static final Logger logger = LoggerFactory.getLogger(Query.class); //TODO may not need these two things private Date startTime;