2

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?

1 Answer 1

7

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[];
Sign up to request clarification or add additional context in comments.

3 Comments

I have another situation here , where i want to initialize the array in groovy with a bean.getXxx() method. Can you please help by giving the syntax.
modifiedScanner.setIncludes( new String[] { sqljFile.getName() } ) my existing java code
try: modifiedScanner.include = [sqljFile.name] as String[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.