Hi Guys!
Gave up on the WSDL2Java-Soap Variant.
Retried SOAP like this:
Code:
String wsdlURL = "http://localhost/sugarcrm/soap.php?wsdl"
//WSClient proxy = new WSClient(wsdlURL,this.class.classLoader,SoapVersion.SOAP_1_1)//
WSClient proxy = webService.getClient(wsdlURL,SoapVersion.SOAP_1_1)
String auth = "'user_auth':{'user_name':'myUser','password':'myMd5Pw','version':'.01'}"
String appName = "'application_name':'Ariadne'"
String loginRequest = "{loginRequest:{${auth},${appName}}}"
//def loginRequest = proxy.create("com.sugarcrm.www.sugarcrm.loginRequest")
//loginRequest.user_auth = proxy.create("com.sugarcrm.www.sugarcrm.user_auth")
//loginReuest.user_auth.user_name = "myUser"
//loginReuest.user_auth.password = "myPW"
//loginReuest.user_auth.version = ".01"
//loginReuest.application_name = "Ariadne"
def result = proxy.invokeMethod("login",loginRequest)
return [msg:result] couldn't get it to work.
So i retried REST and had another look at the WSDL and API spec and saw, oh my, username is user_name.
So this is what got me "Invalid username or password"
So I got it to work.
To make it more elegant and reusable I introduced the Factory Pattern with SugarBeans ... here's the working RESTful groovy-code
CONTROLLER:
Code:
def sugar = {
AbstractSugarBean auth = new SugarAuthenticationBean()
AbstractSugarBean loginRequest = new SugarLoginRequestBean()
try {
auth.setBeanProperty(auth.USER_NAME,"myUser")
auth.setBeanProperty(auth.PASSWORD,"myMd5Pw")
auth.setBeanProperty(auth.VERSION,".01") //not needed
loginRequest.setBeanProperty(loginRequest.USER_AUTH,auth.toJson())
loginRequest.setBeanProperty(loginRequest.APPLICATION,"MyApplication") //not needed
}
catch (groovy.lang.MissingPropertyException e) {
return [msg: e.toString()]
}
catch (java.lang.NullPointerException e) {
return [msg: e.toString()]
}
catch (Exception e) {
return [msg: e.toString()]
}
def myRC = new RESTClient("http://localhost/")
def r = myRC.post( path: "service/v2/rest.php",
query:
[method: "login",
input_type: "JSON",
response_type: "JSON",
rest_data: loginRequest.toJson()])
return [msg: r.data]
} Since grails.converters.JSON didn't output a JSON-Format Sugar understands I had to write my own ...
AbstractSugarBean
Code:
abstract class AbstractSugarBean {
protected Map<String,?> beanProperties = [:]
public void setBeanProperty(String property, String value) {
beanProperties.putAt(property,value)
}
public String toJson (Map<String,?> properties = beanProperties) {
String json = "{"
properties.eachWithIndex { String key, value, i ->
json += "'${key}':"
if(value instanceof java.util.Map) {
json += toJson(value)
}
else {
if(value.charAt(0) == "{") {
json += value
}
else {
json += "'${value}'"
}
}
if(i == properties.size()-1) {
json += "}"
}
else {
json += ","
}
}
json
}
} and the Concrete Beans themselves
Code:
class SugarAuthenticationBean extends AbstractSugarBean {
final String USER_NAME = "user_name"
final String PASSWORD = "password"
final String VERSION = "version"
}
class SugarLoginRequestBean extends AbstractSugarBean {
final String USER_AUTH = "user_auth"
final String APPLICATION = "application_name"
} so now I'm happy like a dog with its owner.
hope this helps anyone
greetz, Johannes
Bookmarks