2014-08-29 06:10:34 +08:00
|
|
|
package org.bigbluebutton
|
2012-10-16 02:36:54 +08:00
|
|
|
/*
|
2012-10-25 04:04:40 +08:00
|
|
|
BigBlueButton open source conferencing system - http://www.bigbluebutton.org/
|
2012-10-16 02:36:54 +08:00
|
|
|
|
2012-10-25 04:04:40 +08:00
|
|
|
Copyright (c) 2012 BigBlueButton Inc. and by respective authors (see below).
|
2012-10-16 02:36:54 +08:00
|
|
|
|
2012-10-25 04:04:40 +08:00
|
|
|
This program is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU Lesser General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3.0 of the License, or (at your option) any later
|
|
|
|
version.
|
2012-10-16 02:36:54 +08:00
|
|
|
|
2012-10-25 04:04:40 +08:00
|
|
|
BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
2012-10-16 02:36:54 +08:00
|
|
|
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
|
|
|
|
2012-10-25 04:04:40 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with BigBlueButton; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2014-08-29 06:10:34 +08:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.text.MessageFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
import org.xml.sax.SAXException;
|
|
|
|
|
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
2012-10-13 04:01:27 +08:00
|
|
|
|
2012-10-17 06:09:56 +08:00
|
|
|
import org.bigbluebutton.api.Proxy
|
|
|
|
import org.bigbluebutton.lti.Role
|
|
|
|
import org.bigbluebutton.lti.Parameter
|
2012-10-13 04:01:27 +08:00
|
|
|
|
2012-10-11 23:52:41 +08:00
|
|
|
class BigbluebuttonService {
|
|
|
|
|
2014-08-29 06:10:34 +08:00
|
|
|
boolean transactional = false
|
|
|
|
|
2012-10-16 23:22:45 +08:00
|
|
|
def url = "http://test-install.blindsidenetworks.com/bigbluebutton"
|
|
|
|
def salt = "8cd8ef52e8e101574e400365b55e11a6"
|
2012-10-13 04:01:27 +08:00
|
|
|
|
2012-10-16 23:30:12 +08:00
|
|
|
Proxy bbbProxy
|
2012-10-17 06:09:56 +08:00
|
|
|
DocumentBuilderFactory docBuilderFactory
|
|
|
|
DocumentBuilder docBuilder
|
2012-10-11 23:52:41 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
BigbluebuttonService(){
|
|
|
|
// Initialize XML libraries
|
2012-10-17 06:09:56 +08:00
|
|
|
docBuilderFactory = DocumentBuilderFactory.newInstance()
|
2012-10-13 04:01:27 +08:00
|
|
|
try {
|
2012-10-17 06:09:56 +08:00
|
|
|
docBuilder = docBuilderFactory.newDocumentBuilder()
|
2012-10-13 04:01:27 +08:00
|
|
|
} catch (ParserConfigurationException e) {
|
2012-10-17 06:09:56 +08:00
|
|
|
logger.error("Failed to initialise BaseProxy", e)
|
2012-10-13 04:01:27 +08:00
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-16 23:30:12 +08:00
|
|
|
//Instantiate bbbProxy and initialize it with default url and salt
|
|
|
|
bbbProxy = new Proxy(url, salt)
|
2012-10-13 04:01:27 +08:00
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-24 06:46:31 +08:00
|
|
|
public String getJoinURL(params, welcome, mode){
|
2012-10-16 23:22:45 +08:00
|
|
|
//Set the injected values
|
2012-10-16 23:30:12 +08:00
|
|
|
if( !url.equals(bbbProxy.url) && !url.equals("") ) bbbProxy.setUrl(url)
|
|
|
|
if( !salt.equals(bbbProxy.salt) && !salt.equals("") ) bbbProxy.setSalt(salt)
|
2012-10-17 06:09:56 +08:00
|
|
|
|
|
|
|
String joinURL = null
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-17 06:09:56 +08:00
|
|
|
String meetingName = getValidatedMeetingName(params.get(Parameter.RESOURCE_LINK_TITLE))
|
2012-10-25 04:04:40 +08:00
|
|
|
String meetingID = getValidatedMeetingId(params.get(Parameter.RESOURCE_LINK_ID), params.get(Parameter.CONSUMER_ID))
|
|
|
|
String attendeePW = DigestUtils.shaHex("ap" + params.get(Parameter.RESOURCE_LINK_ID) + params.get(Parameter.CONSUMER_ID))
|
|
|
|
String moderatorPW = DigestUtils.shaHex("mp" + params.get(Parameter.RESOURCE_LINK_ID) + params.get(Parameter.CONSUMER_ID))
|
2012-10-17 06:09:56 +08:00
|
|
|
String logoutURL = getValidatedLogoutURL(params.get(Parameter.LAUNCH_RETURN_URL))
|
2013-01-29 05:58:55 +08:00
|
|
|
boolean isModerator = isModerator(params)
|
2012-10-25 04:04:40 +08:00
|
|
|
String userFullName = getValidatedUserFullName(params, isModerator)
|
2012-10-17 06:09:56 +08:00
|
|
|
String courseTitle = getValidatedCourseTitle(params.get(Parameter.COURSE_TITLE))
|
2012-10-18 01:58:19 +08:00
|
|
|
String userID = getValidatedUserId(params.get(Parameter.USER_ID))
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-24 06:46:31 +08:00
|
|
|
Integer voiceBridge = 0
|
2013-04-20 05:52:46 +08:00
|
|
|
String record = false
|
2013-01-24 06:46:31 +08:00
|
|
|
Integer duration = 0
|
|
|
|
if( "extended".equals(mode) ){
|
2013-06-08 05:45:47 +08:00
|
|
|
voiceBridge = getValidatedBBBVoiceBridge(params.get(Parameter.CUSTOM_VOICEBRIDGE))
|
|
|
|
record = getValidatedBBBRecord(params.get(Parameter.CUSTOM_RECORD))
|
|
|
|
duration = getValidatedBBBDuration(params.get(Parameter.CUSTOM_DURATION))
|
2013-01-24 06:46:31 +08:00
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2014-10-28 05:42:25 +08:00
|
|
|
Boolean allModerators = Boolean.valueOf(false)
|
|
|
|
if ( params.containsKey(Parameter.CUSTOM_ALL_MODERATORS) ) {
|
|
|
|
allModerators = Boolean.parseBoolean(params.get(Parameter.CUSTOM_ALL_MODERATORS))
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:09:56 +08:00
|
|
|
String[] values = [meetingName, courseTitle]
|
2012-10-18 01:58:19 +08:00
|
|
|
String welcomeMsg = MessageFormat.format(welcome, values)
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-18 01:58:19 +08:00
|
|
|
String meta = getMonitoringMetaData(params)
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-24 06:46:31 +08:00
|
|
|
String createURL = getCreateURL( meetingName, meetingID, attendeePW, moderatorPW, welcomeMsg, voiceBridge, logoutURL, record, duration, meta )
|
2013-01-25 04:52:06 +08:00
|
|
|
log.debug "createURL: " + createURL
|
2012-10-17 06:09:56 +08:00
|
|
|
Map<String, Object> createResponse = doAPICall(createURL)
|
2013-01-25 04:52:06 +08:00
|
|
|
log.debug "createResponse: " + createResponse
|
2012-10-17 06:09:56 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
if( createResponse != null){
|
|
|
|
String returnCode = (String) createResponse.get("returncode")
|
|
|
|
String messageKey = (String) createResponse.get("messageKey")
|
2012-10-17 06:09:56 +08:00
|
|
|
if ( Proxy.APIRESPONSE_SUCCESS.equals(returnCode) ||
|
2014-08-29 06:10:34 +08:00
|
|
|
(Proxy.APIRESPONSE_FAILED.equals(returnCode) && (Proxy.MESSAGEKEY_IDNOTUNIQUE.equals(messageKey) || Proxy.MESSAGEKEY_DUPLICATEWARNING.equals(messageKey)) ) ){
|
2014-10-28 05:42:25 +08:00
|
|
|
joinURL = bbbProxy.getJoinURL( userFullName, meetingID, (isModerator || allModerators)? moderatorPW: attendeePW, (String) createResponse.get("createTime"), userID);
|
2012-10-13 04:01:27 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 23:52:41 +08:00
|
|
|
|
2012-10-17 06:09:56 +08:00
|
|
|
return joinURL
|
2012-10-11 23:52:41 +08:00
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-26 04:10:27 +08:00
|
|
|
public Object getRecordings(params){
|
|
|
|
//Set the injected values
|
|
|
|
if( !url.equals(bbbProxy.url) && !url.equals("") ) bbbProxy.setUrl(url)
|
|
|
|
if( !salt.equals(bbbProxy.salt) && !salt.equals("") ) bbbProxy.setSalt(salt)
|
|
|
|
|
|
|
|
String meetingID = getValidatedMeetingId(params.get(Parameter.RESOURCE_LINK_ID), params.get(Parameter.CONSUMER_ID))
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-29 06:24:11 +08:00
|
|
|
String recordingsURL = bbbProxy.getGetRecordingsURL( meetingID )
|
2013-01-26 04:10:27 +08:00
|
|
|
log.debug "recordingsURL: " + recordingsURL
|
|
|
|
Map<String, Object> recordings = doAPICall(recordingsURL)
|
|
|
|
|
|
|
|
if( recordings != null){
|
|
|
|
String returnCode = (String) recordings.get("returncode")
|
|
|
|
String messageKey = (String) recordings.get("messageKey")
|
|
|
|
if ( Proxy.APIRESPONSE_SUCCESS.equals(returnCode) && messageKey == null ){
|
|
|
|
return recordings.get("recordings")
|
2014-08-28 22:13:40 +08:00
|
|
|
}
|
2013-01-26 04:10:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
2013-01-29 04:53:27 +08:00
|
|
|
|
|
|
|
public Object doDeleteRecordings(params){
|
|
|
|
//Set the injected values
|
|
|
|
if( !url.equals(bbbProxy.url) && !url.equals("") ) bbbProxy.setUrl(url)
|
|
|
|
if( !salt.equals(bbbProxy.salt) && !salt.equals("") ) bbbProxy.setSalt(salt)
|
|
|
|
|
|
|
|
Map<String, Object> result
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-29 04:53:27 +08:00
|
|
|
String recordingId = getValidatedBBBRecordingId(params.get(Parameter.BBB_RECORDING_ID))
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-29 04:53:27 +08:00
|
|
|
if( !recordingId.equals("") ){
|
|
|
|
String deleteRecordingsURL = bbbProxy.getDeleteRecordingsURL( recordingId )
|
|
|
|
log.debug "deleteRecordingsURL: " + deleteRecordingsURL
|
|
|
|
result = doAPICall(deleteRecordingsURL)
|
|
|
|
} else {
|
|
|
|
result = new HashMap<String, String>()
|
|
|
|
result.put("resultMessageKey", "InvalidRecordingId")
|
|
|
|
result.put("resultMessage", "RecordingId is invalid. The recording can not be deleted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-29 04:53:27 +08:00
|
|
|
public Object doPublishRecordings(params){
|
|
|
|
//Set the injected values
|
|
|
|
if( !url.equals(bbbProxy.url) && !url.equals("") ) bbbProxy.setUrl(url)
|
|
|
|
if( !salt.equals(bbbProxy.salt) && !salt.equals("") ) bbbProxy.setSalt(salt)
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-29 04:53:27 +08:00
|
|
|
Map<String, Object> result
|
|
|
|
|
|
|
|
String recordingId = getValidatedBBBRecordingId(params.get(Parameter.BBB_RECORDING_ID))
|
|
|
|
String publish = getValidatedBBBRecordingPublished(params.get(Parameter.BBB_RECORDING_PUBLISHED))
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-29 04:53:27 +08:00
|
|
|
if( !recordingId.equals("") ){
|
|
|
|
String publishRecordingsURL = bbbProxy.getPublishRecordingsURL( recordingId, "true".equals(publish)?"false":"true" )
|
|
|
|
log.debug "publishRecordingsURL: " + publishRecordingsURL
|
|
|
|
result = doAPICall(publishRecordingsURL)
|
|
|
|
} else {
|
|
|
|
result = new HashMap<String, String>()
|
|
|
|
result.put("resultMessageKey", "InvalidRecordingId")
|
|
|
|
result.put("resultMessage", "RecordingId is invalid. The recording can not be deleted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-29 05:58:55 +08:00
|
|
|
public boolean isModerator(params) {
|
|
|
|
boolean isModerator = params.get(Parameter.ROLES) != null? Role.isModerator(params.get(Parameter.ROLES)): true
|
|
|
|
return isModerator
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-04-20 05:52:46 +08:00
|
|
|
private String getCreateURL(String name, String meetingID, String attendeePW, String moderatorPW, String welcome, Integer voiceBridge, String logoutURL, String record, Integer duration, String meta ) {
|
2013-01-24 06:46:31 +08:00
|
|
|
voiceBridge = ( voiceBridge == null || voiceBridge == 0 )? 70000 + new Random(System.currentTimeMillis()).nextInt(10000): voiceBridge;
|
2012-10-16 02:36:54 +08:00
|
|
|
|
2013-04-20 05:52:46 +08:00
|
|
|
String url = bbbProxy.getCreateURL(name, meetingID, attendeePW, moderatorPW, welcome, "", voiceBridge.toString(), "", logoutURL, "", record, duration.toString(), meta );
|
2012-10-16 02:36:54 +08:00
|
|
|
return url;
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-17 06:09:56 +08:00
|
|
|
private String getValidatedMeetingName(String meetingName){
|
|
|
|
return (meetingName == null || meetingName == "")? "Meeting": meetingName
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-24 06:46:31 +08:00
|
|
|
private String getValidatedMeetingId(String resourceId, String consumerId){
|
|
|
|
return DigestUtils.shaHex(resourceId + consumerId)
|
2012-10-17 06:09:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private String getValidatedLogoutURL(String logoutURL){
|
|
|
|
return (logoutURL == null)? "": logoutURL
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-25 04:04:40 +08:00
|
|
|
private String getValidatedUserFullName(params, boolean isModerator){
|
|
|
|
String userFullName = params.get(Parameter.USER_FULL_NAME)
|
|
|
|
String userFirstName = params.get(Parameter.USER_FIRSTNAME)
|
|
|
|
String userLastName = params.get(Parameter.USER_LASTNAME)
|
|
|
|
if( userFullName == null || userFullName == "" ){
|
|
|
|
if( userFirstName != null && userFirstName != "" ){
|
|
|
|
userFullName = userFirstName
|
|
|
|
}
|
|
|
|
if( userLastName != null && userLastName != "" ){
|
2013-06-08 04:07:40 +08:00
|
|
|
userFullName += userFullName.length() > 0? " ": ""
|
2012-10-25 04:04:40 +08:00
|
|
|
userFullName += userLastName
|
|
|
|
}
|
|
|
|
if( userFullName == null || userFullName == "" ){
|
|
|
|
userFullName = isModerator? "Moderator" : "Attendee"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return userFullName
|
2012-10-17 06:09:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private String getValidatedCourseTitle(String courseTitle){
|
|
|
|
return (courseTitle == null || courseTitle == "")? "Course": courseTitle
|
|
|
|
}
|
|
|
|
|
2012-10-18 01:58:19 +08:00
|
|
|
private String getValidatedUserId(String userId){
|
|
|
|
return (userId == null)? "": userId
|
|
|
|
}
|
2014-08-29 06:10:34 +08:00
|
|
|
|
2013-01-24 06:46:31 +08:00
|
|
|
private Integer getValidatedBBBVoiceBridge(String voiceBridge){
|
|
|
|
return (voiceBridge != null )? voiceBridge.toInteger(): 0
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-04-20 05:52:46 +08:00
|
|
|
private String getValidatedBBBRecord(String record){
|
|
|
|
return Boolean.valueOf(record).toString();
|
2013-01-24 06:46:31 +08:00
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-24 06:46:31 +08:00
|
|
|
private Integer getValidatedBBBDuration(String duration){
|
|
|
|
return (duration != null )? duration.toInteger(): 0
|
|
|
|
}
|
|
|
|
|
2013-01-29 04:53:27 +08:00
|
|
|
private String getValidatedBBBRecordingId(String recordingId){
|
|
|
|
return (recordingId != null )? recordingId: ""
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getValidatedBBBRecordingPublished(String published){
|
|
|
|
return (published != null && published.equals("true") )? "true": "false"
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-18 01:58:19 +08:00
|
|
|
private String getMonitoringMetaData(params){
|
|
|
|
String meta
|
|
|
|
|
|
|
|
meta = "meta_origin=" + bbbProxy.getStringEncoded(params.get(Parameter.TOOL_CONSUMER_CODE) == null? "": params.get(Parameter.TOOL_CONSUMER_CODE))
|
|
|
|
meta += "&meta_originVersion=" + bbbProxy.getStringEncoded(params.get(Parameter.TOOL_CONSUMER_VERSION) == null? "": params.get(Parameter.TOOL_CONSUMER_VERSION))
|
|
|
|
meta += "&meta_originServerCommonName=" + bbbProxy.getStringEncoded(params.get(Parameter.TOOL_CONSUMER_INSTANCE_DESCRIPTION) == null? "": params.get(Parameter.TOOL_CONSUMER_INSTANCE_DESCRIPTION))
|
|
|
|
meta += "&meta_originServerUrl=" + bbbProxy.getStringEncoded(params.get(Parameter.TOOL_CONSUMER_INSTANCE_URL) == null? "": params.get(Parameter.TOOL_CONSUMER_INSTANCE_URL))
|
|
|
|
meta += "&meta_context=" + bbbProxy.getStringEncoded(params.get(Parameter.COURSE_TITLE) == null? "": params.get(Parameter.COURSE_TITLE))
|
|
|
|
meta += "&meta_contextId=" + bbbProxy.getStringEncoded(params.get(Parameter.COURSE_ID) == null? "": params.get(Parameter.COURSE_ID))
|
|
|
|
meta += "&meta_contextActivity=" + bbbProxy.getStringEncoded(params.get(Parameter.RESOURCE_LINK_TITLE) == null? "": params.get(Parameter.RESOURCE_LINK_TITLE))
|
|
|
|
meta += "&meta_contextActivityDescription=" + bbbProxy.getStringEncoded(params.get(Parameter.RESOURCE_LINK_DESCRIPTION) == null? "": params.get(Parameter.RESOURCE_LINK_DESCRIPTION))
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-18 01:58:19 +08:00
|
|
|
return meta
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
/** Make an API call */
|
|
|
|
private Map<String, Object> doAPICall(String query) {
|
|
|
|
StringBuilder urlStr = new StringBuilder(query);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// open connection
|
2012-10-17 23:44:18 +08:00
|
|
|
//log.debug("doAPICall.call: " + query );
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
URL url = new URL(urlStr.toString());
|
|
|
|
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
|
|
|
|
httpConnection.setUseCaches(false);
|
|
|
|
httpConnection.setDoOutput(true);
|
|
|
|
httpConnection.setRequestMethod("GET");
|
|
|
|
httpConnection.connect();
|
|
|
|
|
|
|
|
int responseCode = httpConnection.getResponseCode();
|
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
|
|
|
// read response
|
|
|
|
InputStreamReader isr = null;
|
|
|
|
BufferedReader reader = null;
|
|
|
|
StringBuilder xml = new StringBuilder();
|
|
|
|
try {
|
|
|
|
isr = new InputStreamReader(httpConnection.getInputStream(), "UTF-8");
|
|
|
|
reader = new BufferedReader(isr);
|
|
|
|
String line = reader.readLine();
|
|
|
|
while (line != null) {
|
|
|
|
if( !line.startsWith("<?xml version=\"1.0\"?>"))
|
2014-08-29 06:10:34 +08:00
|
|
|
xml.append(line.trim());
|
2012-10-13 04:01:27 +08:00
|
|
|
line = reader.readLine();
|
|
|
|
}
|
|
|
|
} finally {
|
2014-08-29 06:10:34 +08:00
|
|
|
if (reader != null)
|
|
|
|
reader.close();
|
|
|
|
if (isr != null)
|
|
|
|
isr.close();
|
2012-10-13 04:01:27 +08:00
|
|
|
}
|
|
|
|
httpConnection.disconnect();
|
|
|
|
|
|
|
|
// parse response
|
2012-10-17 23:44:18 +08:00
|
|
|
//log.debug("doAPICall.responseXml: " + xml);
|
2012-10-13 04:01:27 +08:00
|
|
|
//Patch to fix the NaN error
|
|
|
|
String stringXml = xml.toString();
|
|
|
|
stringXml = stringXml.replaceAll(">.\\s+?<", "><");
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
Document dom = null;
|
|
|
|
dom = docBuilder.parse(new InputSource( new StringReader(stringXml)));
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
Map<String, Object> response = getNodesAsMap(dom, "response");
|
2012-10-17 23:44:18 +08:00
|
|
|
//log.debug("doAPICall.responseMap: " + response);
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
String returnCode = (String) response.get("returncode");
|
2012-10-16 23:30:12 +08:00
|
|
|
if (Proxy.APIRESPONSE_FAILED.equals(returnCode)) {
|
2012-10-13 04:01:27 +08:00
|
|
|
log.debug("doAPICall." + (String) response.get("messageKey") + ": Message=" + (String) response.get("message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} else {
|
|
|
|
log.debug("doAPICall.HTTPERROR: Message=" + "BBB server responded with HTTP status code " + responseCode);
|
|
|
|
}
|
|
|
|
} catch(IOException e) {
|
|
|
|
log.debug("doAPICall.IOException: Message=" + e.getMessage());
|
|
|
|
} catch(SAXException e) {
|
|
|
|
log.debug("doAPICall.SAXException: Message=" + e.getMessage());
|
|
|
|
} catch(IllegalArgumentException e) {
|
|
|
|
log.debug("doAPICall.IllegalArgumentException: Message=" + e.getMessage());
|
|
|
|
} catch(Exception e) {
|
|
|
|
log.debug("doAPICall.Exception: Message=" + e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2012-10-13 04:01:27 +08:00
|
|
|
/** Get all nodes under the specified element tag name as a Java map */
|
2013-01-26 06:59:11 +08:00
|
|
|
protected Map<String, Object> getNodesAsMap(Document dom, String elementTagName) {
|
2012-10-13 04:01:27 +08:00
|
|
|
Node firstNode = dom.getElementsByTagName(elementTagName).item(0);
|
|
|
|
return processNode(firstNode);
|
|
|
|
}
|
|
|
|
|
2013-01-26 06:59:11 +08:00
|
|
|
protected Map<String, Object> processNode(Node _node) {
|
|
|
|
Map<String, Object> map = new HashMap<String, Object>();
|
2012-10-13 04:01:27 +08:00
|
|
|
NodeList responseNodes = _node.getChildNodes();
|
|
|
|
for (int i = 0; i < responseNodes.getLength(); i++) {
|
|
|
|
Node node = responseNodes.item(i);
|
|
|
|
String nodeName = node.getNodeName().trim();
|
2013-01-26 06:59:11 +08:00
|
|
|
if (node.getChildNodes().getLength() == 1
|
|
|
|
&& ( node.getChildNodes().item(0).getNodeType() == org.w3c.dom.Node.TEXT_NODE || node.getChildNodes().item(0).getNodeType() == org.w3c.dom.Node.CDATA_SECTION_NODE) ) {
|
2012-10-13 04:01:27 +08:00
|
|
|
String nodeValue = node.getTextContent();
|
|
|
|
map.put(nodeName, nodeValue != null ? nodeValue.trim() : null);
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-26 06:59:11 +08:00
|
|
|
} else if (node.getChildNodes().getLength() == 0
|
|
|
|
&& node.getNodeType() != org.w3c.dom.Node.TEXT_NODE
|
|
|
|
&& node.getNodeType() != org.w3c.dom.Node.CDATA_SECTION_NODE) {
|
2012-10-13 04:01:27 +08:00
|
|
|
map.put(nodeName, "");
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-26 06:59:11 +08:00
|
|
|
} else if ( node.getChildNodes().getLength() >= 1
|
|
|
|
&& node.getChildNodes().item(0).getChildNodes().item(0).getNodeType() != org.w3c.dom.Node.TEXT_NODE
|
|
|
|
&& node.getChildNodes().item(0).getChildNodes().item(0).getNodeType() != org.w3c.dom.Node.CDATA_SECTION_NODE ) {
|
|
|
|
|
|
|
|
List<Object> list = new ArrayList<Object>();
|
|
|
|
for (int c = 0; c < node.getChildNodes().getLength(); c++) {
|
|
|
|
Node n = node.getChildNodes().item(c);
|
|
|
|
list.add(processNode(n));
|
2012-10-25 04:04:40 +08:00
|
|
|
}
|
2013-01-26 06:59:11 +08:00
|
|
|
map.put(nodeName, list);
|
2014-08-28 22:13:40 +08:00
|
|
|
|
2013-01-26 06:59:11 +08:00
|
|
|
} else {
|
|
|
|
map.put(nodeName, processNode(node));
|
2012-10-13 04:01:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|
2012-10-11 23:52:41 +08:00
|
|
|
}
|