Search in sources :

Example 1 with UploadException

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);
    }
}
Also used : BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) UploadException(org.jreleaser.model.uploader.spi.UploadException) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) SdkClientException(com.amazonaws.SdkClientException) Map(java.util.Map) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 2 with UploadException

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);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) FormData(feign.form.FormData) UploadException(org.jreleaser.model.uploader.spi.UploadException) IOException(java.io.IOException) Artifact(org.jreleaser.model.Artifact) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with UploadException

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);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) UploadException(org.jreleaser.model.uploader.spi.UploadException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) URL(java.net.URL)

Example 4 with UploadException

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);
        }
    }
}
Also used : Path(java.nio.file.Path) AccessControlList(com.amazonaws.services.s3.model.AccessControlList) AmazonS3(com.amazonaws.services.s3.AmazonS3) InputStream(java.io.InputStream) UploadException(org.jreleaser.model.uploader.spi.UploadException) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Artifact(org.jreleaser.model.Artifact) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 5 with UploadException

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);
    }
}
Also used : UploadException(org.jreleaser.model.uploader.spi.UploadException) JReleaserException(org.jreleaser.util.JReleaserException)

Aggregations

UploadException (org.jreleaser.model.uploader.spi.UploadException)6 IOException (java.io.IOException)4 Path (java.nio.file.Path)3 Artifact (org.jreleaser.model.Artifact)3 FormData (feign.form.FormData)2 LinkedHashMap (java.util.LinkedHashMap)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 SdkClientException (com.amazonaws.SdkClientException)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)1 AccessControlList (com.amazonaws.services.s3.model.AccessControlList)1 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1