TITLE: How To Make a SyncML Request
VERSION: $Id: howto\040make\040a\040SyncML\040request.txt,v 1.3 2003/01/08 10:21:47 stefano_fornari Exp $
DEPENDS ON:
RELATED HOW TOs:
- HOW TO Build
REQUIREMENTS:
- You have the necessary archives in your classpath:
sync4j-clientframework.jar
DESCRIPTION:
See SyncMLPost.java as an example of making a SyncML request to Sync4j.
See s4j_init.xml as example of initialization message.
See s4j_modifications.xml as example of a modifications message.
SyncMLPost.java
import java.net.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.text.MessageFormat;
import sync4j.framework.core.*;
import sync4j.framework.database.Database;
import sync4j.framework.protocol.*;
import sync4j.transport.http.client.HttpClientConnection;
import sync4j.framework.tools.ArrayUtils;
public class PostSyncML
{
public static final String SYNC_URL = "http://localhost/sync4j/sync";
public String nextURL = SYNC_URL;
public PostSyncML(String s, String s1, String credential)
{
idGenerator = new CommandIdGenerator();
clientInitXML = MessageFormat.format(s, new Object[] {sessionId, credential});
modificationsXML = MessageFormat.format(s1, new Object[] {sessionId, credential});
}
protected void sync()
{
syncInitialization();
syncModifications();
}
protected void syncInitialization()
{
String respURI = null;
System.out.println("Sync Initialization...");
Object obj = null;
Message response = null;
try
{
response = postRequest(new Message(clientInitXML));
respURI = response.getHeader().getResponseURI();
if (respURI != null) {
nextURL = respURI.toString();
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
System.out.println("Returned message: ");
System.out.println(response.toXML());
System.out.println("next url: " + nextURL);
}
protected void syncModifications()
{
System.out.println("Sync Modifications...");
Object obj = null;
Message response = null;
try
{
response = postRequest(new Message(modificationsXML));
}
catch(Exception e)
{
e.printStackTrace();
return;
}
System.out.println("Returned message: ");
System.out.println(response.toXML());
}
private StatusCommand[] modificationsStatus(SyncCommand asynccommand[])
{
ArrayList arraylist = new ArrayList();
TargetRef atargetref[] = new TargetRef[0];
SourceRef asourceref[] = new SourceRef[1];
for(int i = 0; asynccommand != null && i < asynccommand.length; i++)
{
AbstractCommand aabstractcommand[] = asynccommand[i].getCommands();
for(int j = 0; aabstractcommand != null && j < aabstractcommand.length; j++)
{
ItemizedCommand itemizedcommand = (ItemizedCommand)aabstractcommand[j];
Item aitem[] = itemizedcommand.getItems();
for(int k = 0; aitem != null && k < aitem.length; k++)
{
asourceref[0] = new SourceRef(aitem[k].getSource().getURI());
arraylist.add(new StatusCommand(idGenerator.next(), "0", itemizedcommand.getCommandIdentifier().getValue(), itemizedcommand.getName(), atargetref, asourceref, null, null, new Data(200L), new Item[0]));
}
}
}
return (StatusCommand[])arraylist.toArray(new StatusCommand[0]);
}
private Message postRequest(Message request)
throws IOException, RepresentationException {
HttpClientConnection syncMLConnection = new HttpClientConnection(nextURL);
System.out.println(">>>>" + Arrays.asList(request.getBody().getCommands()));
return syncMLConnection.sendMessage(request);
}
public static void main(String args[])
throws Exception
{
if(args.length != 3)
System.out.println("Syntax: " + (PostSyncML.class) + " client_init_filename client_modifications credential");
byte abyte0[] = new byte[(int)(new File(args[0])).length()];
FileInputStream fileinputstream = new FileInputStream(args[0]);
fileinputstream.read(abyte0);
fileinputstream.close();
byte abyte1[] = new byte[(int)(new File(args[1])).length()];
fileinputstream = new FileInputStream(args[1]);
fileinputstream.read(abyte1);
fileinputstream.close();
PostSyncML postsyncml = new PostSyncML(new String(abyte0), new String(abyte1), args[2]);
postsyncml.sync();
}
private String clientInitXML;
private String modificationsXML;
private CommandIdGenerator idGenerator;
private String sessionId = String.valueOf(System.currentTimeMillis());
}
s4j_init.xml
<SyncML>
<SyncHdr>
<VerDTD>1.1</VerDTD>
<VerProto>SyncML/1.1</VerProto>
<SessionID>{0}</SessionID>
<MsgID>1</MsgID>
<Target><LocURI>http://www.sync4j.org</LocURI></Target>
<Source><LocURI>Sync4jTest</LocURI></Source>
<Cred> <!--The authentication is optional.-->
<Meta><Type xmlns="syncml:metinf">syncml:auth-clear</Type></Meta>
<Data>{1}</Data>
</Cred>
</SyncHdr>
<SyncBody>
<Alert>
<CmdID>1</CmdID>
<Data>200</Data> <!-- 200 = TWO_WAY_ALERT -->
<Item>
<Target><LocURI>db</LocURI></Target>
<Source><LocURI>db</LocURI></Source>
<Meta>
<Anchor xmlns="syncml:metinf">
<Last>234</Last>
<Next>276</Next>
</Anchor>
</Meta>
</Item>
</Alert>
<Final/>
</SyncBody>
</SyncML>
s4j_modifications.xml
<SyncML>
<SyncHdr>
<VerDTD>1.1</VerDTD>
<VerProto>SyncML/1.1</VerProto>
<SessionID>{0}</SessionID>
<MsgID>2</MsgID>
<Target><LocURI>http://www.sync4j.org</LocURI></Target>
<Source><LocURI>Sync4jTest</LocURI></Source>
</SyncHdr>
<SyncBody>
<Sync>
<CmdID>3</CmdID>
<Target><LocURI>db</LocURI></Target>
<Source><LocURI>db</LocURI></Source>
</Sync>
<Final/>
</SyncBody>
</SyncML>