use of eu.esdihumboldt.hale.common.core.io.ResourceAdvisor in project hale by halestudio.
the class AbstractAppSchemaConfigurator method updateTargetSchemaResources.
// TODO: code adapted from ArchiveProjectWriter: how to avoid duplication?
private Map<URI, String> updateTargetSchemaResources(File targetDirectory, ProgressIndicator progress, IOReporter reporter) throws IOException {
progress.begin("Copy resources", ProgressIndicator.UNKNOWN);
Project project = (Project) getProjectInfo();
// resource locations mapped to new resource path
Map<URI, String> handledResources = new HashMap<>();
try {
List<IOConfiguration> resources = project.getResources();
// every resource needs his own directory
int count = 0;
Iterator<IOConfiguration> iter = resources.iterator();
while (iter.hasNext()) {
IOConfiguration resource = iter.next();
if (resource.getActionId().equals(SchemaIO.ACTION_LOAD_TARGET_SCHEMA)) {
// get resource path
Map<String, Value> providerConfig = resource.getProviderConfiguration();
String path = providerConfig.get(ImportProvider.PARAM_SOURCE).toString();
URI pathUri;
try {
pathUri = new URI(path);
} catch (URISyntaxException e1) {
reporter.error(new IOMessageImpl("Skipped resource because of invalid URI: " + path, e1));
continue;
}
// check if path was already handled
if (handledResources.containsKey(pathUri)) {
// skip copying the resource
continue;
}
String scheme = pathUri.getScheme();
LocatableInputSupplier<? extends InputStream> input = null;
if (scheme != null) {
if (scheme.equals("http") || scheme.equals("https") || scheme.equals("file") || scheme.equals("platform") || scheme.equals("bundle") || scheme.equals("jar")) {
input = new DefaultInputSupplier(pathUri);
} else {
continue;
}
} else {
// now can't open that, can we?
reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + pathUri.toString(), null));
continue;
}
progress.setCurrentTask("Copying resource at " + path);
// every resource file is copied into an own resource
// directory in the target directory
String resourceFolder = "_schemas";
if (count > 0) {
resourceFolder += count;
}
File newDirectory = new File(targetDirectory, resourceFolder);
try {
newDirectory.mkdir();
} catch (SecurityException e) {
throw new IOException("Can not create directory " + newDirectory.toString(), e);
}
// the filename
String name = path.toString().substring(path.lastIndexOf("/") + 1, path.length());
// remove any query string from the filename
int queryIndex = name.indexOf('?');
if (queryIndex >= 0) {
name = name.substring(0, queryIndex);
}
if (name.isEmpty()) {
name = "file";
}
File newFile = new File(newDirectory, name);
Path target = newFile.toPath();
// retrieve the resource advisor
Value ct = providerConfig.get(ImportProvider.PARAM_CONTENT_TYPE);
IContentType contentType = null;
if (ct != null) {
contentType = HalePlatform.getContentTypeManager().getContentType(ct.as(String.class));
}
ResourceAdvisor ra = ResourceAdvisorExtension.getInstance().getAdvisor(contentType);
// copy the resource
progress.setCurrentTask("Copying resource at " + path);
ra.copyResource(input, target, contentType, true, reporter);
// store new path for resource
String newPath = resourceFolder + "/" + name;
handledResources.put(pathUri, newPath);
count++;
}
}
} finally {
progress.end();
}
return handledResources;
}
use of eu.esdihumboldt.hale.common.core.io.ResourceAdvisor in project hale by halestudio.
the class ArchiveProjectWriter method updateResources.
/**
* Update the resources and copy them into the target directory
*
* @param targetDirectory target directory
* @param includeWebResources whether to include web resources in the copy
* @param progress the progress indicator
* @param reporter the reporter to use for the execution report
* @throws IOException if an I/O operation fails
*/
protected void updateResources(File targetDirectory, boolean includeWebResources, ProgressIndicator progress, IOReporter reporter) throws IOException {
progress.begin("Copy resources", ProgressIndicator.UNKNOWN);
try {
List<IOConfiguration> resources = getProject().getResources();
// every resource needs his own directory
int count = 1;
// true if excluded files should be skipped; false is default
boolean excludeDataFiles = getParameter(EXLUDE_DATA_FILES).as(Boolean.class, false);
// resource locations mapped to new resource path
Map<URI, String> handledResources = new HashMap<>();
Iterator<IOConfiguration> iter = resources.iterator();
while (iter.hasNext()) {
IOConfiguration resource = iter.next();
// import not possible due to cycle errors
if (excludeDataFiles && resource.getActionId().equals("eu.esdihumboldt.hale.io.instance.read.source")) {
// delete reference in project file
iter.remove();
continue;
}
// get resource path
Map<String, Value> providerConfig = resource.getProviderConfiguration();
String path = providerConfig.get(ImportProvider.PARAM_SOURCE).toString();
URI pathUri;
try {
pathUri = new URI(path);
} catch (URISyntaxException e1) {
reporter.error(new IOMessageImpl("Skipped resource because of invalid URI: " + path, e1));
continue;
}
if (!pathUri.isAbsolute()) {
if (getPreviousTarget() != null) {
pathUri = getPreviousTarget().resolve(pathUri);
} else {
log.warn("Could not resolve relative path " + pathUri.toString());
}
}
// check if path was already handled
if (handledResources.containsKey(pathUri)) {
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(handledResources.get(pathUri)));
// skip copying the resource
continue;
}
String scheme = pathUri.getScheme();
LocatableInputSupplier<? extends InputStream> input = null;
if (scheme != null) {
if (scheme.equals("http") || scheme.equals("https")) {
// web resource
if (includeWebResources) {
input = new DefaultInputSupplier(pathUri);
} else {
// web resource that should not be included this
// time
// but the resolved URI should be stored
// nevertheless
// otherwise the URI may be invalid if it was
// relative
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(pathUri.toASCIIString()));
continue;
}
} else if (scheme.equals("file") || scheme.equals("platform") || scheme.equals("bundle") || scheme.equals("jar")) {
// files need always to be included
// platform resources (or other internal resources)
// should be included as well
input = new DefaultInputSupplier(pathUri);
} else {
// other type of URI, e.g. JDBC
// not to be included
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(pathUri.toASCIIString()));
continue;
}
} else {
// now can't open that, can we?
reporter.error(new IOMessageImpl("Skipped resource because it cannot be loaded from " + pathUri.toString(), null));
continue;
}
progress.setCurrentTask("Copying resource at " + path);
// every resource file is copied into an own resource
// directory in the target directory
String resourceFolder = "resource" + count;
File newDirectory = new File(targetDirectory, resourceFolder);
try {
newDirectory.mkdir();
} catch (SecurityException e) {
throw new IOException("Can not create directory " + newDirectory.toString(), e);
}
// Extract the file name from pathUri.getPath().
// This will produce a non-URL-encoded file name to be used in
// the File(File parent, String child) constructor below
String fileName = FilenameUtils.getName(pathUri.getPath().toString());
if (path.isEmpty()) {
fileName = "file";
}
File newFile = new File(newDirectory, fileName);
Path target = newFile.toPath();
// retrieve the resource advisor
Value ct = providerConfig.get(ImportProvider.PARAM_CONTENT_TYPE);
IContentType contentType = null;
if (ct != null) {
contentType = HalePlatform.getContentTypeManager().getContentType(ct.as(String.class));
}
ResourceAdvisor ra = ResourceAdvisorExtension.getInstance().getAdvisor(contentType);
// copy the resource
progress.setCurrentTask("Copying resource at " + path);
// Extract the URL-encoded file name of the copied resource and
// build the new relative resource path
String resourceName = FilenameUtils.getName(target.toUri().toString());
String newPath = resourceFolder + "/" + resourceName;
boolean skipCopy = getParameter(EXCLUDE_CACHED_RESOURCES).as(Boolean.class, false) && !resource.getCache().isEmpty();
if (!skipCopy) {
ra.copyResource(input, target, contentType, includeWebResources, reporter);
// store new path for resource
handledResources.put(pathUri, newPath);
}
// update the provider configuration
providerConfig.put(ImportProvider.PARAM_SOURCE, Value.of(newPath));
count++;
}
} finally {
progress.end();
}
}
Aggregations