0

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)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sybuser
  • 735
  • 10
  • 27
  • If it is a `jar` file, why are you accepting `json` ?? ;) – Antoniossss Mar 17 '22 at 09:34
  • Also you dont need `Content-Type` header, as you are not sending any body which might cause server to respond differently – Antoniossss Mar 17 '22 at 09:36
  • right - question upated - same error – Sybuser Mar 17 '22 at 10:30
  • I was accepting json just in case the rest api returns an error in json format instead of the jar file – Sybuser Mar 17 '22 at 12:50
  • @Antoniossss the same link that produces a FileNotFoundException in the console works in a browser if I copy/paste it, provided that I'm properly logged in. Any ideas? – Sybuser Mar 18 '22 at 11:59
  • not really no, you would have to share actual link. Looks like it should work as is. Douyble check that credentials are correct. You can try to use invalid credentials to see if it would generate different response – Antoniossss Mar 18 '22 at 12:48

1 Answers1

0

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);
        }
    }
Sybuser
  • 735
  • 10
  • 27