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

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