Search in sources :

Example 1 with IPlatformMimeResolver

use of org.pentaho.platform.api.mimetype.IPlatformMimeResolver in project pentaho-platform by pentaho.

the class LocaleFilesProcessor method processLocaleFiles.

public void processLocaleFiles(IPlatformImporter importer) throws PlatformImportException {
    RepositoryFileImportBundle.Builder bundleBuilder = new RepositoryFileImportBundle.Builder();
    IPlatformMimeResolver mimeResolver = PentahoSystem.get(IPlatformMimeResolver.class);
    String mimeType = mimeResolver.resolveMimeForFileName(FILE_LOCALE_RESOLVER);
    List<LocaleFileDescriptor> localeFilesToProceed = new ArrayList<>();
    for (LocaleFileDescriptor localeFile : localeFiles) {
        String extension = localeFile.getExtension();
        if (!StringUtils.isEmpty(extension) && extension.equals(LOCALE_EXT)) {
            localeFilesToProceed.add(localeFile);
        }
    }
    if (!localeFilesToProceed.isEmpty()) {
        for (LocaleFileDescriptor localeFile : localeFilesToProceed) {
            proceed(importer, bundleBuilder, mimeType, localeFile);
        }
    } else {
        for (LocaleFileDescriptor localeFile : localeFiles) {
            proceed(importer, bundleBuilder, mimeType, localeFile);
        }
    }
}
Also used : IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) ArrayList(java.util.ArrayList)

Example 2 with IPlatformMimeResolver

use of org.pentaho.platform.api.mimetype.IPlatformMimeResolver in project pentaho-platform by pentaho.

the class DefaultRepositoryVersioningManagerTest method setUp.

@Before
public void setUp() {
    IPlatformMimeResolver platformMimeResolver = mock(IPlatformMimeResolver.class);
    IMimeType mimeTypeMockFullTrue = mock(IMimeType.class);
    IMimeType mimeTypeMockPartialTrue = mock(IMimeType.class);
    IMimeType mimeTypeMockFalse = mock(IMimeType.class);
    when(mimeTypeMockFullTrue.isVersionEnabled()).thenReturn(true);
    when(mimeTypeMockFullTrue.isVersionCommentEnabled()).thenReturn(true);
    when(mimeTypeMockPartialTrue.isVersionEnabled()).thenReturn(true);
    when(mimeTypeMockPartialTrue.isVersionCommentEnabled()).thenReturn(false);
    when(mimeTypeMockFalse.isVersionEnabled()).thenReturn(false);
    when(mimeTypeMockFalse.isVersionCommentEnabled()).thenReturn(true);
    when(platformMimeResolver.resolveMimeTypeForFileName(FULL_VERSIONING_PATH)).thenReturn(mimeTypeMockFullTrue);
    when(platformMimeResolver.resolveMimeTypeForFileName(PART_VERSIONING_PATH)).thenReturn(mimeTypeMockPartialTrue);
    when(platformMimeResolver.resolveMimeTypeForFileName(NO_VERSIONING_PATH)).thenReturn(mimeTypeMockFalse);
    defaultRepositoryVersionManagerFullTrue.setMasterVersioning(true);
    defaultRepositoryVersionManagerFullTrue.setMasterVersionComments(true);
    defaultRepositoryVersionManagerFullTrue.setPlatformMimeResolver(platformMimeResolver);
    defaultRepositoryVersionManagerPartialTrue.setMasterVersioning(true);
    defaultRepositoryVersionManagerPartialTrue.setMasterVersionComments(false);
    defaultRepositoryVersionManagerPartialTrue.setPlatformMimeResolver(platformMimeResolver);
    defaultRepositoryVersionManagerFalse.setMasterVersioning(false);
    defaultRepositoryVersionManagerFalse.setMasterVersionComments(true);
    defaultRepositoryVersionManagerFalse.setPlatformMimeResolver(platformMimeResolver);
}
Also used : IMimeType(org.pentaho.platform.api.mimetype.IMimeType) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) Before(org.junit.Before)

Example 3 with IPlatformMimeResolver

use of org.pentaho.platform.api.mimetype.IPlatformMimeResolver in project pentaho-platform by pentaho.

the class RepositoryFileHelper method getFileData.

public static IRepositoryFileData getFileData(RepositoryFile repositoryFile) {
    IRepositoryContentConverterHandler converterHandler;
    Map<String, Converter> converters;
    IPlatformMimeResolver mimeResolver;
    IRepositoryFileData repositoryFileData = null;
    if (!repositoryFile.isFolder()) {
        // Get the extension
        final String ext = RepositoryFilenameUtils.getExtension(repositoryFile.getName());
        if ((ext == null) || (ext.isEmpty())) {
            return null;
        }
        // Find the converter
        // If we have not been given a handler, try PentahoSystem
        converterHandler = PentahoSystem.get(IRepositoryContentConverterHandler.class);
        // fail if we have no converter handler
        if (converterHandler == null) {
            return null;
        }
        converters = converterHandler.getConverters();
        final Converter converter = converters.get(ext);
        if (converter == null) {
            return null;
        }
        // Check the mime type
        mimeResolver = PentahoSystem.get(IPlatformMimeResolver.class);
        // fail if we have no mime resolver
        if (mimeResolver == null) {
            return null;
        }
        final String mimeType = mimeResolver.resolveMimeTypeForFileName(repositoryFile.getName()).getName();
        if ((mimeType == null) || (mimeType.isEmpty())) {
            return null;
        }
        // Get the input stream
        InputStream inputStream = converter.convert(repositoryFile.getId());
        if (inputStream == null) {
            return null;
        }
        // Get the file data
        repositoryFileData = converter.convert(inputStream, "UTF-8", mimeType);
        if (repositoryFileData == null) {
            return null;
        }
    }
    return repositoryFileData;
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) IRepositoryContentConverterHandler(org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) InputStream(java.io.InputStream) Converter(org.pentaho.platform.api.repository2.unified.Converter)

Example 4 with IPlatformMimeResolver

use of org.pentaho.platform.api.mimetype.IPlatformMimeResolver 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">
 *   &lt;html&gt;
 *   &lt;head&gt;
 *   &lt;title&gt;Repository Import Log&lt;/title&gt;
 *   &lt;/head&gt;
 *   &lt;body bgcolor="#FFFFFF" topmargin="6" leftmargin="6" style="font-family: arial,sans-serif; font-size: x-small"&gt;
 *   &lt;hr size="1" noshade&gt;
 *   Log session start time Thu Feb 26 11:04:19 BRT 2015&lt;br&gt;
 *   &lt;br&gt;
 *   &lt;table cellspacing="0" cellpadding="4" border="1" bordercolor="#224466" width="100%"&gt;
 *   &lt;tr style="background: #336699; color: #FFFFFF; text-align: left"&gt;
 *   &lt;th&gt;Import File&lt;/th&gt;
 *   &lt;th&gt;Level&lt;/th&gt;
 *   &lt;th&gt;Message&lt;/th&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public&lt;/td&gt;
 *   &lt;td title="Level"&gt;INFO&lt;/td&gt;
 *   &lt;td title="Message"&gt;Start Import Job&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public/fileNameOverriden.csv&lt;/td&gt;
 *   &lt;td title="Level"&gt;INFO&lt;/td&gt;
 *   &lt;td title="Message"&gt;Start File Import&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public/fileNameOverriden.csv&lt;/td&gt;
 *   &lt;td title="Level"&gt;&lt;font color="#993300"&gt;&lt;strong&gt;WARN&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
 *   &lt;td title="Message"&gt;fileNameOverriden.csv&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;td title="importFile"&gt;/public&lt;/td&gt;
 *   &lt;td title="Level"&gt;INFO&lt;/td&gt;
 *   &lt;td title="Message"&gt;End Import Job&lt;/td&gt;
 *   &lt;/tr&gt;
 *   &lt;/table&gt;
 *   &lt;br&gt;
 *   &lt;/body&gt;&lt;/html&gt;
 * </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();
}
Also used : FileService(org.pentaho.platform.web.http.api.resources.services.FileService) IRepositoryImportLogger(org.pentaho.platform.plugin.services.importexport.IRepositoryImportLogger) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMondrianCatalogService(org.pentaho.platform.plugin.action.mondrian.catalog.IMondrianCatalogService) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IPlatformImportBundle(org.pentaho.platform.api.repository2.unified.IPlatformImportBundle) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) Level(org.apache.log4j.Level) RepositoryFileImportBundle(org.pentaho.platform.plugin.services.importer.RepositoryFileImportBundle) IPlatformImporter(org.pentaho.platform.plugin.services.importer.IPlatformImporter) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Facet(org.codehaus.enunciate.Facet)

Example 5 with IPlatformMimeResolver

use of org.pentaho.platform.api.mimetype.IPlatformMimeResolver in project pentaho-platform by pentaho.

the class PRPTImportHandlerTest method setUp.

@Before
public void setUp() throws ObjectFactoryException, IOException {
    final IPlatformMimeResolver resolver = mock(IPlatformMimeResolver.class);
    pentahoObjectFactory = mock(IPentahoObjectFactory.class);
    when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactory.get(eq(IPlatformMimeResolver.class), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return resolver;
        }
    });
    when(pentahoObjectFactory.get(eq(IPlatformImporter.class), anyString(), any(IPentahoSession.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return importer;
        }
    });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);
    InputStream stream = new ByteArrayInputStream(SAMPLE_STREAM.getBytes());
    bundle = mock(RepositoryFileImportBundle.class);
    when(bundle.getName()).thenReturn(SAMPLE_NAME);
    when(bundle.getInputStream()).thenReturn(stream);
    Log logger = mock(Log.class);
    ImportSession session = mock(ImportSession.class);
    doReturn(logger).when(session).getLogger();
    IUnifiedRepository repository = mock(IUnifiedRepository.class);
    List<IMimeType> mimeTypes = Arrays.asList(mock(IMimeType.class));
    handler = spy(new PRPTImportHandler(mimeTypes));
    doReturn(session).when(handler).getImportSession();
    handler.setRepository(repository);
}
Also used : ImportSession(org.pentaho.platform.plugin.services.importexport.ImportSession) IPentahoObjectFactory(org.pentaho.platform.api.engine.IPentahoObjectFactory) Log(org.apache.commons.logging.Log) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IMimeType(org.pentaho.platform.api.mimetype.IMimeType) IPlatformMimeResolver(org.pentaho.platform.api.mimetype.IPlatformMimeResolver) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Before(org.junit.Before)

Aggregations

IPlatformMimeResolver (org.pentaho.platform.api.mimetype.IPlatformMimeResolver)6 Before (org.junit.Before)3 IMimeType (org.pentaho.platform.api.mimetype.IMimeType)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Converter (org.pentaho.platform.api.repository2.unified.Converter)2 IRepositoryContentConverterHandler (org.pentaho.platform.api.repository2.unified.IRepositoryContentConverterHandler)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Log (org.apache.commons.logging.Log)1 Level (org.apache.log4j.Level)1 Facet (org.codehaus.enunciate.Facet)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 IPentahoObjectFactory (org.pentaho.platform.api.engine.IPentahoObjectFactory)1 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)1