Search code examples
javaspring-bootalfresco

call transformer of alfresco (all in one) with java spring boot ( call api or direct using package methods)


I want to resize images for generating thumbnails

I have a spring boot application and use this docker yaml block in my docker compose file:

transform-core-aio:
  image: alfresco/alfresco-transform-core-aio
  environment:
    JAVA_OPTS: " -XX:MinRAMPercentage=50 -XX:MaxRAMPercentage=80"
  ports:
    - "8088:8090"

I can see this test page

enter image description here

and I can convert images via this test page and convert them to another format

but i want to do this action via code.

how can I use this docker to call the alfresco API .

    <!-- https://mvnrepository.com/artifact/org.alfresco/alfresco-transform-core -->
    <dependency>
        <groupId>org.alfresco</groupId>
        <artifactId>alfresco-transform-core</artifactId>
        <version>2.3.0</version>
        <type>pom</type>
    </dependency>

i try to use this but i cant.

is there any block of code that help me to use this docker file?


Solution

  • Alfresco provides several ways to transform documents and files into different formats. Here are two approaches you can use to call the transformer of Alfresco (all in one) with Java Spring Boot:

    1. Calling the Alfresco REST API: Alfresco provides a REST API that allows you to perform a wide range of operations, including transforming documents and files. To transform a document using the Alfresco REST API, you can send a POST request to the /api/-default-/public/alfresco/versions/1/transformations endpoint and include the document to be transformed in the request body. The response will include the transformed document in the requested format.

    Here's some sample code that demonstrates how to use the Alfresco REST API to transform a document:

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    
    // Load the document to be transformed into a byte array
    File file = new File("path/to/document.pdf");
    byte[] documentBytes = FileUtils.readFileToByteArray(file);
    
    // Construct the request body
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("filedata", new ByteArrayResource(documentBytes));
    body.add("sourceMimetype", "application/pdf");
    body.add("targetMimetype", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    
    // Send the POST request to the Alfresco REST API
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(
        "http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/transformations",
        requestEntity,
        byte[].class
    );
    
    // Save the transformed document to a file
    byte[] transformedBytes = responseEntity.getBody();
    File transformedFile = new File("path/to/transformed.docx");
    FileUtils.writeByteArrayToFile(transformedFile, transformedBytes);
    
    1. Using the Alfresco Java API: Alfresco also provides a Java API that allows you to perform a wide range of operations, including transforming documents and files. To transform a document using the Alfresco Java API, you can use the ContentService and TransformerService classes to retrieve the content of the document and transform it into the desired format.

    Here's some sample code that demonstrates how to use the Alfresco Java API to transform a document:

    AuthenticationUtils.setFullyAuthenticatedUser("admin", "admin");
    
    // Get a reference to the Alfresco ContentService
    ContentService contentService = serviceRegistry.getContentService();
    
    // Get a reference to the Alfresco TransformerService
    TransformerService transformerService = serviceRegistry.getTransformerService();
    
    // Load the document to be transformed into a ContentData object
    NodeRef nodeRef = new NodeRef("workspace://SpacesStore/01234567-89ab-cdef-0123-456789abcdef");
    ContentData contentData = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT).getContentData();
    
    // Transform the document into the desired format
    ContentReader contentReader = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
    ContentWriter contentWriter = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
    ContentTransformer transformer = transformerService.getTransformer(contentReader.getMimetype(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    transformer.transform(contentReader, contentWriter);
    
    // Save the transformed document to a file
    byte[] transformedBytes = contentData.getContent();
    File transformedFile = new File("path/to/transformed.docx");
    FileUtils.writeByteArrayToFile(transformedFile, transformedBytes);
    

    Overall, these approaches allow you to call the transformer of Alfresco (all in one) with Java Spring Boot by using either the Alfresco REST API or the Alfresco Java API. By using one or more of these approaches, you can transform documents and files into different formats to meet your specific needs.