Skip to content

Commit

Permalink
reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhoener committed Feb 4, 2017
1 parent 5ad4841 commit 8fc282b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 72 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/.project
/bin
/.gradle
/gradle
## intellij
/out
/.idea
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/drewhoener/ArtifactRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public class ArtifactRequest {

private String name;

public ArtifactRequest(String name, YamlWrapper wrapper){
public ArtifactRequest(String name, YamlWrapper wrapper) {

this.name = name;

Validate.isTrue(wrapper.hasKey("file_data") && wrapper.isSection("file_data"));

this.artifact = new MavenArtifact(wrapper.getSection("file_data"));

if(wrapper.hasKey("repository_url"))
if (wrapper.hasKey("repository_url"))
this.repositoryUrl = wrapper.getString("repository_url");

this.user = wrapper.getString("user");
Expand All @@ -46,9 +46,9 @@ public ArtifactRequest(String name, YamlWrapper wrapper){

this.replaceExisting = wrapper.getBoolean("replace_existing", true);

if(repositoryUrl.endsWith("/"))
if (repositoryUrl.endsWith("/"))
repositoryUrl = repositoryUrl.substring(0, repositoryUrl.length() - 1);
if(outputFolderName.endsWith(File.separator))
if (outputFolderName.endsWith(File.separator))
outputFolderName = outputFolderName.substring(0, outputFolderName.length() - 1);
}

Expand All @@ -68,7 +68,7 @@ public void downloadArtifact() throws Exception {
rbc.close();
}

public String getLatestVersion() throws Exception{
public String getLatestVersion() throws Exception {

URL url = new URL(this.repositoryUrl + this.artifact.getPath(true) + "/maven-metadata.xml");
HttpURLConnection connection = getCompleteConnection(url);
Expand Down Expand Up @@ -99,8 +99,8 @@ public HttpURLConnection getCompleteConnection(URL url) throws IOException {

HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
if(user != null && password != null)
connection.setRequestProperty ("Authorization", "Basic " + Base64.encodeBase64String((user + ":" + password).getBytes()));
if (user != null && password != null)
connection.setRequestProperty("Authorization", "Basic " + Base64.encodeBase64String((user + ":" + password).getBytes()));

return connection;
}
Expand Down
58 changes: 29 additions & 29 deletions src/main/java/com/drewhoener/Fido.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void main(String[] args) {
String filePath = getArg(args, "--path");
File yamlFile = new File(filePath);

if(!yamlFile.exists() || !yamlFile.getAbsolutePath().toLowerCase().endsWith("yml")){
if (!yamlFile.exists() || !yamlFile.getAbsolutePath().toLowerCase().endsWith("yml")) {
log("Download file not provided!");
log("File: " + yamlFile.getAbsolutePath());
System.exit(1);
Expand All @@ -42,32 +42,6 @@ public static void main(String[] args) {

}

public void parseAndDownload(YamlWrapper yamlWrapper) throws Exception{
List<ArtifactRequest> requestList = new ArrayList<>();
log("");
for(String s : yamlWrapper.getKeys()){
if(yamlWrapper.isSection(s))
try {
requestList.add(new ArtifactRequest(s, yamlWrapper.getSection(s)));
log("Found Key \'" + s + "\' in wrapper");

}catch(Exception e){
e.printStackTrace();
}
}

log("");

for(ArtifactRequest request : requestList){
try {
log("Attempting to download file \'" + request.getOutputFilename() + "\' from Wrapper Request \'" + request.getName() + "\'");
request.downloadArtifact();
}catch(Exception e){
e.printStackTrace();
}
}
}

private static boolean hasArg(String args[], String arg) {
for (String s : args) {
if (s.equals(arg)) {
Expand All @@ -80,14 +54,40 @@ private static boolean hasArg(String args[], String arg) {
private static String getArg(String[] args, String arg) {
for (int i = 0; i < args.length; i++) {
if (args[i].equals(arg) && i < args.length - 1) {
return args[i+1];
return args[i + 1];
}
}
return ""; // Probably shouldn't return null?
}

public static void log(String s){
public static void log(String s) {
System.out.println("[Fido]:\t" + s);
}

public void parseAndDownload(YamlWrapper yamlWrapper) throws Exception {
List<ArtifactRequest> requestList = new ArrayList<>();
log("");
for (String s : yamlWrapper.getKeys()) {
if (yamlWrapper.isSection(s))
try {
requestList.add(new ArtifactRequest(s, yamlWrapper.getSection(s)));
log("Found Key \'" + s + "\' in wrapper");

} catch (Exception e) {
e.printStackTrace();
}
}

log("");

for (ArtifactRequest request : requestList) {
try {
log("Attempting to download file \'" + request.getOutputFilename() + "\' from Wrapper Request \'" + request.getName() + "\'");
request.downloadArtifact();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}
52 changes: 26 additions & 26 deletions src/main/java/com/drewhoener/MavenArtifact.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class MavenArtifact {
private String version;
private String extension;

public MavenArtifact(YamlWrapper wrapper){
public MavenArtifact(YamlWrapper wrapper) {

if(wrapper.hasKey("qualified_name")){
if (wrapper.hasKey("qualified_name")) {
MavenArtifact artifact = parse(wrapper.getString("qualified_name"));
this.groupId = artifact.groupId;
this.artifactId = artifact.artifactId;
Expand All @@ -34,7 +34,7 @@ public MavenArtifact(YamlWrapper wrapper){
this.artifactId = wrapper.getString("artifact");
this.version = wrapper.getString("version");

if(wrapper.hasKey("extension"))
if (wrapper.hasKey("extension"))
this.extension = wrapper.getString("extension");
}
}
Expand All @@ -47,15 +47,29 @@ private MavenArtifact(String groupId, String artifactId, String version, String
this.extension = extension == null ? "jar" : extension;
}

public boolean isSnapshot(){
public static MavenArtifact parse(String str) {
String[] components = str.split(":");
if (components.length < 3)
throw new IllegalArgumentException("Insufficient data!");
String groupId = components[0];
String artifactId = components[1];
String versionNumber = components[components.length - 1];
String extension = null;
if (components.length == 4)
extension = components[2];

return new MavenArtifact(groupId, artifactId, versionNumber, extension);
}

public boolean isSnapshot() {
return this.version.endsWith("-SNAPSHOT");
}

public void setVersion(String version) {
this.version = version;
}

public String getPath(boolean includeVersion){
public String getPath(boolean includeVersion) {

String path = groupId.replaceAll("\\.", "/") + "/" + this.artifactId;
if (includeVersion) {
Expand All @@ -65,53 +79,39 @@ public String getPath(boolean includeVersion){
return "/" + path.trim();
}

public String getURLPortion(String latestVersion){
public String getURLPortion(String latestVersion) {
Validate.notNull(latestVersion);
if(!isSnapshot())
if (!isSnapshot())
latestVersion = this.version;
return this.getPath(true) + "/" + this.artifactId + "-" + latestVersion + "." + this.extension;
}

public String getFilename(String fileName) {

if(fileName == null){
if (fileName == null) {
fileName = generateFileName();
}
File file = new File(fileName);
if(file.isDirectory())
if (file.isDirectory())
return fileName + File.separator + generateFileName();

return fileName;
}

public String getXPathVersion(Document xmlDoc) throws Exception{
public String getXPathVersion(Document xmlDoc) throws Exception {
XPath xpath = XPathFactory.newInstance().newXPath();
if(isSnapshot()) {
if (isSnapshot()) {
String timeStamp = xpath.compile("/metadata/versioning/snapshot/timestamp/text()").evaluate(xmlDoc);
String buildNum = xpath.compile("/metadata/versioning/snapshot/buildNumber/text()").evaluate(xmlDoc);
return this.version.replace("-SNAPSHOT", "").trim() + "-" + timeStamp + "-" + buildNum;
}
return xpath.compile("/metadata/versioning/versions/version[last()]/text()").evaluate(xmlDoc);
}

public String generateFileName(){
public String generateFileName() {
return this.artifactId.toLowerCase() + "." + this.extension.toLowerCase();
}

public static MavenArtifact parse(String str){
String[] components = str.split(":");
if(components.length < 3)
throw new IllegalArgumentException("Insufficient data!");
String groupId = components[0];
String artifactId = components[1];
String versionNumber = components[components.length - 1];
String extension = null;
if(components.length == 4)
extension = components[2];

return new MavenArtifact(groupId, artifactId, versionNumber, extension);
}

@Override
public String toString() {
return "MavenArtifact{" +
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/com/drewhoener/util/YamlWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ public class YamlWrapper {

public Map<String, Object> base;

public YamlWrapper(InputStream stream){
public YamlWrapper(InputStream stream) {
base = ((Map<String, Object>) new Yaml().load(stream));
}

private YamlWrapper(Map<String, Object> base){
private YamlWrapper(Map<String, Object> base) {
this.base = base;
}

public Set<String> getKeys(){
public Set<String> getKeys() {
return base.keySet();
}

public boolean hasKey(String string){
public boolean hasKey(String string) {
return this.base.containsKey(string);
}

public YamlWrapper getSection(String string){
if(!hasKey(string) || !(this.base.get(string) instanceof Map))
public YamlWrapper getSection(String string) {
if (!hasKey(string) || !(this.base.get(string) instanceof Map))
return null;
return new YamlWrapper((Map<String, Object>) this.base.get(string));
}

public boolean isSection(String path){
public boolean isSection(String path) {
return this.base.get(path) instanceof Map;
}

public String getString(String path){
public String getString(String path) {
return this.hasKey(path) ? this.base.get(path).toString() : null;
}

public String getString(String path, String defaultStr){
public String getString(String path, String defaultStr) {
return this.hasKey(path) ? this.base.get(path).toString() : defaultStr;
}

public boolean getBoolean(String path, boolean def) {
if(!hasKey(path))
if (!hasKey(path))
return def;
return (this.base.get(path) instanceof Boolean) ? (Boolean) this.base.get(path) : def;
}
Expand Down

0 comments on commit 8fc282b

Please sign in to comment.