package pingis.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import pingis.config.SubmissionProperties;
import pingis.entities.TmcSubmission;
import pingis.entities.TmcSubmissionStatus;
import pingis.repositories.TmcSubmissionRepository;

import java.util.UUID;

@Service
public class SubmissionSenderService {
    private static final String SUBMISSION_FILENAME = "submission.tar";

    private RestTemplate restTemplate;

    private final SubmissionProperties submissionProperties;

    private final TmcSubmissionRepository submissionRepository;

    @Autowired
    public SubmissionSenderService(RestTemplateBuilder restTemplateBuilder, SubmissionProperties submissionProperties,
                                   TmcSubmissionRepository submissionRepository) {
        this.submissionProperties = submissionProperties;
        this.submissionRepository = submissionRepository;

        restTemplate = restTemplateBuilder
                .rootUri(submissionProperties.getSandboxUrl())
                .build();
    }

    private HttpEntity<MultiValueMap<String, Object>> buildRequestEntity(byte[] packaged, String notifyToken,
                                                                         String notifyUrl) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

        HttpHeaders textHeaders = new HttpHeaders();
        textHeaders.setContentType(MediaType.TEXT_PLAIN);

        HttpHeaders binaryHeaders = new HttpHeaders();
        binaryHeaders.setContentDispositionFormData("file", SUBMISSION_FILENAME);

        map.add("file", new HttpEntity<>(new ByteArrayResource(packaged), binaryHeaders));
        map.add("token", new HttpEntity<>(notifyToken, textHeaders));
        map.add("notify", new HttpEntity<>(notifyUrl, textHeaders));

        return new HttpEntity<>(map);
    }

    public TmcSubmission sendSubmission(byte[] packaged) {
        TmcSubmission submission = new TmcSubmission();
        submission.setId(UUID.randomUUID());
        submission.setStatus(TmcSubmissionStatus.PENDING);

        String notifyUrl = submissionProperties.getNotifyUrl();

        HttpEntity<MultiValueMap<String, Object>> requestEntity = buildRequestEntity(packaged,
                submission.getId().toString(), notifyUrl);

        TmcSubmissionResponse response = restTemplate.postForObject("/tasks.json", requestEntity,
                TmcSubmissionResponse.class);

        if (!response.getStatus().equals(TmcSubmissionResponse.OK)) {
            return null;
        }

        submissionRepository.save(submission);

        return submission;
    }
}

@@ -19,16 +19,73 @@
 vices;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import o
@@ -838,16 +838,107 @@
 rvice %7B%0A
+    private final Logger logger = LoggerFactory.getLogger(SubmissionSenderService.class);%0A%0A
     priv
@@ -2661,24 +2661,101 @@
 .PENDING);%0A%0A
+        logger.debug(%22Created new submission, id: %7B%7D%22, submission.getId());%0A%0A
         Stri
@@ -3103,24 +3103,102 @@
 se.class);%0A%0A
+        logger.debug(%22Received sandbox response: %7B%7D%22, response.getStatus());%0A%0A
         if (
@@ -3251,24 +3251,80 @@
 onse.OK)) %7B%0A
+            logger.error(%22Sandbox submission failed!%22);%0A
             

Add debug logging to SubmissionSenderService.