Skip to content

LUT-29770 : FileService migration #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 ~/tools/mvn388/apache-maven-3.8.8/bin/mvn clean lutece:exploded antrun:run -Dlutece-test-hsql test -Dtest=FileServiceTest
88 changes: 22 additions & 66 deletions src/java/fr/paris/lutece/portal/service/file/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@
*/
package fr.paris.lutece.portal.service.file;

import org.apache.commons.lang3.StringUtils;

import fr.paris.lutece.portal.service.util.AppException;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.inject.Inject;


/**
*
Expand All @@ -48,8 +47,6 @@
@ApplicationScoped
public class FileService
{

// parameters
public static final String PARAMETER_FILE_ID = "file_id";
public static final String PARAMETER_RESOURCE_ID = "resource_id";
public static final String PARAMETER_RESOURCE_TYPE = "resource_type";
Expand All @@ -58,54 +55,35 @@ public class FileService
public static final String PARAMETER_BO = "is_bo";
public static final String PARAMETER_PROVIDER = "provider";

// constants
public static final String PERMISSION_VIEW = "VIEW";

// messages
private static final String MSG_NO_FILE_SERVICE = "No file service Available";


@Inject
private IFileStoreServiceProvider _defaulFileStoreServiceProvider;
@Inject
private Instance<IFileStoreServiceProvider> _fileStoreServiceProviderList;


/**
* init
*/
@PostConstruct
void initFileService( )
{
_defaulFileStoreServiceProvider = getDefaultServiceProvider( );
}

/**
* Returns the unique instance of the {@link FileService} service.
*
* <p>This method is deprecated and is provided for backward compatibility only.
* For new code, use dependency injection with {@code @Inject} to obtain the
* {@link FileService} instance instead.</p>
* <p>
* This method is deprecated and is provided for backward compatibility only. For new code, use dependency injection with {@code @Inject} to obtain the
* {@link FileService} instance instead.
* </p>
*
* @return The unique instance of {@link FileService}.
*
* @deprecated Use {@code @Inject} to obtain the {@link FileService}
* instance. This method will be removed in future versions.
* @deprecated Use {@code @Inject} to obtain the {@link FileService} instance. This method will be removed in future versions.
*/
@Deprecated( since = "8.0", forRemoval = true )
public static FileService getInstance( )
{
return CDI.current( ).select( FileService.class ).get( );
}

/**
/**
* get the current FileStoreService provider
*
* @return the current FileStoreService provider
*/
public IFileStoreServiceProvider getFileStoreServiceProvider( )
{
return getFileStoreServiceProvider( null) ;
return getFileStoreServiceProvider( null );
}

/**
Expand All @@ -116,46 +94,24 @@ public IFileStoreServiceProvider getFileStoreServiceProvider( )
*/
public IFileStoreServiceProvider getFileStoreServiceProvider( String strFileStoreServiceProviderName )
{
Instance<IFileStoreServiceProvider> fileServiceProviders = CDI.current( ).select( IFileStoreServiceProvider.class );

if ( strFileStoreServiceProviderName == null && _defaulFileStoreServiceProvider != null )
{
return _defaulFileStoreServiceProvider;
}

// search file service
if ( !_fileStoreServiceProviderList.isUnsatisfied() && strFileStoreServiceProviderName != null )
if ( fileServiceProviders.stream( ).count( ) == 0 )
{
for ( IFileStoreServiceProvider fss : _fileStoreServiceProviderList )
{
if ( strFileStoreServiceProviderName.equals( fss.getName( ) ) )
{
return fss;
}
}
return _fileStoreServiceProviderList.stream()
.filter(fss -> strFileStoreServiceProviderName.equals(fss.getName()))
.findFirst().orElseThrow(()-> new AppException( MSG_NO_FILE_SERVICE ));
throw new AppException( MSG_NO_FILE_SERVICE );
}
;

// otherwise
throw new AppException( MSG_NO_FILE_SERVICE );
}
/** get default File Store Service Provider
*
* @return the provider
*/
private IFileStoreServiceProvider getDefaultServiceProvider( )
{
// search default file service
if ( !_fileStoreServiceProviderList.isUnsatisfied() )
if ( StringUtils.isNotEmpty( strFileStoreServiceProviderName ) )
{
return fileServiceProviders.stream( ).filter( fss -> strFileStoreServiceProviderName.equals( fss.getName( ) ) ).findFirst( )
.orElseThrow( ( ) -> new AppException( MSG_NO_FILE_SERVICE ) );
}
else
{
return _fileStoreServiceProviderList.stream()
.filter(fss -> fss.isDefault())
.findFirst()
.orElseGet(() -> _fileStoreServiceProviderList.stream().findFirst()
.orElseThrow(() -> new AppException(MSG_NO_FILE_SERVICE)));
// search default file service provider
return fileServiceProviders.stream( ).filter( IFileStoreServiceProvider::isDefault ).findFirst( )
.orElseThrow( ( ) -> new AppException( MSG_NO_FILE_SERVICE ) );
}
// otherwise
throw new AppException( MSG_NO_FILE_SERVICE );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public class FileServiceException extends Exception

private static final long serialVersionUID = -4788782240985061911L;

// The response code status
// The response code status
private Integer _nResponseCodeStatus;

// The response I18N message key
private String _strI18nMessageKey;

/**
* Creates a new instance of ExpiredLinkException
*
Expand All @@ -54,7 +54,7 @@ public FileServiceException( String strMessage )
{
super( strMessage );
}

/**
* Creates a new instance of HttpAccessException.
*
Expand Down Expand Up @@ -83,7 +83,7 @@ public FileServiceException( String strMessage, Integer nResponseCodeStatus, Exc
super( strMessage, e );
_nResponseCodeStatus = nResponseCodeStatus;
}

/**
* the response code (based on http codes)
*
Expand All @@ -99,7 +99,8 @@ public Integer getResponseCode( )
*
* @return the key
*/
public String getI18nMessageKey() {
return _strI18nMessageKey;
}
public String getI18nMessageKey( )
{
return _strI18nMessageKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package fr.paris.lutece.portal.service.file;

import java.io.InputStream;
import fr.paris.lutece.portal.business.file.File;

import fr.paris.lutece.portal.service.upload.MultipartItem;
import jakarta.servlet.http.HttpServletRequest;

public interface IFileStoreService
{

public String getName( );

public default boolean healthCheck( )
{
// default
return true;
}

/**
* {@inheritDoc}
*/
String storeFile( File file ) throws FileServiceException;

/**
* {@inheritDoc}
*/
File getFile( String strKey ) throws FileServiceException;

/**
* {@inheritDoc}
*/
void delete( String strKey );

/**
* {@inheritDoc}
*/
fr.paris.lutece.portal.business.file.File getFileMetaData( String strKey );

/**
* {@inheritDoc}
*/
String storeBytes( byte [ ] blob );

/**
* {@inheritDoc}
*/
String storeInputStream( InputStream inputStream );

/**
* {@inheritDoc}
*/
String storeFileItem( MultipartItem fileItem );

/**
* {@inheritDoc}
*/
InputStream getInputStream( String strKey );
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,18 @@ public interface IFileStoreServiceProvider extends Serializable
* @return true if default
*/
public boolean isDefault( );

/**
* health check
* health check
*
* @return true if available
*/
public default boolean healthCheck( )
{
// default
// default
return true;
}


/**
* Stores a file Lutece File
*
Expand All @@ -93,7 +92,7 @@ public default boolean healthCheck( )
* the fileItem
* @return The key of the blob
*/
String storeFileItem( MultipartItem fileItem )throws FileServiceException;
String storeFileItem( MultipartItem fileItem ) throws FileServiceException;

/**
* Stores an input stream
Expand Down Expand Up @@ -123,8 +122,7 @@ public default boolean healthCheck( )
File getFile( String strKey ) throws FileServiceException;

/**
* Get a file object only filled with the meta data (name, size ...)
* without the physical file content
* Get a file object only filled with the meta data (name, size ...) without the physical file content
*
* @param strKey
* The key of the file
Expand Down
Loading