Just on SharedFileStoreClient

This commit is contained in:
alandavis
2022-08-15 11:54:43 +01:00
parent 70c29fb5a9
commit ae09667526
11 changed files with 64 additions and 64 deletions

View File

@@ -32,6 +32,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.retry</groupId> <groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId> <artifactId>spring-retry</artifactId>

View File

@@ -24,15 +24,20 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.transform.base.clients; package org.alfresco.transform.base.sfs;
import static org.springframework.http.HttpHeaders.ACCEPT;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA; import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import java.io.File; import java.io.File;
import org.alfresco.transform.common.TransformException; import org.alfresco.transform.common.TransformException;
import org.alfresco.transform.base.model.FileRefResponse; import org.alfresco.transform.base.model.FileRefResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
@@ -40,23 +45,40 @@ import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import javax.annotation.PostConstruct;
/** /**
* Simple Rest client that call Alfresco Shared File Store * Simple Rest client that call Alfresco Shared File Store
*/ */
@Service @Service
public class AlfrescoSharedFileStoreClient public class SharedFileStoreClient
{ {
private static final Logger logger = LoggerFactory.getLogger(SharedFileStoreClient.class);
@Value("${fileStoreUrl}") @Value("${fileStoreUrl}")
private String fileStoreUrl; private String url;
@Autowired @Autowired
private RestTemplate restTemplate; private RestTemplate restTemplate;
private WebClient client;
@PostConstruct
public void init()
{
client = WebClient.builder().baseUrl(url.endsWith("/") ? url : url + "/")
.defaultHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.defaultHeader(ACCEPT, APPLICATION_JSON_VALUE)
.build();
}
/** /**
* Retrieves a file from Shared File Store using given file reference * Retrieves a file from Shared File Store using given file reference
* *
@@ -67,7 +89,7 @@ public class AlfrescoSharedFileStoreClient
{ {
try try
{ {
return restTemplate.getForEntity(fileStoreUrl + "/" + fileRef, return restTemplate.getForEntity(url + "/" + fileRef,
org.springframework.core.io.Resource.class); org.springframework.core.io.Resource.class);
} }
catch (HttpClientErrorException e) catch (HttpClientErrorException e)
@@ -94,7 +116,7 @@ public class AlfrescoSharedFileStoreClient
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map,
headers); headers);
ResponseEntity<FileRefResponse> responseEntity = restTemplate ResponseEntity<FileRefResponse> responseEntity = restTemplate
.exchange(fileStoreUrl, POST, requestEntity, FileRefResponse.class); .exchange(url, POST, requestEntity, FileRefResponse.class);
return responseEntity.getBody(); return responseEntity.getBody();
} }
catch (HttpClientErrorException e) catch (HttpClientErrorException e)
@@ -102,4 +124,20 @@ public class AlfrescoSharedFileStoreClient
throw new TransformException(e.getStatusCode(), e.getMessage(), e); throw new TransformException(e.getStatusCode(), e.getMessage(), e);
} }
} }
@Async
public void asyncDelete(final String fileReference)
{
try
{
logger.debug(" Deleting intermediate file {}", fileReference);
client.delete().uri(fileReference)
.exchange().block();
}
catch (Exception e)
{
logger.error("Failed to delete intermediate file {}: {}", fileReference, e.getMessage());
}
}
} }

View File

@@ -27,7 +27,7 @@
package org.alfresco.transform.base.transform; package org.alfresco.transform.base.transform;
import org.alfresco.transform.base.TransformEngine; import org.alfresco.transform.base.TransformEngine;
import org.alfresco.transform.base.clients.AlfrescoSharedFileStoreClient; import org.alfresco.transform.base.sfs.SharedFileStoreClient;
import org.alfresco.transform.base.messaging.TransformReplySender; import org.alfresco.transform.base.messaging.TransformReplySender;
import org.alfresco.transform.base.model.FileRefResponse; import org.alfresco.transform.base.model.FileRefResponse;
import org.alfresco.transform.base.probes.ProbeTransform; import org.alfresco.transform.base.probes.ProbeTransform;
@@ -92,7 +92,7 @@ public class TransformHandler
@Autowired(required = false) @Autowired(required = false)
private CustomTransformers customTransformers; private CustomTransformers customTransformers;
@Autowired @Autowired
private AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient; private SharedFileStoreClient alfrescoSharedFileStoreClient;
@Autowired @Autowired
private TransformRequestValidator transformRequestValidator; private TransformRequestValidator transformRequestValidator;
@Autowired @Autowired

View File

@@ -27,7 +27,7 @@
package org.alfresco.transform.base; package org.alfresco.transform.base;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.alfresco.transform.base.clients.AlfrescoSharedFileStoreClient; import org.alfresco.transform.base.sfs.SharedFileStoreClient;
import org.alfresco.transform.base.executors.CommandExecutor; import org.alfresco.transform.base.executors.CommandExecutor;
import org.alfresco.transform.base.executors.RuntimeExec; import org.alfresco.transform.base.executors.RuntimeExec;
import org.alfresco.transform.base.model.FileRefEntity; import org.alfresco.transform.base.model.FileRefEntity;
@@ -101,7 +101,7 @@ public abstract class AbstractBaseTest
protected ObjectMapper objectMapper; protected ObjectMapper objectMapper;
@MockBean @MockBean
protected AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient; protected SharedFileStoreClient sharedFileStoreClient;
@SpyBean @SpyBean
protected TransformServiceRegistry transformRegistry; protected TransformServiceRegistry transformRegistry;
@@ -393,7 +393,7 @@ public abstract class AbstractBaseTest
transformRequestOptions.put(DIRECT_ACCESS_URL, directUrl); transformRequestOptions.put(DIRECT_ACCESS_URL, directUrl);
when(alfrescoSharedFileStoreClient.saveFile(any())) when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); .thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
// Update the Transformation Request with any specific params before sending it // Update the Transformation Request with any specific params before sending it

View File

@@ -33,7 +33,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase; import ch.qos.logback.core.AppenderBase;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.alfresco.transform.base.clients.AlfrescoSharedFileStoreClient; import org.alfresco.transform.base.sfs.SharedFileStoreClient;
import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers;
import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png; import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png;
import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf; import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf;
@@ -127,7 +127,7 @@ public class TransformControllerTest
@TempDir @TempDir
public File tempDir; public File tempDir;
@MockBean @MockBean
protected AlfrescoSharedFileStoreClient fakeSfsClient; protected SharedFileStoreClient fakeSfsClient;
static void resetProbeForTesting(TransformController transformController) static void resetProbeForTesting(TransformController transformController)
{ {

View File

@@ -27,7 +27,7 @@
package org.alfresco.transform.base.transform; package org.alfresco.transform.base.transform;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import org.alfresco.transform.base.clients.AlfrescoSharedFileStoreClient; import org.alfresco.transform.base.sfs.SharedFileStoreClient;
import org.alfresco.transform.base.fakes.FakeTransformEngineWithFragments; import org.alfresco.transform.base.fakes.FakeTransformEngineWithFragments;
import org.alfresco.transform.base.fakes.FakeTransformerFragments; import org.alfresco.transform.base.fakes.FakeTransformerFragments;
import org.alfresco.transform.base.messaging.TransformReplySender; import org.alfresco.transform.base.messaging.TransformReplySender;
@@ -87,7 +87,7 @@ public class FragmentHandlerTest
private MockMvc mockMvc; private MockMvc mockMvc;
@MockBean @MockBean
protected AlfrescoSharedFileStoreClient fakeSfsClient; protected SharedFileStoreClient fakeSfsClient;
@MockBean @MockBean
private TransformReplySender transformReplySender; private TransformReplySender transformReplySender;
@MockBean @MockBean

View File

@@ -1,40 +0,0 @@
/*
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2022 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
* -
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* -
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* -
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.transform.base.transform;
import org.alfresco.transform.base.TransformControllerTest;
/**
* Tests {@link TransformHandler}, {@link ProcessHandler} and {@link TransformManagerImpl}.
* Note {@link TransformControllerTest} already provides 'good path' coverage.
*/
public class TransformHandlerTest
{
// @Test
// public void httpTransformRequestUsingDirectAccessUrlTest() throws Exception
// }
}

View File

@@ -367,8 +367,8 @@ public class ImageMagickTest extends AbstractBaseTest
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource( ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK); sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())) when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); .thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0); when(mockExecutionResult.getExitValue()).thenReturn(0);

View File

@@ -52,10 +52,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.alfresco.transform.base.CustomTransformer;
import org.alfresco.transform.base.registry.CustomTransformers; import org.alfresco.transform.base.registry.CustomTransformers;
import org.alfresco.transform.client.model.TransformReply; import org.alfresco.transform.client.model.TransformReply;
import org.alfresco.transform.client.model.TransformRequest; import org.alfresco.transform.client.model.TransformRequest;
@@ -213,8 +211,8 @@ public class LibreOfficeTest extends AbstractBaseTest
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource( ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK); sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())) when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); .thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0); when(mockExecutionResult.getExitValue()).thenReturn(0);

View File

@@ -274,8 +274,8 @@ public class PdfRendererTest extends AbstractBaseTest
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource( ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK); sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())) when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); .thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0); when(mockExecutionResult.getExitValue()).thenReturn(0);

View File

@@ -450,8 +450,8 @@ public class TikaTest extends AbstractBaseTest
ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource( ResponseEntity<Resource> response = new ResponseEntity<>(new FileSystemResource(
sourceFile), headers, OK); sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response); when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any())) when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef))); .thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0); when(mockExecutionResult.getExitValue()).thenReturn(0);