xlawrence    2005/01/28 18:44:19 CET

  Modified files:
    syncServer/src/java/org/jahia/suite/calendar/sync4jModule 
                                                              
CalendarSyncSourceOutlook.java 
  Log:
  added parser method generating outlook compatible iCalendar
  
  Revision  Changes    Path
  1.3       +108 -52   
uwcal_JSR168/syncServer/src/java/org/jahia/suite/calendar/sync4jModule/CalendarSyncSourceOutlook.java
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/uwcal_JSR168/syncServer/src/java/org/jahia/suite/calendar/sync4jModule/CalendarSyncSourceOutlook.java.diff?r1=1.2&r2=1.3&f=h
  
  
  
  Index: CalendarSyncSourceOutlook.java
  ===================================================================
  RCS file: 
/home/cvs/repository/uwcal_JSR168/syncServer/src/java/org/jahia/suite/calendar/sync4jModule/CalendarSyncSourceOutlook.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CalendarSyncSourceOutlook.java    25 Jan 2005 16:14:45 -0000      1.2
  +++ CalendarSyncSourceOutlook.java    28 Jan 2005 17:44:19 -0000      1.3
  @@ -54,13 +54,15 @@
   import sync4j.foundation.pdi.converter.Converter;
   import sync4j.foundation.pdi.parser.XMLParser;
   import sync4j.foundation.pdi.parser.ICalendarParser;
  -import sync4j.foundation.engine.source.SourceUtils; 
  +import sync4j.foundation.engine.source.SourceUtils;
   
   import java.security.Principal;
   
   import java.sql.Timestamp;
   
   import java.io.ByteArrayInputStream;
  +import java.io.BufferedReader;
  +import java.io.StringReader;
   
   import org.apache.log4j.Logger;
   
  @@ -73,48 +75,48 @@
    * @author  Xavier Lawrence
    */
   public class CalendarSyncSourceOutlook extends CalendarSyncSource
  -implements SyncSource {
  -
  +        implements SyncSource {
  +    
       public static final String NAME = "CalendarSyncSourceOutlook";
  -
  +    
       static Logger log = Logger.getLogger(CalendarSyncSourceOutlook.class);
       
       public String getName() {
           return NAME;
       }
  -
  +    
       public SyncItem[] getAllSyncItems(Principal principal)
       throws SyncSourceException {
           SyncItem[] items = super.getAllSyncItems(principal);
           return toXML(items);
       }
  -
  +    
       public SyncItem[] getDeletedSyncItems(Principal principal,
  -    Timestamp timestamp) throws SyncSourceException {
  +            Timestamp timestamp) throws SyncSourceException {
           SyncItem[] items = super.getDeletedSyncItems(principal, timestamp);
           return toXML(items);
       }
  -
  +    
       public SyncItem[] getNewSyncItems(Principal principal, Timestamp 
timestamp)
       throws SyncSourceException {
           SyncItem[] items = super.getNewSyncItems(principal, timestamp);
           return toXML(items);
       }
  -
  +    
       public SyncItem getSyncItemFromId(Principal principal,
  -    SyncItemKey syncItemKey) throws SyncSourceException {
  +            SyncItemKey syncItemKey) throws SyncSourceException {
           SyncItem item = super.getSyncItemFromId(principal, syncItemKey);
           return toXML(item);
       }
  -
  +    
       public SyncItem[] getSyncItemsFromIds(Principal principal,
  -    SyncItemKey[] syncItemKeys) throws SyncSourceException {
  +            SyncItemKey[] syncItemKeys) throws SyncSourceException {
           SyncItem[] items = super.getSyncItemsFromIds(principal, 
syncItemKeys);
           return toXML(items);
       }
  -
  +    
       public SyncItem getSyncItemFromTwin(Principal principal,
  -    SyncItem syncItem) throws SyncSourceException {
  +            SyncItem syncItem) throws SyncSourceException {
           SyncItem item = super.getSyncItemFromTwin(principal, syncItem);
           if (item != null) {
               return toXML(item);
  @@ -122,7 +124,7 @@
               return null;
           }
       }
  -
  +    
       public void removeSyncItem(Principal principal, SyncItem syncItem)
       throws SyncSourceException {
           SyncItem result = toICalendar(syncItem);
  @@ -136,17 +138,17 @@
       }
       
       public SyncItem[] getSyncItemsFromTwins(Principal principal,
  -    SyncItem[] syncItems) throws SyncSourceException {
  +            SyncItem[] syncItems) throws SyncSourceException {
           SyncItem[] items = super.getSyncItemsFromTwins(principal, syncItems);
           return toXML(items);
       }
  -
  +    
       public SyncItem[] getUpdatedSyncItems(Principal principal,
  -    Timestamp timestamp) throws SyncSourceException {
  +            Timestamp timestamp) throws SyncSourceException {
           SyncItem[] items = super.getUpdatedSyncItems(principal, timestamp);
           return toXML(items);
       }
  -
  +    
       public SyncItem setSyncItem(Principal principal, SyncItem syncItem)
       throws SyncSourceException {
           SyncItem result = toICalendar(syncItem);
  @@ -158,27 +160,27 @@
           SyncItem[] results = toICalendar(syncItems);
           return super.setSyncItems(principal, results);
       }
  -
  +    
       /**
  -     * This method converts a SyncItem, the content of which is in the 
iCalendar 
  +     * This method converts a SyncItem, the content of which is in the 
iCalendar
        * format into the Sync4j XML format.
        * @param item The SyncItem to convert
  -     * @return A new SyncItem, identical as the given parameter, but in 
  +     * @return A new SyncItem, identical as the given parameter, but in
        *         the Sync4j XML format
        * @throws SyncSourceException If something goes wrong
        */
       private SyncItem toXML(SyncItem item) throws SyncSourceException {
  -        log.debug("toXML ( "+ item.getKey().getKeyAsString() + " )"); 
  +        log.debug("toXML ( "+ item.getKey().getKeyAsString() + " )");
           SyncItem result =
  -        new SyncItemImpl(this, item.getKey().getKeyAsString(), 
item.getState());
  +                new SyncItemImpl(this, item.getKey().getKeyAsString(), 
item.getState());
           result.setProperties(item.getProperties());
  -
  +        
           byte[] byteContent = (byte[])
           item.getPropertyValue(SyncItem.PROPERTY_BINARY_CONTENT);
           if (byteContent == null) {
               byteContent = new byte[0];
           }
  -               
  +        
           // Content may be encoded
           if (!new String(byteContent).startsWith("BEGIN")) {
               byteContent = Base64.decode(byteContent);
  @@ -189,7 +191,7 @@
               return result;
           }
           
  -        try {            
  +        try {
               // Converting the i-cal item into a Calendar object
               String event = new String(byteContent);
               
  @@ -197,45 +199,47 @@
                   event += "\r\n";
               }
               
  -            String content = 
  -            "BEGIN:VCALENDAR\r\n" +           
  -            "PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN\r\n" +
  -            "VERSION:2.0\r\n" +
  -            "METHOD:PUBLISH\r\n" + 
  -             event + 
  -            "END:VCALENDAR";
  +            String content =
  +                    "BEGIN:VCALENDAR\r\n" +
  +                    "PRODID:-//Microsoft Corporation//Outlook 11.0 
MIMEDIR//EN\r\n" +
  +                    "VERSION:2.0\r\n" +
  +                    "METHOD:PUBLISH\r\n" +
  +                    event +
  +                    "END:VCALENDAR";
               
               log.debug("content: "+content);
               
  -            ByteArrayInputStream buffer = new ByteArrayInputStream 
(content.getBytes());
  +            content = compatibleString(content);
  +            
  +            ByteArrayInputStream buffer = new 
ByteArrayInputStream(content.getBytes());
               
               ICalendarParser parser = new ICalendarParser(buffer);
               Calendar calendar = (Calendar)parser.iCalendar();
               
  -//            log.debug("calendar: "+calendar.toString());
  +            //            log.debug("calendar: "+calendar.toString());
               
               // Converting the Calendar object into XML
               Converter converter = new Converter();
               String xml = converter.calendarToXML(calendar);
               
               result.setProperty(new 
SyncProperty(SyncItem.PROPERTY_BINARY_CONTENT,
  -            Base64.encode(xml.getBytes())));
  -//            log.debug("XML: "+xml);
  +                    Base64.encode(xml.getBytes())));
  +            //            log.debug("XML: "+xml);
               
           } catch (Exception e) {
               log.error(e.getClass().getName()+": "+e.getMessage());
               e.printStackTrace();
               throw new SyncSourceException(e.getMessage(), e);
           }
  -
  +        
           return result;
       }
  -
  +    
       /**
  -     * This method converts an array of SyncItems, the content of which is 
in 
  +     * This method converts an array of SyncItems, the content of which is in
        * the iCalendar format into the Sync4j XML format.
        * @param item The SyncItem to convert
  -     * @return A new SyncItem, identical as the given parameter, but in 
  +     * @return A new SyncItem, identical as the given parameter, but in
        *         the Sync4j XML format
        * @throws SyncSourceException If something goes wrong
        */
  @@ -246,19 +250,19 @@
           }
           return result;
       }
  -
  +    
       /**
        * This method converts a SyncItem, the content of which is in the Sync4j
        * XML format, into the iCalendar format.
        * @param item The SyncItem to convert
  -     * @return A new SyncItem, identical as the given parameter, but in 
  +     * @return A new SyncItem, identical as the given parameter, but in
        *         the iCalendar format
        * @throws SyncSourceException If something goes wrong
        */
       private SyncItem toICalendar(SyncItem item) throws SyncSourceException {
  -        log.debug("toICalendar ( "+ item.getKey().getKeyAsString() + " )"); 
  +        log.debug("toICalendar ( "+ item.getKey().getKeyAsString() + " )");
           SyncItem result =
  -        new SyncItemImpl(this, item.getKey().getKeyAsString(), 
item.getState());
  +                new SyncItemImpl(this, item.getKey().getKeyAsString(), 
item.getState());
           result.setProperties(item.getProperties());
           
           byte[] byteContent = (byte[])
  @@ -268,7 +272,7 @@
           }
           
           final String content = new String(Base64.decode(byteContent));
  -//        log.debug("content: "+content);
  +        //        log.debug("content: "+content);
           try {
               // Converting the xmlStream into a Calendar object
               ByteArrayInputStream xmlStream = new 
ByteArrayInputStream(content.getBytes());
  @@ -283,8 +287,8 @@
               ical = ical.substring(0, 
ical.length()-"END:VCALENDAR".length()-3);
               String event = new String(ical.getBytes());
               result.setProperty(new 
SyncProperty(SyncItem.PROPERTY_BINARY_CONTENT,
  -            (event.getBytes())));
  -//            log.debug("iCal: "+event);
  +                    (event.getBytes())));
  +            //            log.debug("iCal: "+event);
               
           } catch (Exception e) {
               e.printStackTrace();
  @@ -293,12 +297,12 @@
           }
           return result;
       }
  -
  +    
       /**
  -     * This method converts an array of SyncItems, the content of which is 
in 
  +     * This method converts an array of SyncItems, the content of which is in
        * the Sync4j XML format, into the iCalendar format.
        * @param item The SyncItem to convert
  -     * @return A new SyncItem, identical as the given parameter, but in 
  +     * @return A new SyncItem, identical as the given parameter, but in
        *         the iCalendar format
        * @throws SyncSourceException If something goes wrong
        */
  @@ -309,4 +313,56 @@
           }
           return result;
       }
  +    
  +    /**
  +     * This method goes through a vCalendar represented by a String and 
removes
  +     * all unknown iCalendar properties preventing MS-Outlook from failing
  +     * the whole synch.
  +     * At the moment the following proerties are removed:
  +     * <ul>
  +     *   <li>CONTACT</li>
  +     * </ul>
  +     */
  +    private String compatibleString(String vCalendar) throws 
SyncSourceException {
  +        BufferedReader reader = new BufferedReader(new 
StringReader(vCalendar));
  +        
  +        int state = 0;
  +        String line, result = "";
  +        try {
  +            while ((line = reader.readLine()) != null) {
  +                
  +                switch (state) {
  +                    // Main state
  +                    case 0:
  +                        // Remove the CONTACT property
  +                        if (!line.startsWith("CONTACT")) {
  +                            result += line;
  +                            break;
  +                        }
  +                        log.debug("Removed a line in state 0: "+line);
  +                        state = 1;
  +                        break;
  +                        
  +                        // State removing any extra lines if the desired 
removed
  +                        // property goes through more than 1 line
  +                    case 1:
  +                        if (line.endsWith("\r\n")) {
  +                            result += line;
  +                            state = 0;
  +                            break;
  +                        }
  +                        log.debug("Removed a line in state 1: "+line);
  +                        break;
  +                        
  +                        // Should not happen
  +                    default:
  +                        throw new SyncSourceException("state != 0 or 1");
  +                }
  +            }
  +        } catch (Exception e) {
  +            throw new SyncSourceException(e.getMessage(), e);
  +        }
  +        
  +        return result;
  +    }
   }
  

Reply via email to