Service.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  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 
00030 package org.openmobileis.services;
00031 
00032 import org.openmobileis.services.cache.WebPageCacheManager;
00033 import org.openmobileis.services.navigation.NavigationBarManager;
00034 import org.openmobileis.services.navigation.NavigationBarService;
00035 import org.openmobileis.services.security.PasswordFormServlet;
00036 import org.openmobileis.services.security.SessionLoginManager;
00037 
00038 import java.io.IOException;
00039 
00040 import javax.servlet.ServletException;
00041 import javax.servlet.http.HttpServletRequest;
00042 import javax.servlet.http.HttpServletResponse;
00043 
00052 public abstract class Service {
00053         
00054 // private String uri;
00055  private boolean isNavigationService = false;
00056  protected PasswordFormServlet passServlet;
00057 
00058  public Service() {
00059         if (this instanceof NavigationBarService)       {
00060                 isNavigationService = true;
00061         }
00062         passServlet = new PasswordFormServlet();
00063  }
00064 
00065 /* Call by Service manager to perform to internal process before run call
00066  * @param req the servlet request
00067  * @param req the servlet response
00068  *  @exception ServletException when an exception has occurred
00069  */
00070   public void runService(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException  {
00071         //      LogManager.traceNotice(0, "Service :Begin run");
00072 
00073         // validate if its not a auth pass request
00074         //if yes validate pass. Use by synchro to begin sync.
00075                 if (SessionLoginManager.getManager().isInitializedPass())       {
00076                 //manage password authentification
00077                         if ((SessionLoginManager.getManager().getMaxTimeout()!=0) && SessionLoginManager.getManager().isInitializedPass())      { //validate password timeout
00078                                 if (SessionLoginManager.getManager().isServiceCallElapseTime()) { // ask pass
00079                                         //add service in session
00080                                         req.getSession(true).setAttribute("passservletreturnservice", this);
00081                                         req.getSession(true).setAttribute("passservletreturnservicerequri", req.getRequestURI());
00082                                         req.getSession(true).setAttribute("passservletreturnservicereqparam", req.getParameterMap());
00083                                         passServlet.service(req, res);
00084                                         return;
00085                                 }
00086                         }
00087                 SessionLoginManager.getManager().setServiceCall();
00088                 }
00089                 
00090                 //manage navigation bar.
00091                 if (isNavigationService)        {                       
00092                         NavigationBarManager.getManager().setCurrentService((NavigationBarService)this, req);
00093                 }
00094         
00095           
00096           //manage cached data.
00097           if (this.useWebCache())       {
00098           byte[] cachedResp = WebPageCacheManager.getManager().getCachedPage(req, this);
00099           if (cachedResp != null)       {
00100               res.getWriter().print(cachedResp);
00101                 //((WebServerConnection)res).setResponse(cachedResp);
00102                 return;
00103           }
00104         }
00105                 
00106         //      LogManager.traceNotice(0, "Service :BEfore run service");
00107                 this.run(req, res);
00108         //      LogManager.traceNotice(0, "Service :AFTER run service");
00109         
00110     //add to cache
00111     if (this.useWebCache())     {
00112         WebPageCacheManager.getManager().setCachedPage(req, res.getOutputStream().toString().getBytes(), this);
00113         //WebPageCacheManager.getManager().setCachedPage(req, ((WebServerConnection)res).getResponseData(), this);
00114     }
00115   }
00116 
00117 /* Process the service method to implement 
00118  * @param req the servlet request
00119  * @param req the servlet response
00120  *  @exception ServletException when an exception has occurred
00121  */
00122   public abstract void run( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException;
00123 
00124   /*
00125   * * Return the service name
00126   */
00127  //  public abstract String getName();
00128 
00133   public boolean useWebCache()  {
00134         return false;
00135   }
00136         
00143         public String getNavigationBarLastServiceURI()  {
00144                 return null;
00145         }
00146 
00150         public boolean isNavigationService() {
00151                 return isNavigationService;
00152         }
00153         
00154         public abstract  String getServiceUri();
00155         
00156         /*public void setServiceURI(String uri) {
00157                 this.uri = uri;
00158         } */
00159         
00160         public boolean equals(Object obj)       {
00161                 if( obj instanceof Service){
00162                         Service in = (Service) obj;
00163                         if (in.getServiceUri().equals(this.getServiceUri()))    {
00164                                 return true;
00165                         }
00166                 }
00167                 return false;
00168         }
00169         
00170         public int hashCode()   {
00171                 return this.getServiceUri().hashCode();
00172         }
00173 
00174 }

Generated on Tue May 22 23:01:11 2007 for OpenMobileIS by  doxygen 1.5.1-p1