001: package com.xoetrope.service.servlet;
002:
003: import com.xoetrope.service.ModelHelper;
004: import java.io.IOException;
005: import java.io.PrintWriter;
006: import java.io.StringWriter;
007: import java.lang.reflect.Constructor;
008: import java.net.URLEncoder;
009: import java.util.Enumeration;
010: import java.util.Hashtable;
011: import java.util.Vector;
012:
013: import net.xoetrope.optional.data.XOptionalDataSource;
014: import net.xoetrope.optional.service.ServiceProxy;
015: import net.xoetrope.optional.service.ServiceProxyArgs;
016: import net.xoetrope.optional.service.ServiceProxyException;
017: import net.xoetrope.optional.service.XRouteManager;
018: import net.xoetrope.optional.service.XServiceModelNode;
019: import net.xoetrope.xui.XProjectManager;
020: import net.xoetrope.xui.data.XModel;
021: import net.xoetrope.xui.data.XBaseModel;
022: import net.xoetrope.xml.XmlParserFactory;
023: import net.xoetrope.optional.service.ServiceContext;
024:
025: import javax.servlet.http.HttpServlet;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import javax.servlet.ServletException;
029: import net.xoetrope.debug.DebugLogger;
030: import net.xoetrope.xui.data.XModelHelper;
031: import net.xoetrope.xui.XProject;
032:
033: /**
034: * Provides a response for a http request. This servlet is intended to simulate
035: * a real response service
036: *
037: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
038: * the GNU Public License (GPL), please see license.txt for more details. If
039: * you make commercial use of this software you must purchase a commercial
040: * license from Xoetrope.</p>
041: * <p> $Revision: 1.22 $</p>
042: */
043: public class XServiceServlet extends HttpServlet {
044: /**
045: * Error sent back in the case that a connection cannot be established.
046: */
047: public static final String ERR_INIT_FAIL = "xserviceservlet:initfailure";
048:
049: /**
050: * Error sent back in the case that a connection cannot be established.
051: */
052: public static final String ERR_SERVICE_INVALID = "xserviceservlet:invalidservice";
053:
054: protected static final String CONTENT_TYPE = "text/html";
055: protected String startupFile = "xuiproserver.properties";
056:
057: protected XRouteManager routeMgr;
058: protected XModel model;
059:
060: private boolean initOK = false;
061: private boolean validServiceName = false;
062:
063: /**
064: * The owner project and the context in which this object operates.
065: */
066: protected XProject currentProject = XProjectManager
067: .getCurrentProject(null);
068:
069: /**
070: * Initialize global variables. If the routeMgr servlet context variable is
071: * null open the datasets file and build up the model
072: * @throws javax.servlet.ServletException Throw ServletException if there is a problem
073: */
074: public void init() throws ServletException {
075: try {
076: routeMgr = (XRouteManager) getServletContext()
077: .getAttribute("routeMgr");
078: model = (XModel) getServletContext().getAttribute("model");
079: if (routeMgr == null) {
080: routeMgr = XRouteManager
081: .setupRouteManager(currentProject);
082: routeMgr.setSide(routeMgr.SERVER_SIDE);
083:
084: currentProject.setStartupFile(startupFile);
085:
086: XOptionalDataSource modelDataSource;
087: try {
088: String startDataSource = currentProject
089: .getStartupParam("XDataSourceClass");
090: if (startDataSource != null) {
091: Class clazz = Class.forName(startDataSource
092: .trim());
093: Class[] params = new Class[1];
094: Object[] args = new Object[1];
095: params[0] = XProject.class;
096: args[0] = currentProject;
097: Constructor ctor = clazz.getConstructor(params);
098: modelDataSource = (XOptionalDataSource) ctor
099: .newInstance(args);
100: } else
101: modelDataSource = new XOptionalDataSource(
102: currentProject);
103: } catch (Exception ex2) {
104: return;
105: }
106:
107: try {
108: String fileName = currentProject
109: .getStartupParam("ModelData");
110: modelDataSource.read(currentProject
111: .getBufferedReader(fileName, "UTF8"));
112: } catch (Exception ex) {
113: ex.printStackTrace();
114: }
115: model = currentProject.getModel();
116: getServletContext().setAttribute("routeMgr", routeMgr);
117: getServletContext().setAttribute("model",
118: XProjectManager.getModel());
119: }
120: initOK = true;
121: } catch (Exception e) {
122: initOK = false;
123: e.printStackTrace();
124: }
125: }
126:
127: /**
128: * Process the HTTP Get request
129: * @param request The HTTPServletRequest Object
130: * @param response The HTTPServletResponse Object
131: * @throws javax.servlet.ServletException Throw ServletException if there is a problem
132: * @throws java.io.IOException Throw ServletException if there is an IO problem
133: */
134: public void doGet(HttpServletRequest request,
135: HttpServletResponse response) throws ServletException,
136: IOException {
137: try {
138: ServiceContext context = null;
139: if (initOK) {
140: Object ret = getResponse(request, response);
141: if (ret == null) {
142: context = new ServiceContext();
143: context.addError(ERR_SERVICE_INVALID);
144: } else
145: context = (ServiceContext) ret;
146: } else {
147: context = new ServiceContext();
148: context.addError(ERR_INIT_FAIL);
149: }
150:
151: ServiceProxyArgs result = context.getArgs();
152: XmlParserFactory parserFact = XmlParserFactory
153: .getInstance();
154: Hashtable returnArgs = context.getReturnArgs();
155: Enumeration keys = returnArgs.keys();
156: XBaseModel argsMdl = new XBaseModel();
157: while (keys.hasMoreElements()) {
158: String name = (String) keys.nextElement();
159: String value = (String) returnArgs.get(name);
160: XBaseModel mdl = new XBaseModel(argsMdl, name, value);
161: }
162:
163: Vector errors = context.getErrors();
164: for (int i = 0; i < errors.size(); i++) {
165: String error = (String) errors.get(i);
166: String name = "error" + i;
167: XBaseModel mdl = new XBaseModel(argsMdl, name, error);
168: XModelHelper.setAttrib(mdl, "type", "error");
169: }
170:
171: StringWriter sw = new StringWriter();
172: XOptionalDataSource datasource = ModelHelper
173: .getDataSource(currentProject);
174: String retParams = datasource.getModelXML(argsMdl);
175:
176: response.setContentType(CONTENT_TYPE);
177: PrintWriter out = response.getWriter();
178: out.print(retParams == null ? "" : URLEncoder.encode(
179: retParams.toString(), "UTF-8"));
180: out.flush();
181: } catch (Exception ex) {
182: PrintWriter out = response.getWriter();
183: out.print(ex.getMessage());
184: out.flush();
185: ex.printStackTrace();
186: }
187: }
188:
189: /**
190: * Process the HTTP Post request
191: * @param request The HTTPServletRequest Object
192: * @param response The HTTPServletResponset Object
193: * @throws javax.servlet.ServletException Throw ServletException if there is a problem
194: * @throws java.io.IOException Throw ServletException if there is an IO problem
195: */
196: public void doPost(HttpServletRequest request,
197: HttpServletResponse response) throws ServletException,
198: IOException {
199: doGet(request, response);
200: }
201:
202: /**
203: * Process the HTTP put request
204: * @param request The HTTPServletRequest Object
205: * @param response The HTTPServletResponse Object
206: * @throws javax.servlet.ServletException Throw ServletException if there is a problem
207: * @throws java.io.IOException Throw ServletException if there is an IO problem
208: */
209: public void doPut(HttpServletRequest request,
210: HttpServletResponse response) throws ServletException,
211: IOException {
212: }
213:
214: /**
215: * Process the HTTP Delete request
216: * @param request The HTTPServletRequest Object
217: * @param response The HTTPServletResponse Object
218: * @throws javax.servlet.ServletException Throw ServletException if there is a problem
219: * @throws java.io.IOException Throw ServletException if there is an IO problem
220: */
221: public void doDelete(HttpServletRequest request,
222: HttpServletResponse response) throws ServletException,
223: IOException {
224: }
225:
226: /**
227: * Clean up resources
228: */
229: public void destroy() {
230: }
231:
232: /**
233: * Construct a new ServiceProxyArgs object and set the passed parameters from
234: * the request object. The request object is added to the arguments, the
235: * ServiceProxy is called and the request object is then removed
236: * @param request the HttpServletServlet request object
237: * @param response the HttpServletServlet response object
238: * @return return null
239: */
240: public Object getResponse(HttpServletRequest request,
241: HttpServletResponse response) {
242: Object service = request.getParameter("service");
243: if (service == null)
244: return null;
245: String methodName = service.toString();
246: XModel mdl = (XModel) model.get(methodName);
247: Object serviceMdlNode = mdl.get();
248: if (serviceMdlNode instanceof XServiceModelNode) {
249: ServiceProxy sp = ((XServiceModelNode) serviceMdlNode)
250: .getServiceProxy();
251:
252: Enumeration enumeration = request.getParameterNames();
253: ServiceContext context = new ServiceContext();
254: ServiceProxyArgs args = context.getArgs();
255: int i = 0;
256: while (enumeration.hasMoreElements()) {
257: String paramName = enumeration.nextElement().toString();
258: if (paramName != null
259: && paramName.compareTo("service") != 0) {
260: args.setPassParam(paramName, request
261: .getParameter(paramName));
262: i++;
263: }
264: }
265:
266: Object result = null;
267: try {
268: args.setPassParam("session", request);
269: result = sp.call(methodName, context);
270: args.removePassParam("session");
271: } catch (ServiceProxyException ex) {
272: }
273: return context;
274: } else
275: DebugLogger
276: .logWarning("Service not available, it may not be registered : "
277: + methodName);
278:
279: return null;
280: }
281: }
|