Search code examples
javanexus3

Download works with curl but not with Java program


I'm having trouble downloading an artifact from Nexus 3 with a Java program. It works fine with curl but not with this Java program. There might be some differences that make the download fail.

This curl command works:

curl -u user:password -k -L -X GET "https://somecompany.com:8443/nexus/service/rest/v1/search/assets/download?group=org.apache.activemq&name=activemq-broker&version=5.16.2&maven.classifier=sources" -H "accept: application/json" -o activemq-broker-5.16.2-sources.jar

However, this Java program fails:

import java.io.File;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class Main {

    public static void main(String[] args) throws Exception {
        String nexus3Url = args[0];
        String nexus3User = args[1];
        String nexus3Password = args[2];

        URL url = new URL(nexus3Url
                + "/service/rest/v1/search/assets/download?group=org.apache.activemq&name=activemq-broker&version=5.16.2&maven.classifier=sources");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setAuthenticator(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(nexus3User, nexus3Password.toCharArray());
            }
        });
        connection.setInstanceFollowRedirects(true);
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(10000);

        File file = new File("activemq-broker-5.16.2-sources.jar");
        try (InputStream in = connection.getInputStream()) {
            Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
    }
}

The error is FileNotFoundException :

Exception in thread "main" java.io.FileNotFoundException: https://somecompany.com:8443/nexus/service/rest/v1/search/assets/download?group=org.apache.activemq&name=activemq-broker&version=5.16.2&maven.classifier=sources
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1993)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
    at Main.main(Main.java:33)

Solution

  • The Basic authentication in Authorization header worked for me.

        public static void main(String[] args) throws Exception {
            String nexus3Url = args[0];
            String nexus3User = args[1];
            String nexus3Password = args[2];
    
            URL url = new URL(nexus3Url
                    + "/service/rest/v1/search/assets/download?group=org.apache.activemq&name=activemq-broker&version=5.16.2&maven.classifier=sources");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            String auth = nexus3User + ":" + nexus3Password;
            byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
            String authHeaderValue = "Basic " + new String(encodedAuth);
            connection.setRequestProperty("Authorization", authHeaderValue);
            connection.setInstanceFollowRedirects(true);
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(10000);
            File file = new File("activemq-broker-5.16.2-sources.jar");
            try (InputStream in = connection.getInputStream()) {
                Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
        }