Search in sources :

Example 1 with User

use of com.twinsoft.tas.User in project convertigo by convertigo.

the class JavelinConnector method prepareForTransaction.

@Override
public void prepareForTransaction(Context context) throws EngineException {
    String t = context.statistics.start(EngineStatistics.GET_JAVELIN_OBJECT);
    try {
        // Quick and dirty workaround for ticket #1280
        if (!checkKeys())
            throw new EngineException("No more key available; check your license keys!");
        // if something append during transaction execution
        com.twinsoft.api.Session session = Engine.theApp.sessionManager.getSession(context.contextID);
        if (session != null)
            session.resetSomethingChange();
        if (Engine.isStudioMode()) {
            if (javelin != null) {
                Engine.logBeans.debug("(JavelinConnector) Using the studio Javelin object");
                return;
            }
            throw new EngineException("Studio mode: the Legacy connector must be open in order to execute transactions");
        }
        Engine.logBeans.debug("(JavelinConnector) Retrieving the Javelin object");
        JavelinTransaction javelinTransaction = null;
        try {
            javelinTransaction = (JavelinTransaction) context.requestedObject;
        } catch (ClassCastException e) {
            throw new EngineException("Requested object is not a transaction", e);
        }
        int timeout = javelinTransaction.getTimeoutForDataStable();
        int threshold = javelinTransaction.getDataStableThreshold();
        Authentication auth = null;
        if (context.isRequestFromVic) {
            // instance, from a web service call).
            if (!context.isTrustedRequest) {
                try {
                    VicApi vicApi = new VicApi();
                    if (!vicApi.isServiceAuthorized(context.tasUserName, context.tasVirtualServerName, context.tasServiceCode)) {
                        throw new EngineException("The service '" + context.tasServiceCode + "' is not authorized for the user '" + context.tasUserName);
                    }
                } catch (IOException e) {
                    throw new EngineException("Unable to retrieve authorization from the VIC database.", e);
                }
            }
        } else {
            // Check the Carioca authorizations only if this is a non trusted request
            if (context.isTrustedRequest) {
            // Nothing to do: all the tas* variables from the context must have been set
            // by the caller.
            // Means we must not execute the TAS API.
            } else {
                // Getting Authentication object
                context.tasVirtualServerName = getVirtualServer();
                try {
                    String authName = (context.tasSessionKey == null ? context.tasUserName : context.tasSessionKey);
                    auth = Engine.theApp.getAuthenticationObject(context.tasVirtualServerName, authName);
                    // Logging to Carioca only if needed
                    User user = auth.getCurrentUser();
                    if (user == null) {
                        // Request from Carioca
                        if (context.tasSessionKey != null) {
                            auth.login(context.tasSessionKey);
                            User currentUser = auth.getCurrentUser();
                            context.tasUserName = currentUser.getName();
                            context.tasUserPassword = currentUser.getPassword();
                            context.tasVirtualServerName = "(SV #" + currentUser.getServerID() + ")";
                            String message = "Authentication to Carioca with sesskey = '" + context.tasSessionKey + "' => user: \"" + context.tasUserName + "\"";
                            Engine.logBeans.debug("(JavelinConnector) " + message);
                        } else // Specific user
                        if ((context.tasUserName != null) && (context.tasUserPassword != null)) {
                            auth.login(context.tasUserName, context.tasUserPassword);
                            String message = "Authentication to Carioca with user = '" + context.tasUserName + "' and password = '" + context.tasUserPassword + "'";
                            Engine.logBeans.debug("(JavelinConnector) " + message);
                        } else {
                            context.tasUserName = EnginePropertiesManager.getProperty(PropertyName.CARIOCA_DEFAULT_USER_NAME);
                            context.tasUserPassword = EnginePropertiesManager.getProperty(PropertyName.CARIOCA_DEFAULT_USER_PASSWORD);
                            auth.login(context.tasUserName, context.tasUserPassword);
                            String message = "Default authentication to Carioca";
                            Engine.logBeans.debug("(JavelinConnector) " + message);
                        }
                    } else {
                        context.tasUserName = user.getName();
                        context.tasUserPassword = user.getPassword();
                        context.tasUserGroup = user.getMainGroupName();
                        context.tasVirtualServerName = "(#" + Long.toString(user.getServerID()) + ")";
                        String message = "Already authenticated to Carioca with user = '" + context.tasUserName + "' and password = '" + context.tasUserPassword + "'";
                        Engine.logBeans.debug("(JavelinConnector) " + message);
                    }
                } catch (Exception e) {
                    auth = null;
                    String message = "Unable to authenticate to Carioca.\n" + "Carioca virtual server: " + context.tasVirtualServerName + "\n" + "SessKey: \"" + context.tasSessionKey + "\"\n" + "User: \"" + context.tasUserName + "\"\n" + "Password: \"" + context.tasUserPassword + "\"";
                    EngineException ee = new EngineException(message, e);
                    throw ee;
                }
            }
        }
        // We retrieve the current project
        String javelinServiceCode = context.tasServiceCode;
        if (javelinServiceCode == null) {
            javelinServiceCode = getServiceCode();
            Engine.logBeans.debug("(JavelinConnector)  No service code provided; getting the connector service code.");
        }
        // Analyzes/overrrides service code for device number pooling
        javelinServiceCode = analyzeServiceCode(javelinServiceCode);
        Engine.logBeans.debug("(JavelinConnector) Service code: " + javelinServiceCode);
        session = Engine.theApp.sessionManager.getSession(context.contextID);
        if (session == null) {
            Engine.logBeans.debug("(JavelinConnector) No session has been found; creation of a new one and ignoring the user request.");
            context.inputDocument = null;
            try {
                if (context.isRequestFromVic) {
                    session = Engine.theApp.sessionManager.addVicSession(javelinServiceCode, context.tasUserName, context.tasUserGroup + "@" + context.tasVirtualServerName, context.tasDteAddress, context.tasCommDevice, context.contextID);
                } else if (context.isTrustedRequest) {
                    String connectionParameters = getServiceCode();
                    try {
                        StringTokenizer st = new StringTokenizer(connectionParameters, ",");
                        String appType = st.nextToken();
                        if (appType.equals("vic")) {
                            javelinServiceCode = st.nextToken();
                            context.tasDteAddress = st.nextToken();
                            context.tasCommDevice = st.nextToken();
                            Engine.logBeans.debug("(JavelinConnector) Trusted request => the connector handles connection parameters : " + connectionParameters);
                            Engine.logBeans.debug("(JavelinConnector) serviceCode: " + javelinServiceCode);
                            Engine.logBeans.debug("(JavelinConnector) dteAddress: " + context.tasDteAddress);
                            Engine.logBeans.debug("(JavelinConnector) commDevice: " + context.tasCommDevice);
                            Engine.logBeans.debug("(JavelinConnector) user: " + context.tasUserName);
                            String group = context.tasUserGroup + "@" + context.tasVirtualServerName;
                            Engine.logBeans.debug("(JavelinConnector) group: " + group);
                            session = Engine.theApp.sessionManager.addVicSession(javelinServiceCode, context.tasUserName, group, context.tasDteAddress, context.tasCommDevice, context.contextID);
                        }
                    } catch (NoSuchElementException e) {
                        Engine.logBeans.error("(JavelinConnector) Invalid connector connection parameters: " + connectionParameters);
                    }
                } else {
                    session = Engine.theApp.sessionManager.addSession((int) emulatorID, auth, javelinServiceCode, context.contextID, getJavelinLanguage(), isSslEnabled(), isSslTrustAllServerCertificates(), getIbmTerminalType());
                }
            } catch (Exception e) {
                String message = "Unable to open the Javelin session: serviceCode= '" + javelinServiceCode + "', contextID = '" + context.contextID + "'";
                EngineException ee = new EngineException(message, e);
                throw ee;
            }
            if (session == null) {
                String message = "Unable to add a new session: the access to the service '" + javelinServiceCode + "' has been forbidden by the Carioca administrator.";
                EngineException ee = new EngineException(message);
                throw ee;
            }
            javelin = session.getJavelinObject();
            javelin.setLog(new LogWrapper(Engine.logEmulators));
            javelin.setDataStableOnCursorOn(false);
            javelin.connect(javelinTransaction.getTimeoutForConnect());
            boolean isConnected = javelin.isConnected();
            Engine.logBeans.debug("(JavelinConnector) isConnected=" + isConnected);
            if (!isConnected) {
                throw new ConnectionException("Unable to connect the session! See the emulator logs for more details...");
            }
            executeConnectionSyncCode(javelin, timeout, threshold);
        } else {
            Engine.logBeans.debug("(JavelinConnector) Using the existing session");
            if (context.isNewSession) {
                Engine.logBeans.debug("(JavelinConnector) New session required and ignoring the user request");
                context.inputDocument = null;
                // First, we remove the previous session
                Engine.theApp.sessionManager.removeSession(context.contextID);
                try {
                    if (context.isRequestFromVic) {
                        session = Engine.theApp.sessionManager.addVicSession(javelinServiceCode, context.tasUserName, context.tasUserGroup, context.tasDteAddress, context.tasCommDevice, context.contextID);
                    } else {
                        session = Engine.theApp.sessionManager.addSession((int) emulatorID, auth, javelinServiceCode, context.contextID, getJavelinLanguage(), isSslEnabled(), isSslTrustAllServerCertificates(), getIbmTerminalType());
                    }
                } catch (Exception e) {
                    String message = "Unable to open the Javelin session: serviceCode= '" + javelinServiceCode + "', contextID = '" + context.contextID + "'";
                    Engine.logBeans.warn("(JavelinConnector) " + message);
                    EngineException ee = new EngineException(message, e);
                    throw ee;
                }
                if (session == null) {
                    String message = "Unable to add a new session: the access to the service '" + javelinServiceCode + "' has been forbidden by the Carioca administrator.";
                    Engine.logBeans.warn("(JavelinConnector) " + message);
                    EngineException ee = new EngineException(message);
                    throw ee;
                }
                javelin = session.getJavelinObject();
                javelin.setLog(new LogWrapper(Engine.logEmulators));
                javelin.setDataStableOnCursorOn(false);
                javelin.connect(javelinTransaction.getTimeoutForConnect());
                if (!javelin.isConnected()) {
                    throw new ConnectionException("Unable to connect the session! See the emulator logs for more details...");
                }
                executeConnectionSyncCode(javelin, timeout, threshold);
            } else {
                javelin = session.getJavelinObject();
                // Reconnect only if the requested transaction is not the end transaction
                if ((!context.isDestroying) && (!session.isConnected())) {
                    javelin.setDataStableOnCursorOn(false);
                    javelin.connect(javelinTransaction.getTimeoutForConnect());
                    if (!javelin.isConnected()) {
                        throw new ConnectionException("Unable to connect the session! See the emulator logs for more details...");
                    }
                    executeConnectionSyncCode(javelin, timeout, threshold);
                }
            }
        }
        context.isNewSession = false;
    } finally {
        context.statistics.stop(t);
    }
}
Also used : User(com.twinsoft.tas.User) EngineException(com.twinsoft.convertigo.engine.EngineException) FindString(com.twinsoft.convertigo.beans.common.FindString) IOException(java.io.IOException) LogWrapper(com.twinsoft.convertigo.engine.util.LogWrapper) RESyntaxException(org.apache.regexp.RESyntaxException) NoSuchElementException(java.util.NoSuchElementException) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) EngineException(com.twinsoft.convertigo.engine.EngineException) JavelinTransaction(com.twinsoft.convertigo.beans.transactions.JavelinTransaction) StringTokenizer(java.util.StringTokenizer) Authentication(com.twinsoft.tas.Authentication) VicApi(com.twinsoft.convertigo.engine.plugins.VicApi) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

FindString (com.twinsoft.convertigo.beans.common.FindString)1 JavelinTransaction (com.twinsoft.convertigo.beans.transactions.JavelinTransaction)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 VicApi (com.twinsoft.convertigo.engine.plugins.VicApi)1 LogWrapper (com.twinsoft.convertigo.engine.util.LogWrapper)1 Authentication (com.twinsoft.tas.Authentication)1 User (com.twinsoft.tas.User)1 IOException (java.io.IOException)1 NoSuchElementException (java.util.NoSuchElementException)1 StringTokenizer (java.util.StringTokenizer)1 RESyntaxException (org.apache.regexp.RESyntaxException)1 EvaluatorException (org.mozilla.javascript.EvaluatorException)1 JavaScriptException (org.mozilla.javascript.JavaScriptException)1