Search in sources :

Example 51 with SimpleRepositoryFileData

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

the class PentahoMetadataDomainRepository method addLocalizationFile.

@Override
public void addLocalizationFile(final String domainId, final String locale, final InputStream inputStream, final boolean overwrite) throws DomainStorageException {
    if (logger.isDebugEnabled()) {
        logger.debug("addLocalizationFile(" + domainId + ", " + locale + ", inputStream)");
    }
    if (null != inputStream) {
        if (StringUtils.isEmpty(domainId) || StringUtils.isEmpty(locale)) {
            throw new IllegalArgumentException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0004_DOMAIN_ID_INVALID", domainId));
        }
        lock.writeLock().lock();
        try {
            // Check for duplicates
            final RepositoryFile localeFile = metadataMapping.getLocaleFile(domainId, locale);
            if (!overwrite && localeFile != null) {
                throw new DomainStorageException(messages.getErrorString("PentahoMetadataDomainRepository.ERROR_0009_LOCALE_ALREADY_EXISTS", domainId, locale), null);
            }
            final SimpleRepositoryFileData data = new SimpleRepositoryFileData(inputStream, DEFAULT_ENCODING, LOCALE_MIME_TYPE);
            if (localeFile == null) {
                final RepositoryFile newLocaleFile = createUniqueFile(domainId, locale, data);
                metadataMapping.addLocale(domainId, locale, newLocaleFile);
            } else {
                repository.updateFile(localeFile, data, null);
            }
            // This invalidates any cached information
            flushDomains();
        } finally {
            lock.writeLock().unlock();
        }
    }
}
Also used : DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 52 with SimpleRepositoryFileData

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

the class ActionSequenceJCRHelper method getURL.

public String getURL(String filePath) {
    RepositoryFile file = repository.getFile(filePath);
    if (file == null || !file.getName().endsWith(".url")) {
        // $NON-NLS-1$
        return "";
    }
    SimpleRepositoryFileData data = null;
    data = repository.getDataForRead(file.getId(), SimpleRepositoryFileData.class);
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(data.getStream(), 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;
            }
        }
    }
    // No URL found
    return "";
}
Also used : StringTokenizer(java.util.StringTokenizer) StringWriter(java.io.StringWriter) SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IOException(java.io.IOException)

Example 53 with SimpleRepositoryFileData

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

the class ActionSequenceJCRHelper method getLocaleText.

protected String getLocaleText(final String key, final RepositoryFile file) throws IOException {
    if (file != null) {
        SimpleRepositoryFileData data = null;
        data = repository.getDataForRead(file.getId(), SimpleRepositoryFileData.class);
        Properties p = new Properties();
        p.load(data.getStream());
        String localeText = p.getProperty(key.substring(1));
        if (localeText == null) {
            localeText = p.getProperty(key);
        }
        if (localeText != null) {
            return localeText;
        }
    }
    return null;
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) Properties(java.util.Properties)

Example 54 with SimpleRepositoryFileData

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

the class ActionSequenceJCRHelper method getSolutionDocument.

public Document getSolutionDocument(final String documentPath, final RepositoryFilePermission actionOperation) {
    RepositoryFile file = repository.getFile(documentPath);
    Document document = null;
    SimpleRepositoryFileData data = null;
    if (file != null) {
        data = repository.getDataForRead(file.getId(), SimpleRepositoryFileData.class);
        if (data != null) {
            try {
                document = XmlDom4JHelper.getDocFromStream(data.getStream());
            } catch (Throwable t) {
                logger.error(Messages.getInstance().getErrorString("ActionSequenceJCRHelper.ERROR_0017_INVALID_XML_DOCUMENT", documentPath), // $NON-NLS-1$
                t);
                return null;
            }
        } else {
            logger.error(Messages.getInstance().getErrorString("ActionSequenceJCRHelper.ERROR_0019_NO_DATA_IN_FILE", // $NON-NLS-1$
            file.getName()));
            return null;
        }
        if ((document == null) && (file != null) && (data != null)) {
            // the document exists but cannot be parsed
            logger.error(Messages.getInstance().getErrorString("ActionSequenceJCRHelper.ERROR_0009_INVALID_DOCUMENT", // $NON-NLS-1$
            documentPath));
            return null;
        }
        localizeDoc(document, file);
    }
    return document;
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Document(org.dom4j.Document)

Example 55 with SimpleRepositoryFileData

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

the class SimpleRepositoryFileDataTest method testNoEncoding.

@Test
public void testNoEncoding() {
    file = new SimpleRepositoryFileData(inputStreamSpy, "", MIME_TYPE);
    assertNotNull(file.toString());
    verify(inputStreamSpy).markSupported();
    verify(inputStreamSpy).mark(Integer.MAX_VALUE);
    try {
        verify(inputStreamSpy, atLeastOnce()).read(any(byte[].class));
        verify(inputStreamSpy).reset();
    } catch (Exception e) {
        fail("No exception should be thrown.");
    }
}
Also used : SimpleRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData) IOException(java.io.IOException) Test(org.junit.Test)

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