Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

It is possible before the object is created in the system to access the previously stored binary content from a WebHook. This opens possibilities to set meta data based on the binary document.

Setting up the Webhook Service

The webhook is to be set up according to Preprocessing Metadata using Webhooks. To access the already stored binary content the WebHook-type dms.request.objects.upsert.database-before is suitable.

The following example calculates the MD5 digest of the binary content of all objects that contain content. The repository can only deliver the content of one object at a time; in the case of a batch import, it must be iterated over the individual objects.

Calculate MD5-Digest of binary content before import
@RestController
@RequestMapping("/api")
public class WebhookRestController
{    
    @PostMapping(value = "/process/checkcontent", produces = "application/json;charset=UTF-8", consumes = "application/json")
    public ResponseEntity<?> checkContent(@RequestBody final Map<String, Object> dmsApiObList, @RequestHeader HttpHeaders incomingHeaders) throws Exception
    {
        try
        {
            String authorization = incomingHeaders.getFirst(HttpHeaders.AUTHORIZATION);
            List<Map<String, Object>> apiObjectList = (List<Map<String, Object>>)dmsApiObList.get("objects");

            for (Map<String, Object> apiObject : apiObjectList)
            {
                String objectId = (String)getMap(getMap(apiObject, "properties"), "system:objectId").get("value");

                boolean hasContent = apiObject.get("contentStreams") != null && ((List)apiObject.get("contentStreams")).size() > 0;

                if (hasContent)
                {
                    Map<String, Object> requestObject = new LinkedHashMap<>();
                    LinkedList<Map<String, Object>> requestList = new LinkedList<>();
                    requestList.add(apiObject);
                    requestObject.put("objects", requestList);

                    // @formatter:off
                    String md5Digest = 
                        restTemplate.execute(
                            "http://repository/api/dms/objects/" + objectId, 
                            HttpMethod.POST, 
                            (ClientHttpRequest requestCallback) -> {
                              if (StringUtils.hasLength(authorization))
                              {
                                requestCallback.getHeaders().add(HttpHeaders.AUTHORIZATION, authorization);
                              }
                              requestCallback.getHeaders().setContentType(MediaType.APPLICATION_JSON);
                              requestCallback.getBody().write(this.objectMapper.writeValueAsString(requestObject).getBytes("UTF-8"));
                            }, 
                            new ResponseExtractor<String>()
                            {
                                @Override
                                public String extractData(ClientHttpResponse response) throws IOException
                                {
                                    if (response.getStatusCode().is2xxSuccessful())
                                    {
                                        // calculate MD5 Hash
                                        return DigestUtils.md5DigestAsHex(response.getBody());
                                    }
                                    throw new IllegalStateException("error in getting content: " + response.getRawStatusCode() + " " + response.getStatusText());
                                }
                            }   
                        );
                    // @formatter:on

                    System.out.println("for objectId[" + objectId + "] the md5-digest of content is[" + md5Digest + "]");
                }
            }
        }
        catch (Throwable e)
        {

            return new ResponseEntity<>(e.getMessage(), HttpStatus.UNPROCESSABLE_ENTITY);
        }

        // just return input no changes
        return new ResponseEntity<Map<String, Object>>(dmsApiObList, HttpStatus.OK);
    }
}



  • No labels