use of org.pentaho.platform.plugin.services.importer.PlatformImportException in project pentaho-platform by pentaho.
the class RepositoryPublishServiceTest method testWriteFileException.
@Test
public void testWriteFileException() throws Exception {
String pathId = "path:to:file";
InputStream stubInputStream = IOUtils.toInputStream("some test data for my input stream");
Boolean overwriteFile = true;
doReturn(mockRepositoryFileImportBundle).when(repositoryPublishService).buildBundle(pathId, stubInputStream, overwriteFile);
/*
* Test 1
*/
doReturn(false).when(repositoryPublishService.policy).isAllowed(anyString());
doReturn(repositoryPublishService.policy).when(repositoryPublishService).getPolicy();
try {
repositoryPublishService.writeFile(pathId, stubInputStream, overwriteFile);
fail();
} catch (PentahoAccessControlException e) {
// Expected
} catch (Throwable t) {
fail();
}
/*
* Test 2
*/
doReturn(true).when(repositoryPublishService.policy).isAllowed(anyString());
doThrow(new PlatformImportException("")).when(repositoryPublishService.platformImporter).importFile(mockRepositoryFileImportBundle);
try {
repositoryPublishService.writeFile(pathId, stubInputStream, overwriteFile);
fail();
} catch (PlatformImportException e) {
// Expected
} catch (Exception e) {
fail();
}
/*
* Test 3
*/
doReturn(true).when(repositoryPublishService.policy).isAllowed(anyString());
doThrow(new InternalError()).when(repositoryPublishService.platformImporter).importFile(mockRepositoryFileImportBundle);
try {
repositoryPublishService.writeFile(pathId, stubInputStream, overwriteFile);
fail();
} catch (PlatformImportException e) {
fail();
} catch (InternalError e) {
// Expected
}
}
use of org.pentaho.platform.plugin.services.importer.PlatformImportException in project data-access by pentaho.
the class MetadataServiceTest method testImportMetadataDatasourceError.
@Test
public void testImportMetadataDatasourceError() throws Exception {
String domainId = DOMAIN_ID;
InputStream metadataFile = mock(InputStream.class);
FormDataContentDisposition metadataFileInfo = mock(FormDataContentDisposition.class);
boolean overwrite = true;
FormDataBodyPart mockFormDataBodyPart = mock(FormDataBodyPart.class);
List<FormDataBodyPart> localeFiles = new ArrayList<FormDataBodyPart>();
localeFiles.add(mockFormDataBodyPart);
List<FormDataContentDisposition> localeFilesInfo = new ArrayList<FormDataContentDisposition>();
FormDataContentDisposition mockFormDataContentDisposition = mock(FormDataContentDisposition.class);
localeFilesInfo.add(mockFormDataContentDisposition);
FileResource mockFileResource = mock(FileResource.class);
Response mockResponse = mock(Response.class);
doNothing().when(metadataService).accessValidation();
when(metadataFile.read(any())).thenReturn(-1);
doReturn(mockFileResource).when(metadataService).createNewFileResource();
doReturn(mockResponse).when(mockFileResource).doGetReservedChars();
doReturn(null).when(mockResponse).getEntity();
doReturn("\t\n/").when(metadataService).objectToString(null);
doReturn("").when(metadataService).prohibitedSymbolMessage(domainId, mockFileResource);
try {
metadataService.importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
fail();
} catch (PlatformImportException e) {
// expected
} catch (IllegalStateException e) {
// expected
}
verify(metadataService, times(1)).importMetadataDatasource(domainId, metadataFile, metadataFileInfo, overwrite, localeFiles, localeFilesInfo, null);
}
use of org.pentaho.platform.plugin.services.importer.PlatformImportException in project data-access by pentaho.
the class AnalysisResourceTest method testImportAnalysisDatasourceError.
@Test
public void testImportAnalysisDatasourceError() throws Exception {
Response mockResponse = mock(Response.class);
InputStream uploadAnalysis = mock(InputStream.class);
FormDataContentDisposition schemaFileInfo = mock(FormDataContentDisposition.class);
String catalogName = "catalogName";
String origCatalogName = "origCatalogName";
String datasourceName = "datasourceName";
String overwrite = "overwrite";
String xmlaEnabledFlag = "xmlaEnabledFlag";
String parameters = "parameters";
// Test 1
PentahoAccessControlException mockPentahoAccessControlException = mock(PentahoAccessControlException.class);
doThrow(mockPentahoAccessControlException).when(analysisResource.service).putMondrianSchema(uploadAnalysis, schemaFileInfo, catalogName, origCatalogName, datasourceName, false, false, parameters, null);
doReturn(mockResponse).when(analysisResource).buildOkResponse("5");
Response response = analysisResource.importMondrianSchema(uploadAnalysis, schemaFileInfo, catalogName, origCatalogName, datasourceName, overwrite, xmlaEnabledFlag, parameters, null);
assertEquals(mockResponse, response);
// Test 2
PlatformImportException mockPlatformImportException = mock(PlatformImportException.class);
doThrow(mockPlatformImportException).when(analysisResource.service).putMondrianSchema(uploadAnalysis, schemaFileInfo, catalogName, origCatalogName, datasourceName, true, true, parameters, null);
doReturn(mockResponse).when(analysisResource).buildOkResponse("0");
response = analysisResource.importMondrianSchema(uploadAnalysis, schemaFileInfo, catalogName, origCatalogName, datasourceName, overwrite, xmlaEnabledFlag, parameters, null);
assertEquals(mockResponse, response);
// Test 3
RuntimeException mockException = mock(RuntimeException.class);
doThrow(mockException).when(analysisResource.service).putMondrianSchema(uploadAnalysis, schemaFileInfo, catalogName, origCatalogName, datasourceName, true, true, parameters, null);
doReturn(mockResponse).when(analysisResource).buildOkResponse("2");
response = analysisResource.importMondrianSchema(uploadAnalysis, schemaFileInfo, catalogName, origCatalogName, datasourceName, overwrite, xmlaEnabledFlag, parameters, null);
assertEquals(mockResponse, response);
verify(analysisResource, times(3)).importMondrianSchema(uploadAnalysis, schemaFileInfo, catalogName, origCatalogName, datasourceName, overwrite, xmlaEnabledFlag, parameters, null);
}
use of org.pentaho.platform.plugin.services.importer.PlatformImportException in project data-access by pentaho.
the class MetadataService method importMetadataDatasource.
public void importMetadataDatasource(String domainId, InputStream metadataFile, boolean overwrite, List<InputStream> localeFileStreams, List<String> localeFileNames, RepositoryFileAclDto acl) throws PentahoAccessControlException, PlatformImportException, Exception {
if (StringUtils.isEmpty(domainId)) {
throw new PlatformImportException(Messages.getString("MetadataDatasourceService.ERROR_005_DOMAIN_NAME_EMPTY"));
}
accessValidation();
FileResource fr = createNewFileResource();
Object reservedCharsObject = fr.doGetReservedChars().getEntity();
String reservedChars = objectToString(reservedCharsObject);
if (reservedChars != null && domainId.matches(".*[" + reservedChars.replaceAll("/", "") + "]+.*")) {
String msg = prohibitedSymbolMessage(domainId, fr);
throw new PlatformImportException(msg, PlatformImportException.PUBLISH_PROHIBITED_SYMBOLS_ERROR);
}
metadataFile = validateFileSize(metadataFile, domainId);
// domain ID comes with ".xmi" suffix when creating or editing domain
// (see ModelerService.serializeModels( Domain, String, boolean ) ),
// but when the user enters domain ID manually when importing metadata file,
// it will unlikely contain that suffix, so let's add it forcibly.
domainId = forceXmiSuffix(domainId);
RepositoryFileImportBundle.Builder bundleBuilder = createNewRepositoryFileImportBundleBuilder(metadataFile, overwrite, domainId, acl);
if (localeFileStreams != null) {
for (int i = 0; i < localeFileStreams.size(); i++) {
IPlatformImportBundle localizationBundle = createNewRepositoryFileImportBundle(localeFileStreams.get(i), localeFileNames.get(i), domainId);
bundleBuilder.addChildBundle(localizationBundle);
}
}
IPlatformImportBundle bundle = bundleBuilder.build();
IPlatformImporter importer = getImporter();
importer.importFile(bundle);
IPentahoSession pentahoSession = getSession();
publish(pentahoSession);
}
use of org.pentaho.platform.plugin.services.importer.PlatformImportException in project data-access by pentaho.
the class AnalysisResource method putSchema.
/**
* Import Analysis Schema.
*
* <p><b>Example Request:</b><br />
* PUT pentaho/plugin/data-access/api/datasource/analysis/catalog/SampleSchema
* <br /><b>PUT data:</b>
* <pre function="syntax.xml">
* ------WebKitFormBoundaryNLNb246RTFIn1elY
* Content-Disposition: form-data; name="uploadAnalysis"; filename="SampleData2.mondrian.xml"
* Content-Type: text/xml
*
* <?xml version="1.0"?>
* <Schema name="SampleData2">
* <!-- Shared dimensions -->
*
* <Dimension name="Region">
* <Hierarchy hasAll="true" allMemberName="All Regions">
* <Table name="QUADRANT_ACTUALS"/>
* <Level name="Region" column="REGION" uniqueMembers="true"/>
* </Hierarchy>
* </Dimension>
* <Dimension name="Department">
* <Hierarchy hasAll="true" allMemberName="All Departments">
* <Table name="QUADRANT_ACTUALS"/>
* <Level name="Department" column="DEPARTMENT" uniqueMembers="true"/>
* </Hierarchy>
* </Dimension>
*
* <Dimension name="Positions">
* <Hierarchy hasAll="true" allMemberName="All Positions">
* <Table name="QUADRANT_ACTUALS"/>
* <Level name="Positions" column="POSITIONTITLE" uniqueMembers="true"/>
* </Hierarchy>
* </Dimension>
*
* <Cube name="Quadrant Analysis">
* <Table name="QUADRANT_ACTUALS"/>
* <DimensionUsage name="Region" source="Region"/>
* <DimensionUsage name="Department" source="Department" />
* <DimensionUsage name="Positions" source="Positions" />
* <Measure name="Actual" column="ACTUAL" aggregator="sum" formatString="#,###.00"/>
* <Measure name="Budget" column="BUDGET" aggregator="sum" formatString="#,###.00"/>
* <Measure name="Variance" column="VARIANCE" aggregator="sum" formatString="#,###.00"/>
* <!-- <CalculatedMember name="Variance Percent" dimension="Measures" formula="([Measures].[Variance]/[Measures].[Budget])*100" /> -->
* </Cube>
*
* </Schema>
*
* ------WebKitFormBoundaryNLNb246RTFIn1elY
* Content-Disposition: form-data; name="parameters"
*
* DataSource=SampleData2;overwrite=true
* ------WebKitFormBoundaryNLNb246RTFIn1elY
* Content-Disposition: form-data; name="schemaFileInfo"
*
* test.xml
* ------WebKitFormBoundaryNLNb246RTFIn1elY
* Content-Disposition: form-data; name="catalogName"
*
* Catalog Name
* ------WebKitFormBoundaryNLNb246RTFIn1elY
* Content-Disposition: form-data; name="xmlaEnabledFlag"
*
* true
* ------WebKitFormBoundaryNLNb246RTFIn1elY--
* </pre>
* </p>
*
* @param catalog (optional) The catalog name.
* @param uploadAnalysis A Mondrian schema XML file.
* @param schemaFileInfo User selected name for the file.
* @param origCatalogName (optional) The original catalog name.
* @param datasourceName (optional) The datasource name.
* @param overwrite Flag for overwriting existing version of the file.
* @param xmlaEnabledFlag Is XMLA enabled or not.
* @param parameters Import parameters.
* @param acl acl information for the data source. This parameter is optional.
*
* @return Response containing the success of the method.
*
* <p><b>Example Response:</b></p>
* <pre function="syntax.xml">
* 200
* </pre>
*/
@PUT
@Path("/catalog/{catalogId : .+}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
@StatusCodes({ @ResponseCode(code = 409, condition = "Content already exists (use overwrite flag to force)"), @ResponseCode(code = 401, condition = "Import failed because publish is prohibited"), @ResponseCode(code = 500, condition = "Unspecified general error has occurred"), @ResponseCode(code = 412, condition = "Analysis datasource import failed. Error code or message included in response entity"), @ResponseCode(code = 403, condition = "Access Control Forbidden"), @ResponseCode(code = 201, condition = "Indicates successful import") })
public Response putSchema(// Optional
@PathParam(CATALOG_ID) String catalog, @FormDataParam(UPLOAD_ANALYSIS) InputStream uploadAnalysis, @FormDataParam(UPLOAD_ANALYSIS) FormDataContentDisposition schemaFileInfo, // Optional
@FormDataParam(ORIG_CATALOG_NAME) String origCatalogName, // Optional
@FormDataParam(DATASOURCE_NAME) String datasourceName, @FormDataParam(OVERWRITE_IN_REPOS) Boolean overwrite, @FormDataParam(XMLA_ENABLED_FLAG) Boolean xmlaEnabledFlag, @FormDataParam(PARAMETERS) String parameters, @FormDataParam(DATASOURCE_ACL) RepositoryFileAclDto acl) throws PentahoAccessControlException {
try {
service.putMondrianSchema(uploadAnalysis, schemaFileInfo, catalog, origCatalogName, datasourceName, overwrite, xmlaEnabledFlag, parameters, acl);
Response response = Response.status(Response.Status.CREATED).build();
logger.debug("putMondrianSchema Response " + response);
return response;
} catch (PentahoAccessControlException pac) {
int statusCode = PlatformImportException.PUBLISH_USERNAME_PASSWORD_FAIL;
logger.error("Error putMondrianSchema " + pac.getMessage() + " status = " + statusCode);
throw new ResourceUtil.AccessControlException(pac.getMessage());
} catch (PlatformImportException pe) {
if (pe.getErrorStatus() == PlatformImportException.PUBLISH_PROHIBITED_SYMBOLS_ERROR) {
throw new ResourceUtil.PublishProhibitedException(pe.getMessage());
} else {
String msg = pe.getMessage();
logger.error("Error import analysis: " + msg + " status = " + pe.getErrorStatus());
Throwable throwable = pe.getCause();
if (throwable != null) {
msg = throwable.getMessage();
logger.error("Root cause: " + msg);
}
int status = pe.getErrorStatus();
if (status == 8) {
throw new ResourceUtil.ContentAlreadyExistsException(msg);
} else {
throw new ResourceUtil.ImportFailedException(msg);
}
}
} catch (Exception e) {
int statusCode = PlatformImportException.PUBLISH_GENERAL_ERROR;
logger.error("Error putMondrianSchema " + e.getMessage() + " status = " + statusCode);
throw new ResourceUtil.UnspecifiedErrorException(e.getMessage());
}
}
Aggregations