001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: *
024: */
025: package com.bostechcorp.cbesb.console.server;
026:
027: import java.lang.reflect.InvocationTargetException;
028: import java.lang.reflect.Method;
029:
030: import javax.servlet.http.HttpSession;
031:
032: import com.bostechcorp.cbesb.common.i18n.CoreMessageConstants;
033: import com.bostechcorp.cbesb.common.i18n.Messages;
034: import com.bostechcorp.cbesb.console.common.Constants;
035: import com.bostechcorp.cbesb.console.common.SecurityLoginInfo;
036: import com.bostechcorp.cbesb.console.common.ServerSideException;
037: import com.bostechcorp.cbesb.console.common.TimeoutException;
038: import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
039: import com.google.gwt.user.client.rpc.IsSerializable;
040: import com.google.gwt.user.client.rpc.SerializationException;
041: import com.google.gwt.user.server.rpc.RPC;
042: import com.google.gwt.user.server.rpc.RPCRequest;
043: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
044: import com.google.gwt.user.server.rpc.SerializationPolicy;
045:
046: public abstract class ConsoleRemoteServiceServlet extends
047: RemoteServiceServlet {
048:
049: private static final long serialVersionUID = -5837655682595174821L;
050:
051: public ConsoleRemoteServiceServlet() {
052: super ();
053: }
054:
055: public String processCall(String payload)
056: throws SerializationException {
057: RPCRequest rpcRequest = RPC.decodeRequest(payload, this
058: .getClass(), this );
059: Method method = rpcRequest.getMethod();
060: String result = null;
061: HttpSession session = this .getThreadLocalRequest().getSession();
062: if (!method.getName().startsWith("polling")
063: && !method.getName().equals("logout")
064: && !method.getName().equals("login")) {
065:
066: SecurityLoginInfo sli = (SecurityLoginInfo) session
067: .getAttribute(Constants.SECURITY_LOGIN_INFO);
068: if (sli != null && (sli.getTimeout() > 0)) {
069: Long lastCallTimeStamp = (Long) session
070: .getAttribute(Constants.LAST_CALL_TIMESTAMP);
071: if (lastCallTimeStamp + sli.getTimeout() * 1000 < System
072: .currentTimeMillis()) {
073: session.setAttribute(Constants.SECURITY_LOGIN_INFO,
074: null);
075: session.setAttribute(Constants.SECURITY_LOGIN_ID,
076: null);
077: return RPC.encodeResponseForFailure(null,
078: new TimeoutException());
079: } else {
080: session.setAttribute(Constants.LAST_CALL_TIMESTAMP,
081: System.currentTimeMillis());
082: }
083: } else if (sli == null) {
084: return RPC.encodeResponseForFailure(null,
085: new TimeoutException());
086: }
087: }
088: try {
089: result = invokeAndEncodeResponse(this , rpcRequest
090: .getMethod(), rpcRequest.getParameters(),
091: rpcRequest.getSerializationPolicy());
092: } catch (IncompatibleRemoteServiceException ex) {
093: getServletContext()
094: .log(
095: Messages
096: .get(CoreMessageConstants.INCOMPATIBLE_REMOTE_SERVICE_EXCEPTION),
097: ex);
098: result = RPC.encodeResponseForFailure(null, ex);
099: }
100:
101: return result;
102: }
103:
104: public static String invokeAndEncodeResponse(Object target,
105: Method serviceMethod, Object[] args,
106: SerializationPolicy serializationPolicy)
107: throws SerializationException {
108: if (serviceMethod == null) {
109: throw new NullPointerException(Messages
110: .get(CoreMessageConstants.SERVICE_METHOD));
111: }
112:
113: if (serializationPolicy == null) {
114: throw new NullPointerException(Messages
115: .get(CoreMessageConstants.SERIALIZATION_POLICY));
116: }
117:
118: String responsePayload;
119: try {
120: Object result = serviceMethod.invoke(target, args);
121: // if(result instanceof IRPCResultInfo){
122: // IRPCResultInfo rpcResult=(IRPCResultInfo)result;
123: // if(rpcResult.isError()&& rpcResult.isDBConnectionError()){
124: // startDerbyServer();
125: // result = serviceMethod.invoke(target, args);
126: // }
127: // }
128: responsePayload = RPC.encodeResponseForSuccess(
129: serviceMethod, result, serializationPolicy);
130: } catch (IllegalAccessException e) {
131: responsePayload = RPC.encodeResponseForFailure(null, e
132: .getCause());
133:
134: } catch (IllegalArgumentException e) {
135: responsePayload = RPC.encodeResponseForFailure(null, e
136: .getCause());
137:
138: } catch (InvocationTargetException e) {
139: Throwable cause = e.getCause();
140: if (cause instanceof IsSerializable)
141: responsePayload = RPC.encodeResponseForFailure(null,
142: cause);
143: else
144: responsePayload = RPC.encodeResponseForFailure(null,
145: new ServerSideException(cause));
146: }
147:
148: return responsePayload;
149: }
150:
151: protected String getCurrentUserId() throws ServerSideException {
152: HttpSession session = this .getThreadLocalRequest().getSession();
153: String userId = (String) session
154: .getAttribute(Constants.SECURITY_LOGIN_ID);
155: if (userId == null) {
156: throw new ServerSideException(Messages
157: .get(CoreMessageConstants.SESSION_TIME_OUT));
158: }
159: return userId;
160:
161: }
162:
163: }
|