diff --git a/src/main/java/fr/packageviewer/ArgParse.java b/src/main/java/fr/packageviewer/ArgParse.java index 1776788..19a33f6 100644 --- a/src/main/java/fr/packageviewer/ArgParse.java +++ b/src/main/java/fr/packageviewer/ArgParse.java @@ -5,18 +5,20 @@ import com.beust.jcommander.JCommander; /** * Class to parse the command line arguments given by the user + * * @author Capelier-Marla */ public class ArgParse { - /* distribution the user want */ + /* distribution the user want */ private static String distribution; - private static String packet; + private static String packet; /** * Get the command line argument given by the user, parse it with the parser and store it in the corresponding variable - * @author Capelier-Marla + * * @param args the command line arguments given by the user + * @author Capelier-Marla */ static void parseArguments(String[] args) { // create JCommander and CommandLineParams objects @@ -24,43 +26,45 @@ public class ArgParse { CommandLineParams params = new CommandLineParams(); // add argument required by the params to the JCommander object jCommander.addObject(params); - try { - // parse the argument from list of String - jCommander.parse(args); - if(params.help) { - jCommander.setProgramName("PackageViewer"); - jCommander.usage(); - System.exit(0); - } else { - // store the argument parsed in the variable - packet = params.packet; - distribution = params.distribution; - } + try { + // parse the argument from list of String + jCommander.parse(args); + if (params.help) { + jCommander.setProgramName("PackageViewer"); + jCommander.usage(); + System.exit(0); + } else { + // store the argument parsed in the variable + packet = params.packet; + distribution = params.distribution; + } - } catch (Exception e) { - // if the parsing failed, print the error message and exit the program - System.out.println("You forgot something, please enter the package name and the distribution name if you want to search in a specific one"); - jCommander.setProgramName("PackageViewer"); - jCommander.usage(); - System.exit(0); - } + } catch (Exception e) { + // if the parsing failed, print the error message and exit the program + System.out.println("You forgot something, please enter the package name and the distribution name if you want to search in a specific one"); + jCommander.setProgramName("PackageViewer"); + jCommander.usage(); + System.exit(0); + } } - /** - * Get the distribution name. If the user didn't give any or if we didn't parse it, return null - * @author Capelier-Marla - * @return String: the distribution name - */ - public static String getDistribution() { - return distribution; - } + /** + * Get the distribution name. If the user didn't give any or if we didn't parse it, return null + * + * @return String: the distribution name + * @author Capelier-Marla + */ + public static String getDistribution() { + return distribution; + } - /** - * Get the packet name, this one isn't optional - * @author Capelier-Marla - * @return String: the packet name - */ - public static String getPacket() { - return packet; - } + /** + * Get the packet name, this one isn't optional + * + * @return String: the packet name + * @author Capelier-Marla + */ + public static String getPacket() { + return packet; + } } diff --git a/src/main/java/fr/packageviewer/CommandLineParams.java b/src/main/java/fr/packageviewer/CommandLineParams.java index d36a866..0590458 100644 --- a/src/main/java/fr/packageviewer/CommandLineParams.java +++ b/src/main/java/fr/packageviewer/CommandLineParams.java @@ -4,29 +4,30 @@ import com.beust.jcommander.Parameter; /** * Class to store and get the command line arguments given by the user + * * @author Capelier-Marla */ public class CommandLineParams { - /** - * Packet the user want to search, only parameter without names - */ - @Parameter(description = "Package to search", - required = true) - public String packet; + /** + * Packet the user want to search, only parameter without names + */ + @Parameter(description = "Package to search", + required = true) + public String packet; - /** - * Distribution the user want to search packages in - */ - @Parameter(names = {"--distro", "-d"}, - description = "Linux distribution to search in") - public String distribution; + /** + * Distribution the user want to search packages in + */ + @Parameter(names = {"--distro", "-d"}, + description = "Linux distribution to search in") + public String distribution; - /** - * Displays the help - */ - @Parameter(names = {"--help", "-h"}, - description = "Display this help", - help = true) - public boolean help = false; + /** + * Displays the help + */ + @Parameter(names = {"--help", "-h"}, + description = "Display this help", + help = true) + public boolean help = false; } diff --git a/src/main/java/fr/packageviewer/DistributionEnum.java b/src/main/java/fr/packageviewer/DistributionEnum.java index 0f2899f..0cf4042 100644 --- a/src/main/java/fr/packageviewer/DistributionEnum.java +++ b/src/main/java/fr/packageviewer/DistributionEnum.java @@ -9,55 +9,59 @@ import java.util.List; /** * Enum containing distribution information to get them by their name + * * @author Capelier-Marla */ public enum DistributionEnum { - ARCH("arch", new ArchDistribution()), - FEDORA("fedora", new FedoraDistribution()), - ; + ARCH("arch", new ArchDistribution()), + FEDORA("fedora", new FedoraDistribution()), + ; - private final String name; - private final Distribution distributionConstructor; + private final String name; + private final Distribution distributionConstructor; - /** - * Constructor for enums - * @param name the name of the distribution - * @param distributionConstructor the instance of the distribution - * @author Capelier-Marla - */ - DistributionEnum(String name, Distribution distributionConstructor) { - this.name = name; - this.distributionConstructor = distributionConstructor; - } + /** + * Constructor for enums + * + * @param name the name of the distribution + * @param distributionConstructor the instance of the distribution + * @author Capelier-Marla + */ + DistributionEnum(String name, Distribution distributionConstructor) { + this.name = name; + this.distributionConstructor = distributionConstructor; + } - /** - * Get the distribution instance for the distribution requested in String - * @param name name of the distribution requested - * @return the instance of the distribution requested - * @author Capelier-Marla - */ - public static Distribution getDistributionConstructorByName(String name) { - // loop for all distributions stored in enum - for(var distrib : values()) { - // return the instance if it's the same as enum name - if(distrib.name.equals(name)) { - return distrib.distributionConstructor; - } - } - return null; - } + /** + * Get the distribution instance for the distribution requested in String + * + * @param name name of the distribution requested + * @return the instance of the distribution requested + * @author Capelier-Marla + */ + public static Distribution getDistributionConstructorByName(String name) { + // loop for all distributions stored in enum + for (var distrib : values()) { + // return the instance if it's the same as enum name + if (distrib.name.equals(name)) { + return distrib.distributionConstructor; + } + } + return null; + } - /** - * Get all distribution instances available in this enum - * @return the list of distribution instances - */ - public static List getAllDistributionsInstances() { - // create the set that will be returned - List result = new ArrayList<>(); - // add all the distribution instances in the set - for(var distrib : values()) { - result.add(distrib.distributionConstructor); - } - return result; - } + /** + * Get all distribution instances available in this enum + * + * @return the list of distribution instances + */ + public static List getAllDistributionsInstances() { + // create the set that will be returned + List result = new ArrayList<>(); + // add all the distribution instances in the set + for (var distrib : values()) { + result.add(distrib.distributionConstructor); + } + return result; + } } diff --git a/src/main/java/fr/packageviewer/LoggerManager.java b/src/main/java/fr/packageviewer/LoggerManager.java index 2c8d54b..fe4dc3a 100644 --- a/src/main/java/fr/packageviewer/LoggerManager.java +++ b/src/main/java/fr/packageviewer/LoggerManager.java @@ -5,7 +5,7 @@ import java.util.logging.*; /** * The LoggerManager class allows for basic debug output management using * Java's default logging class. - * + * * @author R.Thomas * @version 1.0 */ @@ -17,7 +17,7 @@ public class LoggerManager { /** * Static factory for the Logger class - * + * * @param name String, of the logger to create * @return Logger, a new logger */ @@ -41,7 +41,7 @@ public class LoggerManager { Handler handler = new StreamHandler(System.err, new SimpleFormatter()); logger.addHandler(handler); - + logger.setUseParentHandlers(false); return logger; } diff --git a/src/main/java/fr/packageviewer/Pair.java b/src/main/java/fr/packageviewer/Pair.java index c51ab4e..28e5358 100644 --- a/src/main/java/fr/packageviewer/Pair.java +++ b/src/main/java/fr/packageviewer/Pair.java @@ -2,7 +2,7 @@ package fr.packageviewer; /** * The Pair class stores two objects of distinct type - * + * * @author R.Thomas * @version 1.0 */ @@ -19,7 +19,7 @@ public class Pair { /** * Main Constructor for the Pair class - * + * * @param first, the first object to be stored * @param second the second object to be stored */ @@ -30,7 +30,7 @@ public class Pair { /** * Getter for the attribute first - * + * * @return the object stored in the attribute first */ public K getFirst() { @@ -39,7 +39,7 @@ public class Pair { /** * Setter for the attribute first - * + * * @param first Store the given object in the attribute first */ public void setFirst(K first) { @@ -48,7 +48,7 @@ public class Pair { /** * Getter for the attribute second - * + * * @return the object stored in the attribute second */ public V getSecond() { @@ -57,7 +57,7 @@ public class Pair { /** * Setter for the attribute second - * + * * @param second Store the given object in the attribute second */ public void setSecond(V second) { @@ -65,10 +65,10 @@ public class Pair { } /** - * Returns a string representation of the pair - * - * @return String, string representation of the pair - */ + * Returns a string representation of the pair + * + * @return String, string representation of the pair + */ @Override public String toString() { return "Pair{key=%s,value=%s}".formatted(first, second); diff --git a/src/main/java/fr/packageviewer/Searcher.java b/src/main/java/fr/packageviewer/Searcher.java index 50ed3f8..58076df 100644 --- a/src/main/java/fr/packageviewer/Searcher.java +++ b/src/main/java/fr/packageviewer/Searcher.java @@ -20,6 +20,7 @@ public class Searcher { /** * Get the list of all packages in the distribution set before + * * @param packageName the name of the package wanted * @return the list of all packages found * @author Capelier-Marla @@ -28,11 +29,11 @@ public class Searcher { // we add all instanced constructors in a list, only one if defined at creation of the object List distributions; - if(distributionName == null) { + if (distributionName == null) { distributions = DistributionEnum.getAllDistributionsInstances(); } else { distributions = Collections.singletonList(DistributionEnum.getDistributionConstructorByName(distributionName)); - if(distributions.get(0) == null) { + if (distributions.get(0) == null) { System.out.println("Distribution non trouvée"); System.exit(0); } @@ -48,7 +49,7 @@ public class Searcher { } // we get all packages waiting for them to be received - for(Future> futurePackageList : listFuturePackagesList ) { + for (Future> futurePackageList : listFuturePackagesList) { try { List tempList = futurePackageList.get(); allPackages.addAll(tempList); @@ -61,7 +62,7 @@ public class Searcher { public Package getPackage(SearchedPackage packetInput) { - if(distributionName == null) { + if (distributionName == null) { distributionName = packetInput.getDistribution(); } String packageName = packetInput.getName(); diff --git a/src/main/java/fr/packageviewer/distribution/ArchDistribution.java b/src/main/java/fr/packageviewer/distribution/ArchDistribution.java index 34ea960..0eb2f70 100644 --- a/src/main/java/fr/packageviewer/distribution/ArchDistribution.java +++ b/src/main/java/fr/packageviewer/distribution/ArchDistribution.java @@ -19,129 +19,129 @@ import java.util.logging.Logger; /** * This class handles package requests for Arch linux. All return objects in * this class are wrapped by a CompletableFuture to ensure async workload. - * + * * @author C.Marla, R.Thomas * @version 1.0 */ public class ArchDistribution extends AsyncRequestsParser implements Distribution { - /** - * Logger object used to split debug output and the application output - */ - private static final Logger logger = LoggerManager.getLogger("ArchDistribution"); + /** + * Logger object used to split debug output and the application output + */ + private static final Logger logger = LoggerManager.getLogger("ArchDistribution"); - /** - * This method remove all characters in the first string passed as - * parameter after one of the character in the second string if found - * in the first string - * - * @param str String, the string to trim - * @param trimAfterCharacters String, the character that delimits our string - * @return the string after being trimmed - */ - private static String trimAfterCharacters(String str, String trimAfterCharacters) { - for (char c : trimAfterCharacters.toCharArray()) { - int index = str.indexOf(c); - if (index > 0) - str = str.substring(index); - } - return str; - } + /** + * This method remove all characters in the first string passed as + * parameter after one of the character in the second string if found + * in the first string + * + * @param str String, the string to trim + * @param trimAfterCharacters String, the character that delimits our string + * @return the string after being trimmed + */ + private static String trimAfterCharacters(String str, String trimAfterCharacters) { + for (char c : trimAfterCharacters.toCharArray()) { + int index = str.indexOf(c); + if (index > 0) + str = str.substring(index); + } + return str; + } - /** - * This function return a package from arch package api in the form of a Pair - * Composed of a Package object, and a set of string containing the names of - * the dependencies of the package. - * - * @param packageName String, The package's exact name - * @return Pair of Package and Set of String - */ - @Override - public CompletableFuture>> getPackageFromAPI(String packageName) { - // create a new http client and make a request to the arch research api - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest - .newBuilder(URI.create("https://archlinux.org/packages/search/json/?name=" + packageName)).build(); + /** + * This function return a package from arch package api in the form of a Pair + * Composed of a Package object, and a set of string containing the names of + * the dependencies of the package. + * + * @param packageName String, The package's exact name + * @return Pair of Package and Set of String + */ + @Override + public CompletableFuture>> getPackageFromAPI(String packageName) { + // create a new http client and make a request to the arch research api + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest + .newBuilder(URI.create("https://archlinux.org/packages/search/json/?name=" + packageName)).build(); - CompletableFuture>> futureResult = new CompletableFuture<>(); - // send the request and when there's a response - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { - //parse the json response - JSONObject json = new JSONObject(result.body()); - - // check if the response contains something - JSONArray resultsArrayJson = json.getJSONArray("results"); - if (resultsArrayJson.length() == 0) { - // unknown package, probably an abstract dependency - futureResult.complete(null); - return; - } - JSONObject resultJson = resultsArrayJson.getJSONObject(0); - Set dependenciesNames = new HashSet<>(); - // parse dependencies without version requirements (bash >= 3.0) -> (bash) - for (Object dependency : resultJson.getJSONArray("depends")) { - dependenciesNames.add(trimAfterCharacters((String) dependency, "<>=")); - } - //Create the package and store it and its set of deps in a pair - futureResult.complete(new Pair<>( - new Package( - resultJson.getString("pkgname"), - resultJson.getString("pkgver"), - resultJson.getString("repo"), - resultJson.getString("pkgdesc"), - "arch"), - dependenciesNames)); - }).exceptionally(error -> { - error.printStackTrace(); - logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error)); - futureResult.complete(null); - return null; - }); + CompletableFuture>> futureResult = new CompletableFuture<>(); + // send the request and when there's a response + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { + //parse the json response + JSONObject json = new JSONObject(result.body()); - return futureResult; + // check if the response contains something + JSONArray resultsArrayJson = json.getJSONArray("results"); + if (resultsArrayJson.length() == 0) { + // unknown package, probably an abstract dependency + futureResult.complete(null); + return; + } + JSONObject resultJson = resultsArrayJson.getJSONObject(0); + Set dependenciesNames = new HashSet<>(); + // parse dependencies without version requirements (bash >= 3.0) -> (bash) + for (Object dependency : resultJson.getJSONArray("depends")) { + dependenciesNames.add(trimAfterCharacters((String) dependency, "<>=")); + } + //Create the package and store it and its set of deps in a pair + futureResult.complete(new Pair<>( + new Package( + resultJson.getString("pkgname"), + resultJson.getString("pkgver"), + resultJson.getString("repo"), + resultJson.getString("pkgdesc"), + "arch"), + dependenciesNames)); + }).exceptionally(error -> { + error.printStackTrace(); + logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error)); + futureResult.complete(null); + return null; + }); - } + return futureResult; - /** - * Search for a package matching a pattern and return a list of packages and - * return a list of string matching this pattern. - * - * @param packageName String, the pattern to search in the repositories - * @return List of SearchedPackage objects - */ - public CompletableFuture> searchPackage(String packageName) { - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create("https://archlinux.org/packages/search/json/?q=" + packageName)) - .build(); + } - CompletableFuture> futureSearchedPackages = new CompletableFuture<>(); + /** + * Search for a package matching a pattern and return a list of packages and + * return a list of string matching this pattern. + * + * @param packageName String, the pattern to search in the repositories + * @return List of SearchedPackage objects + */ + public CompletableFuture> searchPackage(String packageName) { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create("https://archlinux.org/packages/search/json/?q=" + packageName)) + .build(); - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { + CompletableFuture> futureSearchedPackages = new CompletableFuture<>(); - List searchedPackagesList = new ArrayList<>(); - JSONObject json = new JSONObject(result.body()); + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { - // iterate for every package in the list - for (Object searchResultObj : json.getJSONArray("results")) { - // convert object into String - JSONObject searchResultJson = (JSONObject) searchResultObj; - // add package into to list - searchedPackagesList.add(new SearchedPackage( - searchResultJson.getString("pkgname"), - searchResultJson.getString("pkgver"), - searchResultJson.getString("repo"), - searchResultJson.getString("pkgdesc"), - "arch")); - } - futureSearchedPackages.complete(searchedPackagesList); - }).exceptionally(error -> { - error.printStackTrace(); - futureSearchedPackages.complete(Collections.emptyList()); - return null; - }); + List searchedPackagesList = new ArrayList<>(); + JSONObject json = new JSONObject(result.body()); - return futureSearchedPackages; + // iterate for every package in the list + for (Object searchResultObj : json.getJSONArray("results")) { + // convert object into String + JSONObject searchResultJson = (JSONObject) searchResultObj; + // add package into to list + searchedPackagesList.add(new SearchedPackage( + searchResultJson.getString("pkgname"), + searchResultJson.getString("pkgver"), + searchResultJson.getString("repo"), + searchResultJson.getString("pkgdesc"), + "arch")); + } + futureSearchedPackages.complete(searchedPackagesList); + }).exceptionally(error -> { + error.printStackTrace(); + futureSearchedPackages.complete(Collections.emptyList()); + return null; + }); - } + return futureSearchedPackages; + + } } diff --git a/src/main/java/fr/packageviewer/distribution/Distribution.java b/src/main/java/fr/packageviewer/distribution/Distribution.java index 127ea0a..7b59920 100644 --- a/src/main/java/fr/packageviewer/distribution/Distribution.java +++ b/src/main/java/fr/packageviewer/distribution/Distribution.java @@ -8,7 +8,7 @@ import java.util.concurrent.Future; /** * This interface specifies the methods needed by a distribution to be parsable. - * + * * @author R.Thomas * @version 1.0 */ @@ -16,7 +16,7 @@ public interface Distribution { /** * Search for a package matching a pattern and return a list of packages and * return a list of string matching this pattern. - * + * * @param packageName String, the pattern to search in the repositories * @return List of SearchedPackage objects */ @@ -28,7 +28,7 @@ public interface Distribution { * parameter, the package contains in its dependency list fully formed * packages that also contains its dependencies, the dependency depth is * specified by the parameter with the same name. - * + * * @param packageName String, The package's exact name * @param depth int, the depth of the dependency tree * @return Package, the fully completed package diff --git a/src/main/java/fr/packageviewer/distribution/FedoraDistribution.java b/src/main/java/fr/packageviewer/distribution/FedoraDistribution.java index 9ded657..3577275 100644 --- a/src/main/java/fr/packageviewer/distribution/FedoraDistribution.java +++ b/src/main/java/fr/packageviewer/distribution/FedoraDistribution.java @@ -24,128 +24,128 @@ import java.util.logging.Logger; */ public class FedoraDistribution extends AsyncRequestsParser implements Distribution { - /** - * Logger object used to split debug output and the application output - */ - private static final Logger logger = LoggerManager.getLogger("FedoraDistribution"); + /** + * Logger object used to split debug output and the application output + */ + private static final Logger logger = LoggerManager.getLogger("FedoraDistribution"); - /** - * This function return a package from Fedora metadata api in the form of a - * Pair Composed of a Package object, and a set of string containing the - * names of the dependencies of the package. - * - * @param packageName String, The package's exact name - * @return Pair of Package and Set of String - */ - protected CompletableFuture>> getPackageFromAPI(String packageName) { - // create a new http client - HttpClient client = HttpClient.newHttpClient(); - // and create its url - String url = "https://mdapi.fedoraproject.org/rawhide/pkg/" + packageName + ""; - HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build(); + /** + * This function return a package from Fedora metadata api in the form of a + * Pair Composed of a Package object, and a set of string containing the + * names of the dependencies of the package. + * + * @param packageName String, The package's exact name + * @return Pair of Package and Set of String + */ + protected CompletableFuture>> getPackageFromAPI(String packageName) { + // create a new http client + HttpClient client = HttpClient.newHttpClient(); + // and create its url + String url = "https://mdapi.fedoraproject.org/rawhide/pkg/" + packageName + ""; + HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build(); - CompletableFuture>> futureResult = new CompletableFuture<>(); - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { + CompletableFuture>> futureResult = new CompletableFuture<>(); + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { - String body = result.body(); + String body = result.body(); - if (body.contains("404: Not Found")) { - futureResult.complete(null); - return; - } + if (body.contains("404: Not Found")) { + futureResult.complete(null); + return; + } - JSONObject json = new JSONObject(result.body()); + JSONObject json = new JSONObject(result.body()); - // get infos - Set dependenciesNames = new HashSet<>(); + // get infos + Set dependenciesNames = new HashSet<>(); - for (Object depPackageObj : json.getJSONArray("requires")) { - // convert object into String - JSONObject depPackageJson = (JSONObject) depPackageObj; - // add package into Package List - String depName = depPackageJson.getString("name"); - if (depName.contains(".so")) - continue; - if (depName.contains("/")) - continue; - dependenciesNames.add(depName); - } + for (Object depPackageObj : json.getJSONArray("requires")) { + // convert object into String + JSONObject depPackageJson = (JSONObject) depPackageObj; + // add package into Package List + String depName = depPackageJson.getString("name"); + if (depName.contains(".so")) + continue; + if (depName.contains("/")) + continue; + dependenciesNames.add(depName); + } - futureResult.complete(new Pair<>( - new Package( - json.getString("basename"), - json.getString("version"), - "rawhide", - json.getString("summary"), - "fedora" - ), - dependenciesNames - )); - }).exceptionally(error->{ - error.printStackTrace(); - logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error)); - futureResult.complete(null); - return null; - }); - // if there's an error, return an empty string - return futureResult; - } + futureResult.complete(new Pair<>( + new Package( + json.getString("basename"), + json.getString("version"), + "rawhide", + json.getString("summary"), + "fedora" + ), + dependenciesNames + )); + }).exceptionally(error -> { + error.printStackTrace(); + logger.warning("Error while fetching package %s from the API : \n%s".formatted(packageName, error)); + futureResult.complete(null); + return null; + }); + // if there's an error, return an empty string + return futureResult; + } - /** - * Search for a package matching a pattern and return a list of packages and - * return a list of string matching this pattern. - * - * @param packageName String, the pattern to search in the repositories - * @return List of SearchedPackage objects - */ - @Override - public CompletableFuture> searchPackage(String packageName) { - // create a new http client and make a request to the fedora research api - HttpClient client = HttpClient.newHttpClient(); - HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create( - "https://src.fedoraproject.org/api/0/projects?namepace=rpms&per_page=100&short=true&pattern=*" - + packageName + "*")) - .build(); + /** + * Search for a package matching a pattern and return a list of packages and + * return a list of string matching this pattern. + * + * @param packageName String, the pattern to search in the repositories + * @return List of SearchedPackage objects + */ + @Override + public CompletableFuture> searchPackage(String packageName) { + // create a new http client and make a request to the fedora research api + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create( + "https://src.fedoraproject.org/api/0/projects?namepace=rpms&per_page=100&short=true&pattern=*" + + packageName + "*")) + .build(); - CompletableFuture> futureSearchedPackages = new CompletableFuture<>(); + CompletableFuture> futureSearchedPackages = new CompletableFuture<>(); - // send the request and when there's a response - client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { - //parse the json response - JSONObject json = new JSONObject(result.body()); + // send the request and when there's a response + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(result -> { + //parse the json response + JSONObject json = new JSONObject(result.body()); - List searchedPackagesList = new ArrayList<>(); + List searchedPackagesList = new ArrayList<>(); - // iterate for every package in the list - for (Object searchResultObj : json.getJSONArray("projects")) { - // convert object into String - JSONObject searchResultJson = (JSONObject) searchResultObj; + // iterate for every package in the list + for (Object searchResultObj : json.getJSONArray("projects")) { + // convert object into String + JSONObject searchResultJson = (JSONObject) searchResultObj; - // get infos - String name = searchResultJson.getString("name"); + // get infos + String name = searchResultJson.getString("name"); - // do not include fork projects in the list - if(!name.startsWith("fork/")){ + // do not include fork projects in the list + if (!name.startsWith("fork/")) { - // add package into to list - searchedPackagesList.add(new SearchedPackage( - name, - null, - "rawhide", - searchResultJson.getString("description"), - "fedora" - )); - } + // add package into to list + searchedPackagesList.add(new SearchedPackage( + name, + null, + "rawhide", + searchResultJson.getString("description"), + "fedora" + )); + } - } - futureSearchedPackages.complete(searchedPackagesList); - }).exceptionally(error -> { - error.printStackTrace(); - futureSearchedPackages.complete(Collections.emptyList()); - return null; - }); + } + futureSearchedPackages.complete(searchedPackagesList); + }).exceptionally(error -> { + error.printStackTrace(); + futureSearchedPackages.complete(Collections.emptyList()); + return null; + }); - return futureSearchedPackages; - } + return futureSearchedPackages; + } } diff --git a/src/main/java/fr/packageviewer/frontend/Frontend.java b/src/main/java/fr/packageviewer/frontend/Frontend.java index 5365242..e95b36e 100644 --- a/src/main/java/fr/packageviewer/frontend/Frontend.java +++ b/src/main/java/fr/packageviewer/frontend/Frontend.java @@ -6,6 +6,7 @@ import fr.packageviewer.pack.SearchedPackage; import java.util.List; public interface Frontend { - SearchedPackage askUserToChoosePackage(List packets); - void showPackageTree(Package packet, int depth); + SearchedPackage askUserToChoosePackage(List packets); + + void showPackageTree(Package packet, int depth); } diff --git a/src/main/java/fr/packageviewer/frontend/FrontendFactory.java b/src/main/java/fr/packageviewer/frontend/FrontendFactory.java index 6a5d4a1..253f12e 100644 --- a/src/main/java/fr/packageviewer/frontend/FrontendFactory.java +++ b/src/main/java/fr/packageviewer/frontend/FrontendFactory.java @@ -2,15 +2,11 @@ package fr.packageviewer.frontend; public class FrontendFactory { - public static Frontend get(String name){ - switch(name){ - case "terminal":{ - return new FrontendTerminal(); - } - default:{ - throw new IllegalArgumentException("Invalid frontend"); - } - } - } + public static Frontend get(String name) { + if (name.equals("terminal")) { + return new FrontendTerminal(); + } + throw new IllegalArgumentException("Invalid frontend"); + } } diff --git a/src/main/java/fr/packageviewer/frontend/FrontendTerminal.java b/src/main/java/fr/packageviewer/frontend/FrontendTerminal.java index 48d283b..6407c24 100644 --- a/src/main/java/fr/packageviewer/frontend/FrontendTerminal.java +++ b/src/main/java/fr/packageviewer/frontend/FrontendTerminal.java @@ -6,15 +6,16 @@ import fr.packageviewer.pack.SearchedPackage; import java.util.List; import java.util.Scanner; -public class FrontendTerminal implements Frontend{ +public class FrontendTerminal implements Frontend { - /** + /** * Check if the String given is a number + * * @param i the String given * @return true if the String is a number * @author Capelier-Marla */ - private static boolean isNumeric(String i) { + private static boolean isNumeric(String i) { try { Integer.parseInt(i); return true; @@ -23,21 +24,21 @@ public class FrontendTerminal implements Frontend{ } } - @Override - public SearchedPackage askUserToChoosePackage(List packets) { - // list all packages in reverse order + @Override + public SearchedPackage askUserToChoosePackage(List packets) { + // list all packages in reverse order for (int i = packets.size(); i-- > 0; ) { SearchedPackage searchedPacket = packets.get(i); System.out.printf("%s - %s/%s/%s %s%n\t%s%n", - i, - searchedPacket.getDistribution(), - searchedPacket.getRepo(), - searchedPacket.getName(), - searchedPacket.getVersion()==null?"":searchedPacket.getVersion(), - searchedPacket.getDescription()); + i, + searchedPacket.getDistribution(), + searchedPacket.getRepo(), + searchedPacket.getName(), + searchedPacket.getVersion() == null ? "" : searchedPacket.getVersion(), + searchedPacket.getDescription()); } - System.out.printf("Pick a package to see in details (0-%s) : ", packets.size()-1); + System.out.printf("Pick a package to see in details (0-%s) : ", packets.size() - 1); Scanner input = new Scanner(System.in); // we create vars for the loop @@ -49,9 +50,9 @@ public class FrontendTerminal implements Frontend{ packetNumberString = input.nextLine(); // reset notValid to false, we set it in true only if something is wrong notValid = false; - if(isNumeric(packetNumberString)) { + if (isNumeric(packetNumberString)) { packetNumber = Integer.parseInt(packetNumberString); - if(packetNumber < 0 || packetNumber >= packets.size()) { + if (packetNumber < 0 || packetNumber >= packets.size()) { // this number is too big or too small System.out.println("Enter a valid number"); notValid = true; @@ -61,22 +62,22 @@ public class FrontendTerminal implements Frontend{ System.out.println("Enter a valid number"); notValid = true; } - } while(notValid); + } while (notValid); input.close(); - return packets.get(packetNumber); - } + return packets.get(packetNumber); + } - @Override - public void showPackageTree(Package packet, int depth) { + @Override + public void showPackageTree(Package packet, int depth) { System.out.printf("%s%s / %s : %s%n", - " ".repeat(depth), - packet.getName(), - packet.getVersion(), - packet.getDescription()); - for (Package dep : packet.getDeps()) { - showPackageTree(dep, depth+1); - } - } + " ".repeat(depth), + packet.getName(), + packet.getVersion(), + packet.getDescription()); + for (Package dep : packet.getDeps()) { + showPackageTree(dep, depth + 1); + } + } } diff --git a/src/main/java/fr/packageviewer/pack/Package.java b/src/main/java/fr/packageviewer/pack/Package.java index ea2eefd..76914e8 100644 --- a/src/main/java/fr/packageviewer/pack/Package.java +++ b/src/main/java/fr/packageviewer/pack/Package.java @@ -5,73 +5,73 @@ import java.util.List; /** * The Package class stores all metadata needed for a fully completed package. - * + * * @author C.Marla, R.Thomas, S.Djalim * @version 1.0 */ public class Package extends SearchedPackage { - /** - * List of package storing all the dependencies of the package - */ - private final List deps; + /** + * List of package storing all the dependencies of the package + */ + private final List deps; - /** - * Getter for the deps attribute - * - * @return List, List of package storing all the dependencies of the package - */ - public List getDeps() { - return deps; - } + /** + * Getter for the deps attribute + * + * @return List, List of package storing all the dependencies of the package + */ + public List getDeps() { + return deps; + } - /** - * This method adds to the dependency list the package passed as parameter. - * - * @param pack Package, the package to add as dependency - */ - public void addDep(Package pack) { - deps.add(pack); - } + /** + * This method adds to the dependency list the package passed as parameter. + * + * @param pack Package, the package to add as dependency + */ + public void addDep(Package pack) { + deps.add(pack); + } - /** - * Second constructor for the Package class, allows to create a package - * without supplying a list of dependencies. - * - * @param name String, name of the package - * @param version String, version of the package - * @param repo String, repository where the package is located - * @param description String, description of the package - * @param distribution String, the distribution where this specific package - * belongs - */ - public Package(String name, String version, String repo, String description, String distribution) { - this(name, version, repo, description, distribution, new ArrayList<>()); - } + /** + * Second constructor for the Package class, allows to create a package + * without supplying a list of dependencies. + * + * @param name String, name of the package + * @param version String, version of the package + * @param repo String, repository where the package is located + * @param description String, description of the package + * @param distribution String, the distribution where this specific package + * belongs + */ + public Package(String name, String version, String repo, String description, String distribution) { + this(name, version, repo, description, distribution, new ArrayList<>()); + } - /** - * Main constructor for the Package class - * - * @param name String, name of the package - * @param version String, version of the package - * @param repo String, repository where the package is located - * @param description String, description of the package - * @param distribution String, the distribution where this specific package - * belongs - * @param deps List of Package, dependencies of the package - */ - public Package(String name, String version, String repo, String description, String distribution, - List deps) { - super(name, version, repo, description, distribution); - this.deps = deps; - } + /** + * Main constructor for the Package class + * + * @param name String, name of the package + * @param version String, version of the package + * @param repo String, repository where the package is located + * @param description String, description of the package + * @param distribution String, the distribution where this specific package + * belongs + * @param deps List of Package, dependencies of the package + */ + public Package(String name, String version, String repo, String description, String distribution, + List deps) { + super(name, version, repo, description, distribution); + this.deps = deps; + } - /** - * Returns a string representation of the package - * - * @return String, string representation of the package - */ - @Override - public String toString() { - return "Package{%s,deps=%s}".formatted(super.toString(), deps); - } + /** + * Returns a string representation of the package + * + * @return String, string representation of the package + */ + @Override + public String toString() { + return "Package{%s,deps=%s}".formatted(super.toString(), deps); + } } diff --git a/src/main/java/fr/packageviewer/pack/SearchedPackage.java b/src/main/java/fr/packageviewer/pack/SearchedPackage.java index 532b924..a964974 100644 --- a/src/main/java/fr/packageviewer/pack/SearchedPackage.java +++ b/src/main/java/fr/packageviewer/pack/SearchedPackage.java @@ -3,103 +3,103 @@ package fr.packageviewer.pack; /** * The SearchedPackage class stores metadata found when searching for a * package. - * + * * @author C.Marla, R.Thomas, S.Djalim * @version 1.0 */ public class SearchedPackage { - /** - * Name of the package - */ - private final String name; - /** - * Version of the package - */ - private final String version; - /** - * Repository where the package is located - */ - private final String repo; - /** - * Description of the package - */ - private final String description; - /** - * Distribution where this specific package belongs - */ - private final String distribution; + /** + * Name of the package + */ + private final String name; + /** + * Version of the package + */ + private final String version; + /** + * Repository where the package is located + */ + private final String repo; + /** + * Description of the package + */ + private final String description; + /** + * Distribution where this specific package belongs + */ + private final String distribution; - /** - * Getter for the name attribute - * - * @return String, the name of the package - */ - public String getName() { - return name; - } + /** + * Getter for the name attribute + * + * @return String, the name of the package + */ + public String getName() { + return name; + } - /** - * Getter for the version attribute - * - * @return String, the version of the package - */ - public String getVersion() { - return version; - } + /** + * Getter for the version attribute + * + * @return String, the version of the package + */ + public String getVersion() { + return version; + } - /** - * Getter for the repo attribute - * - * @return String, repository where the package is located - */ - public String getRepo() { - return repo; - } + /** + * Getter for the repo attribute + * + * @return String, repository where the package is located + */ + public String getRepo() { + return repo; + } - /** - * Getter for the description attribute - * - * @return String, Description of the package - */ - public String getDescription() { - return description; - } + /** + * Getter for the description attribute + * + * @return String, Description of the package + */ + public String getDescription() { + return description; + } - /** - * Getter for the distribution attribute - * - * @return String, distribution where this specific package belongs - */ - public String getDistribution() { - return distribution; - } + /** + * Getter for the distribution attribute + * + * @return String, distribution where this specific package belongs + */ + public String getDistribution() { + return distribution; + } - /** - * Constructor for the SearchedPackage class - * - * @param name String, name of the package - * @param version String, version of the package - * @param repo String, repository where the package is located - * @param description String, description of the package - * @param distribution String, the distribution where this specific package - * belongs - */ - public SearchedPackage(String name, String version, String repo, String description, String distribution) { - this.name = name; - this.version = version; - this.repo = repo; - this.description = description; - this.distribution = distribution; - } + /** + * Constructor for the SearchedPackage class + * + * @param name String, name of the package + * @param version String, version of the package + * @param repo String, repository where the package is located + * @param description String, description of the package + * @param distribution String, the distribution where this specific package + * belongs + */ + public SearchedPackage(String name, String version, String repo, String description, String distribution) { + this.name = name; + this.version = version; + this.repo = repo; + this.description = description; + this.distribution = distribution; + } - /** - * Returns a string representation of the package - * - * @return String, string representation of the package - */ - @Override - public String toString() { - return "SearchedPackage{name=%s,version=%s,repo=%s,description=%s,distribution=%s}".formatted(name, version, - repo, description, distribution); - } + /** + * Returns a string representation of the package + * + * @return String, string representation of the package + */ + @Override + public String toString() { + return "SearchedPackage{name=%s,version=%s,repo=%s,description=%s,distribution=%s}".formatted(name, version, + repo, description, distribution); + } } diff --git a/src/main/java/fr/packageviewer/parser/AsyncRequestsParser.java b/src/main/java/fr/packageviewer/parser/AsyncRequestsParser.java index 6bd774f..ab455a6 100644 --- a/src/main/java/fr/packageviewer/parser/AsyncRequestsParser.java +++ b/src/main/java/fr/packageviewer/parser/AsyncRequestsParser.java @@ -15,10 +15,9 @@ import java.util.logging.Logger; * This abstract class defines the method that a distribution will use * in order to get a package and fill its dependency list. It does all that * in an asynchronous manner - * + * * @author R.Thomas * @version 1.0 - * */ public abstract class AsyncRequestsParser { /** @@ -30,7 +29,7 @@ public abstract class AsyncRequestsParser { * This function returns a package from the distribution's api in the form * of a Pair Composed of a Package object and a set of string containing * the names of the dependencies of the package. - * + * * @param packageName String, The package's exact name * @return Pair of Package and Set of String */ @@ -42,7 +41,7 @@ public abstract class AsyncRequestsParser { * parameter, the package contains in its dependency list fully formed * packages that also contains its dependencies, the dependency depth is * specified by the parameter with the same name. - * + * * @param packageName String, The package's exact name * @param depth int, the depth of the dependency tree * @return Package, the fully completed package diff --git a/src/test/java/fr/packageviewer/DistroTest.java b/src/test/java/fr/packageviewer/DistroTest.java index 2f63cf9..d80a1bc 100644 --- a/src/test/java/fr/packageviewer/DistroTest.java +++ b/src/test/java/fr/packageviewer/DistroTest.java @@ -14,19 +14,21 @@ import java.util.concurrent.Future; public abstract class DistroTest { protected abstract T createInstance(); - protected List helperSearchPackage(String packageName){ + + protected List helperSearchPackage(String packageName) { Distribution distribution = createInstance(); Future> future = distribution.searchPackage(packageName); - try{ + try { return future.get(); } catch (ExecutionException | InterruptedException e) { throw new RuntimeException(e); } } - protected Package helperGetPackageTree(String packageName, int depth){ + + protected Package helperGetPackageTree(String packageName, int depth) { Distribution distribution = createInstance(); Future future = distribution.getPackageTree(packageName, depth); - try{ + try { return future.get(); } catch (ExecutionException | InterruptedException e) { throw new RuntimeException(e); @@ -34,7 +36,7 @@ public abstract class DistroTest { } @Test - public void testBasicQueryDoNotFail(){ + public void testBasicQueryDoNotFail() { helperGetPackageTree("bash", 0); } @@ -56,7 +58,7 @@ public abstract class DistroTest { public void testQueryWithDepth1hasOneLevelOfDeps() { Package pack = helperGetPackageTree("bash", 1); Assertions.assertNotEquals(pack.getDeps().size(), 0); - for(Package dep : pack.getDeps()){ + for (Package dep : pack.getDeps()) { Assertions.assertEquals(dep.getDeps().size(), 0); } } @@ -83,10 +85,11 @@ public abstract class DistroTest { public void testThatBashSearchReturnsResults() { Assertions.assertNotEquals(helperSearchPackage("bash").size(), 0); } + @Test public void testThatBashSearchContainsBash() { - for(SearchedPackage pack : helperSearchPackage("bash")){ - if(pack.getName().equals("bash")){ + for (SearchedPackage pack : helperSearchPackage("bash")) { + if (pack.getName().equals("bash")) { Assertions.assertTrue(true); return; }