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);
}
}
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;
}
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);
}
}
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;
}
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());
}
}
Aggregations