import groovyx.net.http.* class ExchangeSyncService { static transactional = true static DOMAIN = 'my-domain' static CTMS_SYNC_USERNAME = 'usr' static CTMS_SYNC_PASSWORD = 'pwd' static HTTP_BUILDER_POOL = [:] private static getHttpBuilder(username) { HTTP_BUILDER_POOL[username] ?: ( HTTP_BUILDER_POOL[username] = new HTTPBuilder( "https://webmail.${DOMAIN}.com" ) ) } def sendAppointment(params) { def username = CTMS_SYNC_USERNAME, password = CTMS_SYNC_PASSWORD _login(username, password) getHttpBuilder(username).post( path: "/Exchange/${username}/日历", body: getAppointmentBody(params)) {res-> println res.statusLine.statusCode } } def saveTask(params) { def username = params.session.user?.username, password = params.session.user?.password _login(username, password) getHttpBuilder(username).post( path: "/Exchange/${username}/任务", body: getTaskBody(params)) {res-> println res.statusLine.statusCode } } private _login(username, password) { getHttpBuilder(username).post( path: '/exchweb/bin/auth/owaauth.dll', body: [destination:"https://webmail.${DOMAIN}.com/Exchange", flags:'0', username:"${DOMAIN}/${username}", password:password] ) {res-> if( res.statusLine.statusCode == 302 ) { println "${username} logged in to exchange server successfully." } } } private getAppointmentBody(m) { [ "Cmd": "sendappt", "CmdReferring": "new", "Embedded": "0", "FORMTYPE": "appointment", "Importance": "2", "Optional": m.optional,//"[email protected]; [email protected]", "ReadForm": "1", "Required": m.required,//"[email protected]", "Resource": m.resource,//"[email protected]", "urn:schemas:calendar:alldayevent": "1", "urn:schemas:calendar:busystatus": "BUSY", "urn:schemas:calendar:dtend": m.dtend,//"2011-09-21T16:00:00.000Z", //bug:总是用Exchange服务器所在的美国时区 "urn:schemas:calendar:dtstart": m.dtstart,//"2011-09-21T15:30:00.000Z", //bug:总是用Exchange服务器所在的美国时区 "urn:schemas:calendar:location": "", "urn:schemas:calendar:reminderoffset": m.reminderoffset,//"900", "urn:schemas:calendar:responserequested": "0", "urn:schemas:httpmail:importance": "1", "urn:schemas:httpmail:subject": m.subject,//"(sent by ctmssync)", "urn:schemas:httpmail:textdescription": "Accept it to get it on your calendar" ] } private getTaskBody(m) { [ "Cmd": "savetask", "CmdReferring": "new", "Embedded": "0", "FORMTYPE": "task", "MsgClass": "IPM.Task", "ReadForm": "1", "SENDUPDATE": "0", "exception": "", "http://schemas.microsoft.com/exchange/tasks/percentcomplete": "0", "http://schemas.microsoft.com/exchange/tasks/resetreminder": "", "http://schemas.microsoft.com/mapi/reminderset": "1", "task_due": m.task_due,//"2011-08-04T00:00:00.000Z", "task_remindertime": m.remindertime,//"2011-10-06T08:00:00.000Z", "task_start": m.task_start,//"2011-10-04T00:00:00.000Z", "task_status": "0", "urn:schemas:httpmail:importance": "1", "urn:schemas:httpmail:subject": m.subject,//"Title_2", "urn:schemas:httpmail:textdescription": "This is a task created by SmartCTMS on behalf of you" ] } }