Search in sources :

Example 11 with EngineEvent

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

the class Step method stepExecute.

protected boolean stepExecute(Context javascriptContext, Scriptable scope) throws EngineException {
    if (isEnabled && sequence.isRunning()) {
        if (Engine.logBeans.isDebugEnabled())
            Engine.logBeans.debug("Executing step named '" + this + "' (" + this.getName() + ")");
        Long key = priority;
        // We fire engine events only in studio mode.
        if (Engine.isStudioMode()) {
            Step loadedStep = (Step) sequence.loadedSteps.get(key).getOriginal();
            Engine.theApp.fireObjectDetected(new EngineEvent(loadedStep));
            if (Engine.logBeans.isTraceEnabled())
                Engine.logBeans.trace("(Step) Step reached before its execution \"" + getName() + "\" ( " + this + " [" + hashCode() + "] ).");
            Engine.theApp.fireStepReached(new EngineEvent(loadedStep));
        }
        // Generates execution ID
        executeTimeID = getExecuteTimeID();
        // Adds step's reference to executed steps
        executedSteps.put(key, executeTimeID);
        if (Engine.logBeans.isTraceEnabled())
            Engine.logBeans.trace("Step copy [" + executeTimeID + "] contains " + executedSteps.size() + " executed steps.");
        // Adds step's reference to sequence copies
        sequence.addCopy(executeTimeID, this);
        sequence.setCurrentStep(this);
        sequence.appendStepNode(this);
        return true;
    }
    return false;
}
Also used : EngineEvent(com.twinsoft.convertigo.engine.EngineEvent) IfExistThenElseStep(com.twinsoft.convertigo.beans.steps.IfExistThenElseStep) SourceStep(com.twinsoft.convertigo.beans.steps.SourceStep) ElementStep(com.twinsoft.convertigo.beans.steps.ElementStep) IfFileExistStep(com.twinsoft.convertigo.beans.steps.IfFileExistStep) AttributeStep(com.twinsoft.convertigo.beans.steps.AttributeStep) SimpleSourceStep(com.twinsoft.convertigo.beans.steps.SimpleSourceStep) XMLAttributeStep(com.twinsoft.convertigo.beans.steps.XMLAttributeStep) SequenceStep(com.twinsoft.convertigo.beans.steps.SequenceStep) IfFileExistThenElseStep(com.twinsoft.convertigo.beans.steps.IfFileExistThenElseStep) IsInThenElseStep(com.twinsoft.convertigo.beans.steps.IsInThenElseStep) XMLElementStep(com.twinsoft.convertigo.beans.steps.XMLElementStep) XMLConcatStep(com.twinsoft.convertigo.beans.steps.XMLConcatStep) IsInStep(com.twinsoft.convertigo.beans.steps.IsInStep) IfExistStep(com.twinsoft.convertigo.beans.steps.IfExistStep) IfStep(com.twinsoft.convertigo.beans.steps.IfStep) TransactionStep(com.twinsoft.convertigo.beans.steps.TransactionStep) IfThenElseStep(com.twinsoft.convertigo.beans.steps.IfThenElseStep)

Example 12 with EngineEvent

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

the class HtmlTransaction method applyExtractionRules.

public void applyExtractionRules(HtmlScreenClass screenClass, boolean bNotFirstLoop) throws EngineException {
    String t = context.statistics.start(EngineStatistics.APPLY_EXTRACTION_RULES);
    try {
        // We apply the extraction rules for this screen class
        int extractionRuleInitReason;
        List<ExtractionRule> vExtractionRules = screenClass.getExtractionRules();
        for (ExtractionRule extractionRule : vExtractionRules) {
            HtmlExtractionRule htmlExtractionRule = (HtmlExtractionRule) extractionRule;
            if (!runningThread.bContinue)
                break;
            if (!extractionRule.isEnabled()) {
                Engine.logBeans.trace("(HtmlTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because it has been disabled.");
                continue;
            }
            Engine.logBeans.debug("(HtmlTransaction) Applying the extraction rule \"" + extractionRule.getName() + "\"");
            extractionRule.checkSymbols();
            String extractionRuleQName = extractionRule.getQName();
            if (vExtractionRulesInited.contains(extractionRuleQName)) {
                extractionRuleInitReason = ExtractionRule.ACCUMULATING;
            } else {
                extractionRuleInitReason = ExtractionRule.INITIALIZING;
                vExtractionRulesInited.add(extractionRuleQName);
            }
            Engine.logBeans.trace("(HtmlTransaction) Initializing extraction rule (reason = " + extractionRuleInitReason + ")...");
            extractionRule.init(extractionRuleInitReason);
            // We fire engine events only in studio mode.
            if (Engine.isStudioMode()) {
                Engine.theApp.fireObjectDetected(new EngineEvent(extractionRule));
            }
            boolean hasMatched = htmlExtractionRule.apply(currentXmlDocument, context);
            if (hasMatched) {
                htmlExtractionRule.addToScope(scope);
                Engine.logBeans.trace("(HtmlTransaction) Applying extraction rule '" + extractionRule.getName() + "': matching");
            } else
                Engine.logBeans.trace("(HtmlTransaction) Applying extraction rule '" + extractionRule.getName() + "': not matching");
            // We fire engine events only in studio mode.
            if (Engine.isStudioMode()) {
                Engine.logBeans.debug("(HtmlTransaction) Step reached after having applied the extraction rule \"" + extractionRule.getName() + "\".");
                Engine.theApp.fireStepReached(new EngineEvent(extractionRule));
            }
            extractionRule = null;
        }
        vExtractionRules = null;
    } finally {
        context.statistics.stop(t, bNotFirstLoop);
    }
}
Also used : EngineEvent(com.twinsoft.convertigo.engine.EngineEvent) ExtractionRule(com.twinsoft.convertigo.beans.core.ExtractionRule) HtmlExtractionRule(com.twinsoft.convertigo.beans.extractionrules.HtmlExtractionRule) HtmlExtractionRule(com.twinsoft.convertigo.beans.extractionrules.HtmlExtractionRule)

Example 13 with EngineEvent

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

the class JavelinTransaction method applyExtractionRules.

public void applyExtractionRules(JavelinScreenClass screenClass, BlockFactory blockFactory, iJavelin javelin, boolean bNotFirstLoop) throws EngineException {
    String t = context.statistics.start(EngineStatistics.APPLY_EXTRACTION_RULES);
    try {
        // We apply the extraction rules for this screen class to
        // the words list.
        Block block = null;
        JavelinExtractionRuleResult extractionRuleResult;
        int extractionRuleInitReason;
        boolean panelExtractionRuleFound = false;
        List<ExtractionRule> vExtractionRules = screenClass.getExtractionRules();
        for (ExtractionRule extractionRule : vExtractionRules) {
            JavelinExtractionRule javelinExtractionRule = (JavelinExtractionRule) extractionRule;
            if (!runningThread.bContinue)
                break;
            /*
    			if ((Engine.objectsProvider == null) && !javelin.isConnected()) {
    				throw new ConnectionException("The emulator has been disconnected! See the emulator logs for more details...");
    			}
    			*/
            blockFactory.moveToFirstBlock();
            if (!extractionRule.isEnabled()) {
                // if extraction rule is disabled
                Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because it has been disabled.");
                continue;
            }
            if (blockFactory instanceof PanelBlockFactory && !panelExtractionRuleFound) {
                // if we apply the rules in a panel and the rule is before the rule which has created the panel
                Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because it has been applied before the panel creation.");
                if (extractionRule instanceof Nptui)
                    panelExtractionRuleFound = true;
                continue;
            }
            if (blockFactory instanceof PanelBlockFactory && extractionRule instanceof TabBox) {
                // if we try to apply tabbox rule in a panel
                Block panel = ((PanelBlockFactory) blockFactory).getPanel();
                XMLRectangle zone = ((TabBox) extractionRule).getSelectionScreenZone();
                if (zone.contains(panel.column, panel.line, Integer.parseInt(panel.getOptionalAttribute("width")), Integer.parseInt(panel.getOptionalAttribute("height")))) {
                    // if the tabbox screen zone is larger than the panel
                    Engine.logContext.trace("(JavelinTransaction) Skipping the extraction rule \"" + extractionRule.getName() + "\" because the screen zone is larger than the panel.");
                    continue;
                }
            }
            Engine.logContext.debug("(JavelinTransaction) Applying the extraction rule \"" + extractionRule.getName() + "\" on blocks");
            String extractionRuleQName = extractionRule.getQName();
            if (vExtractionRulesInited.contains(extractionRuleQName)) {
                extractionRuleInitReason = ExtractionRule.ACCUMULATING;
            } else {
                extractionRuleInitReason = ExtractionRule.INITIALIZING;
                vExtractionRulesInited.add(extractionRuleQName);
            }
            Engine.logContext.trace("(JavelinTransaction) Initializing extraction rule (reason = " + extractionRuleInitReason + ")...");
            extractionRule.init(extractionRuleInitReason);
            // We fire engine events only in studio mode.
            if (Engine.isStudioMode()) {
                Engine.theApp.fireObjectDetected(new EngineEvent(extractionRule));
            }
            // We try to apply the current extraction rule on each block.
            while (runningThread.bContinue && ((block = blockFactory.getNextBlock(block)) != null)) {
                Engine.logContext.trace("(JavelinTransaction) Analyzing block \"" + block.getText() + "\"");
                // We skip final blocks.
                if (block.bFinal) {
                    Engine.logContext.trace("(JavelinTransaction) The block has been marked as final; skipping it.");
                    continue;
                }
                extractionRuleResult = javelinExtractionRule.apply(javelin, block, blockFactory, context.outputDocument);
                if (extractionRuleResult.hasMatched) {
                    // extraction rules on this block and pass on the next block.
                    if (javelinExtractionRule.isFinal()) {
                        block.bFinal = true;
                        Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': matching and final");
                    } else {
                        Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': matching");
                    }
                    // We need to update the current block only if the
                    // rule has matched.
                    block = extractionRuleResult.newCurrentBlock;
                } else {
                    Engine.logContext.trace("(JavelinTransaction) Applying extraction rule '" + extractionRule.getName() + "': not matching");
                }
            }
            // We fire engine events only in studio mode.
            if (Engine.isStudioMode()) {
                Engine.theApp.fireBlocksChanged(new EngineEvent(blockFactory));
                Engine.logContext.debug("(JavelinTransaction) Step reached after having applied the extraction rule \"" + extractionRule.getName() + "\".");
                Engine.theApp.fireStepReached(new EngineEvent(extractionRule));
            }
        }
        vExtractionRules = null;
        extractionRuleResult = null;
    } finally {
        context.statistics.stop(t, bNotFirstLoop);
    }
}
Also used : PanelBlockFactory(com.twinsoft.convertigo.beans.common.PanelBlockFactory) JavelinExtractionRule(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule) ExtractionRule(com.twinsoft.convertigo.beans.core.ExtractionRule) JavelinExtractionRule(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRule) TabBox(com.twinsoft.convertigo.beans.common.TabBox) JavelinExtractionRuleResult(com.twinsoft.convertigo.beans.extractionrules.JavelinExtractionRuleResult) XMLRectangle(com.twinsoft.convertigo.beans.common.XMLRectangle) Block(com.twinsoft.convertigo.beans.core.Block) Nptui(com.twinsoft.convertigo.beans.sna.Nptui) EngineEvent(com.twinsoft.convertigo.engine.EngineEvent)

Example 14 with EngineEvent

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

the class ProjectTreeObject method checkMissingProjects.

public void checkMissingProjects(final boolean doReload) {
    synchronized (this) {
        if (isCheckMissingProjects) {
            return;
        }
        isCheckMissingProjects = true;
    }
    final Project project = getObject();
    Job.create("Check missing project for " + project.getName(), (monitor) -> {
        try {
            final Set<String> missingProjects = project.getMissingProjects().keySet();
            final Set<String> missingProjectReferences = project.getMissingProjectReferences().keySet();
            if (!missingProjects.isEmpty() || !missingProjectReferences.isEmpty()) {
                List<String> allProjects = Engine.theApp.databaseObjectsManager.getAllProjectNamesList(false);
                for (Iterator<String> i = missingProjects.iterator(); i.hasNext(); ) {
                    String targetProjectName = i.next();
                    if (allProjects.contains(targetProjectName)) {
                        Display.getDefault().syncExec(() -> {
                            try {
                                ProjectExplorerView pev = getProjectExplorerView();
                                TreeObject obj = pev.getProjectRootObject(targetProjectName);
                                if (obj != null && obj instanceof UnloadedProjectTreeObject) {
                                    pev.loadProject(((UnloadedProjectTreeObject) obj));
                                    i.remove();
                                }
                            } catch (Exception e) {
                                Engine.logStudio.warn("Failed to open \"" + targetProjectName + "\"", e);
                            }
                        });
                    }
                }
                Map<String, ProjectUrlParser> refToImport = new HashMap<>();
                for (Reference ref : project.getReferenceList()) {
                    if (ref instanceof ProjectSchemaReference) {
                        ProjectSchemaReference prjRef = (ProjectSchemaReference) ref;
                        if (missingProjects.contains(prjRef.getParser().getProjectName()) && prjRef.getParser().isValid()) {
                            refToImport.put(prjRef.getParser().getProjectName(), prjRef.getParser());
                        }
                    }
                }
                if (!refToImport.isEmpty()) {
                    Engine.execute(() -> {
                        boolean loaded = false;
                        for (ProjectUrlParser parser : refToImport.values()) {
                            try {
                                loaded |= Engine.theApp.referencedProjectManager.importProject(parser) != null;
                            } catch (Exception e) {
                                Engine.logStudio.warn("Failed to load '" + parser.getProjectName() + "'", e);
                            }
                        }
                        if (loaded) {
                            Engine.theApp.fireMigrationFinished(new EngineEvent(""));
                        }
                    });
                    return;
                }
                String message = "For \"" + project.getName() + "\" project :\n";
                for (String targetProjectName : missingProjects) {
                    message += "  > The project \"" + targetProjectName + "\" is missing\n";
                }
                for (String targetProjectName : missingProjectReferences) {
                    message += "  > The reference to project \"" + targetProjectName + "\" is missing\n";
                }
                message += "\nPlease create missing reference(s) and import missing project(s), or correct your project.";
                if (!missingProjectReferences.isEmpty()) {
                    final String msg = message;
                    final String warn = message;
                    Display.getDefault().syncExec(() -> {
                        CustomDialog customDialog = new CustomDialog(null, "Project references", msg + "\n\nDo you want to automatically add reference objects ?", 670, 250, new ButtonSpec("Always", true), new ButtonSpec("Never", false));
                        String autoCreate = ConvertigoPlugin.getProperty(ConvertigoPlugin.PREFERENCE_AUTO_CREATE_PROJECT_REFERENCE);
                        int response = autoCreate.isEmpty() ? customDialog.open() : (autoCreate.equalsIgnoreCase("true") ? 0 : 1);
                        ConvertigoPlugin.setProperty(ConvertigoPlugin.PREFERENCE_AUTO_CREATE_PROJECT_REFERENCE, response == 0 ? "true" : "false");
                        if (response == 0) {
                            for (String targetProjectName : missingProjectReferences) {
                                try {
                                    ProjectSchemaReference reference = new ProjectSchemaReference();
                                    String projectName = targetProjectName;
                                    reference.setName(targetProjectName + "_reference");
                                    projectName = ProjectUrlParser.getUrl(projectName);
                                    reference.setProjectName(projectName);
                                    reference.hasChanged = true;
                                    project.add(reference);
                                } catch (Exception e) {
                                    ConvertigoPlugin.logException(e, "failed to add a reference to '" + targetProjectName + "'");
                                }
                            }
                            try {
                                if (doReload || autoCreate.isEmpty()) {
                                    ProjectExplorerView pev = getProjectExplorerView();
                                    pev.reloadTreeObject(ProjectTreeObject.this);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            hasBeenModified(true);
                        } else {
                            Engine.logBeans.warn(warn);
                        }
                    });
                } else if (!missingProjects.isEmpty()) {
                    ConvertigoPlugin.warningMessageBox(message);
                }
            }
        } finally {
            isCheckMissingProjects = false;
        }
    }).schedule();
}
Also used : TreeObjectEvent(com.twinsoft.convertigo.eclipse.views.projectexplorer.TreeObjectEvent) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) CoreException(org.eclipse.core.runtime.CoreException) Step(com.twinsoft.convertigo.beans.core.Step) JScriptEditorInput(com.twinsoft.convertigo.eclipse.editors.jscript.JScriptEditorInput) NamespaceUriValidator(com.twinsoft.convertigo.eclipse.property_editors.validators.NamespaceUriValidator) IPath(org.eclipse.core.runtime.IPath) PartInitException(org.eclipse.ui.PartInitException) Map(java.util.Map) IViewPart(org.eclipse.ui.IViewPart) ConnectorEditor(com.twinsoft.convertigo.eclipse.editors.connector.ConnectorEditor) Cursor(org.eclipse.swt.graphics.Cursor) RequestableStep(com.twinsoft.convertigo.beans.core.RequestableStep) GenericUtils(com.twinsoft.convertigo.engine.util.GenericUtils) IEditorInput(org.eclipse.ui.IEditorInput) PlatformUI(org.eclipse.ui.PlatformUI) Set(java.util.Set) Display(org.eclipse.swt.widgets.Display) StepWithExpressions(com.twinsoft.convertigo.beans.core.StepWithExpressions) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ConvertigoPlugin(com.twinsoft.convertigo.eclipse.ConvertigoPlugin) List(java.util.List) Reference(com.twinsoft.convertigo.beans.core.Reference) Sequence(com.twinsoft.convertigo.beans.core.Sequence) Transaction(com.twinsoft.convertigo.beans.core.Transaction) Path(org.eclipse.core.runtime.Path) SWT(org.eclipse.swt.SWT) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) SequenceEditorInput(com.twinsoft.convertigo.eclipse.editors.sequence.SequenceEditorInput) EngineException(com.twinsoft.convertigo.engine.EngineException) SequenceEditor(com.twinsoft.convertigo.eclipse.editors.sequence.SequenceEditor) SourcePickerView(com.twinsoft.convertigo.eclipse.views.sourcepicker.SourcePickerView) CouchDbConnector(com.twinsoft.convertigo.beans.connectors.CouchDbConnector) IFolder(org.eclipse.core.resources.IFolder) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) TraceFileEditorInput(com.twinsoft.convertigo.eclipse.editors.text.TraceFileEditorInput) EngineEvent(com.twinsoft.convertigo.engine.EngineEvent) HashMap(java.util.HashMap) ProjectUrlParser(com.twinsoft.convertigo.engine.util.ProjectUrlParser) ConvertigoException(com.twinsoft.convertigo.engine.ConvertigoException) JsonIndex(com.twinsoft.convertigo.beans.couchdb.JsonIndex) ArrayList(java.util.ArrayList) MobileBuilder(com.twinsoft.convertigo.engine.mobile.MobileBuilder) SequenceStep(com.twinsoft.convertigo.beans.steps.SequenceStep) Connector(com.twinsoft.convertigo.beans.core.Connector) IProject(org.eclipse.core.resources.IProject) IResourceDelta(org.eclipse.core.resources.IResourceDelta) CustomDialog(com.twinsoft.convertigo.eclipse.dialogs.CustomDialog) IFile(org.eclipse.core.resources.IFile) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) ICellEditorValidator(org.eclipse.jface.viewers.ICellEditorValidator) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ProjectSchemaReference(com.twinsoft.convertigo.beans.references.ProjectSchemaReference) Shell(org.eclipse.swt.widgets.Shell) Iterator(java.util.Iterator) Viewer(org.eclipse.jface.viewers.Viewer) Job(org.eclipse.core.runtime.jobs.Job) Engine(com.twinsoft.convertigo.engine.Engine) IResourceDeltaVisitor(org.eclipse.core.resources.IResourceDeltaVisitor) CouchDbManager(com.twinsoft.convertigo.engine.providers.couchdb.CouchDbManager) Project(com.twinsoft.convertigo.beans.core.Project) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) IResource(org.eclipse.core.resources.IResource) MessageBox(org.eclipse.swt.widgets.MessageBox) IEditorReference(org.eclipse.ui.IEditorReference) ButtonSpec(com.twinsoft.convertigo.eclipse.dialogs.ButtonSpec) TransactionStep(com.twinsoft.convertigo.beans.steps.TransactionStep) ConnectorEditorInput(com.twinsoft.convertigo.eclipse.editors.connector.ConnectorEditorInput) ProjectUrlParser(com.twinsoft.convertigo.engine.util.ProjectUrlParser) Set(java.util.Set) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) ProjectSchemaReference(com.twinsoft.convertigo.beans.references.ProjectSchemaReference) Reference(com.twinsoft.convertigo.beans.core.Reference) ProjectSchemaReference(com.twinsoft.convertigo.beans.references.ProjectSchemaReference) IEditorReference(org.eclipse.ui.IEditorReference) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) EngineException(com.twinsoft.convertigo.engine.EngineException) ConvertigoException(com.twinsoft.convertigo.engine.ConvertigoException) IProject(org.eclipse.core.resources.IProject) Project(com.twinsoft.convertigo.beans.core.Project) CustomDialog(com.twinsoft.convertigo.eclipse.dialogs.CustomDialog) Iterator(java.util.Iterator) EngineEvent(com.twinsoft.convertigo.engine.EngineEvent) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ButtonSpec(com.twinsoft.convertigo.eclipse.dialogs.ButtonSpec)

Example 15 with EngineEvent

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

the class SiteClipperConnector method doProcessRequest.

private void doProcessRequest(Shuttle shuttle) throws IOException, ServletException, EngineException {
    shuttle.statisticsTaskID = context.statistics.start(EngineStatistics.GET_DOCUMENT);
    try {
        shuttle.sharedScope = context.getSharedScope();
        String domain = shuttle.getRequest(QueryPart.host) + shuttle.getRequest(QueryPart.port);
        Engine.logSiteClipper.trace("(SiteClipperConnector) Prepare the request for the domain " + domain);
        if (!shouldRewrite(domain)) {
            Engine.logSiteClipper.info("(SiteClipperConnector) The domain " + domain + " is not allowed with this connector");
            shuttle.response.sendError(HttpServletResponse.SC_FORBIDDEN, "The domain " + domain + " is not allowed with this connector");
            return;
        }
        String uri = shuttle.getRequest(QueryPart.uri);
        Engine.logSiteClipper.info("Preparing " + shuttle.request.getMethod() + " " + shuttle.getRequestUrl());
        HttpMethod httpMethod = null;
        XulRecorder xulRecorder = context.getXulRecorder();
        if (xulRecorder != null) {
            httpMethod = shuttle.httpMethod = xulRecorder.getRecord(shuttle.getRequestUrlAndQuery());
        }
        if (httpMethod == null) {
            try {
                switch(shuttle.getRequestHttpMethodType()) {
                    case GET:
                        httpMethod = new GetMethod(uri);
                        break;
                    case POST:
                        httpMethod = new PostMethod(uri);
                        ((PostMethod) httpMethod).setRequestEntity(new InputStreamRequestEntity(shuttle.request.getInputStream()));
                        break;
                    case PUT:
                        httpMethod = new PutMethod(uri);
                        ((PutMethod) httpMethod).setRequestEntity(new InputStreamRequestEntity(shuttle.request.getInputStream()));
                        break;
                    case DELETE:
                        httpMethod = new DeleteMethod(uri);
                        break;
                    case HEAD:
                        httpMethod = new HeadMethod(uri);
                        break;
                    case OPTIONS:
                        httpMethod = new OptionsMethod(uri);
                        break;
                    case TRACE:
                        httpMethod = new TraceMethod(uri);
                        break;
                    default:
                        throw new ServletException("(SiteClipperConnector) unknown http method " + shuttle.request.getMethod());
                }
                httpMethod.setFollowRedirects(false);
            } catch (Exception e) {
                throw new ServletException("(SiteClipperConnector) unexpected exception will building the http method : " + e.getMessage());
            }
            shuttle.httpMethod = httpMethod;
            SiteClipperScreenClass screenClass = getCurrentScreenClass();
            Engine.logSiteClipper.info("Request screen class: " + screenClass.getName());
            for (String name : Collections.list(GenericUtils.<Enumeration<String>>cast(shuttle.request.getHeaderNames()))) {
                if (requestHeadersToIgnore.contains(HeaderName.parse(name))) {
                    Engine.logSiteClipper.trace("(SiteClipperConnector) Ignoring request header " + name);
                } else {
                    String value = shuttle.request.getHeader(name);
                    Engine.logSiteClipper.trace("(SiteClipperConnector) Copying request header " + name + "=" + value);
                    shuttle.setRequestCustomHeader(name, value);
                }
            }
            Engine.logSiteClipper.debug("(SiteClipperConnector) applying request rules for the screenclass " + screenClass.getName());
            for (IRequestRule rule : screenClass.getRequestRules()) {
                if (rule.isEnabled()) {
                    Engine.logSiteClipper.trace("(SiteClipperConnector) applying request rule " + rule.getName());
                    rule.fireEvents();
                    boolean done = rule.applyOnRequest(shuttle);
                    Engine.logSiteClipper.debug("(SiteClipperConnector) the request rule " + rule.getName() + " is " + (done ? "well" : "not") + " applied");
                } else {
                    Engine.logSiteClipper.trace("(SiteClipperConnector) skip the disabled request rule " + rule.getName());
                }
            }
            for (Entry<String, String> header : shuttle.requestCustomHeaders.entrySet()) {
                Engine.logSiteClipper.trace("(SiteClipperConnector) Push request header " + header.getKey() + "=" + header.getValue());
                httpMethod.addRequestHeader(header.getKey(), header.getValue());
            }
            String queryString = shuttle.request.getQueryString();
            if (queryString != null) {
                try {
                    // Fake test in order to check query string validity
                    new URI("http://localhost/index?" + queryString, true, httpMethod.getParams().getUriCharset());
                } catch (URIException e) {
                    // Bugfix #2103
                    StringBuffer newQuery = new StringBuffer();
                    for (String part : RegexpUtils.pattern_and.split(queryString)) {
                        String[] pair = RegexpUtils.pattern_equals.split(part, 2);
                        try {
                            newQuery.append('&').append(URLEncoder.encode(URLDecoder.decode(pair[0], "UTF-8"), "UTF-8"));
                            if (pair.length > 1) {
                                newQuery.append('=').append(URLEncoder.encode(URLDecoder.decode(pair[1], "UTF-8"), "UTF-8"));
                            }
                        } catch (UnsupportedEncodingException ee) {
                            Engine.logSiteClipper.trace("(SiteClipperConnector) failed to encode query part : " + part);
                        }
                    }
                    queryString = newQuery.length() > 0 ? newQuery.substring(1) : newQuery.toString();
                    Engine.logSiteClipper.trace("(SiteClipperConnector) re-encode query : " + queryString);
                }
            }
            Engine.logSiteClipper.debug("(SiteClipperConnector) Copying the query string : " + queryString);
            httpMethod.setQueryString(queryString);
            // if (context.httpState == null) {
            // Engine.logSiteClipper.debug("(SiteClipperConnector) Creating new HttpState for context id " + context.contextID);
            // context.httpState = new HttpState();
            // } else {
            // Engine.logSiteClipper.debug("(SiteClipperConnector) Using HttpState of context id " + context.contextID);
            // }
            getHttpState(shuttle);
            HostConfiguration hostConfiguration = getHostConfiguration(shuttle);
            HttpMethodParams httpMethodParams = httpMethod.getParams();
            httpMethodParams.setBooleanParameter("http.connection.stalecheck", true);
            httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
            Engine.logSiteClipper.info("Requesting " + httpMethod.getName() + " " + hostConfiguration.getHostURL() + httpMethod.getURI().toString());
            HttpClient httpClient = context.getHttpClient3(shuttle.getHttpPool());
            HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, shuttle.getHttpPool());
            httpClient.executeMethod(hostConfiguration, httpMethod, context.httpState);
        } else {
            Engine.logSiteClipper.info("Retrieve recorded response from Context");
        }
        int status = httpMethod.getStatusCode();
        shuttle.processState = ProcessState.response;
        Engine.logSiteClipper.info("Request terminated with status " + status);
        shuttle.response.setStatus(status);
        if (Engine.isStudioMode() && status == HttpServletResponse.SC_OK && shuttle.getResponseMimeType().startsWith("text/")) {
            fireDataChanged(new ConnectorEvent(this, shuttle.getResponseAsString()));
        }
        SiteClipperScreenClass screenClass = getCurrentScreenClass();
        Engine.logSiteClipper.info("Response screen class: " + screenClass.getName());
        if (Engine.isStudioMode()) {
            Engine.theApp.fireObjectDetected(new EngineEvent(screenClass));
        }
        for (Header header : httpMethod.getResponseHeaders()) {
            String name = header.getName();
            if (responseHeadersToIgnore.contains(HeaderName.parse(name))) {
                Engine.logSiteClipper.trace("(SiteClipperConnector) Ignoring response header " + name);
            } else {
                String value = header.getValue();
                Engine.logSiteClipper.trace("(SiteClipperConnector) Copying response header " + name + "=" + value);
                shuttle.responseCustomHeaders.put(name, value);
            }
        }
        String contentLength = HeaderName.ContentLength.getResponseHeader(httpMethod);
        Engine.logSiteClipper.debug("(SiteClipperConnector) applying response rules for the screenclass " + screenClass.getName());
        for (IResponseRule rule : screenClass.getResponseRules()) {
            if (rule.isEnabled()) {
                Engine.logSiteClipper.trace("(SiteClipperConnector) applying response rule " + rule.getName());
                rule.fireEvents();
                boolean done = rule.applyOnResponse(shuttle);
                Engine.logSiteClipper.debug("(SiteClipperConnector) the response rule " + rule.getName() + " is " + (done ? "well" : "not") + " applied");
            } else {
                Engine.logSiteClipper.trace("(SiteClipperConnector) skip the disabled response rule " + rule.getName());
            }
        }
        for (Entry<String, String> header : shuttle.responseCustomHeaders.entrySet()) {
            Engine.logSiteClipper.trace("(SiteClipperConnector) Push request header " + header.getKey() + "=" + header.getValue());
            shuttle.response.addHeader(header.getKey(), header.getValue());
        }
        if (shuttle.postInstructions != null) {
            JSONArray instructions = new JSONArray();
            for (IClientInstruction instruction : shuttle.postInstructions) {
                try {
                    instructions.put(instruction.getInstruction());
                } catch (JSONException e) {
                    Engine.logSiteClipper.error("(SiteClipperConnector) Failed to add a post instruction due to a JSONException", e);
                }
            }
            String codeToInject = "<script>C8O_postInstructions = " + instructions.toString() + "</script>\n" + "<script src=\"" + shuttle.getRequest(QueryPart.full_convertigo_path) + "/scripts/jquery.min.js\"></script>\n" + "<script src=\"" + shuttle.getRequest(QueryPart.full_convertigo_path) + "/scripts/siteclipper.js\"></script>\n";
            String content = shuttle.getResponseAsString();
            Matcher matcher = HtmlLocation.head_top.matcher(content);
            String newContent = RegexpUtils.inject(matcher, codeToInject);
            if (newContent == null) {
                matcher = HtmlLocation.body_top.matcher(content);
                newContent = RegexpUtils.inject(matcher, codeToInject);
            }
            if (newContent != null) {
                shuttle.setResponseAsString(newContent);
            } else {
                Engine.logSiteClipper.info("(SiteClipperConnector) Failed to find a head or body tag in the response content");
                Engine.logSiteClipper.trace("(SiteClipperConnector) Response content : \"" + content + "\"");
            }
        }
        long nbBytes = 0L;
        String responseContentLength = HeaderName.ContentLength.getHeader(shuttle.response);
        if (shuttle.responseAsString != null && shuttle.responseAsString.hashCode() != shuttle.responseAsStringOriginal.hashCode()) {
            OutputStream os = shuttle.response.getOutputStream();
            shuttle.responseAsByte = shuttle.responseAsString.getBytes(shuttle.getResponseCharset());
            nbBytes = shuttle.responseAsByte.length;
            switch(shuttle.getResponseContentEncoding()) {
                case gzip:
                    os = new GZIPOutputStream(os);
                    break;
                case deflate:
                    os = new DeflaterOutputStream(os, new Deflater(Deflater.DEFAULT_COMPRESSION | Deflater.DEFAULT_STRATEGY, true));
                    break;
                default:
                    if (responseContentLength == null) {
                        HeaderName.ContentLength.setHeader(shuttle.response, "" + nbBytes);
                    }
                    break;
            }
            IOUtils.write(shuttle.responseAsByte, os);
            os.close();
        } else {
            InputStream is;
            if (shuttle.responseAsByte == null) {
                if (responseContentLength == null && contentLength != null) {
                    HeaderName.ContentLength.setHeader(shuttle.response, contentLength);
                }
                is = httpMethod.getResponseBodyAsStream();
            } else {
                if (responseContentLength == null) {
                    HeaderName.ContentLength.setHeader(shuttle.response, "" + shuttle.responseAsByte.length);
                }
                is = new ByteArrayInputStream(shuttle.responseAsByte);
            }
            if (is != null) {
                nbBytes = StreamUtils.copyAutoFlush(is, shuttle.response.getOutputStream());
                Engine.logSiteClipper.trace("(SiteClipperConnector) Response body copyied (" + nbBytes + " bytes)");
            }
        }
        shuttle.response.getOutputStream().close();
        shuttle.score = getScore(nbBytes);
        Engine.logSiteClipper.debug("(SiteClipperConnector) Request terminated with a score of " + shuttle.score);
    } finally {
        long duration = context.statistics.stop(shuttle.statisticsTaskID);
        if (context.requestedObject != null) {
            try {
                Engine.theApp.billingManager.insertBilling(context, Long.valueOf(duration), Long.valueOf(shuttle.score));
            } catch (Exception e) {
                Engine.logContext.warn("Unable to insert billing ticket (the billing is thus ignored): [" + e.getClass().getName() + "] " + e.getMessage());
            }
        }
    }
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) Matcher(java.util.regex.Matcher) GZIPOutputStream(java.util.zip.GZIPOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) OutputStream(java.io.OutputStream) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) XulRecorder(com.twinsoft.convertigo.engine.parsers.XulRecorder) SiteClipperScreenClass(com.twinsoft.convertigo.beans.screenclasses.SiteClipperScreenClass) URI(org.apache.commons.httpclient.URI) ServletException(javax.servlet.ServletException) HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) OptionsMethod(org.apache.commons.httpclient.methods.OptionsMethod) URIException(org.apache.commons.httpclient.URIException) Deflater(java.util.zip.Deflater) GZIPOutputStream(java.util.zip.GZIPOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) ConnectorEvent(com.twinsoft.convertigo.beans.core.ConnectorEvent) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) TraceMethod(org.apache.commons.httpclient.methods.TraceMethod) JSONArray(org.codehaus.jettison.json.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.codehaus.jettison.json.JSONException) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) ServletException(javax.servlet.ServletException) URIException(org.apache.commons.httpclient.URIException) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) Header(org.apache.commons.httpclient.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) IResponseRule(com.twinsoft.convertigo.beans.extractionrules.siteclipper.IResponseRule) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) EngineEvent(com.twinsoft.convertigo.engine.EngineEvent) HttpMethod(org.apache.commons.httpclient.HttpMethod) IRequestRule(com.twinsoft.convertigo.beans.extractionrules.siteclipper.IRequestRule) IClientInstruction(com.twinsoft.convertigo.engine.siteclipper.clientinstruction.IClientInstruction)

Aggregations

EngineEvent (com.twinsoft.convertigo.engine.EngineEvent)19 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)5 EngineException (com.twinsoft.convertigo.engine.EngineException)5 EventObject (java.util.EventObject)5 RequestableEngineEvent (com.twinsoft.convertigo.engine.RequestableEngineEvent)4 JavelinConnector (com.twinsoft.convertigo.beans.connectors.JavelinConnector)3 Step (com.twinsoft.convertigo.beans.core.Step)3 ConnectorEditor (com.twinsoft.convertigo.eclipse.editors.connector.ConnectorEditor)3 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)3 Javelin (com.twinsoft.twinj.Javelin)3 IOException (java.io.IOException)3 JSONException (org.codehaus.jettison.json.JSONException)3 Cursor (org.eclipse.swt.graphics.Cursor)3 Display (org.eclipse.swt.widgets.Display)3 Shell (org.eclipse.swt.widgets.Shell)3 PanelBlockFactory (com.twinsoft.convertigo.beans.common.PanelBlockFactory)2 XMLRectangle (com.twinsoft.convertigo.beans.common.XMLRectangle)2 HtmlConnector (com.twinsoft.convertigo.beans.connectors.HtmlConnector)2 Connector (com.twinsoft.convertigo.beans.core.Connector)2 ExtractionRule (com.twinsoft.convertigo.beans.core.ExtractionRule)2