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.services.Service;
00051 import org.openmobileis.services.common.ServiceManager;
00052
00053 public class OpenMISServlet extends HttpServlet {
00054 static final long serialVersionUID = 5521257935120563452L;
00055
00056
00057 private static ApplicationContextManager applicationContextManager;
00058
00059
00060
00061
00062
00063
00064
00065 public OpenMISServlet() {
00066 applicationContextManager = ApplicationContextManager.getManager();
00067 applicationContextManager.getClass();
00068 }
00069
00070 public void init() throws ServletException {
00071
00072
00073 Properties props = new Properties();
00074 LogManager.registerLogManager(props);
00075
00076
00077 String installpath = this.getInitParameter("org.openmis.services.installpath");
00078 if (installpath == null) {
00079 installpath = "";
00080 }
00081 String userdir = this.getServletContext().getRealPath(installpath);
00082 File file = new File(userdir);
00083 if (!file.isDirectory()) {
00084 throw new ServletException("Invalid installation path : " + userdir + ". Can't Initialize application !!!");
00085 }
00086 try {
00087 userdir = file.getCanonicalPath();
00088 } catch (Throwable ex) {
00089 ex.printStackTrace();
00090 }
00091
00092 System.setProperty("user.dir", userdir);
00093
00094
00095 String propsFile = this.getInitParameter("org.openmis.services.conffile");
00096 if (propsFile == null || propsFile.trim().length() == 0) {
00097 throw new ServletException("Servlet didn't find 'org.openmis.services.conffile' init parameter. Can't Initialize application !!!");
00098 }
00099
00100 LogManager.traceInfo(0, "OpenMISServlet install path :" + installpath);
00101 LogManager.traceInfo(0, "OpenMISServlet userdir :" + userdir);
00102 LogManager.traceInfo(0, "OpenMISServlet propsFile :" + propsFile);
00103
00104 initOpenMIS(propsFile);
00105 }
00106
00107 private void initOpenMIS(String propertiesFileName) throws ServletException {
00108 OpenMISInit openmisInit = null;
00109
00110
00111 String absolutePath = System.getProperty("user.dir") + propertiesFileName;
00112 File confFile = new File(absolutePath);
00113
00114 if ((!confFile.exists()) || (!confFile.isFile())) {
00115 absolutePath = propertiesFileName;
00116 }
00117 try {
00118 try {
00119 PropertiesManager.getManager().addPropertiesFileFromFilePath(absolutePath);
00120 } catch (Throwable ex) {
00121 throw new ServletException("Servlet didn't find property file. Didn't find " + absolutePath + " neither " + propertiesFileName
00122 + ". Can't Initialize application !!!");
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 this.loadServices(listServices);
00160 } else {
00161 try {
00162 ServiceManager.getManager().init(null, "/" + this.getServletName());
00163 } catch (ServiceException ex) {
00164 LogManager.traceError(0, ex);
00165 }
00166 }
00167
00168 if (openmisInit != null) {
00169 try {
00170 openmisInit.postLoadingInit();
00171 } catch (Exception ex) {
00172 throw new ServletException("error during init server properties and log", ex);
00173 }
00174 }
00175 }
00176
00177 private void loadServices(String listServicesFile) {
00178 java.io.File file = new java.io.File(listServicesFile);
00179 if (!file.exists())
00180 LogManager.traceAlert(LogServices.WEBSERVICE, "OpenMIS service list file not found. " + listServicesFile);
00181 else {
00182 Properties props = new Properties();
00183 FileInputStream fileservice = null;
00184 try {
00185 fileservice = new java.io.FileInputStream(file);
00186 props.load(fileservice);
00187 ServiceManager.getManager().init(props, "/" + this.getServletName());
00188 } catch (Exception ex) {
00189 LogManager.traceAlert(LogServices.WEBSERVICE, "Error loading services");
00190 LogManager.traceAlert(LogServices.WEBSERVICE, ex);
00191 } finally {
00192 if (fileservice != null) {
00193 try {
00194 fileservice.close();
00195 } catch (Exception ex) {
00196 LogManager.traceAlert(LogServices.WEBSERVICE, "Unable to close file : " + listServicesFile);
00197 }
00198 }
00199 }
00200 }
00201 }
00202
00203 public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00204 this.processRequest(req, res);
00205 }
00206
00217 public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00218 this.processRequest(req, res);
00219 }
00220
00221 private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00222 String serviceName = req.getPathInfo();
00223 if (serviceName == null) {
00224 res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00225 }
00226 Service service = ServiceManager.getManager().getServiceByURI(serviceName);
00227 if (service == null) {
00228
00229 LogManager.traceNotice(0, "ServiceError Service not found " + serviceName);
00230 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00231 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.UnknownService");
00232 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + serviceName);
00233
00234 } else {
00235 if (this.isAuthorized(service)) {
00236
00237 this.getValideSessionContext(req.getSession(true).getId());
00238
00239 try {
00240 service.runService(new OpenmisHttpServletRequest(req), res);
00241 } finally {
00242 SessionContextManager.getManager().leaveSessionContext();
00243 }
00244
00245 } else {
00246 LogManager.traceNotice(0, "ServiceError unauthorized service error " + serviceName);
00247 IntlResourceManager resourceManager = IntlResourceManager.getManager();
00248 String title = resourceManager.getLocalizedProperty("ServiceManagerServlet.AccessDeny");
00249 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, title + service.getServiceUri());
00250 }
00251 }
00252 }
00253
00258 private boolean isAuthorized(Service service) {
00259 return true;
00260 }
00261
00266 protected SessionContext getValideSessionContext(String contextID) {
00267 SessionContext session = SessionContextManager.getManager().getSessionContext(contextID);
00268 if (session == null) {
00269 synchronized (SessionContextManager.getManager()) {
00270 session = SessionContextManager.getManager().getSessionContext(contextID);
00271 if (session == null) {
00272 session = SessionContextManager.getManager().createSessionContext(contextID);
00273 }
00274 }
00275 }
00276 SessionContextManager.getManager().joinSessionContext(session.getId());
00277 return session;
00278 }
00279
00280 }