Search in sources :

Example 46 with SimpleRepositoryFileData

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

the class RepositoryUtilsTest method testGetFile.

@Test
public void testGetFile() throws Exception {
    final MockUnifiedRepository repository = new MockUnifiedRepository(new SpringSecurityCurrentUserProvider());
    final RepositoryUtils repositoryUtils = new RepositoryUtils(repository);
    final SimpleRepositoryFileData data = new SimpleRepositoryFileData(new ByteArrayInputStream("Test".getBytes()), "UTF-8", "text/plain");
    RepositoryFile test = repositoryUtils.getFile("/public/one/two/three.prpt", data, true, true, null);
    assertNotNull(test);
    assertEquals("The filename is invalid", "three.prpt", test.getName());
    assertEquals("The path is invalid", "/public/one/two/three.prpt", test.getPath());
    assertFalse("The file should not be defined as a folder", test.isFolder());
    // Make sure it created the parents
    RepositoryFile one = repositoryUtils.getFolder("/public/one", false, false, null);
    assertNotNull(one);
    RepositoryFile two = repositoryUtils.getFolder("/public/one/two", false, false, null);
    assertNotNull(two);
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) SpringSecurityCurrentUserProvider(org.pentaho.test.platform.repository2.unified.MockUnifiedRepository.SpringSecurityCurrentUserProvider) MockUnifiedRepository(org.pentaho.test.platform.repository2.unified.MockUnifiedRepository) Test(org.junit.Test)

Example 47 with SimpleRepositoryFileData

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

the class RepositoryFileOutputStreamTest method convertTest.

@Test
public void convertTest() throws Exception {
    RepositoryFileOutputStream spy = Mockito.spy(new RepositoryFileOutputStream("1.ktr", "UTF-8"));
    Converter converter = Mockito.mock(Converter.class);
    ByteArrayInputStream bis = Mockito.mock(ByteArrayInputStream.class);
    Mockito.doReturn(Mockito.mock(NodeRepositoryFileData.class)).when(converter).convert(bis, "UTF-8", "");
    IRepositoryFileData data = spy.convert(null, bis, "");
    Assert.assertTrue(data instanceof SimpleRepositoryFileData);
    data = spy.convert(converter, bis, "");
    Assert.assertTrue(data instanceof NodeRepositoryFileData);
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) NodeRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData) Converter(org.pentaho.platform.api.repository2.unified.Converter) Test(org.junit.Test)

Example 48 with SimpleRepositoryFileData

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

the class PentahoMetadataDomainRepository method storeDomain.

@Override
public void storeDomain(InputStream inputStream, String domainId, boolean overwrite, RepositoryFileAcl acl) throws DomainIdNullException, DomainAlreadyExistsException, DomainStorageException {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("storeDomain(inputStream, %s, %s, %s)", domainId, overwrite, acl));
    }
    if (null == inputStream) {
        throw new IllegalArgumentException();
    }
    if (StringUtils.isEmpty(domainId)) {
        throw new DomainIdNullException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0001_DOMAIN_ID_NULL"));
    }
    // Check to see if the domain already exists
    RepositoryFile domainFile = getMetadataRepositoryFile(domainId);
    if (domainFile == null && domainId.endsWith(XMI_EXTENSION)) {
        domainFile = getMetadataRepositoryFile(domainId.substring(0, domainId.length() - XMI_EXTENSION.length()));
    }
    if (!overwrite && domainFile != null) {
        final String errorString = messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0002_DOMAIN_ALREADY_EXISTS", domainId);
        logger.error(errorString);
        throw new DomainAlreadyExistsException(errorString);
    }
    // Check if this is valid xml
    InputStream inputStream2;
    String xmi;
    try {
        // try to see if the xmi can be parsed (ie, check if it's valid xmi)
        // first, convert our input stream to a string
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_ENCODING));
        try {
            while ((xmi = reader.readLine()) != null) {
                stringBuilder.append(xmi);
            }
        } finally {
            inputStream.close();
        }
        if (!isDomainIdXmiEqualsOrNotPresent(domainId, getDomainIdFromXmi(stringBuilder))) {
            domainId = replaceDomainId(stringBuilder, domainId);
        }
        xmi = stringBuilder.toString();
        // now, try to see if the xmi can be parsed (ie, check if it's valid xmi)
        byte[] xmiBytes = xmi.getBytes(DEFAULT_ENCODING);
        inputStream2 = new java.io.ByteArrayInputStream(xmiBytes);
        xmiParser.parseXmi(inputStream2);
        // xmi is valid. Create a new inputstream for the actual import action.
        inputStream2.reset();
    } catch (Exception ex) {
        logger.error(ex.getMessage());
        // throw new
        // DomainStorageException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0010_ERROR_PARSING_XMI"),
        // ex);
        java.io.ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ex.printStackTrace(new java.io.PrintStream(byteArrayOutputStream));
        throw new DomainStorageException(byteArrayOutputStream.toString(), ex);
    }
    final SimpleRepositoryFileData data = new SimpleRepositoryFileData(inputStream2, DEFAULT_ENCODING, DOMAIN_MIME_TYPE);
    final RepositoryFile newDomainFile;
    if (domainFile == null) {
        newDomainFile = createUniqueFile(domainId, null, data);
    } else {
        newDomainFile = repository.updateFile(domainFile, data, null);
    }
    // This invalidates any caching
    flushDomains();
    getAclHelper().setAclFor(newDomainFile, acl);
}
Also used : InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) RepositoryFileInputStream(org.pentaho.platform.repository2.unified.fileio.RepositoryFileInputStream) InputStream(java.io.InputStream) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) BufferedReader(java.io.BufferedReader) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 49 with SimpleRepositoryFileData

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

the class PentahoMetadataDomainRepository method createUniqueFile.

/**
 * Creates a new repository file (with the supplied data) and applies the proper metadata to this file.
 *
 * @param domainId the Domain id associated with this file
 * @param locale   the locale associated with this file (or null for a domain file)
 * @param data     the data to put in the file
 * @return the repository file created
 */
protected RepositoryFile createUniqueFile(final String domainId, final String locale, final SimpleRepositoryFileData data) {
    // Generate a "unique" filename
    final String filename = UUID.randomUUID().toString();
    // Create the new file
    final RepositoryFile file = repository.createFile(getMetadataDir().getId(), new RepositoryFile.Builder(filename).build(), data, null);
    // Add metadata to the file
    final Map<String, Serializable> metadataMap = new HashMap<String, Serializable>();
    metadataMap.put(PROPERTY_NAME_DOMAIN_ID, domainId);
    if (StringUtils.isEmpty(locale)) {
        // This is a domain file
        metadataMap.put(PROPERTY_NAME_TYPE, TYPE_DOMAIN);
    } else {
        // This is a locale property file
        metadataMap.put(PROPERTY_NAME_TYPE, TYPE_LOCALE);
        metadataMap.put(PROPERTY_NAME_LOCALE, locale);
    }
    // Update the metadata
    repository.setFileMetadata(file.getId(), metadataMap);
    return file;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 50 with SimpleRepositoryFileData

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

the class PentahoMetadataDomainRepository method createOrUpdateAnnotationsXml.

public void createOrUpdateAnnotationsXml(final RepositoryFile domainFile, final RepositoryFile annotationsFile, final String annotationsXml) {
    if (domainFile == null) {
        // exit early
        return;
    }
    try {
        ByteArrayInputStream in = new ByteArrayInputStream(annotationsXml.getBytes(DEFAULT_ENCODING));
        final SimpleRepositoryFileData data = new SimpleRepositoryFileData(in, DEFAULT_ENCODING, DOMAIN_MIME_TYPE);
        if (annotationsFile == null) {
            // Generate a filename based on the domainId
            final String filename = domainFile.getId() + ANNOTATIONS_FILE_ID_POSTFIX;
            // Create the new file
            getRepository().createFile(getMetadataDir().getId(), new RepositoryFile.Builder(filename).build(), data, null);
        } else {
            getRepository().updateFile(annotationsFile, data, null);
        }
    } catch (Exception e) {
        getLogger().warn("Unable to save annotations xml", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) PentahoAccessControlException(org.pentaho.platform.api.engine.PentahoAccessControlException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) 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