Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,18 @@ public void saveFile(String subPath, boolean isProtectedResource, InputStream is

private void create(String subPath, boolean isProtectedResource, Optional<InputStream> fileInputStream) {
try {
Optional<TenantConfig> config = getTenantConfig();
Optional<TenantConfig> config = this.getTenantConfig();
if(StringUtils.isBlank(subPath)){
throw new EntRuntimeException(ERROR_VALIDATING_PATH_MSG);
}
this.validateAndReturnResourcePath(config, subPath, false, isProtectedResource);
URI apiUrl = CdsUrlUtils.buildCdsInternalApiUrl(config, configuration, "/upload/");
CdsCreateResponseDto response = caller.executePostCall(apiUrl,
CdsCreateResponseDto response = this.caller.executePostCall(apiUrl,
subPath,
isProtectedResource,
fileInputStream,
config,
false);

if (!response.isStatusOk()) {
throw new EntRuntimeException("Invalid status - Response " + response.isStatusOk());
}
Expand All @@ -112,8 +111,8 @@ public void deleteDirectory(String subPath, boolean isProtectedResource) throws
@Override
public boolean deleteFile(String subPath, boolean isProtectedResource) {
try {
Optional<TenantConfig> config = getTenantConfig();
if(StringUtils.isBlank(subPath)){
Optional<TenantConfig> config = this.getTenantConfig();
if (StringUtils.isBlank(subPath)){
throw new EntRuntimeException(ERROR_VALIDATING_PATH_MSG);
}
this.validateAndReturnResourcePath(config, subPath, true, isProtectedResource);
Expand All @@ -123,7 +122,7 @@ public boolean deleteFile(String subPath, boolean isProtectedResource) {
.path(CdsUrlUtils.getSection(isProtectedResource, config, this.configuration, true))
.path(subPath)
.build();
return caller.executeDeleteCall(apiUrl, config, false);
return this.caller.executeDeleteCall(apiUrl, config, false);
} catch (EntRuntimeException ert) {
throw ert;
} catch (Exception e) {
Expand All @@ -136,32 +135,31 @@ public InputStream getStream(String subPath, boolean isProtectedResource) throws
final String ERROR_EXTRACTING_FILE = "Error extracting file";
URI url = null;
try {
Optional<TenantConfig> config = getTenantConfig();
if(StringUtils.isBlank(subPath)){
Optional<TenantConfig> config = this.getTenantConfig();
if (StringUtils.isBlank(subPath)) {
throw new EntRuntimeException(ERROR_VALIDATING_PATH_MSG);
}

if (!this.exists(subPath, isProtectedResource)) {
throw new EntResourceNotFoundException(
String.format("File \"%s\", protected \"%s\", Not Found", subPath, isProtectedResource));
}
this.validateAndReturnResourcePath(config, subPath, true, isProtectedResource);

url = (isProtectedResource) ?
CdsUrlUtils.buildCdsInternalApiUrl(config, configuration) :
CdsUrlUtils.buildCdsExternalPublicResourceUrl(config, configuration);

url = EntUrlBuilder.builder()
.url(url)
.path(CdsUrlUtils.getSection(isProtectedResource, config, this.configuration, true))
.path(subPath).build();

Optional<ByteArrayInputStream> is = caller.getFile(url, config, isProtectedResource);
return is.orElseThrow(IOException::new);

} catch (EntRuntimeException ert) {
return is.orElse(new ByteArrayInputStream(new byte[0]));
} catch (EntResourceNotFoundException | EntRuntimeException ert) {
throw ert;
} catch (HttpClientErrorException e) {
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
log.info("File Not found - uri {}", url);
return null;
}
}
throw new EntResourceNotFoundException(ERROR_EXTRACTING_FILE, e);
} catch (Exception e) {
throw new EntResourceNotFoundException(ERROR_EXTRACTING_FILE, e);
Expand All @@ -171,7 +169,7 @@ public InputStream getStream(String subPath, boolean isProtectedResource) throws
@Override
public String getResourceUrl(String subPath, boolean isProtectedResource) {
try {
Optional<TenantConfig> config = getTenantConfig();
Optional<TenantConfig> config = this.getTenantConfig();
return this.validateAndReturnResourcePath(config, subPath, false, isProtectedResource);
} catch (Exception e) {
throw new EntRuntimeException("Error extracting resource url", e);
Expand All @@ -187,7 +185,7 @@ public boolean exists(String subPath, boolean isProtectedResource) {

// when frontend wants to retrieve public or protected folder contents it gets request with an empty subpath
private boolean isSubPathPresent(String[] filenames, String subPath){
if(StringUtils.isEmpty(subPath)) {
if (StringUtils.isEmpty(subPath)) {
return filenames.length > 0;
} else {
return Arrays.asList(filenames).contains(subPath);
Expand Down Expand Up @@ -250,7 +248,7 @@ private List<BasicFileAttributeView> listAttributes(String subPath, boolean isPr
.path(CdsUrlUtils.getSection(isProtectedResource, config, this.configuration, true))
.path(subPath)
.build();

Optional<CdsFileAttributeViewDto[]> cdsFileList = caller.getFileAttributeView(apiUrl, config);

return remapAndSort(cdsFileList, filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.entando.entando.aps.system.services.tenants.ITenantManager;
import org.entando.entando.aps.system.services.tenants.TenantConfig;
import org.entando.entando.ent.exception.EntException;
import org.entando.entando.ent.exception.EntResourceNotFoundException;
import org.entando.entando.ent.exception.EntRuntimeException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -238,13 +239,19 @@ void shouldManageErrorWhenCallGetStream() throws Exception {
).isInstanceOf(EntRuntimeException.class).hasMessageStartingWith("Error validating path");

String testFilePath = "/testfolder/test.txt";

Assertions.assertThatThrownBy(
()-> cdsStorageManager.getStream(testFilePath, false)
).isInstanceOf(EntResourceNotFoundException.class).hasMessageStartingWith("File \"" + testFilePath);

URI testFile = URI.create( baseUrl + "/custom-path" + testFilePath);
this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/protected/testfolder", "test.txt");
Mockito.when(cdsRemoteCaller.getFile(eq(testFile),
any(),
eq(false))).thenReturn(null);
Assertions.assertThatThrownBy(
()-> cdsStorageManager.getStream(testFilePath,false)
).isInstanceOf(EntException.class).hasMessageStartingWith("Error extracting file");
).isInstanceOf(EntResourceNotFoundException.class).hasMessageStartingWith("Error extracting file");

String testFilePathBadGateway = "/testfolder/test-badgw.txt";
URI testFileBadGateway = URI.create( baseUrl + "/custom-path" + testFilePathBadGateway);
Expand All @@ -256,15 +263,9 @@ void shouldManageErrorWhenCallGetStream() throws Exception {
()-> cdsStorageManager.getStream(testFilePathBadGateway,false)
).isInstanceOf(EntException.class).hasMessageStartingWith("Error extracting file");


String testFilePathNotFound = "/testfolder/test-notfound.txt";
URI testFileNotFound = URI.create( baseUrl + "/custom-path" + testFilePathNotFound);
Mockito.when(cdsRemoteCaller.getFile(eq(testFileNotFound),
any(),
eq(false))).thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));

Assertions.assertThat(cdsStorageManager.getStream(testFilePathNotFound,false)).isNull();

Assertions.assertThatThrownBy(() -> cdsStorageManager.getStream(testFilePathNotFound,false))
.isInstanceOf(EntResourceNotFoundException.class).hasMessageStartingWith("Error extracting file");
}

@Test
Expand All @@ -276,20 +277,22 @@ void shouldReturnDataWhenCallGetStream() throws Exception {
"cdsPath","/mytenant/api/v1/");
TenantConfig tc = new TenantConfig(configMap);
Mockito.when(tenantManager.getConfig("my-tenant")).thenReturn(Optional.ofNullable(tc));

Mockito.when(cdsRemoteCaller.getFile(eq(URI.create("http://my-server/tenant1/cms-resources/test-folder/test.txt")),
any(),
eq(false))).thenReturn(Optional.ofNullable(new ByteArrayInputStream("text random".getBytes(StandardCharsets.UTF_8))));


this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/test-folder", "test.txt");
ApsTenantApplicationUtils.setTenant("my-tenant");
InputStream is = cdsStorageManager.getStream(testFilePath,false);
Assertions.assertThat(new BufferedReader(new InputStreamReader(is))
.lines().collect(Collectors.joining(""))).isEqualTo("text random");

Mockito.when(cdsRemoteCaller.getFile(eq(URI.create("http://cds-kube-service:8081/mytenant/api/v1/protected/test-folder/test.txt")),
any(),
eq(true))).thenReturn(Optional.ofNullable(new ByteArrayInputStream("text random".getBytes(StandardCharsets.UTF_8))));


this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/protected/test-folder", "test.txt");
is = cdsStorageManager.getStream(testFilePath,true);
Assertions.assertThat(new BufferedReader(new InputStreamReader(is))
.lines().collect(Collectors.joining(""))).isEqualTo("text random");
Expand Down Expand Up @@ -466,24 +469,25 @@ void shouldReadFile() throws Exception {
String testFilePath = "/testfolder/test.txt";

Map<String,String> configMap = Map.of("cdsPublicUrl","http://my-server/tenant1/cms-resources",
"cdsPrivateUrl","http://cds-kube-service:8081/",
"cdsPrivateUrl","http://cdsmaster-kube-service:8081/",
"cdsPath","/mytenant/api/v1/");
TenantConfig tc = new TenantConfig(configMap);
Mockito.when(tenantManager.getConfig("my-tenant")).thenReturn(Optional.ofNullable(tc));

Mockito.when(cdsRemoteCaller.getFile(any(),
any(),
eq(false))).thenReturn(Optional.ofNullable(new ByteArrayInputStream("text random".getBytes(StandardCharsets.UTF_8))));



this.mockExecuteListFolder("http://cdsmaster-kube-service:8081/mytenant/api/v1/list/testfolder", "test.txt");

ApsTenantApplicationUtils.setTenant("my-tenant");
Assertions.assertThat(cdsStorageManager.readFile(testFilePath,false))
.isEqualTo("text random");
}

@Test
void shouldManageExceptionWhenReadFile() throws Exception {
String testFilePath = "/testfolder/test.txt";
String testFilePath = "/testfolder/subfolder/test.txt";

Map<String,String> configMap = Map.of("cdsPublicUrl","http://my-server/tenant1/cms-resources",
"cdsPrivateUrl","http://cds-kube-service:8081/",
Expand All @@ -500,7 +504,7 @@ void shouldManageExceptionWhenReadFile() throws Exception {
try (MockedStatic<IOUtils> ioUtils = Mockito.mockStatic(IOUtils.class)) {
ioUtils.when(() -> IOUtils.toString(any(InputStream.class), eq(StandardCharsets.UTF_8)))
.thenThrow(new IOException());

this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/testfolder/subfolder", "test.txt");
Assertions.assertThatThrownBy(() -> cdsStorageManager.readFile(testFilePath, false))
.isInstanceOf(EntException.class)
.hasMessageStartingWith("Error extracting text");
Expand Down Expand Up @@ -585,5 +589,12 @@ void testListAttributes() throws Throwable {
}
assertTrue(containsCms);
}

private void mockExecuteListFolder(String uri, String filename) {
CdsFileAttributeViewDto file = new CdsFileAttributeViewDto();
file.setName(filename);
Mockito.when(cdsRemoteCaller.getFileAttributeView(eq(URI.create(uri)),
any())).thenReturn(Optional.ofNullable(new CdsFileAttributeViewDto[]{file}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
import org.entando.entando.ent.util.EntLogging.EntLogger;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.context.ServletContextAware;

Expand All @@ -96,6 +97,9 @@ public class DatabaseManager extends AbstractInitializerManager
private int lockFallbackMinutes;

private ServletContext servletContext;

@Autowired
private ITenantManager tenantManager;

public void init() {
logger.debug("DatabaseManager ready");
Expand Down Expand Up @@ -568,7 +572,7 @@ public boolean dropAndRestoreBackup(String subFolderName) throws EntException {
return false;
}
//TODO future improvement - execute 'lifeline' backup
this.getDatabaseRestorer().dropAndRestoreBackup(subFolderName);
this.getDatabaseRestorer().dropAndRestoreBackup(subFolderName, this.tenantManager);
ApsWebApplicationUtils.executeSystemRefresh(this.getServletContext());
return true;
} catch (Throwable t) {
Expand All @@ -586,18 +590,24 @@ private boolean restoreBackup(String subFolderName) throws EntException {
logger.error("backup not available - subfolder '{}'", subFolderName);
return false;
}
this.getDatabaseRestorer().restoreBackup(subFolderName);
this.getDatabaseRestorer().restoreBackup(subFolderName, this.tenantManager);
return true;
} catch (Throwable t) {
logger.error("Error while restoring local backup", t);
throw new EntException("Error while restoring local backup", t);
}
}

private String[] extractBeanNames(Class beanClass) {
private String[] extractBeanNames(Class<?> beanClass) {
ListableBeanFactory factory = (ListableBeanFactory) this.getBeanFactory();
return factory.getBeanNamesForType(beanClass);
}







@Override
public InputStream getTableDump(String tableName, String dataSourceName, String subFolderName) throws EntException {
Expand Down
Loading