org.broadleafcommerce.cms.file.service
Class StaticAssetStorageServiceImpl

java.lang.Object
  extended by org.broadleafcommerce.cms.file.service.StaticAssetStorageServiceImpl
All Implemented Interfaces:
StaticAssetStorageService

@Service(value="blStaticAssetStorageService")
public class StaticAssetStorageServiceImpl
extends Object
implements StaticAssetStorageService

Author:
Jeff Fischer, Brian Polster

Field Summary
protected  ArtifactService artifactService
           
protected  String assetFileClasspathDirectory
           
protected  String assetFileSystemPath
           
protected  int assetServerMaxGeneratedDirectories
           
protected  String cacheDirectory
           
protected  int fileBufferSize
           
protected  long maxUploadableFileSize
           
protected  NamedOperationManager namedOperationManager
           
protected  StaticAssetService staticAssetService
           
protected  StaticAssetStorageDao staticAssetStorageDao
           
 
Constructor Summary
StaticAssetStorageServiceImpl()
           
 
Method Summary
protected  String constructCacheFileName(StaticAsset staticAsset, Map<String,String> parameterMap)
           
 StaticAssetStorage create()
           
 Blob createBlob(org.springframework.web.multipart.MultipartFile uploadedFile)
           
protected  void createCacheFile(InputStream is, File cacheFile)
           
 void createStaticAssetStorageFromFile(org.springframework.web.multipart.MultipartFile file, StaticAsset staticAsset)
          Persists the file being based in according to the staticAsset's StorageType.
 void delete(StaticAssetStorage assetStorage)
           
protected  InputStream findInputStreamForStaticAsset(StaticAsset staticAsset)
           
protected  StaticAsset findStaticAsset(String fullUrl, SandBox sandBox)
           
 StaticAssetStorage findStaticAssetStorageById(Long id)
           
protected  String fixPath(String path)
          Removes trailing "/" and ensures that there is a beginning "/"
 String generateStorageFileName(StaticAsset staticAsset)
          By default, delegates a call to StaticAssetStorageService.generateStorageFileName(String) using staticAsset.getFullUrl() as the passed in argument.
 String generateStorageFileName(String fullUrl)
          Stores the file on the filesystem by performing an MD5 hash of the the staticAsset.fullUrl.
protected  String getBaseDirectory()
          Returns the baseDirectory for writing and reading files as the property assetFileSystemPath if it exists or java.tmp.io if that property has not been set.
 Map<String,String> getCacheFileModel(String fullUrl, SandBox sandBox, Map<String,String> parameterMap)
           
protected  InputStream getResourceFromClasspath(StaticAsset staticAsset)
           
static void main(String[] args)
           
protected  String pad(String s, int length, char pad)
           
 StaticAssetStorage readStaticAssetStorageByStaticAssetId(Long id)
           
 StaticAssetStorage save(StaticAssetStorage assetStorage)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

assetFileSystemPath

@Value(value="${asset.server.file.system.path}")
protected String assetFileSystemPath

assetFileClasspathDirectory

@Value(value="${asset.server.file.classpath.directory}")
protected String assetFileClasspathDirectory

assetServerMaxGeneratedDirectories

@Value(value="${asset.server.max.generated.file.system.directories}")
protected int assetServerMaxGeneratedDirectories

maxUploadableFileSize

@Value(value="${asset.server.max.uploadable.file.size}")
protected long maxUploadableFileSize

fileBufferSize

@Value(value="${asset.server.file.buffer.size}")
protected int fileBufferSize

cacheDirectory

protected String cacheDirectory

staticAssetService

protected StaticAssetService staticAssetService

artifactService

protected ArtifactService artifactService

staticAssetStorageDao

protected StaticAssetStorageDao staticAssetStorageDao

namedOperationManager

protected NamedOperationManager namedOperationManager
Constructor Detail

StaticAssetStorageServiceImpl

public StaticAssetStorageServiceImpl()
Method Detail

findStaticAsset

protected StaticAsset findStaticAsset(String fullUrl,
                                      SandBox sandBox)

fixPath

protected String fixPath(String path)
Removes trailing "/" and ensures that there is a beginning "/"

Parameters:
path -
Returns:

generateStorageFileName

public String generateStorageFileName(StaticAsset staticAsset)
Description copied from interface: StaticAssetStorageService
By default, delegates a call to StaticAssetStorageService.generateStorageFileName(String) using staticAsset.getFullUrl() as the passed in argument.

Specified by:
generateStorageFileName in interface StaticAssetStorageService
Parameters:
staticAsset - StaticAsset for which a filename is desired.
Returns:

getBaseDirectory

protected String getBaseDirectory()
Returns the baseDirectory for writing and reading files as the property assetFileSystemPath if it exists or java.tmp.io if that property has not been set.


generateStorageFileName

public String generateStorageFileName(String fullUrl)
Description copied from interface: StaticAssetStorageService
Stores the file on the filesystem by performing an MD5 hash of the the staticAsset.fullUrl. To ensure that files can be stored and accessed in an efficient manner, the system creates directories based on the characters in the hash. For example, if the URL is /product/myproductimage.jpg, then the MD5 would be 35ec52a8dbd8cf3e2c650495001fe55f resulting in the following file on the filesystem {assetFileSystemPath}/35/ec/myproductimage.jpg. If there is a "siteId" in the BroadleafRequestContext then the site is also distributed using a similar algorithm but the system attempts to keep images for sites in their own directory resulting in an extra two folders required to reach any given product. So, for site with id 125, the system will MD5 "/site-125" in order to build the URL string. "/site-125" has an md5 string of "7fde295edac6ca7f85d0368ea741b241". So, in this case with the above product URL in site125, the full URL on the filesystem will be: {assetFileSystemPath}/7f/site-125/35/ec/myproductimage.jpg. This algorithm has the following benefits: - Efficient file-system storage with - Balanced tree of files that supports 10 million files If support for more files is needed, implementors should consider one of the following approaches: 1. Overriding the maxGeneratedFileSystemDirectories property from its default of 2 to 3 2. Overriding this method to introduce an alternate approach

Specified by:
generateStorageFileName in interface StaticAssetStorageService
Parameters:
fullUrl - The URL used to represent an asset for which a name on the fileSystem is desired.
Returns:

getCacheFileModel

@Transactional(value="blTransactionManagerAssetStorageInfo")
public Map<String,String> getCacheFileModel(String fullUrl,
                                                          SandBox sandBox,
                                                          Map<String,String> parameterMap)
                                     throws Exception
Specified by:
getCacheFileModel in interface StaticAssetStorageService
Throws:
Exception

findInputStreamForStaticAsset

protected InputStream findInputStreamForStaticAsset(StaticAsset staticAsset)
                                             throws SQLException,
                                                    IOException
Throws:
SQLException
IOException

getResourceFromClasspath

protected InputStream getResourceFromClasspath(StaticAsset staticAsset)

findStaticAssetStorageById

@Transactional(value="blTransactionManagerAssetStorageInfo")
public StaticAssetStorage findStaticAssetStorageById(Long id)
Specified by:
findStaticAssetStorageById in interface StaticAssetStorageService

create

@Transactional(value="blTransactionManagerAssetStorageInfo")
public StaticAssetStorage create()
Specified by:
create in interface StaticAssetStorageService
Returns:

readStaticAssetStorageByStaticAssetId

@Transactional(value="blTransactionManagerAssetStorageInfo")
public StaticAssetStorage readStaticAssetStorageByStaticAssetId(Long id)
Specified by:
readStaticAssetStorageByStaticAssetId in interface StaticAssetStorageService

save

@Transactional(value="blTransactionManagerAssetStorageInfo")
public StaticAssetStorage save(StaticAssetStorage assetStorage)
Specified by:
save in interface StaticAssetStorageService

delete

@Transactional(value="blTransactionManagerAssetStorageInfo")
public void delete(StaticAssetStorage assetStorage)
Specified by:
delete in interface StaticAssetStorageService

createBlob

@Transactional(value="blTransactionManagerAssetStorageInfo")
public Blob createBlob(org.springframework.web.multipart.MultipartFile uploadedFile)
                throws IOException
Specified by:
createBlob in interface StaticAssetStorageService
Returns:
Throws:
IOException

createCacheFile

protected void createCacheFile(InputStream is,
                               File cacheFile)
                        throws SQLException,
                               IOException
Throws:
SQLException
IOException

constructCacheFileName

protected String constructCacheFileName(StaticAsset staticAsset,
                                        Map<String,String> parameterMap)

pad

protected String pad(String s,
                     int length,
                     char pad)

createStaticAssetStorageFromFile

public void createStaticAssetStorageFromFile(org.springframework.web.multipart.MultipartFile file,
                                             StaticAsset staticAsset)
                                      throws IOException
Description copied from interface: StaticAssetStorageService
Persists the file being based in according to the staticAsset's StorageType.

Specified by:
createStaticAssetStorageFromFile in interface StaticAssetStorageService
Throws:
IOException

main

public static void main(String[] args)


Copyright © 2013. All Rights Reserved.