Search in sources :

Example 21 with RequestableHttpVariable

use of com.twinsoft.convertigo.beans.variables.RequestableHttpVariable in project convertigo by convertigo.

the class HttpConnector method getData.

public byte[] getData(Context context, String sUrl) throws IOException, EngineException {
    HttpMethod method = null;
    try {
        // Fire event for plugins
        long t0 = System.currentTimeMillis();
        Engine.theApp.pluginsManager.fireHttpConnectorGetDataStart(context);
        Engine.logBeans.trace("(HttpConnector) Retrieving data as a bytes array...");
        Engine.logBeans.debug("(HttpConnector) Connecting to: " + sUrl);
        // Setting the referer
        referer = sUrl;
        Engine.logBeans.debug("(HttpConnector) Https: " + https);
        URL url = new URL(sUrl);
        String host = url.getHost();
        int port = url.getPort();
        if (sUrl.toLowerCase().startsWith("https:")) {
            if (!https) {
                Engine.logBeans.debug("(HttpConnector) Setting up SSL properties");
                certificateManager.collectStoreInformation(context);
            }
            if (port == -1)
                port = 443;
            Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port);
            Engine.logBeans.debug("(HttpConnector) CertificateManager has changed: " + certificateManager.hasChanged);
            if (certificateManager.hasChanged || (!host.equalsIgnoreCase(hostConfiguration.getHost())) || (hostConfiguration.getPort() != port)) {
                Engine.logBeans.debug("(HttpConnector) Using MySSLSocketFactory for creating the SSL socket");
                Protocol myhttps = new Protocol("https", MySSLSocketFactory.getSSLSocketFactory(certificateManager.keyStore, certificateManager.keyStorePassword, certificateManager.trustStore, certificateManager.trustStorePassword, this.trustAllServerCertificates), port);
                hostConfiguration.setHost(host, port, myhttps);
            }
            sUrl = url.getFile();
            Engine.logBeans.debug("(HttpConnector) Updated URL for SSL purposes: " + sUrl);
        } else {
            Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port);
            hostConfiguration.setHost(host, port);
        }
        // Retrieving httpState
        getHttpState(context);
        // Proxy configuration
        Engine.theApp.proxyManager.setProxy(hostConfiguration, httpState, url);
        AbstractHttpTransaction httpTransaction = (AbstractHttpTransaction) context.transaction;
        // Retrieve HTTP method
        HttpMethodType httpVerb = httpTransaction.getHttpVerb();
        String sHttpVerb = httpVerb.name();
        final String sCustomHttpVerb = httpTransaction.getCustomHttpVerb();
        if (sCustomHttpVerb.length() > 0) {
            Engine.logBeans.debug("(HttpConnector) HTTP verb: " + sHttpVerb + " overridden to '" + sCustomHttpVerb + "'");
            switch(httpVerb) {
                case GET:
                    method = new GetMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case POST:
                    method = new PostMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case PUT:
                    method = new PutMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case DELETE:
                    method = new DeleteMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case HEAD:
                    method = new HeadMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case OPTIONS:
                    method = new OptionsMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
                case TRACE:
                    method = new TraceMethod(sUrl) {

                        @Override
                        public String getName() {
                            return sCustomHttpVerb;
                        }
                    };
                    break;
            }
        } else {
            Engine.logBeans.debug("(HttpConnector) HTTP verb: " + sHttpVerb);
            switch(httpVerb) {
                case GET:
                    method = new GetMethod(sUrl);
                    break;
                case POST:
                    method = new PostMethod(sUrl);
                    break;
                case PUT:
                    method = new PutMethod(sUrl);
                    break;
                case DELETE:
                    method = new DeleteMethod(sUrl);
                    break;
                case HEAD:
                    method = new HeadMethod(sUrl);
                    break;
                case OPTIONS:
                    method = new OptionsMethod(sUrl);
                    break;
                case TRACE:
                    method = new TraceMethod(sUrl);
                    break;
            }
        }
        // Setting HTTP parameters
        boolean hasUserAgent = false;
        for (List<String> httpParameter : httpParameters) {
            String key = httpParameter.get(0);
            String value = httpParameter.get(1);
            if (key.equalsIgnoreCase("host") && !value.equals(host)) {
                value = host;
            }
            if (!key.startsWith(DYNAMIC_HEADER_PREFIX)) {
                method.setRequestHeader(key, value);
            }
            if (HeaderName.UserAgent.is(key)) {
                hasUserAgent = true;
            }
        }
        // set user-agent header if not found
        if (!hasUserAgent) {
            HeaderName.UserAgent.setRequestHeader(method, getUserAgent(context));
        }
        // Setting POST or PUT parameters if any
        Engine.logBeans.debug("(HttpConnector) Setting " + httpVerb + " data");
        if (method instanceof EntityEnclosingMethod) {
            EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) method;
            AbstractHttpTransaction transaction = (AbstractHttpTransaction) context.requestedObject;
            if (doMultipartFormData) {
                RequestableHttpVariable body = (RequestableHttpVariable) httpTransaction.getVariable(Parameter.HttpBody.getName());
                if (body != null && body.getDoFileUploadMode() == DoFileUploadMode.multipartFormData) {
                    String stringValue = httpTransaction.getParameterStringValue(Parameter.HttpBody.getName());
                    String filepath = Engine.theApp.filePropertyManager.getFilepathFromProperty(stringValue, getProject().getName());
                    File file = new File(filepath);
                    if (file.exists()) {
                        HeaderName.ContentType.setRequestHeader(method, contentType);
                        entityEnclosingMethod.setRequestEntity(new FileRequestEntity(file, contentType));
                    } else {
                        throw new FileNotFoundException(file.getAbsolutePath());
                    }
                } else {
                    List<Part> parts = new LinkedList<Part>();
                    for (RequestableVariable variable : transaction.getVariablesList()) {
                        if (variable instanceof RequestableHttpVariable) {
                            RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable;
                            if ("POST".equals(httpVariable.getHttpMethod())) {
                                Object httpObjectVariableValue = transaction.getVariableValue(httpVariable.getName());
                                if (httpVariable.isMultiValued()) {
                                    if (httpObjectVariableValue instanceof Collection<?>) {
                                        for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
                                            addFormDataPart(parts, httpVariable, httpVariableValue);
                                        }
                                    }
                                } else {
                                    addFormDataPart(parts, httpVariable, httpObjectVariableValue);
                                }
                            }
                        }
                    }
                    MultipartRequestEntity mre = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), entityEnclosingMethod.getParams());
                    HeaderName.ContentType.setRequestHeader(method, mre.getContentType());
                    entityEnclosingMethod.setRequestEntity(mre);
                }
            } else if (MimeType.TextXml.is(contentType)) {
                final MimeMultipart[] mp = { null };
                for (RequestableVariable variable : transaction.getVariablesList()) {
                    if (variable instanceof RequestableHttpVariable) {
                        RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable;
                        if (httpVariable.getDoFileUploadMode() == DoFileUploadMode.MTOM) {
                            Engine.logBeans.trace("(HttpConnector) Variable " + httpVariable.getName() + " detected as MTOM");
                            MimeMultipart mimeMultipart = mp[0];
                            try {
                                if (mimeMultipart == null) {
                                    Engine.logBeans.debug("(HttpConnector) Preparing the MTOM request");
                                    mimeMultipart = new MimeMultipart("related; type=\"application/xop+xml\"");
                                    MimeBodyPart bp = new MimeBodyPart();
                                    bp.setText(postQuery, "UTF-8");
                                    bp.setHeader(HeaderName.ContentType.value(), contentType);
                                    mimeMultipart.addBodyPart(bp);
                                }
                                Object httpObjectVariableValue = transaction.getVariableValue(httpVariable.getName());
                                if (httpVariable.isMultiValued()) {
                                    if (httpObjectVariableValue instanceof Collection<?>) {
                                        for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) {
                                            addMtomPart(mimeMultipart, httpVariable, httpVariableValue);
                                        }
                                    }
                                } else {
                                    addMtomPart(mimeMultipart, httpVariable, httpObjectVariableValue);
                                }
                                mp[0] = mimeMultipart;
                            } catch (Exception e) {
                                Engine.logBeans.warn("(HttpConnector) Failed to add MTOM part for " + httpVariable.getName(), e);
                            }
                        }
                    }
                }
                if (mp[0] == null) {
                    entityEnclosingMethod.setRequestEntity(new StringRequestEntity(postQuery, MimeType.TextXml.value(), "UTF-8"));
                } else {
                    Engine.logBeans.debug("(HttpConnector) Commit the MTOM request with the ContentType: " + mp[0].getContentType());
                    HeaderName.ContentType.setRequestHeader(method, mp[0].getContentType());
                    entityEnclosingMethod.setRequestEntity(new RequestEntity() {

                        @Override
                        public void writeRequest(OutputStream outputStream) throws IOException {
                            try {
                                mp[0].writeTo(outputStream);
                            } catch (MessagingException e) {
                                new IOException(e);
                            }
                        }

                        @Override
                        public boolean isRepeatable() {
                            return true;
                        }

                        @Override
                        public String getContentType() {
                            return mp[0].getContentType();
                        }

                        @Override
                        public long getContentLength() {
                            return -1;
                        }
                    });
                }
            } else {
                String charset = httpTransaction.getComputedUrlEncodingCharset();
                HeaderName.ContentType.setRequestHeader(method, contentType);
                entityEnclosingMethod.setRequestEntity(new StringRequestEntity(postQuery, contentType, charset));
            }
        }
        // Getting the result
        Engine.logBeans.debug("(HttpConnector) HttpClient: getting response body");
        byte[] result = executeMethod(method, context);
        long length = result != null ? result.length : 0;
        if (context.transaction instanceof DownloadHttpTransaction) {
            try {
                length = (long) context.get("__downloadedFileLength");
            } catch (Exception e) {
            }
        }
        Engine.logBeans.debug("(HttpConnector) Total read bytes: " + length);
        // Fire event for plugins
        long t1 = System.currentTimeMillis();
        Engine.theApp.pluginsManager.fireHttpConnectorGetDataEnd(context, t0, t1);
        StringBuilder sb = new StringBuilder();
        sb.append("HTTP result {ContentType: " + context.contentType + ", Length: " + length + "}\n\n");
        if (result != null && context.contentType != null && (context.contentType.startsWith("text/") || context.contentType.startsWith("application/xml") || context.contentType.startsWith("application/json"))) {
            sb.append(new String(result, "UTF-8"));
        }
        fireDataChanged(new ConnectorEvent(this, sb.toString()));
        return result;
    } finally {
        if (method != null)
            method.releaseConnection();
    }
}
Also used : RequestableHttpVariable(com.twinsoft.convertigo.beans.variables.RequestableHttpVariable) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) HttpMethodType(com.twinsoft.convertigo.engine.enums.HttpMethodType) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) AbstractHttpTransaction(com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction) HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) OptionsMethod(org.apache.commons.httpclient.methods.OptionsMethod) MimeMultipart(javax.mail.internet.MimeMultipart) BigMimeMultipart(com.twinsoft.convertigo.engine.util.BigMimeMultipart) Protocol(org.apache.commons.httpclient.protocol.Protocol) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) ConnectorEvent(com.twinsoft.convertigo.beans.core.ConnectorEvent) MessagingException(javax.mail.MessagingException) TraceMethod(org.apache.commons.httpclient.methods.TraceMethod) EntityEnclosingMethod(org.apache.commons.httpclient.methods.EntityEnclosingMethod) RequestableVariable(com.twinsoft.convertigo.beans.variables.RequestableVariable) IOException(java.io.IOException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) LinkedList(java.util.LinkedList) URIException(org.apache.commons.httpclient.URIException) SocketTimeoutException(java.net.SocketTimeoutException) OAuthException(oauth.signpost.exception.OAuthException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) MessagingException(javax.mail.MessagingException) FileNotFoundException(java.io.FileNotFoundException) EngineException(com.twinsoft.convertigo.engine.EngineException) MalformedURLException(java.net.MalformedURLException) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) DownloadHttpTransaction(com.twinsoft.convertigo.beans.transactions.DownloadHttpTransaction) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) MimePart(javax.mail.internet.MimePart) Part(org.apache.commons.httpclient.methods.multipart.Part) MimeBodyPart(javax.mail.internet.MimeBodyPart) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Collection(java.util.Collection) MimeBodyPart(javax.mail.internet.MimeBodyPart) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) File(java.io.File) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Aggregations

RequestableHttpVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpVariable)21 RequestableHttpMultiValuedVariable (com.twinsoft.convertigo.beans.variables.RequestableHttpMultiValuedVariable)9 RequestableVariable (com.twinsoft.convertigo.beans.variables.RequestableVariable)9 EngineException (com.twinsoft.convertigo.engine.EngineException)9 AbstractHttpTransaction (com.twinsoft.convertigo.beans.transactions.AbstractHttpTransaction)7 XMLVector (com.twinsoft.convertigo.beans.common.XMLVector)6 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)6 IOException (java.io.IOException)6 Element (org.w3c.dom.Element)5 HTTPStatement (com.twinsoft.convertigo.beans.statements.HTTPStatement)4 XmlHttpTransaction (com.twinsoft.convertigo.beans.transactions.XmlHttpTransaction)4 HttpStatementVariable (com.twinsoft.convertigo.beans.variables.HttpStatementVariable)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 XmlQName (com.twinsoft.convertigo.beans.common.XmlQName)3 HttpConnector (com.twinsoft.convertigo.beans.connectors.HttpConnector)3 TestCase (com.twinsoft.convertigo.beans.core.TestCase)3 Variable (com.twinsoft.convertigo.beans.core.Variable)3 HttpStatementMultiValuedVariable (com.twinsoft.convertigo.beans.variables.HttpStatementMultiValuedVariable)3 RequestableMultiValuedVariable (com.twinsoft.convertigo.beans.variables.RequestableMultiValuedVariable)3 HttpMethodType (com.twinsoft.convertigo.engine.enums.HttpMethodType)3