use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project pentaho-platform by pentaho.
the class DefaultContentSystemListener method startup.
@Override
public boolean startup(IPentahoSession arg0) {
// By default we'll run in a separate thread. This checks to see if someone has disabled this.
ISystemConfig systemSettings = PentahoSystem.get(ISystemConfig.class);
Boolean enableAsyncLoading = true;
if (systemSettings != null) {
String disableLoadAsyncStr = systemSettings.getProperty("system.enable-async-default-content-loading");
enableAsyncLoading = Boolean.valueOf(disableLoadAsyncStr);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
SecurityHelper.getInstance().runAsSystem(new Callable<Void>() {
@Override
public Void call() throws Exception {
Logger.info(this.getClass().getName(), "Default content importer has started");
// get a File reference to the directory
String solutionPath = PentahoSystem.getApplicationContext().getSolutionPath(DEFAULT_CONTENT_FOLDER);
File directory = new File(solutionPath);
// Instantiate the importer
IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
ArchiveLoader archiveLoader = new ArchiveLoader(importer);
archiveLoader.loadAll(directory, ArchiveLoader.ZIPS_FILTER);
return null;
}
});
} catch (Exception e) {
Logger.error(this.getClass().getName(), e.getMessage());
}
}
};
if (enableAsyncLoading) {
Thread t = new Thread(runnable);
t.setDaemon(true);
t.setName("Default Content Loader Thread");
t.start();
} else {
runnable.run();
}
return true;
}
use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project pentaho-platform by pentaho.
the class RepositoryImportResource method doPostImport.
/**
* Attempts to import all files from the zip archive or single file. A log file is produced at the end of import.
*
* <p><b>Example Request:</b><br />
* POST pentaho/api/repo/files/import
* <br /><b>POST data:</b>
* <pre function="syntax.xml">
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="importDir"
*
* /public
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="fileUpload"; filename="test.csv"
* Content-Type: application/vnd.ms-excel
*
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="overwriteFile"
*
* true
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="overwriteAclPermissions"
*
* true
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="applyAclPermissions"
*
* true
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="retainOwnership"
*
* true
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="charSet"
*
* UTF-8
* ------WebKitFormBoundaryB9hzsGp4wR5SGAZD
* Content-Disposition: form-data; name="logLevel"
*
* INFO
* ------WebKitFormBoundaryd1z6iZhXyx12RYxV
* Content-Disposition: form-data; name="fileNameOverride"
*
* fileNameOverriden.csv
* ------WebKitFormBoundaryd1z6iZhXyx12RYxV--
* </pre>
* </p>
*
* @param importDir JCR Directory to which the zip structure or single file will be uploaded to.
* @param fileUpload Input stream for the file.
* @param overwriteFile The flag indicates ability to overwrite existing file.
* @param overwriteAclPermissions The flag indicates ability to overwrite Acl permissions.
* @param applyAclPermissions The flag indicates ability to apply Acl permissions.
* @param retainOwnership The flag indicates ability to retain ownership.
* @param charSet The charset for imported file.
* @param logLevel The level of logging.
* @param fileNameOverride If present and the content represents a single file, this parameter contains the filename to use
* when storing the file in the repository. If not present, the fileInfo.getFileName will be used.
* Note that the later cannot reliably handle foreign character sets.
*
* @return A jax-rs Response object with the appropriate header and body.
*
* <p><b>Example Response:</b></p>
* <pre function="syntax.xml">
* <html>
* <head>
* <title>Repository Import Log</title>
* </head>
* <body bgcolor="#FFFFFF" topmargin="6" leftmargin="6" style="font-family: arial,sans-serif; font-size: x-small">
* <hr size="1" noshade>
* Log session start time Thu Feb 26 11:04:19 BRT 2015<br>
* <br>
* <table cellspacing="0" cellpadding="4" border="1" bordercolor="#224466" width="100%">
* <tr style="background: #336699; color: #FFFFFF; text-align: left">
* <th>Import File</th>
* <th>Level</th>
* <th>Message</th>
* </tr>
* <td title="importFile">/public</td>
* <td title="Level">INFO</td>
* <td title="Message">Start Import Job</td>
* </tr>
* <td title="importFile">/public/fileNameOverriden.csv</td>
* <td title="Level">INFO</td>
* <td title="Message">Start File Import</td>
* </tr>
* <td title="importFile">/public/fileNameOverriden.csv</td>
* <td title="Level"><font color="#993300"><strong>WARN</strong></font></td>
* <td title="Message">fileNameOverriden.csv</td>
* </tr>
* <td title="importFile">/public</td>
* <td title="Level">INFO</td>
* <td title="Message">End Import Job</td>
* </tr>
* </table>
* <br>
* </body></html>
* </pre>
*/
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_HTML)
@Facet(name = "Unsupported")
public Response doPostImport(@FormDataParam("importDir") String importDir, @FormDataParam("fileUpload") InputStream fileUpload, @FormDataParam("overwriteFile") String overwriteFile, @FormDataParam("overwriteAclPermissions") String overwriteAclPermissions, @FormDataParam("applyAclPermissions") String applyAclPermission, @FormDataParam("retainOwnership") String retainOwnership, @FormDataParam("charSet") String charSet, @FormDataParam("logLevel") String logLevel, @FormDataParam("fileUpload") FormDataContentDisposition fileInfo, @FormDataParam("fileNameOverride") String fileNameOverride) {
IRepositoryImportLogger importLogger = null;
ByteArrayOutputStream importLoggerStream = new ByteArrayOutputStream();
boolean logJobStarted = false;
if (StringUtils.isBlank(charSet)) {
charSet = DEFAULT_CHAR_SET;
}
try {
validateAccess(importDir);
boolean overwriteFileFlag = ("false".equals(overwriteFile) ? false : true);
boolean overwriteAclSettingsFlag = ("true".equals(overwriteAclPermissions) ? true : false);
boolean applyAclSettingsFlag = ("true".equals(applyAclPermission) ? true : false);
boolean retainOwnershipFlag = ("true".equals(retainOwnership) ? true : false);
// If logLevel is null then we will default to ERROR
if (logLevel == null || logLevel.length() <= 0) {
logLevel = "ERROR";
}
// Non-admins cannot process a manifest
FileService fileService = new FileService();
if (!fileService.doCanAdminister()) {
applyAclSettingsFlag = false;
retainOwnershipFlag = true;
}
Level level = Level.toLevel(logLevel);
ImportSession.getSession().setAclProperties(applyAclSettingsFlag, retainOwnershipFlag, overwriteAclSettingsFlag);
// The fileNameOverride was added because the formDataContentDispositionfile object cannot reliable
// contain non US-ASCII characters. See RFC283 section 2.3 for details
String fileName = fileNameOverride != null ? fileNameOverride : fileInfo.getFileName();
RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
bundleBuilder.input(fileUpload);
bundleBuilder.charSet(charSet);
bundleBuilder.path(importDir);
bundleBuilder.overwriteFile(overwriteFileFlag);
bundleBuilder.applyAclSettings(applyAclSettingsFlag);
bundleBuilder.overwriteAclSettings(overwriteAclSettingsFlag);
bundleBuilder.retainOwnership(retainOwnershipFlag);
bundleBuilder.name(fileName);
IPlatformImportBundle bundle = bundleBuilder.build();
IPlatformMimeResolver mimeResolver = PentahoSystem.get(IPlatformMimeResolver.class);
String mimeTypeFromFile = mimeResolver.resolveMimeForFileName(fileName);
if (mimeTypeFromFile == null) {
return Response.ok("INVALID_MIME_TYPE", MediaType.TEXT_HTML).build();
}
bundleBuilder.mime(mimeTypeFromFile);
IPlatformImporter importer = PentahoSystem.get(IPlatformImporter.class);
importLogger = importer.getRepositoryImportLogger();
final String mimeType = bundle.getMimeType() != null ? bundle.getMimeType() : mimeResolver.resolveMimeForBundle(bundle);
if (mimeType == null) {
return Response.ok("INVALID_MIME_TYPE", MediaType.TEXT_HTML).build();
}
logJobStarted = true;
importLogger.startJob(importLoggerStream, importDir, level);
importer.importFile(bundle);
// Flush the Mondrian cache to show imported data-sources.
IMondrianCatalogService mondrianCatalogService = PentahoSystem.get(IMondrianCatalogService.class, "IMondrianCatalogService", PentahoSessionHolder.getSession());
mondrianCatalogService.reInit(PentahoSessionHolder.getSession());
} catch (PentahoAccessControlException e) {
return Response.serverError().entity(e.toString()).build();
} catch (Exception e) {
return Response.serverError().entity(e.toString()).build();
} finally {
ImportSession.clearSession();
if (logJobStarted == true) {
importLogger.endJob();
}
}
String responseBody;
try {
responseBody = importLoggerStream.toString(charSet);
} catch (UnsupportedEncodingException e) {
LOGGER.error("Encoding of response body is failed. (charSet=" + charSet + ")", e);
responseBody = importLoggerStream.toString();
}
return Response.ok(responseBody, MediaType.TEXT_HTML).build();
}
use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project data-access by pentaho.
the class DataSourceWizardServiceTest method testPublishDswFromTemp.
@Test
public void testPublishDswFromTemp() throws Exception {
InputStream metadataFile = getClass().getClassLoader().getResourceAsStream(TEST_XMI_FILE_PATH);
boolean overwrite = true;
boolean checkConnection = false;
XmiParser mockXmiParser = mock(XmiParser.class);
Domain mockDomain = mock(Domain.class);
InputStream mockInputStream = mock(InputStream.class);
IPlatformImportBundle mockMetadataBundle = mock(IPlatformImportBundle.class);
IPlatformImportBundle mockMondrianBundle = mock(IPlatformImportBundle.class);
IPlatformImporter mockIPlatformImporter = mock(IPlatformImporter.class);
IPentahoSession mockIPentahoSession = mock(IPentahoSession.class);
final RepositoryFileAclDto aclDto = new RepositoryFileAclDto();
aclDto.setOwner("owner");
aclDto.setOwnerType(RepositoryFileSid.Type.USER.ordinal());
doReturn(true).when(dataSourceWizardService).hasManageAccessCheck();
doReturn(true).when(dataSourceWizardService).endsWith(anyString(), anyString());
doReturn(mockXmiParser).when(dataSourceWizardService).createXmiParser();
doReturn(mockDomain).when(mockXmiParser).parseXmi(metadataFile);
doReturn(mockInputStream).when(dataSourceWizardService).toInputStreamWrapper(mockDomain, mockXmiParser);
doReturn(mockMetadataBundle).when(dataSourceWizardService).createMetadataDswBundle(mockDomain, mockInputStream, overwrite, aclDto);
doReturn(mockMondrianBundle).when(dataSourceWizardService).createMondrianDswBundle(mockDomain, aclDto);
doReturn(mockIPlatformImporter).when(dataSourceWizardService).getIPlatformImporter();
doReturn(mockIPentahoSession).when(dataSourceWizardService).getSession();
doReturn(metadataFile).when(dataSourceWizardService).createInputStreamFromFile(anyString());
MetadataTempFilesListDto fileList = new MetadataTempFilesListDto();
fileList.setXmiFileName(XMI_TEMP_FILE_NAME);
String list = "{\"xmiFileName\":\"" + XMI_TEMP_FILE_NAME + "\"}";
String response = dataSourceWizardService.publishDswFromTemp(DOMAIN_ID, fileList, overwrite, checkConnection, aclDto);
assertEquals(DOMAIN_ID + DataSourceWizardService.METADATA_EXT, response);
}
use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project data-access by pentaho.
the class MetadataServiceTest method fillServiceMock.
private void fillServiceMock(String domainId, InputStream metadataFile) throws Exception {
FileResource mockFileResource = mock(FileResource.class);
Response mockResponse = mock(Response.class);
IPentahoSession mockIPentahoSession = mock(IPentahoSession.class);
IPlatformImporter mockIPlatformImporter = mock(IPlatformImporter.class);
IPlatformImportBundle mockIPlatformImportBundle = mock(IPlatformImportBundle.class);
RepositoryFileImportBundle.Builder mockRepositoryFileImportBundleBuilder = mock(RepositoryFileImportBundle.Builder.class);
RepositoryFileImportBundle mockRepositoryFileImportBundle = mock(RepositoryFileImportBundle.class);
ByteArrayInputStream mockByteArrayInputStream = mock(ByteArrayInputStream.class);
doNothing().when(metadataService).accessValidation();
doReturn(mockFileResource).when(metadataService).createNewFileResource();
doReturn(mockResponse).when(mockFileResource).doGetReservedChars();
doReturn(null).when(mockResponse).getEntity();
doReturn("\t\n/").when(metadataService).objectToString(null);
doReturn(mockRepositoryFileImportBundleBuilder).when(metadataService).createNewRepositoryFileImportBundleBuilder(metadataFile, false, domainId, null);
doReturn(UPLOAD_DIR).when(metadataService).internalGetUploadDir();
doReturn(mockByteArrayInputStream).when(metadataService).createNewByteArrayInputStream(any(byte[].class));
// doReturn( mockRepositoryFileImportBundle ).when( metadataService ).createNewRepositoryFileImportBundle(
// mockByteArrayInputStream, "fileName", domainId );
doReturn(mockRepositoryFileImportBundle).when(mockRepositoryFileImportBundleBuilder).build();
doReturn(mockIPlatformImporter).when(metadataService).getImporter();
doNothing().when(mockIPlatformImporter).importFile(mockIPlatformImportBundle);
doReturn(mockIPentahoSession).when(metadataService).getSession();
doNothing().when(metadataService).publish(mockIPentahoSession);
}
use of org.pentaho.platform.plugin.services.importer.IPlatformImporter in project data-access by pentaho.
the class DataSourceWizardService method publishDsw.
public String publishDsw(String domainId, InputStream metadataFile, List<InputStream> localizeFiles, List<String> localizeFileNames, boolean overwrite, boolean checkConnection, RepositoryFileAclDto acl) throws PentahoAccessControlException, IllegalArgumentException, DswPublishValidationException, Exception {
if (!hasManageAccessCheck()) {
throw new PentahoAccessControlException();
}
if (!endsWith(domainId, METADATA_EXT)) {
// if doesn't end in case-sensitive '.xmi' there will be trouble later on
final String errorMsg = "domainId must end in " + METADATA_EXT;
throw new IllegalArgumentException(errorMsg);
}
if (localizeFiles == null ? (localizeFileNames != null) : (localizeFiles.size() != localizeFileNames.size())) {
throw new IllegalArgumentException("localizeFiles and localizeFileNames must have equal size");
}
if (metadataFile == null) {
throw new IllegalArgumentException("metadataFile is null");
}
if (!overwrite) {
final List<String> overwritten = getOverwrittenDomains(domainId);
if (!overwritten.isEmpty()) {
final String domainIds = StringUtils.join(overwritten, ",");
throw new DswPublishValidationException(DswPublishValidationException.Type.OVERWRITE_CONFLICT, domainIds);
}
}
XmiParser xmiParser = createXmiParser();
Domain domain = null;
try {
domain = xmiParser.parseXmi(metadataFile);
} catch (Exception e) {
throw new DswPublishValidationException(DswPublishValidationException.Type.INVALID_XMI, e.getMessage());
}
domain.setId(domainId);
if (checkConnection) {
final String connectionId = getMondrianDatasourceWrapper(domain);
// Left second check with non-escaped name for backward compatibility
if (datasourceMgmtSvc.getDatasourceByName(sanitizer.escape(connectionId)) == null && datasourceMgmtSvc.getDatasourceByName(connectionId) == null) {
final String msg = "connection not found: '" + connectionId + "'";
throw new DswPublishValidationException(Type.MISSING_CONNECTION, msg);
}
}
// build bundles
IPlatformImportBundle mondrianBundle = createMondrianDswBundle(domain, acl);
InputStream metadataIn = toInputStreamWrapper(domain, xmiParser);
IPlatformImportBundle metadataBundle = createMetadataDswBundle(domain, metadataIn, overwrite, acl);
// add localization bundles
if (localizeFiles != null) {
for (int i = 0; i < localizeFiles.size(); i++) {
IPlatformImportBundle localizationBundle = MetadataService.createNewRepositoryFileImportBundle(localizeFiles.get(i), localizeFileNames.get(i), domainId);
metadataBundle.getChildBundles().add(localizationBundle);
logger.info("created language file");
}
}
// do import
IPlatformImporter importer = getIPlatformImporter();
importer.importFile(metadataBundle);
logger.debug("imported metadata xmi");
importer.importFile(mondrianBundle);
logger.debug("imported mondrian schema");
// trigger refreshes
IPentahoSession session = getSession();
PentahoSystem.publish(session, METADATA_PUBLISHER);
PentahoSystem.publish(session, MONDRIAN_PUBLISHER);
logger.info("publishDsw: Published DSW with domainId='" + domainId + "'.");
return domainId;
}
Aggregations