use of org.pentaho.metadata.repository.DomainIdNullException in project pentaho-platform by pentaho.
the class PentahoPlatformImporter method importFile.
/**
* this is the main method that uses the mime time (from Spring) to determine which handler to invoke.
*/
public void importFile(IPlatformImportBundle file) throws PlatformImportException {
String mime = file.getMimeType() != null ? file.getMimeType() : mimeResolver.resolveMimeForBundle(file);
try {
if (mime == null) {
log.trace(messages.getString("PentahoPlatformImporter.ERROR_0001_INVALID_MIME_TYPE") + file.getName());
repositoryImportLogger.error(messages.getString("PentahoPlatformImporter.ERROR_0001_INVALID_MIME_TYPE") + file.getName());
return;
}
IPlatformImportHandler handler = (importHandlers.containsKey(mime) == false) ? defaultHandler : importHandlers.get(mime);
if (handler == null) {
throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0002_MISSING_IMPORT_HANDLER"), PlatformImportException.PUBLISH_GENERAL_ERROR);
// replace with default handler?
}
try {
logImportFile(file);
handler.importFile(file);
} catch (DomainIdNullException e1) {
throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0004_PUBLISH_TO_SERVER_FAILED"), PlatformImportException.PUBLISH_TO_SERVER_FAILED, e1);
} catch (DomainAlreadyExistsException e1) {
throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0007_PUBLISH_SCHEMA_EXISTS_ERROR"), PlatformImportException.PUBLISH_SCHEMA_EXISTS_ERROR, e1);
} catch (DomainStorageException e1) {
throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0004_PUBLISH_TO_SERVER_FAILED"), PlatformImportException.PUBLISH_DATASOURCE_ERROR, e1);
} catch (IOException e1) {
throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0005_PUBLISH_GENERAL_ERRORR", e1.getLocalizedMessage()), PlatformImportException.PUBLISH_GENERAL_ERROR, e1);
} catch (PlatformImportException pe) {
// if already converted - just rethrow
throw pe;
} catch (Exception e1) {
throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0005_PUBLISH_GENERAL_ERRORR", e1.getLocalizedMessage()), PlatformImportException.PUBLISH_GENERAL_ERROR, e1);
}
} catch (Exception e) {
e.printStackTrace();
// If we are doing a logged import then we do not want to fail on a single file
// so log the error and keep going.
RepositoryFileImportBundle bundle = (RepositoryFileImportBundle) file;
String repositoryFilePath = RepositoryFilenameUtils.concat(bundle.getPath(), bundle.getName());
if (repositoryImportLogger.hasLogger() && repositoryFilePath != null && repositoryFilePath.length() > 0) {
repositoryImportLogger.error(e);
} else {
if (e instanceof PlatformImportException) {
throw (PlatformImportException) e;
} else {
// shouldn't happen but just in case
throw new PlatformImportException(e.getMessage());
}
}
if (e.getCause() instanceof UnifiedRepositoryAccessDeniedException) {
throw new UnifiedRepositoryAccessDeniedException();
}
}
}
use of org.pentaho.metadata.repository.DomainIdNullException in project pentaho-platform by pentaho.
the class PentahoMetadataDomainRepository method storeDomain.
/**
* Store a domain to the repository. The domain should persist between JVM restarts.
*
* @param domain domain object to store
* @param overwrite if true, overwrite existing domain
* @throws DomainIdNullException if domain id is null or empty
* @throws DomainAlreadyExistsException if a domain with the same Domain ID already exists in the repository and
* {@code overwrite == false}
* @throws DomainStorageException if there is a problem storing the domain
*/
@Override
public void storeDomain(final Domain domain, final boolean overwrite) throws DomainIdNullException, DomainAlreadyExistsException, DomainStorageException {
if (logger.isDebugEnabled()) {
logger.debug("storeDomain(domain(id=" + (domain != null ? domain.getId() : "") + ", " + overwrite + ")");
}
if (null == domain || StringUtils.isEmpty(domain.getId())) {
throw new DomainIdNullException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0001_DOMAIN_ID_NULL"));
}
String xmi = "";
try {
// NOTE - a ByteArrayInputStream doesn't need to be closed ...
// ... so this is safe AS LONG AS we use a ByteArrayInputStream
xmi = xmiParser.generateXmi(domain);
// final InputStream inputStream = new ByteArrayInputStream( xmi.getBytes( DEFAULT_ENCODING ) );
final InputStream inputStream = new ByteArrayInputStream(xmi.getBytes("UTF8"));
storeDomain(inputStream, domain.getId(), overwrite);
} catch (DomainStorageException dse) {
throw dse;
} catch (DomainAlreadyExistsException dae) {
throw dae;
} catch (Exception e) {
final String errorMessage = messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0003_ERROR_STORING_DOMAIN", domain.getId(), e.getLocalizedMessage());
logger.error(errorMessage, e);
throw new DomainStorageException(xmi + errorMessage, e);
}
}
use of org.pentaho.metadata.repository.DomainIdNullException in project pentaho-platform by pentaho.
the class PentahoMetadataDomainRepositoryTest method testStoreDomain.
@Test
public void testStoreDomain() throws Exception {
try {
domainRepositorySpy.storeDomain(null, true);
fail("Invalid domain should throw exception");
} catch (DomainIdNullException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(new MockDomain(null), true);
fail("Null domain id should throw exception");
} catch (DomainIdNullException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(new MockDomain(""), true);
fail("Empty domain id should throw exception");
} catch (DomainIdNullException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(null, null, true);
fail("Null InputStream should throw an exception");
} catch (IllegalArgumentException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(null, "", true);
fail("Null InputStream should throw an exception");
} catch (IllegalArgumentException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(null, "valid", true);
fail("Null InputStream should throw an exception");
} catch (IllegalArgumentException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(EMPTY_INPUT_STREAM, null, true);
fail("Invalid domain id should throw exception");
} catch (DomainIdNullException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(EMPTY_INPUT_STREAM, "", true);
fail("Invalid domain id should throw exception");
} catch (DomainIdNullException success) {
// ignored
}
// Have the XmiParser fail
try {
domainRepositorySpy.storeDomain(new MockDomain("exception"), true);
fail("An unexpected exception should throw a DomainStorageException");
} catch (DomainStorageException success) {
// ignored
}
try {
domainRepositorySpy.storeDomain(new ByteArrayInputStream(null), "valid", true);
fail("Error with byte array input stream should throw exception");
} catch (Exception success) {
// ignored
}
domainRepositorySpy.removeDomain("steel-wheels_test");
domainRepositorySpy.removeDomain(STEEL_WHEELS);
domainRepositorySpy.removeDomain(SAMPLE_DOMAIN_ID);
final MockDomain sample = new MockDomain(SAMPLE_DOMAIN_ID);
domainRepositorySpy.storeDomain(sample, false);
doReturn(true).when(aclNodeHelper).canAccess(any(RepositoryFile.class), eq(EnumSet.of(RepositoryFilePermission.READ)));
final Domain domain = domainRepositorySpy.getDomain(SAMPLE_DOMAIN_ID);
assertNotNull(domain);
final List<LogicalModel> logicalModels = domain.getLogicalModels();
assertNotNull(logicalModels);
assertEquals(0, logicalModels.size());
try {
domainRepositorySpy.storeDomain(sample, false);
fail("A duplicate domain with overwrite=false should fail");
} catch (DomainAlreadyExistsException success) {
// ignored
}
sample.addLogicalModel("test");
domainRepositorySpy.storeDomain(sample, true);
assertEquals(1, domainRepositorySpy.getDomain(SAMPLE_DOMAIN_ID).getLogicalModels().size());
MockDomain xmiExtensionSample = new MockDomain(SAMPLE_DOMAIN_ID + ".xmi");
try {
domainRepositorySpy.storeDomain(xmiExtensionSample, false);
fail("A duplicate domain with overwrite=false should fail");
} catch (DomainAlreadyExistsException success) {
// ignored
}
xmiExtensionSample.addLogicalModel("test1");
xmiExtensionSample.addLogicalModel("test2");
domainRepositorySpy.storeDomain(xmiExtensionSample, true);
assertEquals(2, domainRepositorySpy.getDomain(SAMPLE_DOMAIN_ID).getLogicalModels().size());
final RepositoryFile folder = domainRepositorySpy.getMetadataDir();
assertNotNull(folder);
assertTrue(folder.isFolder());
assertEquals(1, repository.getChildren(folder.getId()).size());
final Domain steelWheelsDomain = loadDomain(STEEL_WHEELS, "./steel-wheels.xmi");
domainRepositorySpy.storeDomain(steelWheelsDomain, true);
assertEquals(2, repository.getChildren(folder.getId()).size());
}
use of org.pentaho.metadata.repository.DomainIdNullException in project pentaho-platform by pentaho.
the class SolutionImportHandler method importFile.
@Override
public void importFile(IPlatformImportBundle bundle) throws PlatformImportException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException, IOException {
RepositoryFileImportBundle importBundle = (RepositoryFileImportBundle) bundle;
ZipInputStream zipImportStream = new ZipInputStream(bundle.getInputStream());
SolutionRepositoryImportSource importSource = new SolutionRepositoryImportSource(zipImportStream);
LocaleFilesProcessor localeFilesProcessor = new LocaleFilesProcessor();
setOverwriteFile(bundle.overwriteInRepository());
// importSession.set(ImportSession.getSession());
IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
cachedImports = new HashMap<String, RepositoryFileImportBundle.Builder>();
// Process Manifest Settings
ExportManifest manifest = getImportSession().getManifest();
String manifestVersion = null;
if (manifest != null) {
manifestVersion = manifest.getManifestInformation().getManifestVersion();
}
// Process Metadata
if (manifest != null) {
// import the users
Map<String, List<String>> roleToUserMap = importUsers(manifest.getUserExports());
// import the roles
importRoles(manifest.getRoleExports(), roleToUserMap);
List<ExportManifestMetadata> metadataList = manifest.getMetadataList();
for (ExportManifestMetadata exportManifestMetadata : metadataList) {
String domainId = exportManifestMetadata.getDomainId();
if (domainId != null && !domainId.endsWith(XMI_EXTENSION)) {
domainId = domainId + XMI_EXTENSION;
}
boolean overWriteInRepository = isOverwriteFile();
RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF-8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).preserveDsw(bundle.isPreserveDsw()).overwriteFile(overWriteInRepository).mime("text/xmi+xml").withParam("domain-id", domainId);
cachedImports.put(exportManifestMetadata.getFile(), bundleBuilder);
}
// Process Mondrian
List<ExportManifestMondrian> mondrianList = manifest.getMondrianList();
for (ExportManifestMondrian exportManifestMondrian : mondrianList) {
String catName = exportManifestMondrian.getCatalogName();
Parameters parametersMap = exportManifestMondrian.getParameters();
StringBuilder parametersStr = new StringBuilder();
for (String s : parametersMap.keySet()) {
parametersStr.append(s).append("=").append(parametersMap.get(s)).append(sep);
}
RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder().charSet("UTF_8").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).name(catName).overwriteFile(isOverwriteFile()).mime("application/vnd.pentaho.mondrian+xml").withParam("parameters", parametersStr.toString()).withParam("domain-id", // TODO: this is
catName);
// definitely
// named wrong
// at the very
// least.
// pass as param if not in parameters string
String xmlaEnabled = "" + exportManifestMondrian.isXmlaEnabled();
bundleBuilder.withParam("EnableXmla", xmlaEnabled);
cachedImports.put(exportManifestMondrian.getFile(), bundleBuilder);
String annotationsFile = exportManifestMondrian.getAnnotationsFile();
if (annotationsFile != null) {
RepositoryFileImportBundle.Builder annotationsBundle = new RepositoryFileImportBundle.Builder().path(MondrianCatalogRepositoryHelper.ETC_MONDRIAN_JCR_FOLDER + RepositoryFile.SEPARATOR + catName).name("annotations.xml").charSet("UTF_8").overwriteFile(isOverwriteFile()).mime("text/xml").hidden(RepositoryFile.HIDDEN_BY_DEFAULT).schedulable(RepositoryFile.SCHEDULABLE_BY_DEFAULT).withParam("domain-id", catName);
cachedImports.put(annotationsFile, annotationsBundle);
}
}
}
importMetaStore(manifest, bundle.overwriteInRepository());
for (IRepositoryFileBundle fileBundle : importSource.getFiles()) {
String fileName = fileBundle.getFile().getName();
String actualFilePath = fileBundle.getPath();
if (manifestVersion != null) {
fileName = ExportFileNameEncoder.decodeZipFileName(fileName);
actualFilePath = ExportFileNameEncoder.decodeZipFileName(actualFilePath);
}
String repositoryFilePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
if (this.cachedImports.containsKey(repositoryFilePath)) {
byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
RepositoryFileImportBundle.Builder builder = cachedImports.get(repositoryFilePath);
builder.input(new ByteArrayInputStream(bytes));
importer.importFile(build(builder));
continue;
}
RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
InputStream bundleInputStream = null;
String decodedFilePath = fileBundle.getPath();
RepositoryFile decodedFile = fileBundle.getFile();
if (manifestVersion != null) {
decodedFile = new RepositoryFile.Builder(decodedFile).path(decodedFilePath).name(fileName).title(fileName).build();
decodedFilePath = ExportFileNameEncoder.decodeZipFileName(fileBundle.getPath());
}
if (fileBundle.getFile().isFolder()) {
bundleBuilder.mime("text/directory");
bundleBuilder.file(decodedFile);
fileName = repositoryFilePath;
repositoryFilePath = importBundle.getPath();
} else {
byte[] bytes = IOUtils.toByteArray(fileBundle.getInputStream());
bundleInputStream = new ByteArrayInputStream(bytes);
// If is locale file store it for later processing.
if (localeFilesProcessor.isLocaleFile(fileBundle, importBundle.getPath(), bytes)) {
log.trace("Skipping [" + repositoryFilePath + "], it is a locale property file");
continue;
}
bundleBuilder.input(bundleInputStream);
bundleBuilder.mime(solutionHelper.getMime(fileName));
String filePath = (decodedFilePath.equals("/") || decodedFilePath.equals("\\")) ? "" : decodedFilePath;
repositoryFilePath = RepositoryFilenameUtils.concat(importBundle.getPath(), filePath);
}
bundleBuilder.name(fileName);
bundleBuilder.path(repositoryFilePath);
String sourcePath;
if (fileBundle.getFile().isFolder()) {
sourcePath = fileName;
} else {
sourcePath = RepositoryFilenameUtils.concat(PentahoPlatformImporter.computeBundlePath(actualFilePath), fileName);
}
// may not have rights to such as /home or /public
if (manifest != null && manifest.getExportManifestEntity(sourcePath) == null && fileBundle.getFile().isFolder()) {
continue;
}
getImportSession().setCurrentManifestKey(sourcePath);
bundleBuilder.charSet(bundle.getCharset());
bundleBuilder.overwriteFile(bundle.overwriteInRepository());
bundleBuilder.applyAclSettings(bundle.isApplyAclSettings());
bundleBuilder.retainOwnership(bundle.isRetainOwnership());
bundleBuilder.overwriteAclSettings(bundle.isOverwriteAclSettings());
bundleBuilder.acl(getImportSession().processAclForFile(sourcePath));
RepositoryFile file = getFile(importBundle, fileBundle);
ManifestFile manifestFile = getImportSession().getManifestFile(sourcePath, file != null);
bundleBuilder.hidden(isFileHidden(file, manifestFile, sourcePath));
boolean isSchedulable = isSchedulable(file, manifestFile);
if (isSchedulable) {
bundleBuilder.schedulable(isSchedulable);
} else {
bundleBuilder.schedulable(fileIsScheduleInputSource(manifest, sourcePath));
}
IPlatformImportBundle platformImportBundle = build(bundleBuilder);
importer.importFile(platformImportBundle);
if (bundleInputStream != null) {
bundleInputStream.close();
bundleInputStream = null;
}
}
if (manifest != null) {
importSchedules(manifest.getScheduleList());
// Add Hitachi Vantara Connections
List<org.pentaho.database.model.DatabaseConnection> datasourceList = manifest.getDatasourceList();
if (datasourceList != null) {
IDatasourceMgmtService datasourceMgmtSvc = PentahoSystem.get(IDatasourceMgmtService.class);
for (org.pentaho.database.model.DatabaseConnection databaseConnection : datasourceList) {
if (databaseConnection.getDatabaseType() == null) {
// don't try to import the connection if there is no type it will cause an error
// However, if this is the DI Server, and the connection is defined in a ktr, it will import automatically
log.warn("Can't import connection " + databaseConnection.getName() + " because it doesn't have a databaseType");
continue;
}
try {
IDatabaseConnection existingDBConnection = datasourceMgmtSvc.getDatasourceByName(databaseConnection.getName());
if (existingDBConnection != null && existingDBConnection.getName() != null) {
if (isOverwriteFile()) {
databaseConnection.setId(existingDBConnection.getId());
datasourceMgmtSvc.updateDatasourceByName(databaseConnection.getName(), databaseConnection);
}
} else {
datasourceMgmtSvc.createDatasource(databaseConnection);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
// Process locale files.
localeFilesProcessor.processLocaleFiles(importer);
}
use of org.pentaho.metadata.repository.DomainIdNullException in project pentaho-platform by pentaho.
the class MetadataImportHandler method processMetadataFile.
/**
* Processes the file as a metadata file and returns the domain name. It will import the file into the Pentaho
* Metadata Domain Repository.
*
* @param bundle
* @return
*/
protected String processMetadataFile(final IPlatformImportBundle bundle) throws PlatformImportException {
final String domainId = (String) bundle.getProperty("domain-id");
if (domainId == null) {
throw new PlatformImportException("Bundle missing required domain-id property");
}
try {
log.debug("Importing as metadata - [domain=" + domainId + "]");
final InputStream inputStream;
if (bundle.isPreserveDsw()) {
// storeDomain needs to be able to close the stream
inputStream = cloneStream(bundle.getInputStream());
} else {
inputStream = StripDswFromStream(bundle.getInputStream());
}
if (metadataRepositoryImporter instanceof IAclAwarePentahoMetadataDomainRepositoryImporter) {
IAclAwarePentahoMetadataDomainRepositoryImporter importer = (IAclAwarePentahoMetadataDomainRepositoryImporter) metadataRepositoryImporter;
importer.storeDomain(inputStream, domainId, bundle.overwriteInRepository(), bundle.isApplyAclSettings() ? bundle.getAcl() : null);
} else {
metadataRepositoryImporter.storeDomain(inputStream, domainId, bundle.overwriteInRepository());
}
if (metadataRepositoryImporter instanceof IModelAnnotationsAwareMetadataDomainRepositoryImporter) {
// Store annotations xml with the domain if it exists
final String annotationsXml = (String) bundle.getProperty(IModelAnnotationsAwareMetadataDomainRepositoryImporter.PROPERTY_NAME_ANNOTATIONS);
if (StringUtils.isNotBlank(annotationsXml)) {
// Save annotations
IModelAnnotationsAwareMetadataDomainRepositoryImporter importer = (IModelAnnotationsAwareMetadataDomainRepositoryImporter) metadataRepositoryImporter;
importer.storeAnnotationsXml(domainId, annotationsXml);
}
}
return domainId;
} catch (DomainIdNullException dine) {
throw new PlatformImportException(dine.getMessage(), PlatformImportException.PUBLISH_TO_SERVER_FAILED, dine);
} catch (DomainStorageException dse) {
throw new PlatformImportException(dse.getMessage(), PlatformImportException.PUBLISH_TO_SERVER_FAILED, dse);
} catch (DomainAlreadyExistsException daee) {
throw new PlatformImportException(messages.getString("PentahoPlatformImporter.ERROR_0007_PUBLISH_SCHEMA_EXISTS_ERROR"), PlatformImportException.PUBLISH_SCHEMA_EXISTS_ERROR, daee);
} catch (Exception e) {
final String errorMessage = messages.getErrorString("MetadataImportHandler.ERROR_0001_IMPORTING_METADATA", domainId, e.getLocalizedMessage());
log.error(errorMessage, e);
throw new PlatformImportException(errorMessage, e);
}
}
Aggregations