Catch throwables, not just exceptions (catches contact parser errors)
[funambol-groupdav-connector.git] / src / main / java / net / bionicmessage / funambol / groupdav / notes / OutboundFunambolNoteObject.java
blobca0f73936ae545161b39363dc5f517b192c67a1b
1 /*
2 * OutboundFunambolContactObject.java
4 * Created on Apr 28 2009
5 *
6 * Copyright (C) 2009 credativ GmbH
7 *
8 * GroupDAV connector for Funambol v6.5
9 * Heavily based on OutboundFunambolContactObject.java [Copyright (C) 2007 Mathew McBride]
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as
13 * published by the Free Software Foundation, either version 3 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 package net.bionicmessage.funambol.groupdav.notes;
26 import net.bionicmessage.funambol.datakit.NoteConverter;
27 import net.bionicmessage.funambol.framework.OutboundObject;
28 import com.funambol.common.pim.converter.BaseConverter;
29 import com.funambol.common.pim.converter.NoteToSIFN;
30 import com.funambol.common.pim.note.Note;
31 import com.funambol.common.pim.sif.SIFNParser;
32 import com.funambol.common.pim.vnote.VNoteParser;
33 import com.funambol.framework.engine.SyncItem;
34 import com.funambol.framework.engine.SyncItemImpl;
35 import com.funambol.framework.engine.source.SyncSource;
36 import java.io.ByteArrayInputStream;
37 import java.io.UnsupportedEncodingException;
38 import java.util.logging.Logger;
39 import net.bionicmessage.funambol.framework.ObjectTransformationException;
40 import net.bionicmessage.funambol.framework.Constants;
41 import net.bionicmessage.funambol.groupdav.calendar.iCalSourceObject;
42 import net.bionicmessage.utils.QPDecode;
43 import net.fortuna.ical4j.data.CalendarBuilder;
44 import net.fortuna.ical4j.model.Calendar;
46 /**
47 * @author Marc Brockschmidt
49 public final class OutboundFunambolNoteObject extends iCalSourceObject implements OutboundObject {
50 /**
51 * The note we are working on.
53 private Note noteObject;
55 /**
56 * Logging object used throughout the code.
58 private Logger log = null;
60 /**
61 * Default constructor for OutboundFunambolNoteObject, creating an
62 * object based on a pre-existing ical calendar.
63 * @param note a Note to be sent to the device
64 * @param l Logger object used throughout the code.
66 public OutboundFunambolNoteObject(final Note note, final Logger l) {
67 noteObject = note;
68 log = l;
71 @Override
72 public Object getPDIDataObject() {
73 return noteObject;
76 @Override
77 public Class getPDIDataClass() {
78 return com.funambol.common.pim.note.Note.class;
81 @Override
82 public String getStringRepresentation(final String type) throws ObjectTransformationException {
83 try {
84 if (type.equals(Constants.TYPE_NOTE_SIFN)) {
85 BaseConverter conv = new NoteToSIFN(null, BaseConverter.CHARSET_UTF8);
86 return conv.convert(noteObject);
87 } else if (type.endsWith(Constants.TYPE_NOTE_VNOTE)) {
88 return NoteConverter.convertNote2VNote(noteObject).toString();
89 } else if (type.equals(Constants.TYPE_NOTE_ICAL)) {
90 return NoteConverter.convertNote2ical4j(noteObject, null).toString();
91 } else if (type.equals(Constants.TYPE_NOTE_PLAIN)) {
92 return NoteConverter.convertNote2Plain(noteObject);
94 } catch (Exception e) {
95 log.throwing(this.getClass().getName(), "getStringRepresentation", e);
96 throw new ObjectTransformationException("Error transforming contact to" + type, e);
98 throw new UnsupportedOperationException("Type: " + type + " unsupported");
101 @Override
102 public void parseIntoObjectFromString(final String str, final String type) throws ObjectTransformationException {
103 try {
104 parseIntoObjectFromBytes(str.getBytes("UTF-8"), type);
105 } catch (UnsupportedEncodingException e) {
106 throw new ObjectTransformationException("Error outputting content in UTF-8");
111 * Parse byte array into a funambol Note object.
113 * @param content array of bytes representing the decoded input
114 * @param type data type of the input.
115 * @throws ObjectTransformationException if conversion of the input fails
117 private void parseIntoObjectFromBytes(final byte[] content, final String type) throws ObjectTransformationException {
118 try {
119 ByteArrayInputStream bis = new ByteArrayInputStream(content);
120 if (type.equals(Constants.TYPE_NOTE_SIFN)) {
121 SIFNParser scp = new SIFNParser(bis);
122 noteObject = scp.parse();
123 } else if (type.equals(Constants.TYPE_NOTE_VNOTE)) {
124 new VNoteParser(bis);
125 noteObject = NoteConverter.convertVNote2Note(VNoteParser.VNote());
126 } else if (type.equals(Constants.TYPE_NOTE_ICAL)) {
127 CalendarBuilder cbuild = new CalendarBuilder();
128 Calendar cd = cbuild.build(bis);
129 /* Decode Quoted Printables */
130 Calendar decoded = QPDecode.decodeQP(cd);
131 noteObject = NoteConverter.convertical4j2Note(decoded, null);
133 } catch (Exception e) {
134 throw new ObjectTransformationException("Error parsing from type: " + type, e);
138 @Override
139 public String getDefaultStringOutputType() {
140 return Constants.TYPE_NOTE_ICAL;
144 * Create a funambol sync item from the parsed note object.
146 * @param source SyncSource for which the sync item is to be created.
147 * @return a syncItem corresponding to this note object
148 * @throws ObjectTransformationException if converting the note object to a string fails
149 * @throws UnsupportedEncodingException if transforming the output string as UTF-8 to a byte array fails
151 public SyncItem generateSyncItem(final SyncSource source) throws ObjectTransformationException, UnsupportedEncodingException {
152 String content = null;
153 String type = source.getInfo().getPreferredType().getType();
154 if (type == null) {
155 content = getStringRepresentation(getDefaultStringOutputType());
156 } else {
157 content = getStringRepresentation(type);
159 String uid = noteObject.getUid().getPropertyValueAsString();
160 SyncItemImpl si = new SyncItemImpl(source, uid);
161 byte[] con = content.getBytes("UTF-8");
162 si.setContent(con);
163 return si;