Search in sources :

Example 1 with Status

use of com.twinsoft.convertigo.engine.AttachmentManager.Status in project convertigo by convertigo.

the class HtmlTransaction method applyUserRequest.

public void applyUserRequest(Object object) throws EngineException {
    final HtmlConnector connector = (HtmlConnector) parent;
    if (!runningThread.bContinue) {
        return;
    }
    Cookie[] cookies = new Cookie[] {};
    final byte[][] httpData = { null };
    try {
        Document dom = null;
        TriggerXMLizer triggerXML = null;
        String t = context.statistics.start(EngineStatistics.HTTP_CONNECT);
        try {
            // Retrieving Data
            if (object instanceof HtmlConnector) {
                Engine.logBeans.trace("(HtmlTransaction) Retrieving data from connector ...");
                httpData[0] = connector.getData(context);
                triggerXML = trigger;
            }
            if (object instanceof HTTPStatement) {
                Engine.logBeans.trace("(HtmlTransaction) Retrieving data from http statement ...");
                connector.prepareForHTTPStatement(context);
                httpData[0] = connector.getData(context);
                triggerXML = ((HTTPStatement) object).getTrigger();
            }
            if (!alreadyConnected)
                alreadyConnected = true;
            Engine.logBeans.trace("(HtmlTransaction) Data retrieved!");
        // Engine.logBeans.trace("(HtmlTransaction) Data html:\n"+ new String(httpData));
        } finally {
            context.statistics.stop(t);
        }
        // Retrieve cookies from HttpClient response
        if (connector.handleCookie) {
            cookies = connector.getCookies();
            if (Engine.logBeans.isTraceEnabled()) {
                Engine.logBeans.trace("(HtmlTransaction) cookies from HttpClient response :" + Arrays.asList(cookies).toString());
            }
        }
        if (!runningThread.bContinue) {
            return;
        }
        // Parse response : push data into HTML parser
        if (!isContentTypeHTML()) {
            final String[] filename = { connector.getReferer() };
            int id;
            if ((id = filename[0].indexOf('?')) != -1) {
                filename[0] = filename[0].substring(0, id);
            }
            if ((id = filename[0].lastIndexOf('/')) != -1) {
                filename[0] = filename[0].substring(id + 1);
            }
            connector.getHtmlParser().downloadRequest(context, new IDownloader() {

                public String getUri() {
                    return connector.getReferer();
                }

                public String getReferrer() {
                    return connector.getReferer();
                }

                public String getFilename() {
                    return filename[0];
                }

                public byte[] getData(long timeout, long threshold) {
                    return httpData[0];
                }

                public String getContentType() {
                    return HtmlTransaction.this.getContentType();
                }

                public void cancel() {
                }

                public Status getStatus() {
                    return Status.direct;
                }
            });
            return;
        }
        dom = connector.parseData(httpData[0], connector.getReferer(), connector.getCharset(), triggerXML.getTrigger());
        if (!runningThread.bContinue)
            return;
        setCurrentXmlDocument(dom);
        if (Engine.logBeans.isTraceEnabled())
            Engine.logBeans.trace("(HtmlTransaction) Parser result dom:\n" + XMLUtils.prettyPrintDOM(dom));
        if (!runningThread.bContinue)
            return;
        // Modify input document if needed
        modifyInputDocument();
    } catch (IOException e) {
        throw new EngineException("An IO exception occured while trying to connect to the URL.\nURL: " + connector.sUrl + "\nPost query: " + connector.postQuery, e);
    } catch (Exception e) {
        throw new EngineException("An unexpected exception occured while trying to get the document via HTTP.", e);
    }
    if (!runningThread.bContinue)
        return;
    // Applying handler
    executeHandler(EVENT_DATA_RETRIEVED, ((RequestableThread) Thread.currentThread()).javascriptContext);
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) Status(com.twinsoft.convertigo.engine.AttachmentManager.Status) TriggerXMLizer(com.twinsoft.convertigo.engine.parsers.triggers.TriggerXMLizer) HtmlConnector(com.twinsoft.convertigo.beans.connectors.HtmlConnector) EngineException(com.twinsoft.convertigo.engine.EngineException) IOException(java.io.IOException) Document(org.w3c.dom.Document) HTTPStatement(com.twinsoft.convertigo.beans.statements.HTTPStatement) IDownloader(com.twinsoft.convertigo.engine.parsers.IDownloader) EngineException(com.twinsoft.convertigo.engine.EngineException) EvaluatorException(org.mozilla.javascript.EvaluatorException) JavaScriptException(org.mozilla.javascript.JavaScriptException) IOException(java.io.IOException) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException)

Aggregations

HtmlConnector (com.twinsoft.convertigo.beans.connectors.HtmlConnector)1 HTTPStatement (com.twinsoft.convertigo.beans.statements.HTTPStatement)1 Status (com.twinsoft.convertigo.engine.AttachmentManager.Status)1 EngineException (com.twinsoft.convertigo.engine.EngineException)1 ObjectWithSameNameException (com.twinsoft.convertigo.engine.ObjectWithSameNameException)1 IDownloader (com.twinsoft.convertigo.engine.parsers.IDownloader)1 TriggerXMLizer (com.twinsoft.convertigo.engine.parsers.triggers.TriggerXMLizer)1 IOException (java.io.IOException)1 Cookie (org.apache.commons.httpclient.Cookie)1 EvaluatorException (org.mozilla.javascript.EvaluatorException)1 JavaScriptException (org.mozilla.javascript.JavaScriptException)1 Document (org.w3c.dom.Document)1