Skip to content

Commit 37651e4

Browse files
committed
ENG-5468: Fix extracting file with empty stream from CDS
1 parent e9a9eba commit 37651e4

4 files changed

Lines changed: 47 additions & 34 deletions

File tree

cds-plugin/src/main/java/org/entando/entando/plugins/jpcds/aps/system/storage/CdsStorageManager.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,30 @@ public InputStream getStream(String subPath, boolean isProtectedResource) throws
137137
URI url = null;
138138
try {
139139
Optional<TenantConfig> config = getTenantConfig();
140-
if(StringUtils.isBlank(subPath)){
140+
if (StringUtils.isBlank(subPath)) {
141141
throw new EntRuntimeException(ERROR_VALIDATING_PATH_MSG);
142142
}
143-
143+
if (!this.exists(subPath, isProtectedResource)) {
144+
throw new EntResourceNotFoundException(
145+
String.format("File \"%s\", protected \"%s\", Not Found", subPath, isProtectedResource));
146+
}
144147
this.validateAndReturnResourcePath(config, subPath, true, isProtectedResource);
145-
146148
url = (isProtectedResource) ?
147149
CdsUrlUtils.buildCdsInternalApiUrl(config, configuration) :
148150
CdsUrlUtils.buildCdsExternalPublicResourceUrl(config, configuration);
149-
150151
url = EntUrlBuilder.builder()
151152
.url(url)
152153
.path(CdsUrlUtils.getSection(isProtectedResource, config, this.configuration, true))
153154
.path(subPath).build();
154-
155155
Optional<ByteArrayInputStream> is = caller.getFile(url, config, isProtectedResource);
156-
return is.orElseThrow(IOException::new);
157-
158-
} catch (EntRuntimeException ert) {
156+
return is.orElse(new ByteArrayInputStream(new byte[0]));
157+
} catch (EntResourceNotFoundException | EntRuntimeException ert) {
159158
throw ert;
160159
} catch (HttpClientErrorException e) {
161160
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
162161
log.info("File Not found - uri {}", url);
163162
return null;
164-
}
163+
}
165164
throw new EntResourceNotFoundException(ERROR_EXTRACTING_FILE, e);
166165
} catch (Exception e) {
167166
throw new EntResourceNotFoundException(ERROR_EXTRACTING_FILE, e);
@@ -250,7 +249,7 @@ private List<BasicFileAttributeView> listAttributes(String subPath, boolean isPr
250249
.path(CdsUrlUtils.getSection(isProtectedResource, config, this.configuration, true))
251250
.path(subPath)
252251
.build();
253-
252+
254253
Optional<CdsFileAttributeViewDto[]> cdsFileList = caller.getFileAttributeView(apiUrl, config);
255254

256255
return remapAndSort(cdsFileList, filter);

cds-plugin/src/test/java/org/entando/entando/plugins/jpcds/aps/system/storage/CdsStorageManagerTest.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.entando.entando.aps.system.services.tenants.ITenantManager;
3636
import org.entando.entando.aps.system.services.tenants.TenantConfig;
3737
import org.entando.entando.ent.exception.EntException;
38+
import org.entando.entando.ent.exception.EntResourceNotFoundException;
3839
import org.entando.entando.ent.exception.EntRuntimeException;
3940
import org.junit.jupiter.api.Test;
4041
import org.junit.jupiter.api.extension.ExtendWith;
@@ -238,13 +239,19 @@ void shouldManageErrorWhenCallGetStream() throws Exception {
238239
).isInstanceOf(EntRuntimeException.class).hasMessageStartingWith("Error validating path");
239240

240241
String testFilePath = "/testfolder/test.txt";
242+
243+
Assertions.assertThatThrownBy(
244+
()-> cdsStorageManager.getStream(testFilePath, false)
245+
).isInstanceOf(EntResourceNotFoundException.class).hasMessageStartingWith("File \"" + testFilePath);
246+
241247
URI testFile = URI.create( baseUrl + "/custom-path" + testFilePath);
248+
this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/protected/testfolder", "test.txt");
242249
Mockito.when(cdsRemoteCaller.getFile(eq(testFile),
243250
any(),
244251
eq(false))).thenReturn(null);
245252
Assertions.assertThatThrownBy(
246253
()-> cdsStorageManager.getStream(testFilePath,false)
247-
).isInstanceOf(EntException.class).hasMessageStartingWith("Error extracting file");
254+
).isInstanceOf(EntResourceNotFoundException.class).hasMessageStartingWith("Error extracting file");
248255

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

259-
260266
String testFilePathNotFound = "/testfolder/test-notfound.txt";
261-
URI testFileNotFound = URI.create( baseUrl + "/custom-path" + testFilePathNotFound);
262-
Mockito.when(cdsRemoteCaller.getFile(eq(testFileNotFound),
263-
any(),
264-
eq(false))).thenThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND));
265-
266-
Assertions.assertThat(cdsStorageManager.getStream(testFilePathNotFound,false)).isNull();
267-
267+
Assertions.assertThatThrownBy(() -> cdsStorageManager.getStream(testFilePathNotFound,false))
268+
.isInstanceOf(EntResourceNotFoundException.class).hasMessageStartingWith("Error extracting file");
268269
}
269270

270271
@Test
@@ -276,20 +277,22 @@ void shouldReturnDataWhenCallGetStream() throws Exception {
276277
"cdsPath","/mytenant/api/v1/");
277278
TenantConfig tc = new TenantConfig(configMap);
278279
Mockito.when(tenantManager.getConfig("my-tenant")).thenReturn(Optional.ofNullable(tc));
279-
280+
280281
Mockito.when(cdsRemoteCaller.getFile(eq(URI.create("http://my-server/tenant1/cms-resources/test-folder/test.txt")),
281282
any(),
282283
eq(false))).thenReturn(Optional.ofNullable(new ByteArrayInputStream("text random".getBytes(StandardCharsets.UTF_8))));
283-
284+
285+
this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/test-folder", "test.txt");
284286
ApsTenantApplicationUtils.setTenant("my-tenant");
285287
InputStream is = cdsStorageManager.getStream(testFilePath,false);
286288
Assertions.assertThat(new BufferedReader(new InputStreamReader(is))
287289
.lines().collect(Collectors.joining(""))).isEqualTo("text random");
288-
290+
289291
Mockito.when(cdsRemoteCaller.getFile(eq(URI.create("http://cds-kube-service:8081/mytenant/api/v1/protected/test-folder/test.txt")),
290292
any(),
291293
eq(true))).thenReturn(Optional.ofNullable(new ByteArrayInputStream("text random".getBytes(StandardCharsets.UTF_8))));
292-
294+
295+
this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/protected/test-folder", "test.txt");
293296
is = cdsStorageManager.getStream(testFilePath,true);
294297
Assertions.assertThat(new BufferedReader(new InputStreamReader(is))
295298
.lines().collect(Collectors.joining(""))).isEqualTo("text random");
@@ -466,24 +469,25 @@ void shouldReadFile() throws Exception {
466469
String testFilePath = "/testfolder/test.txt";
467470

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

474477
Mockito.when(cdsRemoteCaller.getFile(any(),
475478
any(),
476479
eq(false))).thenReturn(Optional.ofNullable(new ByteArrayInputStream("text random".getBytes(StandardCharsets.UTF_8))));
477-
478-
480+
481+
this.mockExecuteListFolder("http://cdsmaster-kube-service:8081/mytenant/api/v1/list/testfolder", "test.txt");
482+
479483
ApsTenantApplicationUtils.setTenant("my-tenant");
480484
Assertions.assertThat(cdsStorageManager.readFile(testFilePath,false))
481485
.isEqualTo("text random");
482486
}
483487

484488
@Test
485489
void shouldManageExceptionWhenReadFile() throws Exception {
486-
String testFilePath = "/testfolder/test.txt";
490+
String testFilePath = "/testfolder/subfolder/test.txt";
487491

488492
Map<String,String> configMap = Map.of("cdsPublicUrl","http://my-server/tenant1/cms-resources",
489493
"cdsPrivateUrl","http://cds-kube-service:8081/",
@@ -500,7 +504,7 @@ void shouldManageExceptionWhenReadFile() throws Exception {
500504
try (MockedStatic<IOUtils> ioUtils = Mockito.mockStatic(IOUtils.class)) {
501505
ioUtils.when(() -> IOUtils.toString(any(InputStream.class), eq(StandardCharsets.UTF_8)))
502506
.thenThrow(new IOException());
503-
507+
this.mockExecuteListFolder("http://cds-kube-service:8081/mytenant/api/v1/list/testfolder/subfolder", "test.txt");
504508
Assertions.assertThatThrownBy(() -> cdsStorageManager.readFile(testFilePath, false))
505509
.isInstanceOf(EntException.class)
506510
.hasMessageStartingWith("Error extracting text");
@@ -585,5 +589,12 @@ void testListAttributes() throws Throwable {
585589
}
586590
assertTrue(containsCms);
587591
}
592+
593+
private void mockExecuteListFolder(String uri, String filename) {
594+
CdsFileAttributeViewDto file = new CdsFileAttributeViewDto();
595+
file.setName(filename);
596+
Mockito.when(cdsRemoteCaller.getFileAttributeView(eq(URI.create(uri)),
597+
any())).thenReturn(Optional.ofNullable(new CdsFileAttributeViewDto[]{file}));
598+
}
588599

589600
}

engine/src/main/java/org/entando/entando/aps/system/services/storage/LocalStorageManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import org.apache.commons.lang3.CharEncoding;
1818
import org.entando.entando.ent.exception.EntException;
1919
import org.entando.entando.ent.exception.EntRuntimeException;
20-
import org.entando.entando.ent.util.EntLogging.EntLogFactory;
21-
import org.entando.entando.ent.util.EntLogging.EntLogger;
2220

2321
import java.io.*;
2422
import java.util.Arrays;
@@ -52,7 +50,7 @@ public class LocalStorageManager implements IStorageManager, InitializingBean {
5250
private String protectedBaseURL;
5351
private String allowedEditExtensions;
5452

55-
53+
@Override
5654
public void afterPropertiesSet() throws Exception {
5755
logger.info("** Enabled Local Storage Manager **");
5856
}

engine/src/main/java/org/entando/entando/ent/exception/EntResourceNotFoundException.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-Present Entando Inc. (http://www.entando.com) All rights reserved.
2+
* Copyright 2023-Present Entando Inc. (http://www.entando.com) All rights reserved.
33
*
44
* This library is free software; you can redistribute it and/or modify it under
55
* the terms of the GNU Lesser General Public License as published by the Free
@@ -14,11 +14,16 @@
1414
package org.entando.entando.ent.exception;
1515

1616
/**
17-
* Generic Entando Runtime Exception
17+
* Generic Resource Not Found Exception
1818
*/
1919
public class EntResourceNotFoundException extends EntException {
2020

21+
public EntResourceNotFoundException(String message) {
22+
super(message); //NOSONAR
23+
}
24+
2125
public EntResourceNotFoundException(String message, Throwable cause) {
22-
super(message, cause);
26+
super(message, cause); //NOSONAR
2327
}
28+
2429
}

0 commit comments

Comments
 (0)