📄 syncmlstatus.java
字号:
/*
* Copyright (C) 2006-2007 Funambol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.funambol.syncml.protocol;
import com.funambol.util.ChunkedString;
import java.util.Vector;
import com.funambol.util.XmlUtil;
import com.funambol.util.Log;
import com.funambol.util.XmlException;
/**
* This class is a container for SyncML status command.
* It can be filled from an incominig SyncML fragment using the static
* method <code>parse</code>, or can be filled with all the info and then
* used to format an outgoing Status command.
*/
public class SyncMLStatus {
//----------------------------------------------------------------- Constants
public static final int SUCCESS = 200 ;
public static final int AUTHENTICATION_ACCEPTED = 212 ;
public static final int INVALID_CREDENTIALS = 401 ;
public static final int FORBIDDEN = 403 ;
public static final int NOT_FOUND = 404 ;
public static final int GENERIC_ERROR = 500 ;
public static final int SERVER_BUSY = 503 ;
public static final int PROCESSING_ERROR = 506 ;
public static final int REFRESH_REQUIRED = 508 ;
//-------------------------------------------------------------- Private data
/** The id of this command */
private String cmdId;
/** The message reference */
private String msgRef;
/** The command reference */
private String cmdRef;
/** The command name that this status is acknowledging */
private String cmd;
/** The source reference of the item to acknowledge */
private String srcRef;
/** The target reference of the item to acknowledge */
private String tgtRef;
/** Source references for multiple items status */
private String[] items;
/** The status code for the command */
private int status;
//------------------------------------------------------------- Constructors
public SyncMLStatus() {
this("", "", "", "", null, null, 200);
}
public SyncMLStatus(String cmdId, String msgref, String cmdref,
String cmd, String src, String tgt, int status) {
setCmdId(cmdId);
setMsgRef(msgRef);
setCmdRef(cmdref);
setCmd(cmd);
setSrcRef(src);
setTgtRef(tgt);
this.items = null;
this.status = status;
}
public SyncMLStatus(String cmdId, String msgref, String cmdref,
String cmd, String[] items, int status) {
setCmdId(cmdId);
setMsgRef(msgRef);
setCmdRef(cmdref);
setCmd(cmd);
this.srcRef = null;
this.tgtRef = null;
this.items = items;
this.status = status;
}
//----------------------------------------------------------- Public methods
/** Get the command id */
public String getCmdId() {
return this.cmdId;
}
/** Set the command id */
public void setCmdId(String cmdId) {
this.cmdId = (cmdId != null) ? cmdId : null;
}
/** Get the message reference */
public String getMsgRef() {
return this.msgRef;
}
/** Set the message reference */
public void setMsgRef(String msgRef) {
this.msgRef = (msgRef != null) ? msgRef : null;
}
/** Get the command reference */
public String getCmdRef() {
return this.cmdRef;
}
/** Set the command reference */
public void setCmdRef(String cmdRef) {
this.cmdRef = (cmdRef != null) ? cmdRef : null;
}
/** Get the command name */
public String getCmd() {
return this.cmd;
}
/** Set the command name */
public void setCmd(String cmd) {
this.cmd = (cmdRef != null) ? cmd : null;
}
/** Get the source reference */
public String getSrcRef() {
return this.srcRef;
}
/** Set the source reference */
public void setSrcRef(String srcRef) {
this.srcRef = (srcRef != null) ? new String(srcRef) : null;
}
/** Get the target reference */
public String getTgtRef() {
return this.tgtRef;
}
/** Set the target reference */
public void setTgtRef(String tgtRef) {
this.tgtRef = (tgtRef != null) ? tgtRef : null;
}
/**
* Get target reference if set, or source reference otherwise
*/
public String getRef() {
return (tgtRef != null ? tgtRef : srcRef);
}
/** Get the keys of the items acknowledged by this status. */
public String[] getItemKeys() {
return this.items;
}
/** Set the keys of the items acknowledged by this status. */
public void setItemKeys(String[] items) {
this.items = items;
}
/** Get the status code */
public int getStatus() {
return this.status;
}
/** Set the status code */
public void setStatus(int status) {
this.status = status;
}
/**
* Return the SyncML representation of this status object
*/
public String toString() {
StringBuffer ret = new StringBuffer("<Status>");
ret.append("<CmdID>").append(cmdId).append("</CmdID>\n").
append("<MsgRef>").append(msgRef).append("</MsgRef>\n").
append("<CmdRef>").append(cmdRef).append("</CmdRef>\n").
append("<Cmd>").append(cmd).append("</Cmd>\n");
if (srcRef != null) {
ret.append("<SourceRef>").append(srcRef).append("</SourceRef>\n");
}
if (tgtRef != null) {
ret.append("<TargetRef>").append(tgtRef).append("</TargetRef>\n");
}
if (items != null) {
for(int i=0, l=items.length; i<l; i++) {
ret.append("<Item><Source><LocURI>").append(items[i])
.append("</LogURI></Source></Item>");
}
}
ret.append("<Data>").append(status).append("</Data>\n").
append("</Status>\n");
return ret.toString();
}
/**
* Parse a SyncML fragment and return the first status command
* found, or null in case of error.
*/
public static SyncMLStatus parse(ChunkedString xmlCommand) {
//Log.trace("Entering SyncMLStatus.parse()");
String tag = null;
// Get command Id
try {
Vector items = new Vector();
SyncMLStatus ret = new SyncMLStatus();
ret.setCmdId(XmlUtil.getTagValue(xmlCommand, SyncML.TAG_CMDID).toString());
ret.setMsgRef(XmlUtil.getTagValue(xmlCommand, SyncML.TAG_MSGREF).toString());
ret.setCmdRef(XmlUtil.getTagValue(xmlCommand, SyncML.TAG_CMDREF).toString());
ret.setCmd(XmlUtil.getTagValue(xmlCommand, SyncML.TAG_CMD).toString());
if(XmlUtil.getTag(xmlCommand, SyncML.TAG_TARGETREF) != -1) {
tag = XmlUtil.getTagValue(xmlCommand, SyncML.TAG_TARGETREF).toString();
ret.setTgtRef(tag);
}
if(XmlUtil.getTag(xmlCommand, SyncML.TAG_SOURCEREF) != -1) {
tag=XmlUtil.getTagValue(xmlCommand, SyncML.TAG_SOURCEREF).toString();
ret.setSrcRef(tag);
}
// Save the LocURI of the items acknowledged by the server
// if present.
if(XmlUtil.getTag(xmlCommand, SyncML.TAG_ITEM) != -1) {
Vector tags=XmlUtil.getTagValues(xmlCommand, SyncML.TAG_ITEM);
// TODO: if there is only one item, look for Data tag for
// the detailed message.
for(int i=0, l=tags.size(); i<l; i++) {
ChunkedString t = (ChunkedString)tags.elementAt(i);
if(XmlUtil.getTag(t, SyncML.TAG_LOC_URI) != -1) {
tag=XmlUtil.getTagValue(t, SyncML.TAG_LOC_URI).toString();
items.addElement(tag);
}
}
}
String status = XmlUtil.getTagValue(xmlCommand, SyncML.TAG_DATA).toString();
ret.setStatus( Integer.parseInt(status) );
// If there is some item, set the items array
int size = items.size();
if( size > 0 ) {
String[] itemarray = new String[size];
for (int i=0; i<size; i++) {
itemarray[i] = (String)items.elementAt(i);
}
ret.setItemKeys(itemarray);
}
//Log.trace("Exiting SyncMLStatus.parse()");
return ret;
}
catch (XmlException e){
e.printStackTrace();
Log.error("[status.parse] Error parsing server status " + xmlCommand);
}
return null;
}
/**
* Return true is the status code of this instance is the range 200-299.
*/
public boolean isSuccess() {
return (this.status >= 200 && this.status < 300);
}
/**
* Return true is the given status code is in the range 200-299.
*/
public static boolean isSuccess(int status) {
return (status >= 200 && status < 300);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -