Created
December 1, 2022 16:47
-
-
Save cbellone/e08808e41d375e5011e3dee6dc2e7347 to your computer and use it in GitHub Desktop.
SOAP Lambda Handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.claranet.vies.proxy; | |
import com.amazonaws.services.lambda.runtime.Context; | |
import com.amazonaws.services.lambda.runtime.RequestHandler; | |
import com.claranet.vies.proxy.model.ValidationRequest; | |
import com.claranet.vies.proxy.model.ValidationResponse; | |
import eu.europa.ec.vies.CheckVatPortType; | |
import eu.europa.ec.vies.CheckVatService; | |
import jakarta.xml.ws.Holder; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import software.amazon.lambda.powertools.metrics.Metrics; | |
import software.amazon.lambda.powertools.tracing.Tracing; | |
import javax.xml.namespace.QName; | |
public class Handler implements RequestHandler<ValidationRequest, ValidationResponse> { | |
private static final Logger LOGGER = LoggerFactory.getLogger(Handler.class); | |
private static final CheckVatPortType PORT; | |
static { | |
var wsdlLocation = Thread.currentThread().getContextClassLoader() | |
.getResource("wsdl/checkVatService.wsdl"); | |
var qName = new QName("urn:ec.europa.eu:taxud:vies:services:checkVat", "checkVatService"); | |
PORT = new CheckVatService(wsdlLocation, qName).getCheckVatPort(); | |
} | |
public Handler() { | |
Core.getGlobalContext().register(this); | |
} | |
@Metrics(captureColdStart = true) | |
@Tracing | |
@Override | |
public ValidationResponse handleRequest(ValidationRequest input, Context context) { | |
LOGGER.info("Entering handleRequest"); | |
var country = input.getCountry(); | |
var vatNumber = input.getVatNumber(); | |
if (country == null || country.strip().length() != 2 || vatNumber == null || vatNumber.isEmpty()) { | |
throw new IllegalArgumentException("Invalid input"); | |
} | |
try { | |
return performValidation(country, vatNumber); | |
} catch (Exception ex) { | |
LOGGER.error("Error while calling VIES service. Returning 500", ex); | |
throw new RuntimeException(ex); | |
} | |
} | |
@Tracing(segmentName = "Call remote webservice") | |
private static ValidationResponse performValidation(String country, String vatNumber) { | |
var valid = new Holder<Boolean>(); | |
var name = new Holder<String>(); | |
var address = new Holder<String>(); | |
PORT.checkVat( | |
new Holder<>(country), | |
new Holder<>(vatNumber), | |
new Holder<>(), | |
valid, | |
name, | |
address | |
); | |
return new ValidationResponse(valid.value, name.value, address.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment