package ee.cybernetica.uxp.securityserverloadtesting.service.endpoints;

import ee.cybernetica.uxp.securityserverloadtesting.service.models.response.GetBinaryAttachmentRequest;
import ee.cybernetica.uxp.securityserverloadtesting.service.models.response.GetBinaryAttachmentResponse;
import ee.cybernetica.uxp.securityserverloadtesting.service.repository.AttachmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.server.endpoint.annotation.SoapHeader;

/**
 * Created by Innova on 24-06-2018.
 */
@Endpoint
public class AttachmentEndpoint {
    private static final String NAMESPACE_URI = "http://cybernetica.ee/uxp/securityserverloadtesting/service/models/response";

    private AttachmentRepository attachmentRepository;

    @Autowired
    public AttachmentEndpoint(AttachmentRepository attachmentRepository) {
        this.attachmentRepository = attachmentRepository;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getBinaryAttachmentRequest")
    @ResponsePayload
    public GetBinaryAttachmentResponse getResponse(
            @RequestPayload GetBinaryAttachmentRequest request) {
        GetBinaryAttachmentResponse response = new GetBinaryAttachmentResponse();

        try {
            response.setResponse(attachmentRepository.getAttachmentBySize(request.getSize()));
        } catch (Exception ignored) {

        }


        return response;
    }
}
