Example.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  * 
00024  */
00025 
00026 package org.openmobileis.synchro.openmsp.protocol.test;
00027 
00028 import org.openmobileis.synchro.openmsp.OpenMSPException;
00029 import org.openmobileis.synchro.openmsp.protocol.*;
00030 
00039 public class Example extends Handler {
00040 
00041   String tabs = "";
00042 
00043   public Example() {
00044   }
00045 
00046   /* build the following sample message :
00047           <OpenMSP>
00048         <SyncHdr>
00049         <Version>1.0</Version>
00050         <SessionID>200</SessionID>
00051         <MsgID>1</MsgID>
00052         <Source><LocURI>header source</LocURI></Source>
00053         <Target><LocURI>target header</LocURI></Target>
00054         <Cred><Meta>cred meta</Meta><Data>cred data</Data></Cred>
00055         </SyncHdr>
00056         <SyncBody>
00057         <Sync>
00058         <CmdID>1</CmdID>
00059         <Source><LocURI>sync source</LocURI></Source>
00060         <Target><LocURI>sync target</LocURI></Target>
00061         <Add>
00062         <CmdID>2</CmdID>
00063         <Cred><Meta>credential add meta</Meta><Data>credential add data</Data></Cred>
00064         <Meta>add meta information</Meta>
00065         <Item>
00066         <Source><LocURI>source item 2</LocURI></Source>
00067         <Target><LocURI>target item 2</LocURI></Target>
00068         <Meta>meta item 2</Meta>
00069         <Data><![CDATA[<xml><truc>
00070         <titi>TOTO</titi>
00071         </truc></xml>]]></Data>
00072         </Item>
00073         <Item>
00074         <Source><LocURI>source item 1</LocURI></Source>
00075         <Target><LocURI>target item 1</LocURI></Target>
00076         <Meta>meta item 1</Meta>
00077         <Data>data item 1</Data>
00078         </Item>
00079         </Add>
00080         </Sync>
00081         </Final></SyncBody></OpenMSP>
00082         */
00083   public static Message buildMessage() throws OpenMSPException {
00084     Message message = MessageFactory.getFactory().createMessage(new Header(200, "header source", "target header", "cred meta", "cred data"));
00085     // create a sync command
00086     ContainerMessage syncMessage = new ContainerMessage(new Command(Element.SYNC, "sync source", "sync target"));
00087     // create add command for sync command
00088     ContainerMessage addMessage = new ContainerMessage(new RequestCommand(Element.ADD));
00089     ((RequestCommand)addMessage.getElement()).setCredential("credential add meta", "credential add data");
00090     ((RequestCommand)addMessage.getElement()).setMetaInformation("add meta information");
00091     // add two data items for add sub command
00092     addMessage.add(new DataItem(Element.ITEM,"meta item 2", "<xml><truc>\n<titi>TOTO</titi>\n</truc></xml>", "source item 2", "target item 2"));
00093     addMessage.add(new DataItem(Element.ITEM,"meta item 1", "data item 1", "source item 1", "target item 1"));
00094     syncMessage.add(addMessage);
00095     message.add(syncMessage);
00096     return message;
00097    }
00098 
00099   /*
00100   * Second Message
00101   *<OpenMSP>
00102           <SyncHdr>
00103           <Version>1.0</Version>
00104           <SessionID>200</SessionID>
00105           <MsgID>1</MsgID>
00106           <Source><LocURI>header source</LocURI></Source>
00107           <Target><LocURI>target header</LocURI></Target>
00108           <Cred><Meta>cred meta</Meta><Data>cred data</Data></Cred>
00109           </SyncHdr>
00110           <SyncBody>
00111           <Sequence>
00112           <CmdID>5</CmdID>
00113           <Map>
00114           <CmdID>4</CmdID>
00115           <Source><LocURI>sync1 source</LocURI></Source>
00116           <Target><LocURI>sync1 target</LocURI></Target>
00117           <MapItem>
00118           <Source><LocURI>source item 1</LocURI></Source>
00119           <Target><LocURI>target item 1</LocURI></Target>
00120           </MapItem>
00121           <MapItem>
00122           <Source><LocURI>source item 2</LocURI></Source>
00123           <Target><LocURI>target item 2</LocURI></Target>
00124           </MapItem>
00125           </Map>
00126           <Sync>
00127           <CmdID>1</CmdID>
00128           <Source><LocURI>sync source</LocURI></Source>
00129           <Target><LocURI>sync target</LocURI></Target>
00130           <Add>
00131           <CmdID>2</CmdID>
00132           <Cred><Meta>credential add meta</Meta><Data>credential add data</Data></Cred>
00133           <Meta>add meta information</Meta>
00134           <Item>
00135           <Source><LocURI>source item 1</LocURI></Source>
00136           <Target><LocURI>target item 1</LocURI></Target>
00137           <Meta>meta item 1</Meta>
00138           <Data>data item 1</Data>
00139           </Item>
00140           <Item>
00141           <Source><LocURI>source item 2</LocURI></Source>
00142           <Target><LocURI>target item 2</LocURI></Target>
00143           </Item>
00144           </Add>
00145           <Delete>
00146           <CmdID>3</CmdID>
00147           <Item>
00148           <Meta>meta item 1 for delete</Meta>
00149           <Data>data item 1  for delete</Data>
00150           </Item>
00151           </Delete>
00152           </Sync>
00153           </Sequence>
00154           <Get>
00155           <CmdID>6</CmdID>
00156           <Item>
00157           <Source><LocURI>source item 1</LocURI></Source>
00158           <Target><LocURI>target item 1</LocURI></Target>
00159           <Meta>meta item 1</Meta>
00160           <Data>data item 1</Data>
00161           </Item>
00162           <Item>
00163           <Source><LocURI>source item 1</LocURI></Source>
00164           <Target><LocURI>target item 1</LocURI></Target>
00165           <Meta>meta item 1</Meta>
00166           <Data>data item 1</Data>
00167           </Item>
00168           </Get>
00169           <Result>
00170           <CmdID>7</CmdID>
00171           <CmdRef>300</CmdRef>
00172           <SourceRef>sourc Ref 0</SourceRef>
00173           <TargetRef>target ref 0</TargetRef>
00174           <Item>
00175           <Source><LocURI>source item 1</LocURI></Source>
00176           <Target><LocURI>target item 1</LocURI></Target>
00177           <Meta>meta item 1</Meta>
00178           <Data>data item 1</Data>
00179           </Item>
00180           <Item>
00181           <Source><LocURI>source item 2</LocURI></Source>
00182           <Target><LocURI>target item 2</LocURI></Target>
00183           <Meta>meta item 2</Meta>
00184           <Data><![CDATA[<xml><truc>TOTO</truc></xml>]]></Data>
00185           </Item>
00186           </Result>
00187           <Result>
00188           <CmdID>8</CmdID>
00189           <CmdRef>301</CmdRef>
00190           <SourceRef>sourc Ref 1</SourceRef>
00191           <TargetRef>target ref 1</TargetRef>
00192           <Item>
00193           <Source><LocURI>source item 1</LocURI></Source>
00194           <Target><LocURI>target item 1</LocURI></Target>
00195           <Meta>meta item 1</Meta>
00196           <Data>data item 1</Data>
00197           </Item>
00198           <Item>
00199           <Source><LocURI>source item 2</LocURI></Source>
00200           <Target><LocURI>target item 2</LocURI></Target>
00201           <Meta>meta item 2</Meta>
00202           <Data><![CDATA[<xml><truc>TOTO</truc></xml>]]></Data>
00203           </Item>
00204           </Result>
00205           <Result>
00206           <CmdID>9</CmdID>
00207           <CmdRef>302</CmdRef>
00208           <SourceRef>sourc Ref 2</SourceRef>
00209           <TargetRef>target ref 2</TargetRef>
00210           <Item>
00211           <Source><LocURI>source item 1</LocURI></Source>
00212           <Target><LocURI>target item 1</LocURI></Target>
00213           <Meta>meta item 1</Meta>
00214           <Data>data item 1</Data>
00215           </Item>
00216           <Item>
00217           <Source><LocURI>source item 2</LocURI></Source>
00218           <Target><LocURI>target item 2</LocURI></Target>
00219           <Meta>meta item 2</Meta>
00220           <Data><![CDATA[<xml><truc>TOTO</truc></xml>]]></Data>
00221           </Item>
00222           </Result>
00223           <Status><CmdID>10</CmdID>
00224           <CmdRef>4000</CmdRef>
00225           <Data>200</Data>
00226           </Status>
00227           </SyncBody></OpenMSP>
00228   */
00229   public static Message buildCompleteMessage() throws OpenMSPException {
00230     Message message = MessageFactory.getFactory().createMessage(new Header(200, "header source", "target header", "cred meta", "cred data"), false);
00232     ContainerMessage syncMessage = new ContainerMessage(new Command(Element.SYNC, "sync source", "sync target"));
00233     // create add command for sync command
00234     ContainerMessage addMessage = new ContainerMessage(new RequestCommand(Element.ADD));
00235     ((RequestCommand)addMessage.getElement()).setCredential("credential add meta", "credential add data");
00236     ((RequestCommand)addMessage.getElement()).setMetaInformation("add meta information");
00237     // add two data items for add sub command
00238     addMessage.add(new DataItem(Element.ITEM,"meta item 1", "data item 1", "source item 1", "target item 1"));
00239     addMessage.add(new Item(Element.ITEM, "source item 2", "target item 2"));
00240     syncMessage.add(addMessage);
00241     // create delete command
00242      ContainerMessage deleteMessage = new ContainerMessage(new RequestCommand(Element.DELETE));
00243     // add item
00244     deleteMessage.add(new DataItem(Element.ITEM,"meta item 1 for delete", "data item 1  for delete", null, null));
00245     syncMessage.add(deleteMessage);
00247     ContainerMessage mapMessage = new ContainerMessage(new Command(Element.MAP, "sync1 source", "sync1 target"));
00248     // add two data items for add sub command
00249     mapMessage.add(new Item(Element.MAP_ITEM, "source item 1", "target item 1"));
00250     mapMessage.add(new Item(Element.MAP_ITEM, "source item 2", "target item 2"));
00251     // create sequence to process map command before sync  command
00252     ContainerMessage sequence = new ContainerMessage(new Sequence());
00253     sequence.add(mapMessage);
00254     sequence.add(syncMessage);
00255     message.add(sequence);
00257     ContainerMessage getMessage = new ContainerMessage(new Command(Element.GET, "get1 source", "get1 target"));
00258     getMessage.add(new DataItem(Element.ITEM,"meta item 1", "data item 1", "source item 1", "target item 1"));
00259     getMessage.add(new DataItem(Element.ITEM,"meta item 1", "data item 1", "source item 1", "target item 1"));
00260     message.add(getMessage);
00262     for (int i = 0; i < 3; i++) {
00263       ContainerMessage resultMessage = new ContainerMessage(new Result(300 + i, "sourc Ref " + i, "target ref " + i));
00264       resultMessage.add(new DataItem(Element.ITEM,"meta item 1", "data item 1", "source item 1", "target item 1"));
00265       resultMessage.add(new DataItem(Element.ITEM,"meta item 2", "<xml><truc>TOTO</truc></xml>", "source item 2", "target item 2"));
00266       message.add(resultMessage);
00267     }
00268     // add a status
00269     message.add(new Status(4000, Status.STATUS_OK));
00270     return message;
00271   }
00272 
00273   private static void printMessage (String tabs, ContainerMessage message) {
00274      System.out.println(tabs + message.getElement());
00275      message.resetCursor();
00276      while (message.hasMoreMessage()) {
00277       ContainerMessage child = message.nextMessage();
00278       printMessage (tabs + "\t", child);
00279      }
00280   }
00281 
00282   public static void main(String[] args) {
00283     // create a sample openMSP message
00284     try {
00285 
00286      Message message = buildMessage();
00287      String cyberML = message.encode();
00288      System.out.println("************ Sample message *********");
00289      System.out.println(cyberML);
00290      Message newMessage = MessageFactory.getFactory().getMessage(message.encode());
00291      //System.out.println("************ Sample message after encoding *********");
00292      System.out.println(newMessage.encode());
00293      // go through the structure
00294    printMessage ("", newMessage.getRoot());
00295 
00296    Message message1 = buildCompleteMessage();
00297    System.out.println(message1.encode());
00298    // go through the structure
00299    printMessage ("", message1.getRoot());
00300    newMessage = MessageFactory.getFactory().getMessage(message1.encode());
00301    newMessage.add(new Status(3, Status.STATUS_FAILED));
00302    printMessage("", newMessage.getRoot());
00303    System.out.println(newMessage.encode());
00304    System.out.println("Is empty : (should not be...) " + newMessage.isEmpty());
00305    System.out.println("Is empty : (should be) " + MessageFactory.getFactory().createMessage(new Header(1,"source","target")).isEmpty());
00306     } catch (OpenMSPException e) {
00307       e.printStackTrace();
00308       }
00309   }
00310 
00311 }

Generated on Mon Dec 4 11:03:26 2006 for OpenMobileIS by  doxygen 1.5.1-p1