Thursday, December 3, 2009

Calling methods with param name: value

One of the things I like in Groovy is that when I call a method that takes one or more arguments, I can call this method in the caller program with explicit names of the method argument, that way I don't have to remember the order of the arguments.
e.g. If the method is:
public List fetchAccountAdmins(Account account, boolean effective)

Use: fetchAccountAdmins(account: myAccount, effective: true)
instead of: fetchAccountAdmins(myAccount, true)

This is a little verbose but gives me more freedom. However, there is one caveat, the target method you are calling must be implemented in Groovy. It can't be a Java method.

Monday, July 6, 2009

Grails javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException No name matching

Recently I was trying to connect to a service via XMLRPC. The server had a cert that was invalid.
I tried the usual thing of importing the remote server cert into my JVM, restart my Grails client app and I still got the following error.

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching [server] found.

It happens because the server name and the name on the cert do not match. To get aorund this you have to turn the name validation off.

This is what you need to put in your Bootstrap.groovy to get around this.


def init = {servletContext ->
// Disable certificate verification
def trustManagerMethods = [
getAcceptedIssuers: { null },
checkClientTrusted: {a, b -> },
checkServerTrusted: {a, b -> }
]

def hostnameVerifierMethods = [
verify: {a, b -> true }
]

def trustManager = ProxyGenerator.instantiateAggregate(trustManagerMethods, [X509TrustManager])
TrustManager[] trustAllCerts = (TrustManager[]) [trustManager]

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL")

def hostnameVerifier = ProxyGenerator.instantiateAggregate(hostnameVerifierMethods, [HostnameVerifier])
HostnameVerifier hv = (HostnameVerifier) hostnameVerifier

sc.init(null, trustAllCerts, new java.security.SecureRandom())
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
HttpsURLConnection.setDefaultHostnameVerifier(hv)
}

Tuesday, June 16, 2009

Acegi security pluing tag with Grails

Here are the most common tags to use with the Acegi and Grails:

g:ifallgranted role="ROLE_ADMIN,ROLE_SUPERVISOR">
g:ifanygranted role="ROLE_ADMIN,ROLE_SUPERVISOR">
g:ifnotgranted role="ROLE_USER">
g:isloggedin>
g:isnotloggedin>

Also you can get some extra info about the logged in user/

g:loggedinuserinfo field="username"> // as a gtag
${loggedInUserInfo(field:'username')} // as a GString-expression

Getting the loggedin or current User
If you have the Grails/Acegi plugin installed

def authenticateService

def list = {
def user = authenticateService.principal()
def username = user?.getUsername()


If you don't have Acegi or Jsecurity plugin installed

Object obj =
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username;
if ( obj instanceof UserDetails ) {
username= ( (UserDetails)obj ).getUsername();
} else {
}

Sunday, June 7, 2009

grails-debug run-app

This is the target to debug the app

Tuesday, April 28, 2009

Left Shift Operation

Recently I tried to use the operator "<<" on a a list,

List contactGroups = ['b','c','d']
List displayContactGroupList = ['a']
displayContactGroupList << contactGroups


I was expecting that at the end of the execution, I will have

displayContactGroupList = ['a', 'b','c','d' ]


However, I was surprised to find that the value was

displayContactGroupList = ['a', ['b','c','d'] ]


Learned a new thing today, So to get the effect of what I wanted to get, I will have to use

displayContactGroupList.addAll(contactGroups)

Monday, April 13, 2009

Run grails on port other than 8080

grails -Dserver.port=9090 run-app