00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 package org.openmobileis.module.profiles.terminal;
00027
00028 import org.openmobileis.common.user.profile.Profile;
00029 import org.openmobileis.common.util.codec.GeneralCoder;
00030 import org.openmobileis.common.util.log.*;
00031 import org.openmobileis.module.terminal.ModuleManager;
00032 import org.openmobileis.synchro.client.SynchroDescriptor;
00033 import org.openmobileis.synchro.journal.JournalEntry;
00034 import org.openmobileis.synchro.journal.JournalManager;
00035 import org.openmobileis.synchro.openmsp.OpenMSPException;
00036 import org.openmobileis.synchro.openmsp.client.DefaultOpenMSPSyncListener;
00037 import org.openmobileis.synchro.openmsp.client.OpenMSPDBSyncListener;
00038 import org.openmobileis.synchro.openmsp.client.core.NumSyncManagerDB;
00039 import org.openmobileis.synchro.openmsp.protocol.AbstractCommand;
00040 import org.openmobileis.synchro.openmsp.protocol.Command;
00041 import org.openmobileis.synchro.openmsp.protocol.ContainerMessage;
00042 import org.openmobileis.synchro.openmsp.protocol.DataItem;
00043 import org.openmobileis.synchro.openmsp.protocol.Element;
00044 import org.openmobileis.synchro.openmsp.protocol.Message;
00045 import org.openmobileis.synchro.openmsp.protocol.Result;
00046 import org.openmobileis.synchro.openmsp.protocol.Status;
00047 import org.openmobileis.synchro.security.auth.Credential;
00048
00060 public class OpenMSPProfileSyncListener extends DefaultOpenMSPSyncListener {
00061
00062 public static final String profilSynchroSourceName = "ProfilSyncListener";
00063
00064 public static final String profilSynchroTargetName = "ProfilSynchroTarget";
00065
00066 private boolean firstSync = false;
00067
00068
00069
00070 public OpenMSPProfileSyncListener() {
00071 }
00072
00073 public void receiveResultCommand(ContainerMessage resultContainer, ContainerMessage initialCommand) throws OpenMSPException {
00074 Result result = (Result) resultContainer.getElement();
00075 if (result.getTargetRef().equals(profilSynchroSourceName)) {
00076
00077 try {
00078 String encodeddata = ((DataItem) (resultContainer.nextMessage().getElement())).getData();
00079 String data = new String(GeneralCoder.decodeBase64(encodeddata.getBytes()));
00080 Profile oldproffile = TerminalProfileManager.getManager().getProfil();
00081 TerminalProfileManager.getManager().saveProfilFile(data);
00082
00083
00084 Profile newproffile = TerminalProfileManager.getManager().getProfil();
00085 if (!newproffile.equals(oldproffile)) {
00086
00087 ModuleManager.getManager().initModule();
00088 }
00089
00090 } catch (Exception e) {
00091 LogManager.traceError(LogServices.WEBSERVICE, "Wrong result format for profile, impossible to read profile data");
00092 LogManager.traceError(LogServices.WEBSERVICE, e);
00093 }
00094 }
00095 }
00096
00097
00098
00099
00100 public void receiveStatusCommand(Status statusCommande, ContainerMessage initialContenaire) throws OpenMSPException {
00101 try {
00102
00103 int status = statusCommande.getStatus();
00104 if (initialContenaire != null) {
00105 AbstractCommand command = (AbstractCommand) initialContenaire.getElement();
00106 this.manageReturnStatusLog(status, command.getElementType(), initialContenaire);
00107 if (status != Status.STATUS_OK) {
00108
00109 this.notifySynchroFailure();
00110 }
00111 }
00112 } catch (Exception ex) {
00113 this.notifySynchroFailure();
00114 LogManager.traceError(0, ex);
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 public void sendData(Message message) {
00127 if (firstSync) {
00128 try {
00129 Command getCommand = new Command(Element.GET, this.getSyncName(), profilSynchroTargetName);
00130 long ns = NumSyncManagerDB.getManager().getSyncNumberForService(this.getSyncName());
00131 getCommand.setSourceSessionID(ns);
00132 ContainerMessage getContainer = new ContainerMessage(getCommand);
00133
00134
00135 DataItem newItem = new DataItem(Element.ITEM, "", "", null, null);
00136
00137 getContainer.add(newItem);
00138 message.add(getContainer);
00139 } catch (OpenMSPException e) {
00140 LogManager.traceError(LogServices.WEBSERVICE, "Error during building syncML message for Sync Profil service : " + e.getMessage());
00141 }
00142 firstSync = false;
00143 }
00144 }
00145
00146
00147
00148
00149 public String getSyncName() {
00150 return profilSynchroSourceName;
00151 }
00152
00153 public void startSync(Credential cred, SynchroDescriptor synchrodescriptor) throws OpenMSPException {
00154 super.startSync(cred, synchrodescriptor);
00155 firstSync = true;
00156 }
00157
00162 protected void manageReturnStatusLog(int statusCode, int cmlType, ContainerMessage container) {
00163 this.setSynchroStatus(statusCode);
00164 if (statusCode != Status.STATUS_OK) {
00165 String message = null;
00166 short journalEntryType = JournalEntry.ERROR_SYNC_MOBILE;
00167 if (statusCode == Status.STATUS_UNAUTHORIZED) {
00168 message = OpenMSPDBSyncListener.SYNC_UNAUTHORIZED_MESSAGE;
00169 } else {
00170 message = OpenMSPDBSyncListener.SYNC_ERROR_MESSAGE;
00171 }
00172 if (message != null) {
00173 JournalManager.getManager().saveJournalEntry(
00174
00175 this.getSyncName()
00176
00177 , journalEntryType
00178
00179 , message
00180
00181 , statusCode
00182
00183 );
00184 }
00185 }
00186
00187 }
00188
00189 }