Search in sources :

Example 1 with PlatformImportException

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
    }
}
Also used : PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) InputStream(java.io.InputStream) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Test(org.junit.Test)

Example 2 with PlatformImportException

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);
}
Also used : Response(javax.ws.rs.core.Response) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringInputStream(org.apache.tools.ant.filters.StringInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDataBodyPart(com.sun.jersey.multipart.FormDataBodyPart) ArrayList(java.util.ArrayList) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) Matchers.anyString(org.mockito.Matchers.anyString) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) Test(org.junit.Test)

Example 3 with PlatformImportException

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);
}
Also used : Response(javax.ws.rs.core.Response) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) InputStream(java.io.InputStream) FormDataContentDisposition(com.sun.jersey.core.header.FormDataContentDisposition) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) Test(org.junit.Test)

Example 4 with PlatformImportException

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);
}
Also used : IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) FileResource(org.pentaho.platform.web.http.api.resources.FileResource) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter)

Example 5 with PlatformImportException

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=&quot;uploadAnalysis&quot;; filename=&quot;SampleData2.mondrian.xml&quot;
 *      Content-Type: text/xml
 *
 *      &lt;?xml version=&quot;1.0&quot;?&gt;
 *      &lt;Schema name=&quot;SampleData2&quot;&gt;
 *      &lt;!-- Shared dimensions --&gt;
 *
 *      &lt;Dimension name=&quot;Region&quot;&gt;
 *      &lt;Hierarchy hasAll=&quot;true&quot; allMemberName=&quot;All Regions&quot;&gt;
 *      &lt;Table name=&quot;QUADRANT_ACTUALS&quot;/&gt;
 *      &lt;Level name=&quot;Region&quot; column=&quot;REGION&quot; uniqueMembers=&quot;true&quot;/&gt;
 *      &lt;/Hierarchy&gt;
 *      &lt;/Dimension&gt;
 *      &lt;Dimension name=&quot;Department&quot;&gt;
 *      &lt;Hierarchy hasAll=&quot;true&quot; allMemberName=&quot;All Departments&quot;&gt;
 *      &lt;Table name=&quot;QUADRANT_ACTUALS&quot;/&gt;
 *      &lt;Level name=&quot;Department&quot; column=&quot;DEPARTMENT&quot; uniqueMembers=&quot;true&quot;/&gt;
 *      &lt;/Hierarchy&gt;
 *      &lt;/Dimension&gt;
 *
 *      &lt;Dimension name=&quot;Positions&quot;&gt;
 *      &lt;Hierarchy hasAll=&quot;true&quot; allMemberName=&quot;All Positions&quot;&gt;
 *      &lt;Table name=&quot;QUADRANT_ACTUALS&quot;/&gt;
 *      &lt;Level name=&quot;Positions&quot; column=&quot;POSITIONTITLE&quot; uniqueMembers=&quot;true&quot;/&gt;
 *      &lt;/Hierarchy&gt;
 *      &lt;/Dimension&gt;
 *
 *      &lt;Cube name=&quot;Quadrant Analysis&quot;&gt;
 *      &lt;Table name=&quot;QUADRANT_ACTUALS&quot;/&gt;
 *      &lt;DimensionUsage name=&quot;Region&quot; source=&quot;Region&quot;/&gt;
 *      &lt;DimensionUsage name=&quot;Department&quot; source=&quot;Department&quot; /&gt;
 *      &lt;DimensionUsage name=&quot;Positions&quot; source=&quot;Positions&quot; /&gt;
 *      &lt;Measure name=&quot;Actual&quot; column=&quot;ACTUAL&quot; aggregator=&quot;sum&quot; formatString=&quot;#,###.00&quot;/&gt;
 *      &lt;Measure name=&quot;Budget&quot; column=&quot;BUDGET&quot; aggregator=&quot;sum&quot; formatString=&quot;#,###.00&quot;/&gt;
 *      &lt;Measure name=&quot;Variance&quot; column=&quot;VARIANCE&quot; aggregator=&quot;sum&quot; formatString=&quot;#,###.00&quot;/&gt;
 *      &lt;!--    &lt;CalculatedMember name=&quot;Variance Percent&quot; dimension=&quot;Measures&quot; formula=&quot;([Measures].[Variance]/[Measures].[Budget])*100&quot; /&gt; --&gt;
 *      &lt;/Cube&gt;
 *
 *      &lt;/Schema&gt;
 *
 *      ------WebKitFormBoundaryNLNb246RTFIn1elY
 *      Content-Disposition: form-data; name=&quot;parameters&quot;
 *
 *      DataSource=SampleData2;overwrite=true
 *      ------WebKitFormBoundaryNLNb246RTFIn1elY
 *      Content-Disposition: form-data; name=&quot;schemaFileInfo&quot;
 *
 *      test.xml
 *      ------WebKitFormBoundaryNLNb246RTFIn1elY
 *      Content-Disposition: form-data; name=&quot;catalogName&quot;
 *
 *      Catalog Name
 *      ------WebKitFormBoundaryNLNb246RTFIn1elY
 *      Content-Disposition: form-data; name=&quot;xmlaEnabledFlag&quot;
 *
 *      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());
    }
}
Also used : Response(javax.ws.rs.core.Response) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) FileNotFoundException(java.io.FileNotFoundException) RepositoryException(org.pentaho.platform.api.repository.RepositoryException) PlatformImportException(org.pentaho.platform.plugin.services.importer.PlatformImportException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) StatusCodes(org.codehaus.enunciate.jaxrs.StatusCodes) PUT(javax.ws.rs.PUT)

Aggregations

PlatformImportException (org.pentaho.platform.plugin.services.importer.PlatformImportException)11 Response (javax.ws.rs.core.Response)7 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)7 InputStream (java.io.InputStream)6 Test (org.junit.Test)6 FormDataContentDisposition (com.sun.jersey.core.header.FormDataContentDisposition)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileResource (org.pentaho.platform.web.http.api.resources.FileResource)3 FormDataBodyPart (com.sun.jersey.multipart.FormDataBodyPart)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 Consumes (javax.ws.rs.Consumes)2 PUT (javax.ws.rs.PUT)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 RepositoryException (org.pentaho.platform.api.repository.RepositoryException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 StringInputStream (org.apache.tools.ant.filters.StringInputStream)1