📄 syncmanager.java
字号:
* serverAlerts
* <p>If this is the first sync for the source, the status code might change
* according to the value of the PARAM_FIRST_TIME_SYNC_MODE configuration
* property
* <p>If firstTimeSyncMode is not set, the alert is left unchanged. If it is
* set to a value, the specified value is used instead
*
* @param msg The message to be checked
*
* @throws SyncException In case of errors
**/
private void checkServerAlerts(ChunkedString msg) throws SyncException {
ChunkedString target = null;
ChunkedString code = null;
Vector alertTags = null;
serverAlerts = new Hashtable();
try {
alertTags = XmlUtil.getTagValues(
XmlUtil.getTagValues(
XmlUtil.getTagValues(
msg,
SyncML.TAG_SYNCML),
SyncML.TAG_SYNCBODY),
SyncML.TAG_ALERT);
for (int i = 0, l = alertTags.size(); i < l; i++) {
ChunkedString alert = (ChunkedString) alertTags.elementAt(i);
code = XmlUtil.getTagValue(alert, SyncML.TAG_DATA);
Vector items = XmlUtil.getTagValues(alert, SyncML.TAG_ITEM);
for (int j = 0, m = items.size(); j < m; j++) {
ChunkedString targetTag = (ChunkedString) items.elementAt(j);
target = XmlUtil.getTagValue(targetTag, SyncML.TAG_TARGET);
target = XmlUtil.getTagValue(target, SyncML.TAG_LOC_URI);
Log.info("The server alert code for " + target + " is " + code);
serverAlerts.put(target.toString(), code.toString());
}
}
} catch (XmlException xe) {
Log.error("checkServerAlerts: error parsing server alert " + msg);
xe.printStackTrace();
throw new SyncException(
SyncException.SERVER_ERROR,
"Invalid alert from server.");
}
}
/**
* Prepare a SyncML Message header.
*
* @param sessionid the session id to use.
* @param msgid the message id to use.
* @param src the source uri
* @param tgt the target uri
* @param tags other SyncML tags to insert in the header.
* (e.g. <Cred> or <Meta>).
*/
private String prepareSyncHeader(String sessionid,
String msgid,
String src,
String tgt,
String tags) {
StringBuffer ret = new StringBuffer();
ret.append("<SyncHdr>\n").append("<VerDTD>1.2</VerDTD>\n").append("<VerProto>SyncML/1.2</VerProto>\n").append("<SessionID>").append(sessionid).append("</SessionID>\n").append("<MsgID>").append(msgid).append("</MsgID>\n").append("<Target><LocURI>").append(tgt).append("</LocURI></Target>\n").append("<Source><LocURI>").append(src).append("</LocURI></Source>\n");
if (tags != null) {
ret.append(tags);
}
ret.append("</SyncHdr>\n");
return ret.toString();
}
/**
* Prepares inizialization SyncML message
*/
private String prepareInizializationMessage(int syncMode)
throws SyncException {
String b64login = new String(Base64.encode(login.getBytes()));
StringBuffer ret = new StringBuffer("<SyncML>\n");
// Add <Cred> and <Meta> to the syncHdr
StringBuffer tags = new StringBuffer("<Cred>\n");
tags.append("<Meta>").append("<Type xmlns=\"syncml:metinf\">syncml:auth-basic</Type>\n").append("<Format xmlns=\"syncml:metinf\">b64</Format>\n").append("</Meta>\n").append("<Data>").append(b64login).append("</Data>").append("</Cred>\n");
// Meta for the maxmsgsize
tags.append("<Meta><MaxMsgSize>").append(maxMsgSize).append("</MaxMsgSize></Meta>\n");
// Add SyncHdr
ret.append(prepareSyncHeader(sessionID, resetMsgID(),
deviceId, serverUrl,
tags.toString()));
// Add SyncBody
ret.append("<SyncBody>\n").append(createAlerts(source, syncMode));
// Add DevInf
if (sendDevInf) {
ret.append(createPut(config.deviceConfig));
}
ret.append("<Final/>").append("</SyncBody>\n");
ret.append("</SyncML>\n");
tags = null;
return ret.toString();
}
/**
* Process the <Format> tag and return the requested modification
* in a String array.
*/
private String[] processFormat(ChunkedString xml) {
String[] ret = null;
try {
if (XmlUtil.getTag(xml, "Format") != -1) {
ChunkedString format = XmlUtil.getTagValue(xml, "Format");
if (format != null && !format.equals("")) {
ret = StringUtil.split(format.toString(), ";");
}
}
} catch (XmlException e) {
Log.error("[processFormat] Error parsing format from server: " + xml + ". Ignoring it.");
e.printStackTrace();
}
return ret;
}
/**
* Get the format string to add to the outgoing message.
*
* @return the Format string, according to the source encoding
*/
private String getFormat() {
// Get the Format tag from the SyncSource encoding.
if (!source.getEncoding().equals(source.ENCODING_NONE)) {
return "<Format xmlns=\'syncml:metinf\'>" + source.getEncoding() + "</Format>\n";
} else {
return "";
}
}
/**
* Encode the item data according to the format specified by the SyncSource.
*
* @param formats the list of requested encodings (des, 3des, b64)
* @param data the byte array of data to encode
* @return the encoded byte array, or <code>null</code> in case of error
*/
private byte[] encodeItemData(String[] formats, byte[] data) {
if (formats != null && data != null) {
// If ecryption types are specified, apply them
for (int count = formats.length - 1; count >= 0; count--) {
String encoding = formats[count];
if (encoding.equals("b64")) {
data = Base64.encode(data);
}
/*
else if (encoding.equals("des")) {
// DES not supported now, ignore SyncSource encoding
}
else if (currentDecodeType.equals("3des")) {
// 3DES not supported now, ignore SyncSource encoding
}
*/
}
}
return data;
}
/**
* Decode the item data according to the format specified by the server.
*
* @param formats the list of requested decodings (des, 3des, b64)
* @param data the byte array of data to decode
* @return the decode byte array, or <code>null</code> in case of error
*
* @throws UnsupportedEncodingException
*/
private byte[] decodeItemData(String[] formats, byte[] data)
throws UnsupportedEncodingException {
if (formats != null && data != null) {
// If ecryption types are specified, apply them
for (int count = formats.length - 1; count >= 0; count--) {
String currentDecodeType = formats[count];
if (currentDecodeType.equals("b64")) {
data = Base64.decode(data);
} else if (currentDecodeType.equals("des")) {
// Error, DES not supported now, send error to the server
return null;
/*
desCrypto = new Sync4jDesCrypto(Base64.encode(login.getBytes()));
data = desCrypto.decryptData(data);
*/
} else if (currentDecodeType.equals("3des")) {
// Error, 3DES not supported now, send error to the server
return null;
/*
sync3desCrypto = new Sync4j3DesCrypto(Base64.encode(login.getBytes()));
data = sync3desCrypto.decryptData(data);
*/
}
}
}
return data;
}
/**
* Get an item from the SyncML tag.
*
* @param command The name command from server
* @param type the mime type of the item
* @param xmlItem the SyncML tag for this item
*
* @return the SyncItem object
*
* @throws SyncException if the command parsing failed
*
*/
private SyncItem getItem(char state, String type, ChunkedString xmlItem, String[] formatList) throws SyncException {
String key = null;
String data = null;
String parent = null;
byte[] content = null;
// Get item key
try {
key = XmlUtil.getTagValue(xmlItem, SyncML.TAG_LOC_URI).toString();
} catch (XmlException e) {
Log.error("[getItem] Invalid item key from server: " + xmlItem);
e.printStackTrace();
throw new SyncException(
SyncException.SERVER_ERROR,
"Invalid item key from server.");
}
// Get item parent, if present
if (XmlUtil.getTag(xmlItem, SyncML.TAG_TARGET_PARENT) > 0) {
try {
parent = XmlUtil.getTagValue(
XmlUtil.getTagValue(
xmlItem, SyncML.TAG_TARGET_PARENT),
SyncML.TAG_LOC_URI).toString();
} catch (XmlException e) {
Log.error("[getItem] Invalid item parent from server: " + e.toString());
throw new SyncException(
SyncException.SERVER_ERROR,
"Invalid item parent from server.");
}
}
// Get the item data, if present
if (XmlUtil.getTag(xmlItem, SyncML.TAG_DATA) != -1) {
try {
// Get item data
data = XmlUtil.getTagValue(xmlItem, SyncML.TAG_DATA).toString();
if (formatList != null) {
// Format tag from server
content = decodeItemData(formatList, data.getBytes());
} else if (!source.getEncoding().equals(source.ENCODING_NONE)) {
// If the server does not send a format, apply the one
// defined for the sync source
formatList = processFormat(new ChunkedString(source.getEncoding()));
content = decodeItemData(formatList, data.toString().getBytes());
} else {
// Else, the data is text/plain,
// and the XML special chars are escaped.
// The encoding must be set to UTF-8
// in order to read the symbols like "euro"
content = XmlUtil.unescapeXml(data).getBytes("UTF-8");
}
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
Log.error("[getItem] Can't decode content for item: " + key);
// in case of error, the content is null
// and this will be reported as an error to the server
content = null;
} catch (XmlException xe) {
xe.printStackTrace();
Log.error("[getItem] Can't parse data tag for item: " + key);
// in case of error, the content is null
// and this will be reported as an error to the server
content = null;
}
}
// Create the item
return new SyncItem(
key, type, state, parent, content);
}
/**
* Processes an item in a modification command received from server.
*
* @param cmd the cmmand info
* @param xmlItem the SyncML tag for this item
* @return the status code for this item
*
* @throws SyncException if the command parsing failed
*
*/
private SyncMLStatus processSyncItem(SyncMLCommand cmd, ChunkedString xmlItem, String [] formatList)
throws SyncException {
int status = 0;
SyncItem item = null;
String guid = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -