public String sendMessage(final String request) throws Exception,
IOException,
NetworkException {
StaticDataHelper.log("*************************************************");
HttpConnection httpCon = null;
DataInputStream inputStream = null;
DataOutputStream outputStream = null;
int rc = 0;
StringBuffer b = null;
byte[] data = null;
synchronized (this) {
boolean signal = true;
if ((RadioInfo.getState() != RadioInfo.STATE_ON) ||
(RadioInfo.getSignalLevel() == RadioInfo.LEVEL_NO_COVERAGE)) {
signal = false;
}
if (!signal) {
throw new NetworkException();
}
try {
StaticDataHelper sdh = new StaticDataHelper();
byte[] dataToSend = request.getBytes();//the SyncML message to send in byte form
// ----------------------------------------------------------------------------------------------------------
String sessionId = SyncManagerImpl.session_id;
StaticDataHelper.log("### Opening the connection nr. " + ++(SyncManagerImpl.c) + " in session " + sessionId);
httpCon = (HttpConnection)Connector.open(requestURL + ";deviceside=true");//'true' direct TCP, 'false' proxy TCP (MDS)
String url = httpCon.getURL();
String jsessionid;
String jsessionidParam = "jsessionid=";
String devicesideParam = ";deviceside=";
//StaticDataHelper.log("[DEBUG] URL: " + url);
int startIndex = url.indexOf(jsessionidParam) + jsessionidParam.length();
int endIndex = url.indexOf(devicesideParam);
//StaticDataHelper.log("[DEBUG] startIndex: " + startIndex);
//StaticDataHelper.log("[DEBUG] endIndex: " + endIndex);
if (url.indexOf(jsessionidParam) != -1 && endIndex != -1) {
jsessionid = url.substring(startIndex, endIndex);
} else {
jsessionid = "jsessionid from server not yet available";
}
StaticDataHelper.log("### Connection nr. " + SyncManagerImpl.c + " in session " + sessionId + " [" + jsessionid + "] opened");
// --------------------------------------------------------------------------------------------------------------------------------------
httpCon.setRequestMethod(HttpConnection.POST);
httpCon.setRequestProperty (PROP_USER_AGENT,
"BlackBerry/" + sdh.getOS());
httpCon.setRequestProperty (PROP_CONTENT_LENGTH,
String.valueOf(dataToSend.length));
httpCon.setRequestProperty (PROP_CONTENT_TYPE,
"application/vnd.syncml+xml");
String locale = System.getProperty(PROP_MICROEDITION_LOCALE);
if (locale != null) {
httpCon.setRequestProperty(PROP_CONTENT_LANGUAGE, locale);
}
outputStream = httpCon.openDataOutputStream();
outputStream.write(dataToSend);
outputStream.flush();
inputStream = httpCon.openDataInputStream();
int size = (int)httpCon.getLength();//in an HTTP connection it has to be the value of the
//'content-length' header field. When not known, -1
b = new StringBuffer(size >= 0 ? (int)size : 4096);
if (size != -1) {
// Read content-Length bytes, or until max is reached.
int ch = 0;
for (int i = 0; i < size; i++) {
if ((ch = inputStream.read()) != -1) {
b.append((char)ch);
}
}
} else {
// Read til the connection is closed, or til max is reached.
// (Typical HTTP/1.0 script generated output)
int ch = 0;
size = 0;
while ((ch = inputStream.read()) != -1) {
b.append((char)ch);
}
}
data = b.toString().getBytes();
}//end try
catch (Exception e) {
StaticDataHelper.log(">>> Exception in SyncMLClientImpl.sendMessage(request): " + e.toString());
throw e;
}
finally {
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
if (outputStream != null) {
outputStream.close();
outputStream = null;
}
if (httpCon != null) {
// ----------------------------------------------------------------------------------------------------
String url = httpCon.getURL();
String sessionId = SyncManagerImpl.session_id;
String jsessionid;
String jsessionidParam = "jsessionid=";
String devicesideParam = ";deviceside=";
//StaticDataHelper.log("[DEBUG] URL: " + url);
int startIndex = url.indexOf(jsessionidParam) + jsessionidParam.length();
int endIndex = url.indexOf(devicesideParam);
//StaticDataHelper.log("[DEBUG] startIndex: " + startIndex);
//StaticDataHelper.log("[DEBUG] endIndex: " + endIndex);
if (url.indexOf(jsessionidParam) != -1 && endIndex != -1) {
jsessionid = url.substring(startIndex, endIndex);
} else {
jsessionid = "jsessionid from server not yet available";
}
StaticDataHelper.log("### Closing connection nr. " + SyncManagerImpl.c + " for session " + sessionId + " [" + jsessionid + "]");
httpCon.close();
StaticDataHelper.log("### Connection nr. " + SyncManagerImpl.c + " closed");
// ----------------------------------------------------------------------------------------------------
httpCon = null;
}