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.Properties;
00036
00037 import javax.servlet.ServletException;
00038 import javax.servlet.http.HttpServlet;
00039 import javax.servlet.http.HttpServletRequest;
00040 import javax.servlet.http.HttpServletResponse;
00041
00042 import org.openmobileis.common.context.ApplicationContextManager;
00043 import org.openmobileis.common.context.SessionContext;
00044 import org.openmobileis.common.context.SessionContextManager;
00045 import org.openmobileis.common.intl.IntlResourceManager;
00046 import org.openmobileis.common.util.PropertiesManager;
00047 import org.openmobileis.common.util.exception.ServiceException;
00048 import org.openmobileis.common.util.log.LogManager;
00049 import org.openmobileis.common.util.log.LogServices;
00050 import org.openmobileis.embedded.webserver.StartWebServer;
00051 import org.openmobileis.services.Service;
00052 import org.openmobileis.services.common.ServiceManager;
00053
00054 public class OpenMISServlet extends HttpServlet {
00055 static final long serialVersionUID = 5521257935120563452L;
00056
00057
00058 private static ApplicationContextManager applicationContextManager;
00059
00060
00061
00062
00063
00064
00065
00066 public OpenMISServlet() {
00067 applicationContextManager = ApplicationContextManager.getManager();
00068 applicationContextManager.getClass();
00069 }
00070
00071 public void init() throws ServletException {
00072
00073
00074 Properties props = new Properties();
00075 LogManager.registerLogManager(props);
00076
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 try {
00089 userdir = file.getCanonicalPath();
00090 } catch (Throwable ex) {
00091 ex.printStackTrace();
00092 }
00093
00094 System.setProperty("user.dir", userdir);
00095
00096
00097 String propsFile = this.getInitParameter("org.openmis.services.conffile");
00098 if (propsFile == null || propsFile.trim().length() == 0) {
00099 throw new ServletException("Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00100 }
00101
00102 LogManager.traceInfo(0, "OpenMISServlet install path :" + installpath);
00103 LogManager.traceInfo(0, "OpenMISServlet userdir :" + userdir);
00104 LogManager.traceInfo(0, "OpenMISServlet propsFile :" + propsFile);
00105
00106 initOpenMIS(propsFile);
00107 }
00108
00109 private void initOpenMIS(String propertiesFileName) throws ServletException {
00110 OpenMISInit openmisInit = null;
00111
00112
00113
00114 String absolutePath = System.getProperty("user.dir") + propertiesFileName;
00115 File confFile = new File(absolutePath);
00116
00117 if ((!confFile.exists()) || (!confFile.isFile())) {
00118 absolutePath = propertiesFileName;
00119 }
00120 try {
00121 try {
00122 PropertiesManager.getManager().addPropertiesFileFromFilePath(absolutePath);
00123 } catch (Throwable ex) {
00124 throw new ServletException("Servlet didn't find property file. Didn't find " + absolutePath + " neither " + propertiesFileName + ". Can't Initialize application !!!");
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 String openmisInitClass = PropertiesManager.getManager().getProperty("org.openmis.services.initclass");
00142 if (openmisInitClass != null && openmisInitClass.length() > 0) {
00143 Object init = Class.forName(openmisInitClass).newInstance();
00144 if (init instanceof OpenMISInit) {
00145 openmisInit = (OpenMISInit) init;
00146 openmisInit.preLoadingInit();
00147 } else {
00148 LogManager.traceError(LogServices.WEBSERVICE, openmisInitClass + " isn't an instance of OpenMISInit !!!");
00149 }
00150 }
00151
00152
00153 LogManager.traceInfo(0, "Open Mobile IS Build Version : " + StartWebServer.class.getPackage().getImplementationVersion());
00154
00155 } catch (Exception ex) {
00156 LogManager.traceError(0, ex);
00157 throw new ServletException(ex);
00158 }
00159
00160
00161 applicationContextManager = ApplicationContextManager.getManager();
00162
00163 String listServices = PropertiesManager.getManager().getProperty("listServicesFile");
00164 if (listServices != null) {
00165 this.loadServices(listServices);
00166 } else {
00167 try {
00168 ServiceManager.getManager().init(null, "/" + this.getServletName());
00169 } catch (ServiceException ex) {
00170 LogManager.traceError(0, ex);
00171 }
00172 }
00173
00174 if (openmisInit != null) {
00175 try {
00176 openmisInit.postLoadingInit();
00177 } catch (Exception ex) {
00178 throw new ServletException("error during init server properties and log", ex);
00179 }
00180 }
00181 }
00182
00183 private void loadServices(String listServicesFile) {
00184 java.io.File file = new java.io.File(listServicesFile);
00185 if (!file.exists())
00186 LogManager.traceAlert(LogServices.WEBSERVICE, "OpenMIS service list file not found. " + listServicesFile);
00187 else {
00188 Properties props = new Properties();
00189 FileInputStream fileservice = null;
00190 try {
00191 fileservice = new java.io.FileInputStream(file);
00192 props.load(fileservice);
00193 ServiceManager.getManager().init(props, "/" + this.getServletName());
00194 } catch (Exception ex) {
00195 LogManager.traceAlert(LogServices.WEBSERVICE, "Error loading services");
00196 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00197 } finally {
00198 if (fileservice != null) {
00199 try {
00200 fileservice.close();
00201 } catch (Exception ex) {
00202 LogManager.traceAlert(LogServices.WEBSERVICE, "Unable to close file : " + listServicesFile);
00203 }
00204 }
00205 }
00206 }
00207 }
00208
00209 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00210 this.processRequest(req, res);
00211 }
00212
00223 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00224 this.processRequest(req, res);
00225 }
00226
00227 private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00228 String serviceName = req.getPathInfo();
00229 if (serviceName == null) {
00230 res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00231 }
00232 Service service = ServiceManager.getManager().getServiceByURI(serviceName);
00233 if (service == null) {
00234
00235 LogManager.traceNotice(0, "ServiceError Service not found " + serviceName);
00236 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00237 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.UnknownService");
00238 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + serviceName);
00239
00240 } else {
00241 if (this.isAuthorized(service)) {
00242
00243 this.getValideSessionContext(req.getSession(true).getId());
00244
00245 try {
00246
00247 service.runService(new OpenmisHttpServletRequest(req), res);
00248 } catch (java.net.SocketException ex) {
00249 LogManager.traceNotice(0, ex);
00250 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Network error");
00251 } catch (Throwable ex) {
00252 LogManager.traceNotice(0, ex);
00253 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "See terminal log");
00254 } finally {
00255 SessionContextManager.getManager().leaveSessionContext();
00256 }
00257
00258 } else {
00259 LogManager.traceNotice(0, "ServiceError unauthorized service error " + serviceName);
00260 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00261 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.AccessDeny");
00262 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + service.getServiceUri());
00263 }
00264 }
00265 }
00266
00271 private boolean isAuthorized(Service service) {
00272 return true;
00273 }
00274
00279 protected SessionContext getValideSessionContext(String contextID) {
00280 SessionContext session = SessionContextManager.getManager().getSessionContext(contextID);
00281 if (session == null) {
00282 synchronized (SessionContextManager.getManager()) {
00283 session = SessionContextManager.getManager().getSessionContext(contextID);
00284 if (session == null) {
00285 session = SessionContextManager.getManager().createSessionContext(contextID);
00286 }
00287 }
00288 }
00289 SessionContextManager.getManager().joinSessionContext(session.getId());
00290 return session;
00291 }
00292
00293 }