Search in sources :

Example 6 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class ZipExportProcessorTest method init.

@Before
public void init() throws IOException, PlatformInitializationException, DomainIdNullException, DomainAlreadyExistsException, DomainStorageException {
    List<Locale> availableLocales = java.util.Collections.singletonList(LOCALE_DEFAULT);
    Properties localePropertries = new Properties();
    localePropertries.setProperty("name1", "value1");
    final RepositoryFile file0 = new RepositoryFile.Builder("").path("/").id("/").folder(true).build();
    final RepositoryFile file1 = new RepositoryFile.Builder("home").path("/home/").id("/home/").folder(true).build();
    final RepositoryFile file2 = new RepositoryFile.Builder("test user").path("/home/test user/").id("/home/test user/").folder(true).build();
    final RepositoryFile file3 = new RepositoryFile.Builder("two words").path("/home/test user/two words/").id("/home/test user/two words/").folder(true).build();
    final RepositoryFile fileX = new RepositoryFile.Builder("eval (+)%.prpt").path("/home/test user/two words/eval (+)%.prpt").id("/home/test user/two words/eval (+)%.prpt").folder(false).build();
    final RepositoryFile[] repoFiles = new RepositoryFile[] { file0, file1, file2, file3, fileX };
    final Map<Serializable, RepositoryFile> repoFilesMap = new HashMap<Serializable, RepositoryFile>();
    for (RepositoryFile f : repoFiles) {
        repoFilesMap.put(f.getId(), f);
    }
    repo = mock(IUnifiedRepository.class);
    final Answer<RepositoryFile> answerRepoGetFile = new Answer<RepositoryFile>() {

        @Override
        public RepositoryFile answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            final Object fileId = args[0];
            return getRepoFile(repoFilesMap, fileId);
        }
    };
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFile(anyString());
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFile(anyString(), Mockito.anyBoolean());
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFile(anyString(), Mockito.anyBoolean(), any(IPentahoLocale.class));
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFile(anyString(), any(IPentahoLocale.class));
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFileById(any(Serializable.class));
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFileById(any(Serializable.class), Mockito.anyBoolean());
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFileById(any(Serializable.class), Mockito.anyBoolean(), any(IPentahoLocale.class));
    Mockito.doAnswer(answerRepoGetFile).when(repo).getFileById(any(Serializable.class), any(IPentahoLocale.class));
    Answer<List<RepositoryFile>> answerRepoGetChildren = new Answer<List<RepositoryFile>>() {

        @Override
        public List<RepositoryFile> answer(InvocationOnMock invocation) throws Throwable {
            // returns the following item from <repoFiles>
            RepositoryRequest r = (RepositoryRequest) invocation.getArguments()[0];
            String path = r.getPath();
            RepositoryFile thisFile = getRepoFile(repoFilesMap, path);
            if (thisFile == null || !thisFile.isFolder()) {
                return Collections.emptyList();
            }
            for (int i = 0, n = repoFiles.length - 1; i < n; i++) {
                RepositoryFile iFile = repoFiles[i];
                if (iFile == thisFile || iFile.getId().equals(thisFile.getId())) {
                    return Collections.singletonList(repoFiles[i + 1]);
                }
            }
            return Collections.emptyList();
        }
    };
    Mockito.doAnswer(answerRepoGetChildren).when(repo).getChildren(any(RepositoryRequest.class));
    doReturn(availableLocales).when(repo).getAvailableLocalesForFile(Mockito.any(RepositoryFile.class));
    doReturn(availableLocales).when(repo).getAvailableLocalesForFileById(Mockito.any(Serializable.class));
    doReturn(availableLocales).when(repo).getAvailableLocalesForFileByPath(Mockito.any(String.class));
    doReturn(localePropertries).when(repo).getLocalePropertiesForFileById(Mockito.any(File.class), Mockito.anyString());
    RepositoryFileSid sid = mock(RepositoryFileSid.class);
    doReturn("testUser").when(sid).getName();
    doReturn(Type.USER).when(sid).getType();
    final RepositoryFileAcl mockAcl = mock(RepositoryFileAcl.class);
    doReturn(sid).when(mockAcl).getOwner();
    doReturn(mockAcl).when(repo).getAcl(any(Serializable.class));
    Answer<IRepositoryFileData> answerGetDataForRead = new Answer<IRepositoryFileData>() {

        @Override
        public IRepositoryFileData answer(InvocationOnMock invocation) throws Throwable {
            Serializable id = (Serializable) invocation.getArguments()[0];
            RepositoryFile file = getRepoFile(repoFilesMap, id);
            if (!file.isFolder()) {
                return new SimpleRepositoryFileData(new ByteArrayInputStream(new byte[0]), "UTF-8", MIME_PRPT.getName());
            }
            return null;
        }
    };
    doAnswer(answerGetDataForRead).when(repo).getDataForRead(Mockito.any(Serializable.class), Mockito.any(Class.class));
    exportHandler = new DefaultExportHandler();
    defaultConverter = new StreamConverter(repo);
    PentahoSystem.clearObjectFactory();
    microPlatform = new MicroPlatform(getSolutionPath());
    microPlatform.defineInstance(IUnifiedRepository.class, repo);
    // microPlatform.defineInstance( IPlatformMimeResolver.class, mimeResolver );
    microPlatform.defineInstance(ISolutionEngine.class, Mockito.mock(SolutionEngine.class));
    microPlatform.defineInstance(IDatasourceMgmtService.class, Mockito.mock(IDatasourceMgmtService.class));
    microPlatform.start();
    exportSession = new StandaloneSession();
    PentahoSessionHolder.setStrategyName(PentahoSessionHolder.MODE_GLOBAL);
    PentahoSessionHolder.setSession(exportSession);
}
Also used : Locale(java.util.Locale) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) Serializable(java.io.Serializable) HashMap(java.util.HashMap) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) Matchers.anyString(org.mockito.Matchers.anyString) Properties(java.util.Properties) IDatasourceMgmtService(org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService) RepositoryFileSid(org.pentaho.platform.api.repository2.unified.RepositoryFileSid) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) MicroPlatform(org.pentaho.test.platform.engine.core.MicroPlatform) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) List(java.util.List) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) SolutionEngine(org.pentaho.platform.engine.services.solution.SolutionEngine) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) IPentahoLocale(org.pentaho.platform.api.locale.IPentahoLocale) RepositoryRequest(org.pentaho.platform.api.repository2.unified.RepositoryRequest) AfterClass(org.junit.AfterClass) BeforeClass(org.junit.BeforeClass) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) File(java.io.File) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository) Before(org.junit.Before)

Example 7 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class RepositoryFileReader method getEncoding.

protected static String getEncoding(RepositoryFile file) throws FileNotFoundException {
    IUnifiedRepository repository = PentahoSystem.get(IUnifiedRepository.class);
    SimpleRepositoryFileData fileData = repository.getDataForRead(file.getId(), SimpleRepositoryFileData.class);
    return fileData.getEncoding();
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository)

Example 8 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class RepositoryFileReader method getEncoding.

protected static String getEncoding(Serializable id) throws FileNotFoundException {
    IUnifiedRepository repository = PentahoSystem.get(IUnifiedRepository.class);
    RepositoryFile file = (new RepositoryFileInputStream(id)).getFile();
    SimpleRepositoryFileData fileData = repository.getDataForRead(file.getId(), SimpleRepositoryFileData.class);
    return fileData.getEncoding();
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository)

Example 9 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class ActionUtil method sendEmail.

/**
 * Sends an email with the file representing the provided {@code filePath}  as an attachment. All information
 * needed to send the email (to, from, cc, bcc etc) is expected to be proviced in the {@code actionParams}
 * {@link Map}.
 *
 * @param actionParams a {@link Map} of parameters needed to send the email
 * @param params       a {@link Map} of parameter used to invoke the action
 * @param filePath     the path of the repository file that was generated when the action was invoked
 */
public static void sendEmail(Map<String, Object> actionParams, Map<String, Serializable> params, String filePath) {
    try {
        IUnifiedRepository repo = PentahoSystem.get(IUnifiedRepository.class);
        RepositoryFile sourceFile = repo.getFile(filePath);
        // add metadata
        Map<String, Serializable> metadata = repo.getFileMetadata(sourceFile.getId());
        String lineageId = (String) params.get(ActionUtil.QUARTZ_LINEAGE_ID);
        metadata.put(ActionUtil.QUARTZ_LINEAGE_ID, lineageId);
        repo.setFileMetadata(sourceFile.getId(), metadata);
        // send email
        SimpleRepositoryFileData data = repo.getDataForRead(sourceFile.getId(), SimpleRepositoryFileData.class);
        // if email is setup and we have tos, then do it
        Emailer emailer = new Emailer();
        if (!emailer.setup()) {
            // email not configured
            return;
        }
        String to = (String) actionParams.get("_SCH_EMAIL_TO");
        String cc = (String) actionParams.get("_SCH_EMAIL_CC");
        String bcc = (String) actionParams.get("_SCH_EMAIL_BCC");
        if ((to == null || "".equals(to)) && (cc == null || "".equals(cc)) && (bcc == null || "".equals(bcc))) {
            // no destination
            return;
        }
        emailer.setTo(to);
        emailer.setCc(cc);
        emailer.setBcc(bcc);
        emailer.setAttachment(data.getInputStream());
        emailer.setAttachmentName("attachment");
        String attachmentName = (String) actionParams.get("_SCH_EMAIL_ATTACHMENT_NAME");
        if (attachmentName != null && !"".equals(attachmentName)) {
            String path = filePath;
            if (path.endsWith(".*")) {
                path = path.replace(".*", "");
            }
            String extension = MimeHelper.getExtension(data.getMimeType());
            if (extension == null) {
                extension = ".bin";
            }
            if (!attachmentName.endsWith(extension)) {
                emailer.setAttachmentName(attachmentName + extension);
            } else {
                emailer.setAttachmentName(attachmentName);
            }
        } else if (data != null) {
            String path = filePath;
            if (path.endsWith(".*")) {
                path = path.replace(".*", "");
            }
            String extension = MimeHelper.getExtension(data.getMimeType());
            if (extension == null) {
                extension = ".bin";
            }
            path = path.substring(path.lastIndexOf("/") + 1, path.length());
            if (!path.endsWith(extension)) {
                emailer.setAttachmentName(path + extension);
            } else {
                emailer.setAttachmentName(path);
            }
        }
        if (data == null || data.getMimeType() == null || "".equals(data.getMimeType())) {
            emailer.setAttachmentMimeType("binary/octet-stream");
        } else {
            emailer.setAttachmentMimeType(data.getMimeType());
        }
        String subject = (String) actionParams.get("_SCH_EMAIL_SUBJECT");
        if (subject != null && !"".equals(subject)) {
            emailer.setSubject(subject);
        } else {
            emailer.setSubject("Pentaho Scheduler: " + emailer.getAttachmentName());
        }
        String message = (String) actionParams.get("_SCH_EMAIL_MESSAGE");
        if (subject != null && !"".equals(subject)) {
            emailer.setBody(message);
        }
        emailer.send();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
    }
}
Also used : Serializable(java.io.Serializable) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) ActionInvocationException(org.pentaho.platform.api.action.ActionInvocationException) PluginBeanException(org.pentaho.platform.api.engine.PluginBeanException) IUnifiedRepository(org.pentaho.platform.api.repository2.unified.IUnifiedRepository)

Example 10 with SimpleRepositoryFileData

use of org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData in project pentaho-platform by pentaho.

the class RepositoryResource method extractUrl.

protected String extractUrl(RepositoryFile file) {
    SimpleRepositoryFileData data = null;
    data = repository.getDataForRead(file.getId(), SimpleRepositoryFileData.class);
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(data.getInputStream(), writer);
    } catch (IOException e) {
        // $NON-NLS-1$
        return "";
    }
    String props = writer.toString();
    // $NON-NLS-1$
    StringTokenizer tokenizer = new StringTokenizer(props, "\n");
    while (tokenizer.hasMoreTokens()) {
        String line = tokenizer.nextToken();
        int pos = line.indexOf('=');
        if (pos > 0) {
            String propname = line.substring(0, pos);
            String value = line.substring(pos + 1);
            if ((value != null) && (value.length() > 0) && (value.charAt(value.length() - 1) == '\r')) {
                value = value.substring(0, value.length() - 1);
            }
            if ("URL".equalsIgnoreCase(propname)) {
                // $NON-NLS-1$
                return value;
            }
        }
    }
    // $NON-NLS-1$
    return "";
}
Also used : StringTokenizer(java.util.StringTokenizer) StringWriter(java.io.StringWriter) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) IOException(java.io.IOException)

Aggregations

SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)58 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)43 ByteArrayInputStream (java.io.ByteArrayInputStream)31 Test (org.junit.Test)25 Matchers.anyString (org.mockito.Matchers.anyString)18 ITenant (org.pentaho.platform.api.mt.ITenant)17 InputStream (java.io.InputStream)16 IOException (java.io.IOException)14 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)12 Serializable (java.io.Serializable)10 File (java.io.File)9 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)9 FileNotFoundException (java.io.FileNotFoundException)5 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)5 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)5 VersionSummary (org.pentaho.platform.api.repository2.unified.VersionSummary)5 FileOutputStream (java.io.FileOutputStream)4 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)4 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)4 RepositoryRequest (org.pentaho.platform.api.repository2.unified.RepositoryRequest)4