Search in sources :

Example 1 with RHExtensionProvider

use of lucee.runtime.extension.RHExtensionProvider in project Lucee by lucee.

the class Admin method doGetRHExtensionProviders.

private void doGetRHExtensionProviders() throws PageException {
    RHExtensionProvider[] providers = config.getRHExtensionProviders();
    lucee.runtime.type.Query qry = new QueryImpl(new Key[] { KeyConstants._url, KeyConstants._readonly }, providers.length, "query");
    RHExtensionProvider provider;
    for (int i = 0; i < providers.length; i++) {
        provider = providers[i];
        int row = i + 1;
        qry.setAt(KeyConstants._url, row, provider.getURL().toExternalForm());
        qry.setAt(KeyConstants._readonly, row, provider.isReadonly());
    }
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Query(lucee.runtime.type.Query) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider)

Example 2 with RHExtensionProvider

use of lucee.runtime.extension.RHExtensionProvider in project Lucee by lucee.

the class XMLConfigWebFactory method loadExtensions.

private static void loadExtensions(ConfigServerImpl configServer, ConfigImpl config, Document doc) {
    Element xmlExtParent = getChildByName(doc.getDocumentElement(), "extensions");
    String strEnabled = getAttr(xmlExtParent, "enabled");
    if (!StringUtil.isEmpty(strEnabled)) {
        config.setExtensionEnabled(Caster.toBooleanValue(strEnabled, false));
    }
    // RH Providers
    {
        // providers
        Element[] xmlProviders = getChildren(xmlExtParent, "rhprovider");
        String strProvider;
        Map<RHExtensionProvider, String> providers = new HashMap<RHExtensionProvider, String>();
        for (int i = 0; i < Constants.RH_EXTENSION_PROVIDERS.length; i++) {
            providers.put(Constants.RH_EXTENSION_PROVIDERS[i], "");
        }
        for (int i = 0; i < xmlProviders.length; i++) {
            strProvider = getAttr(xmlProviders[i], "url");
            if (!StringUtil.isEmpty(strProvider, true)) {
                try {
                    providers.put(new RHExtensionProvider(strProvider.trim(), false), "");
                } catch (MalformedURLException e) {
                    SystemOut.printDate(e);
                }
            }
        }
        config.setRHExtensionProviders(providers.keySet().toArray(new RHExtensionProvider[providers.size()]));
    }
    // classic providers
    {
        Element[] xmlProviders = getChildren(xmlExtParent, "provider");
        String provider;
        Map list = new HashMap();
        for (int i = 0; i < Constants.CLASSIC_EXTENSION_PROVIDERS.length; i++) {
            list.put(Constants.CLASSIC_EXTENSION_PROVIDERS[i], "");
        }
        for (int i = 0; i < xmlProviders.length; i++) {
            provider = getAttr(xmlProviders[i], "url");
            if (!StringUtil.isEmpty(provider, true)) {
                list.put(new ExtensionProviderImpl(provider.trim(), false), "");
            }
        }
        config.setExtensionProviders((ExtensionProvider[]) list.keySet().toArray(new ExtensionProvider[list.size()]));
    }
    // extensions
    Element[] xmlExtensions = getChildren(xmlExtParent, "extension");
    Extension[] extensions = new Extension[xmlExtensions.length];
    Element xmlExtension;
    for (int i = 0; i < xmlExtensions.length; i++) {
        xmlExtension = xmlExtensions[i];
        extensions[i] = new ExtensionImpl(getAttr(xmlExtension, "config"), getAttr(xmlExtension, "id"), getAttr(xmlExtension, "provider"), getAttr(xmlExtension, "version"), getAttr(xmlExtension, "name"), getAttr(xmlExtension, "label"), getAttr(xmlExtension, "description"), getAttr(xmlExtension, "category"), getAttr(xmlExtension, "image"), getAttr(xmlExtension, "author"), getAttr(xmlExtension, "codename"), getAttr(xmlExtension, "video"), getAttr(xmlExtension, "support"), getAttr(xmlExtension, "documentation"), getAttr(xmlExtension, "forum"), getAttr(xmlExtension, "mailinglist"), getAttr(xmlExtension, "network"), DateCaster.toDateAdvanced(getAttr(xmlExtension, "created"), null, null), getAttr(xmlExtension, "type"));
    }
    config.setExtensions(extensions);
}
Also used : MalformedURLException(java.net.MalformedURLException) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider) ExtensionProvider(lucee.runtime.extension.ExtensionProvider) ExtensionProviderImpl(lucee.runtime.extension.ExtensionProviderImpl) lucee.aprint(lucee.aprint) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider) Extension(lucee.runtime.extension.Extension) RHExtension(lucee.runtime.extension.RHExtension) ExtensionImpl(lucee.runtime.extension.ExtensionImpl) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 3 with RHExtensionProvider

use of lucee.runtime.extension.RHExtensionProvider 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 4 with RHExtensionProvider

use of lucee.runtime.extension.RHExtensionProvider 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)

Aggregations

RHExtensionProvider (lucee.runtime.extension.RHExtensionProvider)4 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 HeaderImpl (lucee.commons.net.http.httpclient.HeaderImpl)2 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 lucee.aprint (lucee.aprint)1 PageException (lucee.runtime.exp.PageException)1 Extension (lucee.runtime.extension.Extension)1 ExtensionDefintion (lucee.runtime.extension.ExtensionDefintion)1 ExtensionImpl (lucee.runtime.extension.ExtensionImpl)1 ExtensionProvider (lucee.runtime.extension.ExtensionProvider)1 ExtensionProviderImpl (lucee.runtime.extension.ExtensionProviderImpl)1 RHExtension (lucee.runtime.extension.RHExtension)1 Query (lucee.runtime.type.Query)1