Skip to content

Commit

Permalink
#4: Refactoring around credential support & downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics committed Dec 1, 2018
1 parent f0e5921 commit bc536e6
Show file tree
Hide file tree
Showing 22 changed files with 581 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import io.redskap.swagger.brake.runner.Options;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class LatestArtifactDownloaderFactory {
private final Maven2LatestArtifactDownloader mavenDownloader;
private final ApplicationContext applicationContext;

public LatestArtifactDownloader create(Options options) {
if (StringUtils.isNotBlank(options.getMavenRepoUrl())) {
return mavenDownloader;
return applicationContext.getBean("maven2LatestArtifactDownloader", LatestArtifactDownloader.class);
} else {
throw new RuntimeException("Cannot create downloader");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.redskap.swagger.brake.maven;

import io.redskap.swagger.brake.maven.http.HttpClientErrorHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -12,6 +12,6 @@
public class MavenConfiguration {
@Bean
public HttpClient httpClient() {
return HttpClientBuilder.create().build();
return HttpClientBuilder.create().addInterceptorLast(new HttpClientErrorHandler()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@

public abstract class Base64Utils {
public static String encode(String s) {
byte[] sBytes = s.getBytes(StandardCharsets.UTF_8);
byte[] encodedBytes = Base64.getEncoder().encode(sBytes);
return new String(encodedBytes);
}

public static String decode(String s) {
byte[] encodedBytes = s.getBytes(StandardCharsets.UTF_8);
byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
return new String(decodedBytes);
byte[] pureBytes = s.getBytes(StandardCharsets.UTF_8);
byte[] encodedBytes = Base64.getEncoder().encode(pureBytes);
return new String(encodedBytes, StandardCharsets.UTF_8);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.redskap.swagger.brake.maven.http;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
import org.apache.http.protocol.HttpContext;

public class HttpClientErrorHandler implements HttpResponseInterceptor {
@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
int statusCode = response.getStatusLine().getStatusCode();
if (HttpStatus.SC_UNAUTHORIZED == statusCode) {
throw new UnauthorizedException("Request unauthorized");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.redskap.swagger.brake.maven.exception;
package io.redskap.swagger.brake.maven.http;

public class UnauthorizedException extends RuntimeException {
public UnauthorizedException(String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.redskap.swagger.brake.maven.maven2;

import io.redskap.swagger.brake.maven.DownloadOptions;
import io.redskap.swagger.brake.maven.model.MavenMetadata;
import io.redskap.swagger.brake.maven.url.UrlFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
class LatestArtifactVersionResolver {
private final UrlFactory urlFactory;
private final MavenMetadataDownloader metadataDownloader;
private final RepositoryRequestFactory requestFactory;

String resolve(DownloadOptions options) {
String metadataUrl = urlFactory.createLatestArtifactVersionMetadataUrl(options);
MavenMetadata mavenMetadata = metadataDownloader.download(requestFactory.create(metadataUrl, options));
return mavenMetadata.getVersioning().getLatest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.redskap.swagger.brake.maven.maven2;

import java.io.File;

import io.redskap.swagger.brake.maven.DownloadOptions;
import io.redskap.swagger.brake.maven.url.UrlFactory;
import lombok.RequiredArgsConstructor;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
class LatestJarArtifactDownloader {
private final UrlFactory urlFactory;
private final TemporaryJarFileDownloader temporaryJarFileDownloader;
private final RepositoryRequestFactory requestFactory;

File download(DownloadOptions options, String latestSnapshotName, String latestVersion) {
String url = urlFactory.createLatestArtifactUrl(options, latestVersion, latestSnapshotName);
HttpUriRequest httpRequest = requestFactory.create(url, options);
return temporaryJarFileDownloader.download(httpRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.redskap.swagger.brake.maven.maven2;

import static java.lang.String.format;

import io.redskap.swagger.brake.maven.DownloadOptions;
import io.redskap.swagger.brake.maven.model.MavenMetadata;
import io.redskap.swagger.brake.maven.model.MavenSnapshot;
import io.redskap.swagger.brake.maven.url.UrlFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
class LatestSnapshotNameResolver {
private final UrlFactory urlFactory;
private final MavenMetadataDownloader metadataDownloader;
private final RepositoryRequestFactory requestFactory;

String resolve(DownloadOptions options, String latestVersion) {
String metadataUrl = urlFactory.createLatestArtifactSnapshotMetadataUrl(options, latestVersion);
MavenMetadata snapshotMetadata = metadataDownloader.download(requestFactory.create(metadataUrl, options));
MavenSnapshot snapshot = snapshotMetadata.getVersioning().getSnapshot();
String snapshotVersion = latestVersion.replaceAll("SNAPSHOT", snapshot.getTimestamp());
return format("%s-%s-%s", snapshotMetadata.getArtifactId(), snapshotVersion, snapshot.getBuildNumber());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.redskap.swagger.brake.maven.maven2;

import java.io.File;

import io.redskap.swagger.brake.maven.DownloadOptions;
import io.redskap.swagger.brake.maven.LatestArtifactDownloader;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component("maven2LatestArtifactDownloader")
@RequiredArgsConstructor
class Maven2LatestArtifactDownloader implements LatestArtifactDownloader {
private final LatestArtifactVersionResolver latestArtifactVersionResolver;
private final LatestSnapshotNameResolver latestSnapshotNameResolver;
private final LatestJarArtifactDownloader latestJarArtifactDownloader;

@Override
public File download(DownloadOptions options) {
String latestVersion = latestArtifactVersionResolver.resolve(options);
String latestSnapshotName = latestSnapshotNameResolver.resolve(options, latestVersion);
return latestJarArtifactDownloader.download(options, latestSnapshotName, latestVersion);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package io.redskap.swagger.brake.maven;
package io.redskap.swagger.brake.maven.maven2;

import java.io.IOException;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import io.redskap.swagger.brake.maven.exception.UnauthorizedException;
import io.redskap.swagger.brake.maven.model.MavenMetadata;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.stereotype.Component;
Expand All @@ -25,9 +23,6 @@ MavenMetadata download(HttpUriRequest httpRequest) {
try {
log.debug("Downloading maven metadata from {}", httpRequest.getURI());
HttpResponse response = httpClient.execute(httpRequest);
if (HttpStatus.SC_UNAUTHORIZED == response.getStatusLine().getStatusCode()) {
throw new UnauthorizedException("Unauthorized maven metadata downloading from " + httpRequest.getURI());
}
// TODO: content type check would be great here
return getMetadata(response);
} catch (JAXBException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.redskap.swagger.brake.maven.maven2;

import io.redskap.swagger.brake.maven.DownloadOptions;
import io.redskap.swagger.brake.maven.http.HttpRequestFactory;
import lombok.RequiredArgsConstructor;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class RepositoryRequestFactory {
private final HttpRequestFactory requestFactory;

public HttpUriRequest create(String url, DownloadOptions options) {
try {
if (options.isAuthenticationNeeded()) {
return requestFactory.authenticatedGet(url, options.getUsername(), options.getPassword());
} else {
return requestFactory.get(url);
}
} catch (Exception e) {
throw new RuntimeException("Error while creating the http request", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package io.redskap.swagger.brake.maven;
package io.redskap.swagger.brake.maven.maven2;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;

import io.redskap.swagger.brake.maven.http.HttpRequestFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
class TemporaryJarFileDownloader {
private final HttpClient httpClient;

File download(HttpUriRequest httpRequest, String groupId, String artifactId, String version) {
File download(HttpUriRequest httpRequest) {
try {
File destination = Files.createTempFile("swagger-brake", groupId + "-" + artifactId + "-" + version + ".jar").toFile();
log.debug("Downloading JAR from {}", httpRequest.getURI());
File destination = Files.createTempFile("swagger-brake", ".jar").toFile();
HttpResponse response = httpClient.execute(httpRequest);
FileUtils.copyInputStreamToFile(response.getEntity().getContent(), destination);
log.debug("Created temporary JAR file to {}", destination.getAbsolutePath());
return destination;
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/io/redskap/swagger/brake/maven/url/UrlFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.redskap.swagger.brake.maven.url;

import static java.lang.String.format;

import io.redskap.swagger.brake.maven.DownloadOptions;
import org.springframework.stereotype.Component;

@Component
public class UrlFactory {
public String createLatestArtifactSnapshotMetadataUrl(DownloadOptions options, String latestVersion) {
String artifactBasePathUrl = createLatestArtifactBasePathUrl(options);
return format("%s/%s/maven-metadata.xml", artifactBasePathUrl, latestVersion);
}

public String createLatestArtifactVersionMetadataUrl(DownloadOptions options) {
String artifactBasePathUrl = createLatestArtifactBasePathUrl(options);
return format("%s/maven-metadata.xml", artifactBasePathUrl);
}

public String createLatestArtifactUrl(DownloadOptions options, String latestVersion, String latestSnapshotName) {
String artifactBasePathUrl = createLatestArtifactBasePathUrl(options);
return format("%s/%s/%s.jar", artifactBasePathUrl, latestVersion, latestSnapshotName);
}

private String createLatestArtifactBasePathUrl(DownloadOptions options) {
String groupPath = options.getGroupId().replaceAll("\\.", "/");
return format("%s/%s/%s", options.getRepoUrl(), groupPath, options.getArtifactId());
}
}
Loading

0 comments on commit bc536e6

Please sign in to comment.