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>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>

View File

@@ -24,15 +24,20 @@
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #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.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import java.io.File;
import org.alfresco.transform.common.TransformException;
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.Value;
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.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
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
*/
@Service
public class AlfrescoSharedFileStoreClient
public class SharedFileStoreClient
{
private static final Logger logger = LoggerFactory.getLogger(SharedFileStoreClient.class);
@Value("${fileStoreUrl}")
private String fileStoreUrl;
private String url;
@Autowired
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
*
@@ -67,7 +89,7 @@ public class AlfrescoSharedFileStoreClient
{
try
{
return restTemplate.getForEntity(fileStoreUrl + "/" + fileRef,
return restTemplate.getForEntity(url + "/" + fileRef,
org.springframework.core.io.Resource.class);
}
catch (HttpClientErrorException e)
@@ -94,7 +116,7 @@ public class AlfrescoSharedFileStoreClient
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map,
headers);
ResponseEntity<FileRefResponse> responseEntity = restTemplate
.exchange(fileStoreUrl, POST, requestEntity, FileRefResponse.class);
.exchange(url, POST, requestEntity, FileRefResponse.class);
return responseEntity.getBody();
}
catch (HttpClientErrorException e)
@@ -102,4 +124,20 @@ public class AlfrescoSharedFileStoreClient
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;
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.model.FileRefResponse;
import org.alfresco.transform.base.probes.ProbeTransform;
@@ -92,7 +92,7 @@ public class TransformHandler
@Autowired(required = false)
private CustomTransformers customTransformers;
@Autowired
private AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient;
private SharedFileStoreClient alfrescoSharedFileStoreClient;
@Autowired
private TransformRequestValidator transformRequestValidator;
@Autowired

View File

@@ -27,7 +27,7 @@
package org.alfresco.transform.base;
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.RuntimeExec;
import org.alfresco.transform.base.model.FileRefEntity;
@@ -101,7 +101,7 @@ public abstract class AbstractBaseTest
protected ObjectMapper objectMapper;
@MockBean
protected AlfrescoSharedFileStoreClient alfrescoSharedFileStoreClient;
protected SharedFileStoreClient sharedFileStoreClient;
@SpyBean
protected TransformServiceRegistry transformRegistry;
@@ -393,7 +393,7 @@ public abstract class AbstractBaseTest
transformRequestOptions.put(DIRECT_ACCESS_URL, directUrl);
when(alfrescoSharedFileStoreClient.saveFile(any()))
when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
// 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 com.fasterxml.jackson.databind.ObjectMapper;
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.FakeTransformerPdf2Png;
import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf;
@@ -127,7 +127,7 @@ public class TransformControllerTest
@TempDir
public File tempDir;
@MockBean
protected AlfrescoSharedFileStoreClient fakeSfsClient;
protected SharedFileStoreClient fakeSfsClient;
static void resetProbeForTesting(TransformController transformController)
{

View File

@@ -27,7 +27,7 @@
package org.alfresco.transform.base.transform;
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.FakeTransformerFragments;
import org.alfresco.transform.base.messaging.TransformReplySender;
@@ -87,7 +87,7 @@ public class FragmentHandlerTest
private MockMvc mockMvc;
@MockBean
protected AlfrescoSharedFileStoreClient fakeSfsClient;
protected SharedFileStoreClient fakeSfsClient;
@MockBean
private TransformReplySender transformReplySender;
@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(
sourceFile), headers, OK);
when(alfrescoSharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(alfrescoSharedFileStoreClient.saveFile(any()))
when(sharedFileStoreClient.retrieveFile(sourceFileRef)).thenReturn(response);
when(sharedFileStoreClient.saveFile(any()))
.thenReturn(new FileRefResponse(new FileRefEntity(targetFileRef)));
when(mockExecutionResult.getExitValue()).thenReturn(0);

View File

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

View File

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

View File

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