Search in sources :

Example 1 with IndyGroovyException

use of org.commonjava.indy.subsys.template.IndyGroovyException in project indy by Commonjava.

the class ContentController method renderListing.

public String renderListing(final String acceptHeader, final StoreKey key, final String requestPath, final String serviceUrl, final UriFormatter uriFormatter) throws IndyWorkflowException {
    String path = requestPath;
    if (path.endsWith(LISTING_HTML_FILE)) {
        path = normalize(parentPath(path));
    }
    final List<StoreResource> listed = getListing(key, path);
    if (ApplicationContent.application_json.equals(acceptHeader)) {
        final DirectoryListingDTO dto = new DirectoryListingDTO(StoreResource.convertToEntries(listed));
        try {
            return mapper.writeValueAsString(dto);
        } catch (final JsonProcessingException e) {
            throw new IndyWorkflowException("Failed to render listing to JSON: %s. Reason: %s", e, dto, e.getMessage());
        }
    }
    final Map<String, Set<String>> listingUrls = new TreeMap<>();
    final String storeUrl = uriFormatter.formatAbsolutePathTo(serviceUrl, key.getType().singularEndpointName(), key.getName());
    if (listed != null) {
        // second pass, process the remainder.
        for (int pass = 0; pass < 2; pass++) {
            for (final ConcreteResource res : listed) {
                String p = res.getPath();
                if (pass == 0 && !p.endsWith("/")) {
                    continue;
                } else if (pass == 1) {
                    if (!p.endsWith("/")) {
                        final String dirpath = p + "/";
                        if (listingUrls.containsKey(normalize(storeUrl, dirpath))) {
                            p = dirpath;
                        }
                    } else {
                        continue;
                    }
                }
                final String localUrl = normalize(storeUrl, p);
                Set<String> sources = listingUrls.get(localUrl);
                if (sources == null) {
                    sources = new HashSet<>();
                    listingUrls.put(localUrl, sources);
                }
                sources.add(normalize(res.getLocationUri(), res.getPath()));
            }
        }
    }
    final List<String> sources = new ArrayList<>();
    if (listed != null) {
        for (final ConcreteResource res : listed) {
            // KeyedLocation is all we use in Indy.
            logger.debug("Formatting sources URL for: {}", res);
            final KeyedLocation kl = (KeyedLocation) res.getLocation();
            final String uri = uriFormatter.formatAbsolutePathTo(serviceUrl, kl.getKey().getType().singularEndpointName(), kl.getKey().getName());
            if (!sources.contains(uri)) {
                logger.debug("adding source URI: '{}'", uri);
                sources.add(uri);
            }
        }
    }
    Collections.sort(sources);
    String parentPath = normalize(parentPath(path));
    if (!parentPath.endsWith("/")) {
        parentPath += "/";
    }
    final String parentUrl;
    if (parentPath.equals(path)) {
        parentPath = null;
        parentUrl = null;
    } else {
        parentUrl = uriFormatter.formatAbsolutePathTo(serviceUrl, key.getType().singularEndpointName(), key.getName(), parentPath);
    }
    final Map<String, Object> params = new HashMap<>();
    params.put("items", listingUrls);
    params.put("parentUrl", parentUrl);
    params.put("parentPath", parentPath);
    params.put("path", path);
    params.put("storeKey", key);
    params.put("storeUrl", storeUrl);
    params.put("baseUrl", serviceUrl);
    params.put("sources", sources);
    // render...
    try {
        return templates.render(acceptHeader, "directory-listing", params);
    } catch (final IndyGroovyException e) {
        throw new IndyWorkflowException(e.getMessage(), e);
    }
}
Also used : DirectoryListingDTO(org.commonjava.indy.model.core.dto.DirectoryListingDTO) HashSet(java.util.HashSet) Set(java.util.Set) KeyedLocation(org.commonjava.indy.model.galley.KeyedLocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException) StoreResource(org.commonjava.indy.content.StoreResource) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with IndyGroovyException

use of org.commonjava.indy.subsys.template.IndyGroovyException in project indy by Commonjava.

the class SetBackSettingsManager method updateSettings.

private DataFile updateSettings(final ArtifactStore store, final List<ArtifactStore> allStores, final List<RemoteRepository> remotes) throws SetBackDataException {
    if (!config.isEnabled()) {
        throw new SetBackDataException("SetBack is disabled!");
    }
    final StoreKey key = store.getKey();
    final DataFile settingsXml = getSettingsXml(key);
    final Map<String, Object> params = new HashMap<String, Object>();
    params.put("key", key);
    params.put("store", store);
    params.put("remotes", remotes);
    params.put("allStores", allStores);
    String rendered;
    try {
        rendered = templates.render(ApplicationContent.application_xml, TEMPLATE, params);
    } catch (final IndyGroovyException e) {
        throw new SetBackDataException("Failed to render template: %s for store: %s. Reason: %s", e, TEMPLATE, key, e.getMessage());
    }
    try {
        settingsXml.getParent().mkdirs();
        settingsXml.writeString(rendered, "UTF-8", new ChangeSummary(ChangeSummary.SYSTEM_USER, "SETBACK: Updating generated SetBack settings.xml for: " + key));
    } catch (final IOException e) {
        throw new SetBackDataException("Failed to write SetBack settings.xml for: %s\n  to: %s\n  Reason: %s", e, key, settingsXml, e.getMessage());
    }
    return settingsXml;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) HashMap(java.util.HashMap) IOException(java.io.IOException) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) StoreKey(org.commonjava.indy.model.core.StoreKey) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException)

Example 3 with IndyGroovyException

use of org.commonjava.indy.subsys.template.IndyGroovyException in project indy by Commonjava.

the class SettingsTemplate method formatSettings.

private void formatSettings() throws WebdavException {
    if (content != null) {
        return;
    }
    final String name = key.getName();
    final StoreType type = key.getType();
    final StringBuilder url = new StringBuilder();
    url.append(requestInfo.getBaseUrl());
    logger.debug("settings base-url is: '{}'", url);
    if (url.charAt(url.length() - 1) != '/') {
        url.append('/');
    }
    url.append("api/");
    url.append(type.singularEndpointName()).append('/').append(name);
    try {
        final Map<String, Object> params = new HashMap<String, Object>();
        params.put(TYPE_KEY, type.singularEndpointName());
        params.put(NAME_KEY, name);
        params.put(URL_KEY, url);
        params.put(RELEASES_KEY, advice.isReleasesAllowed());
        params.put(SNAPSHOTS_KEY, advice.isSnapshotsAllowed());
        params.put(DEPLOY_ENABLED_KEY, advice.isDeployable());
        final String rendered = templateEngine.render(TEMPLATE, params);
        content = rendered.getBytes("UTF-8");
    } catch (final UnsupportedEncodingException e) {
        throw new IllegalStateException("Cannot find encoding for UTF-8!", e);
    } catch (final IndyGroovyException e) {
        throw new WebdavException(String.format("Failed to render settings.xml template for: '%s'. Reason: %s", key, e.getMessage()), e);
    }
}
Also used : StoreType(org.commonjava.indy.model.core.StoreType) HashMap(java.util.HashMap) WebdavException(net.sf.webdav.exceptions.WebdavException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException)

Example 4 with IndyGroovyException

use of org.commonjava.indy.subsys.template.IndyGroovyException in project indy by Commonjava.

the class ValidationRuleParser method parseRule.

public ValidationRuleMapping parseRule(final String spec, final String scriptName) throws PromotionValidationException {
    if (spec == null) {
        return null;
    }
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Parsing rule from: {} with content:\n{}\n", scriptName, spec);
    ValidationRule rule = null;
    try {
        rule = scriptEngine.parseScriptInstance(spec, ValidationRule.class);
        logger.debug("Parsed: {}", rule.getClass().getName());
    } catch (final IndyGroovyException e) {
        throw new PromotionValidationException("[PROMOTE] Cannot load validation rule from: {} as an instance of: {}. Reason: {}", e, scriptName, ValidationRule.class.getSimpleName(), e.getMessage());
    }
    if (rule != null) {
        return new ValidationRuleMapping(scriptName, spec, rule);
    }
    return null;
}
Also used : ValidationRuleMapping(org.commonjava.indy.promote.validate.model.ValidationRuleMapping) Logger(org.slf4j.Logger) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException) ValidationRule(org.commonjava.indy.promote.validate.model.ValidationRule)

Example 5 with IndyGroovyException

use of org.commonjava.indy.subsys.template.IndyGroovyException in project indy by Commonjava.

the class StatsController method getActiveAddOnsJavascript.

public String getActiveAddOnsJavascript() throws IndyWorkflowException {
    try {
        final String json = serializer.writeValueAsString(getActiveAddOns());
        final Map<String, Object> params = new HashMap<>();
        final Map<String, String> jsMap = new HashMap<>();
        if (addons != null) {
            final ClassLoader cl = Thread.currentThread().getContextClassLoader();
            for (final IndyAddOn addon : addons) {
                final String jsRef = addon.getId().getInitJavascriptHref();
                if (jsRef == null) {
                    logger.debug("Add-On has no init javascript: {}", addon);
                    continue;
                }
                try (InputStream in = cl.getResourceAsStream(jsRef)) {
                    if (in == null) {
                        logger.error("Add-On failed to load: {}. Initialization javascript NOT FOUND in classpath: {}", addon, jsRef);
                        continue;
                    }
                    jsMap.put(jsRef, IOUtils.toString(in));
                } catch (final IOException e) {
                    logger.error("Add-On failed to load: {}. Cannot load initialization javascript from classpath: {}", addon, jsRef);
                }
            }
        }
        params.put(ADDONS_KEY, json);
        params.put(ADDONS_LOGIC, jsMap);
        return templates.render(ACTIVE_ADDONS_JS, params);
    } catch (final IndyGroovyException e) {
        throw new IndyWorkflowException("Failed to render javascript wrapper for active addons. Reason: %s", e, e.getMessage());
    } catch (final JsonProcessingException e) {
        throw new IndyWorkflowException("Failed to render javascript wrapper for active addons. Reason: %s", e, e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) IndyAddOn(org.commonjava.indy.spi.IndyAddOn) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException)

Aggregations

IndyGroovyException (org.commonjava.indy.subsys.template.IndyGroovyException)6 HashMap (java.util.HashMap)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 IOException (java.io.IOException)2 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)2 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1 WebdavException (net.sf.webdav.exceptions.WebdavException)1 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)1 AutoProxRule (org.commonjava.indy.autoprox.data.AutoProxRule)1 AutoProxRuleException (org.commonjava.indy.autoprox.data.AutoProxRuleException)1 RuleMapping (org.commonjava.indy.autoprox.data.RuleMapping)1 StoreResource (org.commonjava.indy.content.StoreResource)1 StoreKey (org.commonjava.indy.model.core.StoreKey)1 StoreType (org.commonjava.indy.model.core.StoreType)1 DirectoryListingDTO (org.commonjava.indy.model.core.dto.DirectoryListingDTO)1