-1

Answer 1 to this StackOverflow post provides some Groovy code, but it doesn't compile (for me, Groovy Console Version 2.1.3, or in my Grails 2.2.3 app).

Can you please assist in letting me know what I need to change to make this code work? The error I get is:

 unexpected token: public at line 14, column: 52  (... which is the "public X509 Certificate[]")

For quick reference the code solution provided in Answer 1 is:

 import javax.net.ssl.X509TrustManager
 import javax.net.ssl.SSLContext
 import java.security.cert.X509Certificate
 import javax.net.ssl.TrustManager
 import java.security.SecureRandom
 import org.apache.http.conn.ssl.SSLSocketFactory
 import org.apache.http.conn.scheme.Scheme
 import org.apache.http.conn.scheme.SchemeRegistry

 def http = new HTTPBuilder( "https://your_unsecure_certificate_host" )

 //=== SSL UNSECURE CERTIFICATE ===
 def sslContext = SSLContext.getInstance("SSL")              
 sslContext.init(null, [ new X509TrustManager() {public X509Certificate[]   
 getAcceptedIssuers() {null }
 public void checkClientTrusted(X509Certificate[] certs, String authType) { }
 public void checkServerTrusted(X509Certificate[] certs, String authType) { }
 } ] as TrustManager[], new SecureRandom())
 def sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
 def httpsScheme = new Scheme("https", sf, 443)
 http.client.connectionManager.schemeRegistry.register( httpsScheme )
 //================================
lepe
  • 24,677
  • 9
  • 99
  • 108
Ray
  • 5,885
  • 16
  • 61
  • 97

1 Answers1

2

You probably have a newline between the public X509Certificate[] and getAcceptedIssuers() {null} as a result of pasting the code in.

Try removing the newline (and formatting the code to something moderately readable while you're at it) and the error should disappear.

codelark
  • 12,254
  • 1
  • 45
  • 49
  • Thanks. The various anonymous classes are definitely a bit confusing to me perhaps also because of the arrays. You are right removing the newline fixes it, but this too seems unobvious to me -- i.e. can't one have as much whitespace/newlines as desired? – Ray Jul 19 '13 at 16:28
  • There are certain places where whitespace is important. This is one of many reasons why coding standards are important. – codelark Jul 19 '13 at 16:34
  • The issue here is that the second line is a valid expression by itself. Groovy automatically assumes `Object` as the return type since it is not specified. Since the second line is a valid expression, the compiler doesn't know to combine it with the previous (or potentially next) line. It gets ambiguous very quickly. – codelark Jul 19 '13 at 16:38
  • I see, the assumed return type threw me off. Unfortunately I have a new issue now :-(, Groovy Runtime Exception: Could not find matching constructor for: org.apache.http.conn.ssl.SSLSocketFactory(javax.net.ssl.SSLContext, org.apache.http.conn.ssl.AllowAllHostnameVerifier). Any thoughts on this? Running it in Grails 2.2.3. – Ray Jul 19 '13 at 16:38
  • The first comment on the SO post you copied this from handles this exact issue. – codelark Jul 19 '13 at 17:07
  • Thanks man. I used it. Have no idea why the constructor doesn't wor (posted it in another question). I still think I'm not getting some basic Groovy stuff ... – Ray Jul 19 '13 at 19:09