u123
u123

Reputation: 16331

Converting java class to groovy: Groovy:No expression for the array constructor call

For testing purposes I would like to try and use the below java class. But I need it to be ported to groovy.

The java class:

public class HttpsTrustManager implements X509TrustManager {
    private static TrustManager[] trustManagers;
    private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};

    @Override
    public void checkClientTrusted(
            X509Certificate[] x509Certificates, String s)
            throws java.security.cert.CertificateException {

    }

    @Override
    public void checkServerTrusted(
            X509Certificate[] x509Certificates, String s)
            throws java.security.cert.CertificateException {

    }

    public boolean isClientTrusted(X509Certificate[] chain) {
        return true;
    }

    public boolean isServerTrusted(X509Certificate[] chain) {
        return true;
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return _AcceptedIssuers;
    }

    public static void allowAllSSL() {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }

        });

        SSLContext context = null;
        if (trustManagers == null) {
            trustManagers = new TrustManager[]{new HttpsTrustManager()};
        }

        try {
            context = SSLContext.getInstance("TLS");
            context.init(null, trustManagers, new SecureRandom());
        } catch (NoSuchAlgorithmException | KeyManagementException e) {
            e.printStackTrace();
        }

        HttpsURLConnection.setDefaultSSLSocketFactory(context != null ? context.getSocketFactory() : null);
    }
}

When I just move that to a .groovy file I get the following errors on this line:

private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};

Error:

Groovy:No expression for the array constructor call at line:

And this line:

if (trustManagers == null) {
    trustManagers = new TrustManager[]{new HttpsTrustManager()}; 
}

Error:

Groovy:No expression for the array constructor call at line:

This first line I have ported like this:

  private static final X509Certificate[] _AcceptedIssuers = [ new X509Certificate(){} ] as X509Certificate[]

Not 100% sure its correct though. But I still have an error on the second line:

    trustManagers = new TrustManager[]{new HttpsTrustManager()};

Any ideas?

Upvotes: 2

Views: 3931

Answers (1)

Evgeny Smirnov
Evgeny Smirnov

Reputation: 3016

We cannot use Java array initialization syntax in Groovy.

So

private static final X509Certificate[] _AcceptedIssuers = [ new X509Certificate(){} ] as X509Certificate[]

is correct and you also need to change

trustManagers = new TrustManager[]{new HttpsTrustManager()};

to

trustManagers = [new HttpsTrustManager()] as TrustManager[];

Upvotes: 7

Related Questions