DirectSynchroService.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 package org.openmobileis.synchro.openmsp.client.services;
00026 
00027 import java.io.IOException;
00028 
00029 import javax.servlet.ServletException;
00030 import javax.servlet.http.HttpServletRequest;
00031 import javax.servlet.http.HttpServletResponse;
00032 
00033 import org.openmobileis.common.intl.IntlResourceManager;
00034 import org.openmobileis.common.util.PropertiesManager;
00035 import org.openmobileis.common.util.log.LogManager;
00036 import org.openmobileis.services.TemplateService;
00037 import org.openmobileis.services.common.ServiceManager;
00038 import org.openmobileis.services.common.ServletTools;
00039 import org.openmobileis.services.navigation.NavigationBarService;
00040 import org.openmobileis.services.security.SessionLoginManager;
00041 import org.openmobileis.synchro.client.SynchroDescriptor;
00042 import org.openmobileis.synchro.client.SynchroManager;
00043 import org.openmobileis.synchro.openmsp.client.OpenMSPSynchroManager;
00044 import org.openmobileis.synchro.openmsp.client.conduit.ApacheHTTPClientSynchroConduit;
00045 import org.openmobileis.synchro.openmsp.client.conduit.SynchroConduit;
00046 import org.openmobileis.synchro.openmsp.protocol.Status;
00047 import org.openmobileis.synchro.security.auth.Credential;
00048 
00049 import freemarker.template.TemplateModelRoot;
00050 
00058 public class DirectSynchroService extends TemplateService implements NavigationBarService {
00059         private static final String templatePath = "openmischannel" + java.io.File.separator + "synchroresult.htm";
00060         private boolean synchroPending = false;
00061 
00065         public DirectSynchroService() {
00066                 super();
00067 
00068         }
00069 
00070         /* (non-Javadoc)
00071          * @see org.openmobileis.services.common.TemplateService#runTemplate(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, freemarker.template.TemplateModelRoot)
00072          */
00073         public String runTemplate(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00074                 String login = req.getParameter("login");
00075                 String pwd = req.getParameter("pwd");
00076                 String showres = req.getParameter("showres");
00077                 try {
00078 
00079                         this.preProcessSynchronisation(req, res, templateData);
00080 
00081                         String synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00082                         if (synchroInstallPath == null) {
00083                                 String propFilePath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.properties.file.path");
00084                                 PropertiesManager.getManager().addPropertiesFileFromFilePath(propFilePath);
00085                                 synchroInstallPath = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.installpath");
00086                         }
00087                         SynchroDescriptor descriptor = new SynchroDescriptor();
00088                         descriptor.addProperty("OpenMSPsynchrotype", "DR"); // to say direct synchro not mandatory. Can be use if you have different type of synchronisation. To identify them on the server side.
00089                         String useragent = OpenMSPSynchroManager.getManager().getSynchroUserAgent(descriptor);
00090                         String conduitClass = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.conduit.class");
00091                         if (conduitClass != null) {
00092                                 SynchroConduit conduit = (SynchroConduit) Class.forName(conduitClass).newInstance();
00093                                 ;
00094                                 conduit.setSynchroPath(synchroInstallPath); //path where synchro file are stored
00095                                 conduit.setSynchroUserAgent(useragent);
00096                                 descriptor.setSynchroConduit(conduit);
00097                         } else {
00098                                 ApacheHTTPClientSynchroConduit conduit = new ApacheHTTPClientSynchroConduit();
00099                                 conduit.setSynchroPath(synchroInstallPath); //path where synchro file are stored
00100                                 conduit.setSynchroUserAgent(useragent); // synchro client user agent. Use to identify the terminal type on the server side
00101                                 descriptor.setSynchroConduit(conduit);
00102                         }
00103 
00104                         String group = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.group");
00105                         descriptor.setSynchroGroup(group); // group of the user. Use by the server side profil manager to identify the profile to use.
00106                         String url = PropertiesManager.getManager().getProperty("org.openmobileis.synchro.direct.targeturl");
00107                         descriptor.setServerURL(url);//url of the server synchro service ex:/syncro/openmspservice
00108 
00109                         if (showres != null) {
00110                                 //     LogManager.traceDebug(0, "DirectSyncService showres.");
00111                                 return this.showResultMessage(req, res, templateData);
00112                         }
00113                         // manage auhentication always ask user password
00114                         if ((login == null) || (pwd == null)) {
00115                                 //return password error authentication required
00116                                 return null;
00117                         }
00118                         if (!synchroPending) {// should be synchronized but to synchro at the same time never arrive
00119                                 synchronized (this) {
00120                                         synchroPending = true;
00121                                 }
00122                                 try {
00123                                         Credential cred = new Credential(login, pwd);
00124                                         SynchroManager.getManager().doSynchro(cred, descriptor);
00125                                         if (OpenMSPSynchroManager.getManager().getGlobalSynchroStatut() == Status.STATUS_OK) {
00126                                                 SessionLoginManager.getManager().changeSessionPass(pwd);
00127                                         }
00128                                         this.postProcessSynchronisation(req, res, templateData);
00129                                         return this.showResultMessage(req, res, templateData);
00130                                 } catch (Throwable e) {
00131                                         LogManager.traceError(0, e);
00132                                 } finally {
00133                                         synchronized (this) {
00134                                                 synchroPending = false;
00135                                         }
00136                                 }
00137                         }
00138 
00139                 } catch (Throwable ex) {
00140                         LogManager.traceError(0, "Error during init of DirectSynchroService. No direct synchro available");
00141                         LogManager.traceError(0, ex);
00142                 }
00143 
00144                 return this.showResultMessage(req, res, templateData);
00145         }
00146 
00147         /*public String showWaitMessage(HttpServletResponse res, TemplateModelRoot templateData) {
00148           IntlResourceManager resourceManager = IntlResourceManager.getManager();
00149           String title = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.SyncStarted");
00150           if (title == null) title = "Synchronizationn started.";
00151           String message = resourceManager.getLocalizedProperty("org.openmobileis.synchro.DirectSynchroService.Wait");
00152           if (message == null) message = "Please wait until the end.";
00153           ServletTools.sendConfirmationPage(title, message, "/index", res);
00154           return null;
00155         } */
00156 
00157         public String showResultMessage(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) throws ServletException, IOException {
00158                 SynchroJournalDisplayService journalservice = new SynchroJournalDisplayService();
00159                 journalservice.runTemplate(req, res, templateData);
00160                 return DirectSynchroService.templatePath;
00161         }
00162 
00163         public void preProcessSynchronisation(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) {
00164 
00165         }
00166 
00167         public void postProcessSynchronisation(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot templateData) {
00168 
00169         }
00170 
00171         public String getNavigationBarLabel(HttpServletRequest req) {
00172                 return "Synchronisation";
00173         }
00174 
00175         public boolean displayFormExitMessage() {
00176                 return false;
00177         }
00178 
00179         public boolean displayRecursive() {
00180                 return false;
00181         }
00182 
00183         public String getServiceUri() {
00184                 return ServiceManager.getManager().getServiceBaseURI() + "/synchro/direct";
00185         }
00186 
00187 }

Generated on Mon Jan 11 21:19:14 2010 for OpenMobileIS by  doxygen 1.5.4