Search in sources :

Example 1 with HeaderImpl

use of lucee.commons.net.http.httpclient.HeaderImpl in project Lucee by lucee.

the class DeployHandler method downloadExtension.

public static Resource downloadExtension(Config config, ExtensionDefintion ed, Log log) {
    String apiKey = config.getIdentification().getApiKey();
    URL url;
    RHExtensionProvider[] providers = ((ConfigImpl) config).getRHExtensionProviders();
    for (int i = 0; i < providers.length; i++) {
        HTTPResponse rsp = null;
        try {
            url = providers[i].getURL();
            StringBuilder qs = new StringBuilder();
            addQueryParam(qs, "ioid", apiKey);
            addQueryParam(qs, "version", ed.getVersion());
            url = new URL(url, "/rest/extension/provider/full/" + ed.getId() + qs);
            rsp = HTTPEngine.get(url, null, null, -1, false, "UTF-8", "", null, new Header[] { new HeaderImpl("accept", "application/cfml") });
            if (rsp.getStatusCode() != 200)
                throw new IOException("failed to load extension: " + ed);
            // copy it locally
            Resource res = SystemUtil.getTempDirectory().getRealResource(ed.getId() + "-" + ed.getVersion() + ".lex");
            ResourceUtil.touch(res);
            IOUtil.copy(rsp.getContentAsStream(), res, true);
            return res;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            if (log != null)
                log.error("extension", t);
        } finally {
            HTTPEngine.closeEL(rsp);
        }
    }
    return null;
}
Also used : HeaderImpl(lucee.commons.net.http.httpclient.HeaderImpl) HTTPResponse(lucee.commons.net.http.HTTPResponse) Resource(lucee.commons.io.res.Resource) IOException(java.io.IOException) URL(java.net.URL) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider) Header(lucee.commons.net.http.Header)

Example 2 with HeaderImpl

use of lucee.commons.net.http.httpclient.HeaderImpl in project Lucee by lucee.

the class DeployHandler method deployExtension.

/**
 * install a extension based on the given id and version
 * @param config
 * @param id the id of the extension
 * @param version pass null if you don't need a specific version
 * @return
 * @throws IOException
 * @throws PageException
 */
public static boolean deployExtension(Config config, ExtensionDefintion ed, Log log, boolean reload) {
    ConfigImpl ci = (ConfigImpl) config;
    // is the extension already installed
    try {
        if (XMLConfigAdmin.hasRHExtensions(ci, ed) != null)
            return false;
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
    }
    // check if a local extension is matching our id
    Iterator<ExtensionDefintion> it = getLocalExtensions(config).iterator();
    ExtensionDefintion ext = null, tmp;
    log.info("extension", "installing the extension " + ed);
    while (it.hasNext()) {
        tmp = it.next();
        if (ed.equals(tmp)) {
            ext = tmp;
            break;
        }
    }
    // if we have one and also the defined version matches, there is no need to check online
    if (ext != null && ed.getVersion() != null) {
        try {
            log.info("extension", "installing the extension " + ed + " from local provider");
            Resource res = SystemUtil.getTempDirectory().getRealResource(ed.getId() + "-" + ed.getVersion() + ".lex");
            ResourceUtil.touch(res);
            IOUtil.copy(ext.getSource(), res);
            XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
            return true;
        } catch (Exception e) {
            ext = null;
            SystemOut.printDate(e);
        }
    }
    String apiKey = config.getIdentification().getApiKey();
    RHExtensionProvider[] providers = ci.getRHExtensionProviders();
    URL url;
    // if we have a local version, we look if there is a newer remote version
    if (ext != null) {
        String content;
        for (int i = 0; i < providers.length; i++) {
            HTTPResponse rsp = null;
            try {
                url = providers[i].getURL();
                StringBuilder qs = new StringBuilder();
                qs.append("?withLogo=false");
                if (ed.getVersion() != null)
                    qs.append("&version=").append(ed.getVersion());
                if (apiKey != null)
                    qs.append("&ioid=").append(apiKey);
                url = new URL(url, "/rest/extension/provider/info/" + ed.getId() + qs);
                rsp = HTTPEngine.get(url, null, null, -1, false, "UTF-8", "", null, new Header[] { new HeaderImpl("accept", "application/json") });
                if (rsp.getStatusCode() != 200)
                    continue;
                content = rsp.getContentAsString();
                Struct sct = Caster.toStruct(DeserializeJSON.call(null, content));
                String remoteVersion = Caster.toString(sct.get(KeyConstants._version));
                // the local version is as good as the remote
                if (remoteVersion != null && remoteVersion.compareTo(ext.getVersion()) <= 0) {
                    log.info("extension", "installing the extension " + ed + " from local provider");
                    // avoid that the exzension from provider get removed
                    Resource res = SystemUtil.getTempDirectory().getRealResource(ed.getId() + "-" + ed.getVersion() + ".lex");
                    ResourceUtil.touch(res);
                    IOUtil.copy(ext.getSource(), res);
                    XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
                    return true;
                }
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
            } finally {
                HTTPEngine.closeEL(rsp);
            }
        }
    }
    // if we have an ext at this stage this mean the remote providers was not acessible or have not this extension
    if (ext != null) {
        try {
            log.info("extension", "installing the extension " + ed + " from local provider");
            Resource res = SystemUtil.getTempDirectory().getRealResource(ext.getSource().getName());
            ResourceUtil.touch(res);
            IOUtil.copy(ext.getSource(), res);
            XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
            return true;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
        }
    }
    // if not we try to download it
    log.info("extension", "installing the extension " + ed + " from remote extension provider");
    Resource res = downloadExtension(ci, ed, log);
    if (res != null) {
        try {
            XMLConfigAdmin._updateRHExtension((ConfigImpl) config, res, reload);
            return true;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            log.error("extension", t);
        }
    }
    return false;
}
Also used : HeaderImpl(lucee.commons.net.http.httpclient.HeaderImpl) HTTPResponse(lucee.commons.net.http.HTTPResponse) Resource(lucee.commons.io.res.Resource) PageException(lucee.runtime.exp.PageException) IOException(java.io.IOException) URL(java.net.URL) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider) Struct(lucee.runtime.type.Struct) Header(lucee.commons.net.http.Header) ExtensionDefintion(lucee.runtime.extension.ExtensionDefintion)

Example 3 with HeaderImpl

use of lucee.commons.net.http.httpclient.HeaderImpl in project Lucee by lucee.

the class HTTPEngine method toHeaders.

private static Header[] toHeaders(Map<String, String> headers) {
    if (CollectionUtil.isEmpty(headers))
        return null;
    Header[] rtn = new Header[headers.size()];
    Iterator<Entry<String, String>> it = headers.entrySet().iterator();
    Entry<String, String> e;
    int index = 0;
    while (it.hasNext()) {
        e = it.next();
        rtn[index++] = new HeaderImpl(e.getKey(), e.getValue());
    }
    return rtn;
}
Also used : Entry(java.util.Map.Entry) HeaderImpl(lucee.commons.net.http.httpclient.HeaderImpl)

Aggregations

HeaderImpl (lucee.commons.net.http.httpclient.HeaderImpl)3 IOException (java.io.IOException)2 URL (java.net.URL)2 Resource (lucee.commons.io.res.Resource)2 HTTPResponse (lucee.commons.net.http.HTTPResponse)2 Header (lucee.commons.net.http.Header)2 RHExtensionProvider (lucee.runtime.extension.RHExtensionProvider)2 Entry (java.util.Map.Entry)1 PageException (lucee.runtime.exp.PageException)1 ExtensionDefintion (lucee.runtime.extension.ExtensionDefintion)1 Struct (lucee.runtime.type.Struct)1