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
00027
00028
00029
00030 package org.openmobileis.services.servlet;
00031
00032 import java.io.File;
00033 import java.io.FileInputStream;
00034 import java.io.IOException;
00035 import java.util.Hashtable;
00036 import java.util.Properties;
00037
00038 import javax.servlet.ServletContext;
00039 import javax.servlet.ServletException;
00040 import javax.servlet.http.HttpServlet;
00041 import javax.servlet.http.HttpServletRequest;
00042 import javax.servlet.http.HttpServletResponse;
00043
00044 import org.openmobileis.common.context.ApplicationContextManager;
00045 import org.openmobileis.common.context.SessionContext;
00046 import org.openmobileis.common.context.SessionContextManager;
00047 import org.openmobileis.common.intl.IntlResourceManager;
00048 import org.openmobileis.common.util.PropertiesManager;
00049 import org.openmobileis.common.util.log.FileLogManager;
00050 import org.openmobileis.common.util.log.FileOpenCloseLogManager;
00051 import org.openmobileis.common.util.log.LogManager;
00052 import org.openmobileis.common.util.log.LogServices;
00053 import org.openmobileis.embedded.webserver.ServeConfig;
00054 import org.openmobileis.embedded.webserver.WebServerConnection;
00055 import org.openmobileis.services.Service;
00056 import org.openmobileis.services.common.CallingServiceManager;
00057 import org.openmobileis.services.common.ServiceManager;
00058
00059 public class OpenMISServlet extends HttpServlet {
00060 static final long serialVersionUID = 5521257935120563452L;
00061
00062
00063 private static ApplicationContextManager applicationContextManager;
00064
00065
00066
00067
00068
00069
00070
00071 public OpenMISServlet() {
00072 applicationContextManager = ApplicationContextManager.getManager();
00073 applicationContextManager.getClass();
00074 }
00075
00076 public void init() throws ServletException {
00077
00078
00079 String installpath = this.getInitParameter("org.openmis.services.installpath");
00080 if (installpath==null) {
00081 installpath="";
00082 }
00083 String userdir = this.getServletContext().getRealPath(installpath);
00084 File file = new File(userdir);
00085 if (!file.isDirectory()) {
00086 throw new ServletException("Invalid installation path : "+userdir+". Can't Initialize application !!!");
00087 }
00088 System.setProperty("user.dir", userdir);
00089
00090
00091
00092 String propsFile = this.getInitParameter("org.openmis.services.conffile");
00093 if (propsFile == null || propsFile.trim().length() == 0) {
00094 throw new ServletException("Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00095 }
00096
00097 initOpenMIS(propsFile);
00098 }
00099
00100 private void initOpenMIS(String propertiesFileName) throws ServletException {
00101 OpenMISInit openmisInit = null;
00102
00103
00104
00105
00106 String absolutePath = System.getProperty("user.dir") + propertiesFileName;
00107 File confFile = new File(absolutePath);
00108
00109 if ((!confFile.exists()) || (!confFile.isFile())) {
00110
00111 confFile = new File(propertiesFileName);
00112
00113 if ((!confFile.exists()) || (!confFile.isFile())) {
00114 throw new ServletException("Servlet didn't find property file. Didn't find "+absolutePath+" neither "+propertiesFileName+". Can't Initialize application !!!");
00115 }
00116 absolutePath = propertiesFileName;
00117 }
00118
00119 try {
00120 System.out.println("Webserver start property file : " + absolutePath);
00121
00122 PropertiesManager.getManager().addPropertiesFileFromFilePath(absolutePath);
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 String openmisInitClass = PropertiesManager.getManager().getProperty("org.openmis.services.initclass");
00139 if (openmisInitClass != null && openmisInitClass.length() > 0) {
00140 Object init = Class.forName( openmisInitClass ).newInstance();
00141 if (init instanceof OpenMISInit) {
00142 openmisInit = (OpenMISInit)init;
00143 openmisInit.preLoadingInit();
00144 } else {
00145 LogManager.traceError(LogServices.WEBSERVICE, openmisInitClass+" isn't an instance of OpenMISInit !!!");
00146 }
00147 }
00148
00149 } catch (Exception ex) {
00150 LogManager.traceError(0, ex);
00151 throw new ServletException(ex);
00152 }
00153
00154
00155 applicationContextManager = ApplicationContextManager.getManager();
00156
00157 String listServices = PropertiesManager.getManager().getProperty("listServicesFile");
00158 if (listServices==null) {
00159 throw new ServletException("Servlet didn't find property listServicesFile. Can not load services.");
00160 }
00161 this.loadServices(listServices);
00162
00163 if (openmisInit != null) {
00164 try {
00165 openmisInit.postLoadingInit();
00166 } catch (Exception ex) {
00167 throw new ServletException("error during init server properties and log", ex);
00168 }
00169 }
00170 }
00171
00172 private void loadServices(String listServicesFile) {
00173 java.io.File file = new java.io.File(listServicesFile);
00174 if (!file.exists())
00175 LogManager.traceAlert(LogServices.WEBSERVICE,
00176 "OpenMIS service list file not found. " + listServicesFile);
00177 else {
00178 Properties props = new Properties();
00179 FileInputStream fileservice = null;
00180 try {
00181 fileservice = new java.io.FileInputStream(file);
00182 props.load(fileservice);
00183 ServiceManager.getManager().init(props, "/"+this.getServletName());
00184 } catch (Exception ex) {
00185 LogManager.traceAlert(LogServices.WEBSERVICE, "Error loading services");
00186 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00187 } finally {
00188 if (fileservice != null) {
00189 try {
00190 fileservice.close();
00191 } catch (Exception ex) {
00192 LogManager.traceAlert(LogServices.WEBSERVICE, "Unable to close file : "+listServicesFile);
00193 }
00194 }
00195 }
00196 }
00197 }
00198
00199 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00200 this.processRequest(req, res);
00201 }
00202
00213 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00214 this.processRequest(req, res);
00215 }
00216
00217 private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00218 String serviceName = req.getPathInfo();
00219 if (serviceName == null) {
00220 res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00221 }
00222 Service service = ServiceManager.getManager().getServiceByURI(serviceName);
00223 if (service == null) {
00224
00225 LogManager.traceNotice(0, "ServiceError Service not found " + serviceName);
00226 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00227 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.UnknownService");
00228 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + serviceName);
00229
00230 } else {
00231 if (this.isAuthorized(service)) {
00232
00233 this.getValideSessionContext(req.getSession(true).getId());
00234
00235 try {
00236 service.runService(new OpenmisHttpServletRequest(req), res);
00237 } finally {
00238 SessionContextManager.getManager().leaveSessionContext();
00239 }
00240
00241 } else {
00242 LogManager.traceNotice(0, "ServiceError unauthorized service error "+serviceName);
00243 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00244 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.AccessDeny");
00245 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + service.getServiceUri());
00246 }
00247 }
00248 }
00253 private boolean isAuthorized (Service service) {
00254 return true;
00255 }
00256
00261 protected SessionContext getValideSessionContext(String contextID) {
00262 SessionContext session = SessionContextManager.getManager().getSessionContext(contextID);
00263 if (session == null) {
00264 synchronized (SessionContextManager.getManager()) {
00265 session = SessionContextManager.getManager().getSessionContext(contextID);
00266 if (session == null) {
00267 session = SessionContextManager.getManager().createSessionContext(contextID);
00268 }
00269 }
00270 }
00271 SessionContextManager.getManager().joinSessionContext(session.getId());
00272 return session;
00273 }
00274
00275 }