Search in sources :

Example 1 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafFileServiceImplTest method testCreateAddFile.

public void testCreateAddFile() throws Exception {
    FileWorkArea workArea1 = bfs.initializeWorkArea();
    File f1 = new File(workArea1.getFilePathLocation() + "test.txt");
    FileWriter fw = new FileWriter(f1);
    fw.append("Test File");
    fw.close();
    bfs.addOrUpdateResource(workArea1, f1, false);
    bfs.closeWorkArea(workArea1);
    File resource = bfs.getResource("test.txt");
    assertTrue(resource.exists());
    bfs.removeResource("test.txt");
    resource = bfs.getResource("test.txt");
    assertFalse(resource.exists());
}
Also used : FileWriter(java.io.FileWriter) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) File(java.io.File)

Example 2 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class SiteMapServiceImpl method generateSiteMap.

@Override
public SiteMapGenerationResponse generateSiteMap() throws SiteMapException, IOException {
    SiteMapGenerationResponse smgr = new SiteMapGenerationResponse();
    SiteMapConfiguration smc = findActiveSiteMapConfiguration();
    if (smc == null) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("No SiteMap generated since no active configuration was found.");
        }
        smgr.setHasError(true);
        smgr.setErrorCode("No SiteMap Configuration Found");
        return smgr;
    }
    FileWorkArea fileWorkArea = broadleafFileService.initializeWorkArea();
    SiteMapBuilder siteMapBuilder = new SiteMapBuilder(smc, fileWorkArea, baseUrlResolver.getSiteBaseUrl(), getGzipSiteMapFiles());
    if (LOG.isTraceEnabled()) {
        LOG.trace("File work area initalized with path " + fileWorkArea.getFilePathLocation());
    }
    for (SiteMapGeneratorConfiguration currentConfiguration : smc.getSiteMapGeneratorConfigurations()) {
        if (currentConfiguration.isDisabled()) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Skipping disabled sitemap generator configuration" + currentConfiguration.getId());
            }
            continue;
        }
        SiteMapGenerator generator = selectSiteMapGenerator(currentConfiguration);
        if (generator != null) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("SiteMapGenerator found, adding entries" + generator.getClass());
            }
            generator.addSiteMapEntries(currentConfiguration, siteMapBuilder);
        } else {
            LOG.warn("No site map generator found to process generator configuration for " + currentConfiguration.getSiteMapGeneratorType());
        }
    }
    siteMapBuilder.persistSiteMap();
    // Check for GZip
    if (getGzipSiteMapFiles()) {
        gzipAndDeleteFiles(fileWorkArea, siteMapBuilder.getIndexedFileNames(), false);
        List<String> indexFileNames = new ArrayList<String>();
        for (String fileName : siteMapBuilder.getIndexedFileNames()) {
            indexFileNames.add(fileName + ENCODING_EXTENSION);
        }
        smgr.setSiteMapFilePaths(indexFileNames);
    } else {
        smgr.setSiteMapFilePaths(siteMapBuilder.getIndexedFileNames());
    }
    // Move the generated files to their permanent location
    broadleafFileService.addOrUpdateResources(fileWorkArea, true);
    broadleafFileService.closeWorkArea(fileWorkArea);
    return smgr;
}
Also used : SiteMapConfiguration(org.broadleafcommerce.common.sitemap.domain.SiteMapConfiguration) ArrayList(java.util.ArrayList) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) SiteMapGeneratorConfiguration(org.broadleafcommerce.common.sitemap.domain.SiteMapGeneratorConfiguration)

Example 3 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafFileServiceImpl method initializeWorkArea.

/**
 * Create a file work area that can be used for further operations.
 * @return
 */
@Override
public FileWorkArea initializeWorkArea() {
    String baseDirectory = getBaseDirectory(false);
    String tempDirectory = getTempDirectory(baseDirectory);
    FileWorkArea fw = new FileWorkArea();
    fw.setFilePathLocation(tempDirectory);
    File tmpDir = new File(tempDirectory);
    if (!tmpDir.exists()) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Creating temp directory named " + tempDirectory);
        }
        if (!tmpDir.mkdirs()) {
            throw new FileServiceException("Unable to create temporary working directory for " + tempDirectory);
        }
    }
    return fw;
}
Also used : FileServiceException(org.broadleafcommerce.common.file.FileServiceException) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) File(java.io.File)

Example 4 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafFileServiceImplTest method testCreateFilesCopyWorkarea.

public void testCreateFilesCopyWorkarea() throws Exception {
    FileWorkArea workArea1 = bfs.initializeWorkArea();
    File f1 = new File(workArea1.getFilePathLocation() + "test4.txt");
    FileWriter fw = new FileWriter(f1);
    fw.append("Test File 4");
    fw.close();
    File f2 = new File(workArea1.getFilePathLocation() + "test5.txt");
    FileWriter fw2 = new FileWriter(f2);
    fw2.append("Test File 5");
    fw2.close();
    bfs.addOrUpdateResources(workArea1, false);
    bfs.closeWorkArea(workArea1);
    File resource = bfs.getResource("test4.txt");
    assertTrue(resource.exists());
    resource = bfs.getResource("test5.txt");
    assertTrue(resource.exists());
    bfs.removeResource("test4.txt");
    bfs.removeResource("test5.txt");
    resource = bfs.getResource("test5.txt");
    assertFalse(resource.exists());
}
Also used : FileWriter(java.io.FileWriter) FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) File(java.io.File)

Example 5 with FileWorkArea

use of org.broadleafcommerce.common.file.domain.FileWorkArea in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafFileServiceImplTest method testCreateWorkArea.

public void testCreateWorkArea() throws Exception {
    FileWorkArea workArea1 = bfs.initializeWorkArea();
    File f1 = new File(workArea1.getFilePathLocation());
    // The service should return a directory that is ready write to.
    assertTrue(f1.exists());
    // The service should return a unique work area.
    FileWorkArea workArea2 = bfs.initializeWorkArea();
    assertFalse(workArea2.getFilePathLocation().equals(workArea1.getFilePathLocation()));
    // Remove the work areas
    bfs.closeWorkArea(workArea1);
    assertFalse(f1.exists());
    bfs.closeWorkArea(workArea2);
}
Also used : FileWorkArea(org.broadleafcommerce.common.file.domain.FileWorkArea) File(java.io.File)

Aggregations

FileWorkArea (org.broadleafcommerce.common.file.domain.FileWorkArea)9 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)3 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 Blob (java.sql.Blob)1 FileExistsException (org.apache.commons.io.FileExistsException)1 StaticAssetStorage (org.broadleafcommerce.cms.file.domain.StaticAssetStorage)1 FileServiceException (org.broadleafcommerce.common.file.FileServiceException)1 GloballySharedInputStream (org.broadleafcommerce.common.file.service.GloballySharedInputStream)1 SiteMapConfiguration (org.broadleafcommerce.common.sitemap.domain.SiteMapConfiguration)1 SiteMapGeneratorConfiguration (org.broadleafcommerce.common.sitemap.domain.SiteMapGeneratorConfiguration)1 Transactional (org.springframework.transaction.annotation.Transactional)1