use of org.jreleaser.model.uploader.spi.UploadException in project jreleaser by jreleaser.
the class S3ArtifactUploader method createS3Client.
private AmazonS3 createS3Client() throws UploadException {
try {
AmazonS3ClientBuilder s3Builder = AmazonS3ClientBuilder.standard();
if (isNotBlank(uploader.getResolvedAccessKeyId()) && isNotBlank(uploader.getResolvedSecretKey()) && isNotBlank(uploader.getResolvedSessionToken())) {
s3Builder.withCredentials(new AWSStaticCredentialsProvider(new BasicSessionCredentials(uploader.getResolvedAccessKeyId(), uploader.getResolvedSecretKey(), uploader.getResolvedSessionToken())));
} else if (isNotBlank(uploader.getResolvedAccessKeyId()) && isNotBlank(uploader.getResolvedSecretKey())) {
s3Builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(uploader.getResolvedAccessKeyId(), uploader.getResolvedSecretKey())));
}
Map<String, String> headers = uploader.getHeaders();
if (headers != null) {
ClientConfiguration clientConfiguration = new ClientConfiguration();
for (Map.Entry<String, String> header : headers.entrySet()) {
if (header.getKey() != null && header.getValue() != null) {
clientConfiguration.addHeader(header.getKey(), header.getValue());
}
}
s3Builder.setClientConfiguration(clientConfiguration);
}
if (isBlank(uploader.getResolvedEndpoint())) {
s3Builder.withRegion(uploader.getResolvedRegion());
} else {
s3Builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(uploader.getResolvedEndpoint(), uploader.getResolvedRegion()));
}
s3Builder.getClientConfiguration().setConnectionTimeout(uploader.getConnectTimeout() * 1000);
return s3Builder.build();
} catch (SdkClientException e) {
context.getLogger().trace(e);
throw new UploadException(RB.$("ERROR_unexpected_s3_client_config"), e);
}
}
use of org.jreleaser.model.uploader.spi.UploadException in project jreleaser by jreleaser.
the class HttpArtifactUploader method upload.
@Override
public void upload(String name) throws UploadException {
List<Artifact> artifacts = collectArtifacts();
if (artifacts.isEmpty()) {
context.getLogger().info(RB.$("artifacts.no.match"));
}
String username = uploader.getResolvedUsername();
String password = uploader.getResolvedPassword();
for (Artifact artifact : artifacts) {
Path path = artifact.getEffectivePath(context);
context.getLogger().info(" - {}", path.getFileName());
if (!context.isDryrun()) {
try {
FormData data = ClientUtils.toFormData(path);
Map<String, String> headers = new LinkedHashMap<>();
switch(uploader.resolveAuthorization()) {
case NONE:
break;
case BASIC:
String auth = username + ":" + password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes());
auth = new String(encodedAuth);
headers.put("Authorization", "Basic " + auth);
break;
case BEARER:
headers.put("Authorization", "Bearer " + password);
break;
}
resolveHeaders(artifact, headers);
if (uploader.getMethod() == HttpUploader.Method.POST) {
ClientUtils.postFile(context.getLogger(), uploader.getResolvedUploadUrl(context, artifact), uploader.getConnectTimeout(), uploader.getReadTimeout(), data, headers);
} else {
ClientUtils.putFile(context.getLogger(), uploader.getResolvedUploadUrl(context, artifact), uploader.getConnectTimeout(), uploader.getReadTimeout(), data, headers);
}
} catch (IOException e) {
context.getLogger().trace(e);
throw new UploadException(RB.$("ERROR_unexpected_upload", context.getBasedir().relativize(path)), e);
}
}
}
}
use of org.jreleaser.model.uploader.spi.UploadException in project jreleaser by jreleaser.
the class ClientUtils method uploadFile.
private static void uploadFile(JReleaserLogger logger, String url, int connectTimeout, int readTimeout, FormData data, Map<String, String> headers) throws UploadException {
try {
// create URL
URL theUrl = new URL(url);
logger.debug("url: {}", theUrl);
// open connection
logger.debug(RB.$("webhook.connection.open"));
HttpURLConnection connection = (HttpURLConnection) theUrl.openConnection();
// set options
logger.debug(RB.$("webhook.connection.configure"));
connection.setConnectTimeout(connectTimeout * 1000);
connection.setReadTimeout(readTimeout * 1000);
connection.setAllowUserInteraction(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod(headers.remove("METHOD"));
connection.addRequestProperty("Accept", "*/*");
connection.addRequestProperty("User-Agent", "JReleaser/" + JReleaserVersion.getPlainVersion());
connection.addRequestProperty("Content-Length", data.getData().length + "");
connection.setRequestProperty("Content-Type", data.getContentType());
headers.forEach(connection::setRequestProperty);
connection.getRequestProperties().forEach((k, v) -> {
if (JReleaserModelPrinter.isSecret(k)) {
logger.debug("{}: {}", k, Constants.HIDE);
} else {
logger.debug("{}: {}", k, v);
}
});
connection.setDoOutput(true);
// write message
logger.debug(RB.$("webhook.data.send"));
try (OutputStream os = connection.getOutputStream()) {
os.write(data.getData(), 0, data.getData().length);
os.flush();
}
// handle response
logger.debug(RB.$("webhook.response.handle"));
int status = connection.getResponseCode();
if (status >= 400) {
String reason = connection.getResponseMessage();
Reader reader = new InputStreamReader(connection.getErrorStream(), UTF_8);
String message = IOUtils.toString(reader);
StringBuilder b = new StringBuilder("Got ").append(status);
if (isNotBlank(reason)) {
b.append(" reason: ").append(reason).append(",");
}
if (isNotBlank(message)) {
b.append(message);
}
throw new UploadException(b.toString());
}
} catch (IOException e) {
logger.trace(e);
throw new UploadException(e);
}
}
use of org.jreleaser.model.uploader.spi.UploadException in project jreleaser by jreleaser.
the class S3ArtifactUploader method upload.
@Override
public void upload(String name) throws UploadException {
List<Artifact> artifacts = collectArtifacts();
if (artifacts.isEmpty()) {
context.getLogger().info(RB.$("artifacts.no.match"));
}
String bucketName = uploader.getResolvedBucket();
AmazonS3 s3 = createS3Client();
// does the bucket exist?
context.getLogger().debug(RB.$("s3.bucket.check"), bucketName);
if (!s3.doesBucketExistV2(bucketName)) {
// create the bucket
context.getLogger().debug(RB.$("s3.bucket.create"), bucketName);
s3.createBucket(bucketName);
}
for (Artifact artifact : artifacts) {
Path path = artifact.getEffectivePath(context);
context.getLogger().info(" - {}", path.getFileName());
try {
String bucketPath = uploader.getResolvedPath(context, artifact);
context.getLogger().debug(" {}", bucketPath);
if (!context.isDryrun()) {
context.getLogger().debug(RB.$("s3.object.check"), bucketName, bucketPath);
if (s3.doesObjectExist(bucketName, bucketPath)) {
context.getLogger().debug(RB.$("s3.object.create"), bucketName, bucketPath);
s3.deleteObject(bucketName, bucketPath);
}
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(MediaType.parse(TIKA.detect(path)).toString());
meta.setContentLength(Files.size(path));
context.getLogger().debug(RB.$("s3.object.write"), bucketName, bucketPath);
try (InputStream is = Files.newInputStream(path, READ)) {
s3.putObject(new PutObjectRequest(bucketName, bucketPath, is, meta));
}
context.getLogger().debug(RB.$("s3.object.acl"), bucketName, bucketPath);
AccessControlList acl = s3.getObjectAcl(bucketName, bucketPath);
acl.grantPermission(GroupGrantee.AllUsers, Permission.Read);
s3.setObjectAcl(bucketName, bucketPath, acl);
}
} catch (IOException e) {
context.getLogger().trace(e);
throw new UploadException(RB.$("ERROR_unexpected_upload", context.relativizeToBasedir(path)), e);
}
}
}
use of org.jreleaser.model.uploader.spi.UploadException in project jreleaser by jreleaser.
the class Uploaders method upload.
private static void upload(JReleaserContext context, Uploader uploader) {
try {
context.getLogger().increaseIndent();
context.getLogger().setPrefix(uploader.getType());
ProjectUploader projectUploader = createProjectUploader(context, uploader);
projectUploader.upload();
context.getLogger().restorePrefix();
context.getLogger().decreaseIndent();
} catch (UploadException e) {
throw new JReleaserException(RB.$("ERROR_unexpected_error"), e);
}
}
Aggregations