package de.cyberport.aem.webshop30.bundle.commerce.servlets; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.util.Map; import java.util.stream.Collectors; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.apache.sling.commons.json.JSONException; import com.amazon.pay.response.ipn.model.AuthorizationNotification; import com.amazon.pay.response.ipn.model.Notification; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; public class Test { public static void main(final String[] args) throws JSONException, JsonParseException, JsonMappingException, IOException, JAXBException, XMLStreamException { String header = "{\"Connection\":\"keep-alive\", \"x-amz-sns-message-type\":\"Notification\"}"; Map headerMap = new ObjectMapper().readValue(header, new TypeReference>() { }); String json = readJson(); // Notification notification = NotificationFactory.parseNotification(headerMap, json); Map notificationDataAsMap = new Gson().fromJson(json, Map.class); String message = (String) notificationDataAsMap.get("Message"); Map messageDataMap = new Gson().fromJson(message, Map.class); readNotificationData((String) messageDataMap.get("NotificationData")); } private static String readJson() throws UnsupportedEncodingException, IOException { InputStream is = new FileInputStream("/Users/srathakrishnan/Documents/Cyberport/Integrations/Saferpay/amazon-pay-sdk-java-master/tst/com/amazon/pay/impl/ipn/json/AuthorizationNotification3.json"); try (BufferedReader buffer = new BufferedReader(new InputStreamReader(is))) { return buffer.lines().collect(Collectors.joining("\n")); } } private static void readNotificationData(String notificationData) throws JAXBException, XMLStreamException { JAXBContext jaxbContext = JAXBContext.newInstance(AuthorizationNotification.class); if (jaxbContext != null) { notificationData = notificationData.replaceAll("xmlns(?:.*?)?=\"https://mws.amazonservices.com/ipn/OffAmazonPayments/2013-01-01\"", ""); System.out.println(notificationData); StringReader reader = new StringReader(notificationData.trim()); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(final ValidationEvent event) { throw new RuntimeException(event.getMessage(), event.getLinkedException()); } }); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); xmlInputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", Boolean.valueOf(false)); xmlInputFactory.setProperty("javax.xml.stream.supportDTD", Boolean.valueOf(false)); xmlInputFactory.setProperty("javax.xml.stream.isValidating", Boolean.valueOf(false)); xmlInputFactory.setProperty("javax.xml.stream.isNamespaceAware", Boolean.valueOf(false)); XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader); Notification notifData = (Notification) unmarshaller.unmarshal(xmlStreamReader); AuthorizationNotification an = (AuthorizationNotification) notifData; System.out.println(an); } } }