Search in sources :

Example 6 with Header

use of lucee.commons.net.http.Header 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 7 with Header

use of lucee.commons.net.http.Header in project Lucee by lucee.

the class HTTPResource method lastModified.

@Override
public long lastModified() {
    int last = 0;
    HTTPResponse rsp = null;
    try {
        Header cl = (rsp = getHTTPResponse(false)).getLastHeaderIgnoreCase("last-modified");
        if (cl != null && exists())
            last = Caster.toIntValue(cl.getValue(), 0);
    } catch (IOException e) {
    } finally {
        HTTPEngine.closeEL(rsp);
    }
    return last;
}
Also used : Header(lucee.commons.net.http.Header) HTTPResponse(lucee.commons.net.http.HTTPResponse) IOException(java.io.IOException)

Aggregations

Header (lucee.commons.net.http.Header)7 IOException (java.io.IOException)6 URL (java.net.URL)5 HTTPResponse (lucee.commons.net.http.HTTPResponse)5 Resource (lucee.commons.io.res.Resource)4 InputStream (java.io.InputStream)3 MalformedURLException (java.net.MalformedURLException)2 Charset (java.nio.charset.Charset)2 HeaderImpl (lucee.commons.net.http.httpclient.HeaderImpl)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 PageException (lucee.runtime.exp.PageException)2 RHExtensionProvider (lucee.runtime.extension.RHExtensionProvider)2 ProxyData (lucee.runtime.net.proxy.ProxyData)2 Struct (lucee.runtime.type.Struct)2 URLResolver (lucee.runtime.util.URLResolver)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1