Search in sources :

Example 11 with Context

use of com.twinsoft.convertigo.engine.Context in project convertigo by convertigo.

the class JavelinServlet method doRequest.

protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
    try {
        String poolName = null;
        String projectName = null;
        String servletPath = request.getServletPath();
        Engine.logEngine.debug("(JavelinServlet) servlet path: " + servletPath);
        int projectNameStartIndex = servletPath.indexOf("/projects/") + 10;
        int slashIndex = servletPath.indexOf("/", projectNameStartIndex);
        // Find the project name
        try {
            projectName = servletPath.substring(projectNameStartIndex, slashIndex);
            Engine.logEngine.debug("(JavelinServlet) project name: " + projectName);
        } catch (StringIndexOutOfBoundsException e) {
            throw new EngineException("Unable to find the project name into the provided URL (\"" + servletPath + "\").");
        }
        // Find the pool name
        int slashIndex2 = servletPath.lastIndexOf("/");
        String subPath = servletPath.substring(slashIndex, slashIndex2);
        Engine.logEngine.debug("(JavelinServlet) sub path: " + subPath);
        try {
            poolName = servletPath.substring(slashIndex2 + 1, servletPath.lastIndexOf("."));
            Engine.logEngine.debug("(JavelinServlet) pool name: " + poolName);
        } catch (StringIndexOutOfBoundsException e) {
        // Silently ignore
        }
        // Find the context name
        String contextName = request.getParameter(Parameter.Context.getName());
        if ((contextName == null) || (contextName.length() == 0))
            contextName = "default";
        else if (contextName.equals("*"))
            contextName = "default*";
        HttpSession httpSession = request.getSession();
        String sessionID = httpSession.getId();
        String sequenceName = request.getParameter(Parameter.Sequence.getName());
        String connectorName = request.getParameter(Parameter.Connector.getName());
        Context context = Engine.theApp.contextManager.get(null, contextName, sessionID, poolName, projectName, connectorName, sequenceName);
        Engine.logEngine.debug("(JavelinServlet) Found context: " + context.contextID);
        Javelin javelin = ((JavelinConnector) context.getConnector()).javelin;
        JavelinEventHandler semaphore = (JavelinEventHandler) context.get("convertigo.javelin_servlet.semaphore");
        if (semaphore != null) {
            synchronized (semaphore) {
                semaphore.notifyAll();
            }
        }
        semaphore = new JavelinEventHandler();
        context.set("convertigo.javelin_servlet.semaphore", semaphore);
        javelin.addDataAlteredListener(semaphore);
        javelin.addtwinxListener(semaphore);
        try {
            PrintWriter writer = response.getWriter();
            Engine.logEngine.debug("(JavelinServlet) semaphore=" + semaphore);
            synchronized (semaphore) {
                semaphore.dataAltered = false;
                try {
                    semaphore.wait(30000);
                } catch (InterruptedException e) {
                    writeXml(writer, "interrupted");
                    Engine.logEngine.debug("(JavelinServlet) Interrupted");
                    return;
                }
            }
            if (semaphore.dataAltered) {
                writeXml(writer, "refresh");
                Engine.logEngine.debug("(JavelinServlet) Refresh required");
            } else {
                writeXml(writer, "timeout");
                Engine.logEngine.debug("(JavelinServlet) Timeout occured");
            }
        } finally {
            javelin.removeDataAlteredListener(semaphore);
            javelin.removetwinxListener(semaphore);
        }
    } catch (Exception e) {
        Engine.logEngine.error("(JavelinServlet) Unable to process the request!", e);
        throw new ServletException(e);
    }
}
Also used : Context(com.twinsoft.convertigo.engine.Context) HttpSession(javax.servlet.http.HttpSession) EngineException(com.twinsoft.convertigo.engine.EngineException) Javelin(com.twinsoft.twinj.Javelin) ServletException(javax.servlet.ServletException) EngineException(com.twinsoft.convertigo.engine.EngineException) ServletException(javax.servlet.ServletException) JavelinConnector(com.twinsoft.convertigo.beans.connectors.JavelinConnector) PrintWriter(java.io.PrintWriter)

Example 12 with Context

use of com.twinsoft.convertigo.engine.Context in project convertigo by convertigo.

the class ServletRequester method getContext.

public Context getContext() throws Exception {
    HttpServletRequest request = (HttpServletRequest) inputData;
    initInternalVariables();
    if ("true".equals(RequestAttribute.system.string(request))) {
        return new Context("system");
    }
    String contextName = getContextName();
    Engine.logContext.debug("(ServletRequester) requested execution context: " + contextName);
    HttpSession httpSession = request.getSession();
    String sessionID = httpSession.getId();
    Engine.logContext.debug("(ServletRequester) requested execution sessionID: " + sessionID);
    context = Engine.theApp.contextManager.get(this, contextName, sessionID, poolName, projectName, connectorName, sequenceName);
    return context;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(com.twinsoft.convertigo.engine.Context) HttpSession(javax.servlet.http.HttpSession)

Example 13 with Context

use of com.twinsoft.convertigo.engine.Context in project convertigo by convertigo.

the class HttpClient method getData.

private byte[] getData(HttpConnector connector, String resourceUrl, ParameterShuttle infoShuttle) throws IOException, EngineException {
    byte[] result = null;
    try {
        Context context = infoShuttle.context;
        String proxyServer = Engine.theApp.proxyManager.getProxyServer();
        String proxyUser = Engine.theApp.proxyManager.getProxyUser();
        String proxyPassword = Engine.theApp.proxyManager.getProxyPassword();
        int proxyPort = Engine.theApp.proxyManager.getProxyPort();
        HostConfiguration hostConfiguration = connector.hostConfiguration;
        boolean trustAllServerCertificates = connector.isTrustAllServerCertificates();
        // Retrieving httpState
        getHttpState(connector, infoShuttle);
        Engine.logEngine.trace("(HttpClient) Retrieving data as a bytes array...");
        Engine.logEngine.debug("(HttpClient) Connecting to: " + resourceUrl);
        // Proxy configuration
        if (!proxyServer.equals("")) {
            hostConfiguration.setProxy(proxyServer, proxyPort);
            Engine.logEngine.debug("(HttpClient) Using proxy: " + proxyServer + ":" + proxyPort);
        } else {
            // Remove old proxy configuration
            hostConfiguration.setProxyHost(null);
        }
        Engine.logEngine.debug("(HttpClient) Https: " + connector.isHttps());
        CertificateManager certificateManager = connector.certificateManager;
        URL url = null;
        String host = "";
        int port = -1;
        if (resourceUrl.toLowerCase().startsWith("https:")) {
            Engine.logEngine.debug("(HttpClient) Setting up SSL properties");
            certificateManager.collectStoreInformation(context);
            url = new URL(resourceUrl);
            host = url.getHost();
            port = url.getPort();
            if (port == -1)
                port = 443;
            Engine.logEngine.debug("(HttpClient) Host: " + host + ":" + port);
            Engine.logEngine.debug("(HttpClient) CertificateManager has changed: " + certificateManager.hasChanged);
            if (certificateManager.hasChanged || (!host.equalsIgnoreCase(hostConfiguration.getHost())) || (hostConfiguration.getPort() != port)) {
                Engine.logEngine.debug("(HttpClient) Using MySSLSocketFactory for creating the SSL socket");
                Protocol myhttps = new Protocol("https", MySSLSocketFactory.getSSLSocketFactory(certificateManager.keyStore, certificateManager.keyStorePassword, certificateManager.trustStore, certificateManager.trustStorePassword, trustAllServerCertificates), port);
                hostConfiguration.setHost(host, port, myhttps);
            }
            resourceUrl = url.getFile();
            Engine.logEngine.debug("(HttpClient) Updated URL for SSL purposes: " + resourceUrl);
        } else {
            url = new URL(resourceUrl);
            host = url.getHost();
            port = url.getPort();
            Engine.logEngine.debug("(HttpClient) Host: " + host + ":" + port);
            hostConfiguration.setHost(host, port);
        }
        Engine.logEngine.debug("(HttpClient) Building method on: " + resourceUrl);
        Engine.logEngine.debug("(HttpClient) postFromUser=" + infoShuttle.postFromUser);
        Engine.logEngine.debug("(HttpClient) postToSite=" + infoShuttle.postToSite);
        if (infoShuttle.postFromUser && infoShuttle.postToSite) {
            method = new PostMethod(resourceUrl);
            ((PostMethod) method).setRequestEntity(new StringRequestEntity(infoShuttle.userPostData, infoShuttle.userContentType, infoShuttle.context.httpServletRequest.getCharacterEncoding()));
        } else {
            method = new GetMethod(resourceUrl);
        }
        HttpMethodParams httpMethodParams = method.getParams();
        // Cookie configuration
        if (handleCookie) {
            Engine.logEngine.debug("(HttpClient) Setting cookie policy.");
            httpMethodParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        }
        String basicUser = connector.getAuthUser();
        String basicPassword = connector.getAuthPassword();
        String givenBasicUser = connector.getGivenAuthUser();
        String givenBasicPassword = connector.getGivenAuthPassword();
        // Basic authentication configuration
        String realm = null;
        if (!basicUser.equals("") || (basicUser.equals("") && (givenBasicUser != null))) {
            String userName = ((givenBasicUser == null) ? basicUser : givenBasicUser);
            String userPassword = ((givenBasicPassword == null) ? basicPassword : givenBasicPassword);
            httpState.setCredentials(new AuthScope(host, port, realm), new UsernamePasswordCredentials(userName, userPassword));
            Engine.logEngine.debug("(HttpClient) Credentials: " + userName + ":******");
        }
        // Setting basic authentication for proxy
        if (!proxyServer.equals("") && !proxyUser.equals("")) {
            httpState.setProxyCredentials(new AuthScope(proxyServer, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));
            Engine.logEngine.debug("(HttpClient) Proxy credentials: " + proxyUser + ":******");
        }
        // Setting HTTP headers
        Engine.logEngine.debug("(HttpClient) Incoming HTTP headers:");
        String headerName, headerValue;
        for (int k = 0, kSize = infoShuttle.userHeaderNames.size(); k < kSize; k++) {
            headerName = (String) infoShuttle.userHeaderNames.get(k);
            // See #986 (Multiples cookies don't work with some proxies)
            if (headerName.toLowerCase().equals("cookie")) {
                Engine.logEngine.debug("Cookie header ignored");
            } else {
                headerValue = (String) infoShuttle.userHeaderValues.get(k);
                method.setRequestHeader(headerName, headerValue);
                Engine.logEngine.debug(headerName + "=" + headerValue);
            }
        }
        // Getting the result
        executeMethod(method, connector, resourceUrl, infoShuttle);
        result = method.getResponseBody();
    } finally {
        if (method != null)
            method.releaseConnection();
    }
    return result;
}
Also used : Context(com.twinsoft.convertigo.engine.Context) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) CertificateManager(com.twinsoft.convertigo.engine.CertificateManager) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) Protocol(org.apache.commons.httpclient.protocol.Protocol)

Example 14 with Context

use of com.twinsoft.convertigo.engine.Context in project convertigo by convertigo.

the class ConnectorEditorPart method getStudioContext.

private Context getStudioContext(boolean bForce) {
    String projectName = connector.getParent().getName();
    String connectorName = connector.getName();
    String contextType = ContextManager.CONTEXT_TYPE_TRANSACTION;
    String contextID = Engine.theApp.contextManager.computeStudioContextName(contextType, projectName, connectorName);
    Context ctx = Engine.theApp.contextManager.get(contextID);
    if ((ctx == null) || bForce) {
        ctx = new Context(contextID);
        if (connector instanceof HtmlConnector) {
            ctx.cleanXpathApi();
            ctx.htmlParser = ((HtmlConnector) connector).getHtmlParser();
        }
        ctx.contextID = contextID;
        ctx.name = contextID;
        ctx.projectName = projectName;
        ctx.setConnector(connector);
        ctx.lastAccessTime = System.currentTimeMillis();
        Engine.theApp.contextManager.add(ctx);
    }
    return ctx;
}
Also used : Context(com.twinsoft.convertigo.engine.Context) HtmlConnector(com.twinsoft.convertigo.beans.connectors.HtmlConnector)

Example 15 with Context

use of com.twinsoft.convertigo.engine.Context in project convertigo by convertigo.

the class Sequence method removeTransactionContexts.

private void removeTransactionContexts() {
    if (Engine.isEngineMode()) {
        if (useSameJSessionForSteps()) {
            String sessionID, contextName;
            if (Engine.logBeans.isDebugEnabled())
                Engine.logBeans.debug("(Sequence) Executing deletion of sub contexts for sequence \"" + getName() + "\"");
            sessionID = getSessionId();
            for (int i = 0; i < stepContextNames.size(); i++) {
                contextName = (String) stepContextNames.get(i);
                String contextID = sessionID + "_" + contextName;
                Context ctx = Engine.theApp.contextManager.get(contextID);
                if (ctx != null) {
                    String type = ctx.transaction != null ? "transaction's" : "sequence's";
                    if (contextName.startsWith("Container-")) {
                        // Only remove context automatically named
                        if (Engine.logBeans.isDebugEnabled())
                            Engine.logBeans.debug("(Sequence) Removing " + type + " context \"" + contextID + "\"");
                        Engine.theApp.contextManager.remove(contextID);
                    } else {
                        if (Engine.logBeans.isDebugEnabled())
                            Engine.logBeans.debug("(Sequence) Keeping " + type + " context \"" + contextID + "\"");
                    }
                }
            }
            if (Engine.logBeans.isDebugEnabled())
                Engine.logBeans.debug("(Sequence) Deletion of sub contexts for sequence \"" + getName() + "\" done");
        } else {
            if (transactionSessionId != null) {
                if (Engine.logBeans.isDebugEnabled())
                    Engine.logBeans.debug("(Sequence) Executing deletion of sub contexts for sequence \"" + getName() + "\"");
                Engine.theApp.contextManager.removeAll(transactionSessionId);
                if (Engine.logBeans.isDebugEnabled())
                    Engine.logBeans.debug("(Sequence) Deletion of sub contexts for sequence \"" + getName() + "\" done");
            }
        }
    }
}
Also used : Context(com.twinsoft.convertigo.engine.Context)

Aggregations

Context (com.twinsoft.convertigo.engine.Context)18 HttpSession (javax.servlet.http.HttpSession)4 EngineException (com.twinsoft.convertigo.engine.EngineException)3 HtmlConnector (com.twinsoft.convertigo.beans.connectors.HtmlConnector)2 CertificateManager (com.twinsoft.convertigo.engine.CertificateManager)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)2 HttpState (org.apache.commons.httpclient.HttpState)2 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)2 AuthScope (org.apache.commons.httpclient.auth.AuthScope)2 GetMethod (org.apache.commons.httpclient.methods.GetMethod)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2 Protocol (org.apache.commons.httpclient.protocol.Protocol)2 Session (com.twinsoft.api.Session)1 JavelinConnector (com.twinsoft.convertigo.beans.connectors.JavelinConnector)1 ProxyHttpConnector (com.twinsoft.convertigo.beans.connectors.ProxyHttpConnector)1 Project (com.twinsoft.convertigo.beans.core.Project)1