Search in sources :

Example 51 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class LocaleFilesProcessor method isLocaleFile.

/**
 * @param file
 * @param parentPath
 * @param bytes
 * @return false - means discard the file extension type
 * @throws IOException
 */
public boolean isLocaleFile(IRepositoryFileBundle file, String parentPath, byte[] bytes) throws IOException {
    boolean isLocale = false;
    String fileName = file.getFile().getName();
    String actualFilePath = file.getPath();
    RepositoryFile localeRepositoryFile = file.getFile();
    if (ImportSession.getSession().getManifest() != null && ImportSession.getSession().getManifest().getManifestInformation().getManifestVersion() != null) {
        fileName = ExportFileNameEncoder.decodeZipFileName(fileName);
        actualFilePath = ExportFileNameEncoder.decodeZipFileName(actualFilePath);
        localeRepositoryFile = new RepositoryFile.Builder(localeRepositoryFile).name(ExportFileNameEncoder.decodeZipFileName(localeRepositoryFile.getName())).build();
    }
    // 0 = Not a local file, 1 = 4.8 .properties file, 2= Sugar 5.0 .local file
    int sourceVersion = 0;
    if (fileName.endsWith(PROPERTIES_EXT)) {
        sourceVersion = 1;
    } else if (fileName.endsWith(LOCALE_EXT)) {
        sourceVersion = 2;
    }
    if (sourceVersion != 0) {
        InputStream inputStream = new ByteArrayInputStream(bytes);
        Properties properties = loadProperties(inputStream);
        String name = getProperty(properties, NAME, sourceVersion);
        String title = getProperty(properties, TITLE, sourceVersion);
        String description = getProperty(properties, DESCRIPTION, sourceVersion);
        String url_name = getProperty(properties, URL_NAME, sourceVersion);
        String url_description = getProperty(properties, URL_DESCRIPTION, sourceVersion);
        if (!StringUtils.isEmpty(url_name)) {
            name = url_name;
        }
        if (!StringUtils.isEmpty(title)) {
            name = title;
        }
        description = !StringUtils.isEmpty(description) ? description : "";
        if (!StringUtils.isEmpty(url_description)) {
            description = url_description;
        }
        if (!StringUtils.isEmpty(name)) {
            String filePath = (actualFilePath.equals("/") || actualFilePath.equals("\\")) ? "" : actualFilePath;
            filePath = RepositoryFilenameUtils.concat(parentPath, filePath);
            LocaleFileDescriptor localeFile;
            switch(sourceVersion) {
                case 1:
                    localeFile = new LocaleFileDescriptor(name, PROPERTIES_EXT, description, filePath, localeRepositoryFile, inputStream);
                    break;
                case 2:
                    localeFile = new LocaleFileDescriptor(name, LOCALE_EXT, description, filePath, localeRepositoryFile, inputStream);
                    break;
                default:
                    localeFile = new LocaleFileDescriptor(name, description, filePath, localeRepositoryFile, inputStream);
            }
            localeFiles.add(localeFile);
            /**
             * assumes that the properties file has additional localization attributes and should be imported
             */
            if (properties.size() <= 2 || sourceVersion == 2) {
                isLocale = true;
            }
        }
    }
    return isLocale;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) Properties(java.util.Properties)

Example 52 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class Exporter method exportDirectoryAsZip.

/**
 * @param repositoryDir
 * @param zos
 */
private void exportDirectoryAsZip(RepositoryFile repositoryDir, ZipOutputStream zos) throws IOException {
    List<RepositoryFile> children = unifiedRepository.getChildren(repositoryDir.getId());
    for (RepositoryFile repoFile : children) {
        if (repoFile.isFolder()) {
            ZipEntry entry = new ZipEntry(repoFile.getPath().substring(filePath.length() + 1) + "/");
            zos.putNextEntry(entry);
            exportDirectoryAsZip(repoFile, zos);
        } else {
            exportFileAsZip(repoFile, zos);
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 53 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class Exporter method exportDirectory.

/**
 * @param repositoryDir
 * @param parentDir
 * @throws java.io.IOException
 */
public void exportDirectory(RepositoryFile repositoryDir, File parentDir) throws IOException {
    if (repositoryDir == null || !repositoryDir.isFolder()) {
        throw new IllegalArgumentException(Messages.getInstance().getErrorString("Exporter.ERROR_0001_INVALID_SOURCE_DIRECTORY", repositoryDir == null ? "Null" : repositoryDir.getPath()));
    }
    if (parentDir == null) {
        throw new FileNotFoundException(Messages.getInstance().getErrorString("Exporter.ERROR_0002_MISSING_DESTINATION"));
    }
    parentDir = new File(parentDir, repositoryDir.getName());
    if (!parentDir.mkdirs()) {
        throw (new IOException());
    }
    List<RepositoryFile> children = unifiedRepository.getChildren(repositoryDir.getId());
    for (RepositoryFile repoFile : children) {
        if (repoFile.isFolder()) {
            exportDirectory(repoFile, parentDir);
        } else {
            exportFile(repoFile, parentDir);
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) IOException(java.io.IOException) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 54 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class Exporter method doExportAsZip.

/**
 * @param exportRepositoryFile
 * @return
 * @throws java.io.IOException
 */
public File doExportAsZip(RepositoryFile exportRepositoryFile) throws IOException {
    File zipFile = File.createTempFile("repoExport", ".zip");
    zipFile.deleteOnExit();
    filePath = new File(repoPath).getParent();
    if (exportRepositoryFile == null) {
        throw new FileNotFoundException(Messages.getInstance().getErrorString("Exporter.ERROR_0001_INVALID_SOURCE_DIRECTORY", repoPath));
    }
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
    try {
        if (exportRepositoryFile.isFolder()) {
            // Handle recursive export
            ZipEntry entry = new ZipEntry(exportRepositoryFile.getPath().substring(filePath.length() + 1) + "/");
            zos.putNextEntry(entry);
            exportDirectoryAsZip(exportRepositoryFile, zos);
        } else {
            exportFileAsZip(exportRepositoryFile, zos);
        }
    } finally {
        zos.close();
    }
    return zipFile;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile)

Example 55 with RepositoryFile

use of org.pentaho.platform.api.repository2.unified.RepositoryFile in project pentaho-platform by pentaho.

the class ZipExportProcessor method exportDirectory.

/**
 * @param repositoryDir
 * @param outputStream
 */
@Override
public void exportDirectory(RepositoryFile repositoryDir, OutputStream outputStream, String filePath) throws ExportException, IOException {
    addToManifest(repositoryDir);
    List<RepositoryFile> children = getUnifiedRepository().getChildren(new RepositoryRequest(String.valueOf(repositoryDir.getId()), true, 1, null));
    for (RepositoryFile repositoryFile : children) {
        // exclude 'etc' folder - datasources and etc.
        if (isExportCandidate(repositoryFile.getPath())) {
            if (repositoryFile.isFolder()) {
                if (outputStream.getClass().isAssignableFrom(ZipOutputStream.class)) {
                    ZipOutputStream zos = (ZipOutputStream) outputStream;
                    String zipEntryName = getFixedZipEntryName(repositoryFile, filePath);
                    ZipEntry entry = new ZipEntry(zipEntryName);
                    zos.putNextEntry(entry);
                }
                exportDirectory(repositoryFile, outputStream, filePath);
            } else {
                try {
                    exportFile(repositoryFile, outputStream, filePath);
                } catch (ZipException e) {
                    // possible duplicate entry, log it and continue on with the other files in the directory
                    log.debug(e.getMessage(), e);
                }
            }
        }
    }
    createLocales(repositoryDir, filePath, repositoryDir.isFolder(), outputStream);
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryRequest(org.pentaho.platform.api.repository2.unified.RepositoryRequest) ZipException(java.util.zip.ZipException)

Aggregations

RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)455 Test (org.junit.Test)183 ITenant (org.pentaho.platform.api.mt.ITenant)87 Matchers.anyString (org.mockito.Matchers.anyString)86 ArrayList (java.util.ArrayList)85 RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)83 Serializable (java.io.Serializable)56 IUnifiedRepository (org.pentaho.platform.api.repository2.unified.IUnifiedRepository)53 SimpleRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData)49 KettleException (org.pentaho.di.core.exception.KettleException)40 NodeRepositoryFileData (org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData)40 ByteArrayInputStream (java.io.ByteArrayInputStream)39 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)39 IOException (java.io.IOException)37 File (java.io.File)34 MetaStoreException (org.pentaho.metastore.api.exceptions.MetaStoreException)34 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)33 MetaStoreNamespaceExistsException (org.pentaho.metastore.api.exceptions.MetaStoreNamespaceExistsException)33 IdNotFoundException (org.pentaho.di.core.exception.IdNotFoundException)32 KettleFileException (org.pentaho.di.core.exception.KettleFileException)32