<commit_before>/*
 * Copyright 2014-2016 Groupon, Inc
 *
 * Groupon licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.plugin.adyen.client.notification;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.dom.DOMSource;

import org.killbill.adyen.notification.NotificationRequestItem;
import org.killbill.adyen.notification.ObjectFactory;
import org.killbill.adyen.notification.SendNotification;
import org.killbill.adyen.notification.SendNotificationResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.google.common.base.Charsets;

public class AdyenNotificationService {

    private static final Logger logger = LoggerFactory.getLogger(AdyenNotificationService.class);

    private final List<AdyenNotificationHandler> notificationHandlers;
    private final JAXBContext jaxbContext;

    public AdyenNotificationService(final List<AdyenNotificationHandler> notificationHandlers) throws JAXBException {
        this.notificationHandlers = notificationHandlers;
        this.jaxbContext = JAXBContext.newInstance(SendNotification.class, SendNotificationResponse.class, ObjectFactory.class);
    }

    public String handleNotifications(final String input) {
        final ByteArrayOutputStream outputStream = handleNotifications(new ByteArrayInputStream(input.getBytes(Charsets.UTF_8)));
        return outputStream.toString();
    }

    private ByteArrayOutputStream handleNotifications(final InputStream inputStream) {
        String response = "error";

        SendNotification sendNotification = null;
        try {
            sendNotification = parse(inputStream);
        } catch (final Exception e) {
            logger.warn("Error parsing Adyen notification", e);
        }

        if (sendNotification != null) {
            final List<NotificationRequestItem> listOfNotifications = sendNotification.getNotification()
                                                                                      .getNotificationItems()
                                                                                      .getNotificationRequestItem();

            for (final NotificationRequestItem item : listOfNotifications) {
                try {
                    handleNotification(item);
                } catch (final Exception e) {
                    logger.warn("Error handling notification: eventCode='{}', pspReference='{}', originalReference='{}', success='{}', reason='{}', merchantReference='{}'",
                                item.getEventCode(),
                                item.getPspReference(),
                                item.getOriginalReference(),
                                item.isSuccess(),
                                item.getReason(),
                                item.getMerchantReference(),
                                e);
                }
            }
            response = "[accepted]";
        }

        try {
            return createSendNotificationResponse(response);
        } catch (final Exception e) {
            // Not much we can do. Let Adyen retry.
            throw new RuntimeException(e);
        }
    }

    private void handleNotification(final NotificationRequestItem item) {
        logger.info("Handling notification: eventCode='{}', pspReference='{}', originalReference='{}', success='{}', reason='{}', merchantReference='{}'",
                    item.getEventCode(),
                    item.getPspReference(),
                    item.getOriginalReference(),
                    item.isSuccess(),
                    item.getReason(),
                    item.getMerchantReference());

        final AdyenNotificationHandler adyenNotificationHandler = getAdyenNotificationHandler(item);
        if (adyenNotificationHandler == null) {
            logger.warn("No handler available - ignoring");
            return;
        }

        adyenNotificationHandler.handleNotification(item);
    }

    private AdyenNotificationHandler getAdyenNotificationHandler(final NotificationRequestItem notificationRequestItem) {
        for (final AdyenNotificationHandler handler : notificationHandlers) {
            if (handler.canHandleNotification(notificationRequestItem)) {
                return handler;
            }
        }
        return null;
    }

    private SendNotification parse(final InputStream inputStream) throws ParserConfigurationException, IOException, SAXException, JAXBException {
        final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        final DocumentBuilder builder = builderFactory.newDocumentBuilder();
        final Document document = builder.parse(inputStream);
        final Node sendNotificationNode = document.getElementsByTagNameNS("http://notification.services.adyen.com", "sendNotification").item(0);

        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        // Use SOAPMessage to handle the envelope
        return (SendNotification) unmarshaller.unmarshal(new DOMSource(sendNotificationNode));
    }<commit_msg>Add the Merchant Account to log entries when handling Notifications<commit_after>/*
 * Copyright 2014-2016 Groupon, Inc
 *
 * Groupon licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package org.killbill.billing.plugin.adyen.client.notification;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.dom.DOMSource;

import org.killbill.adyen.notification.NotificationRequestItem;
import org.killbill.adyen.notification.ObjectFactory;
import org.killbill.adyen.notification.SendNotification;
import org.killbill.adyen.notification.SendNotificationResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.google.common.base.Charsets;

public class AdyenNotificationService {

    private static final Logger logger = LoggerFactory.getLogger(AdyenNotificationService.class);

    private final List<AdyenNotificationHandler> notificationHandlers;
    private final JAXBContext jaxbContext;

    public AdyenNotificationService(final List<AdyenNotificationHandler> notificationHandlers) throws JAXBException {
        this.notificationHandlers = notificationHandlers;
        this.jaxbContext = JAXBContext.newInstance(SendNotification.class, SendNotificationResponse.class, ObjectFactory.class);
    }

    public String handleNotifications(final String input) {
        final ByteArrayOutputStream outputStream = handleNotifications(new ByteArrayInputStream(input.getBytes(Charsets.UTF_8)));
        return outputStream.toString();
    }

    private ByteArrayOutputStream handleNotifications(final InputStream inputStream) {
        String response = "error";

        SendNotification sendNotification = null;
        try {
            sendNotification = parse(inputStream);
        } catch (final Exception e) {
            logger.warn("Error parsing Adyen notification", e);
        }

        if (sendNotification != null) {
            final List<NotificationRequestItem> listOfNotifications = sendNotification.getNotification()
                                                                                      .getNotificationItems()
                                                                                      .getNotificationRequestItem();

            for (final NotificationRequestItem item : listOfNotifications) {
                try {
                    handleNotification(item);
                } catch (final Exception e) {
                    logger.warn("Error handling notification: eventCode='{}', pspReference='{}', originalReference='{}', success='{}', reason='{}', merchantReference='{}', merchantAccount='{}'",
                                item.getEventCode(),
                                item.getPspReference(),
                                item.getOriginalReference(),
                                item.isSuccess(),
                                item.getReason(),
                                item.getMerchantReference(),
                                item.getMerchantAccountCode(),
                                e);
                }
            }
            response = "[accepted]";
        }

        try {
            return createSendNotificationResponse(response);
        } catch (final Exception e) {
            // Not much we can do. Let Adyen retry.
            throw new RuntimeException(e);
        }
    }

    private void handleNotification(final NotificationRequestItem item) {
        logger.info("Handling notification: eventCode='{}', pspReference='{}', originalReference='{}', success='{}', reason='{}', merchantReference='{}', merchantAccount='{}'",
                    item.getEventCode(),
                    item.getPspReference(),
                    item.getOriginalReference(),
                    item.isSuccess(),
                    item.getReason(),
                    item.getMerchantReference(),
                    item.getMerchantAccountCode());

        final AdyenNotificationHandler adyenNotificationHandler = getAdyenNotificationHandler(item);
        if (adyenNotificationHandler == null) {
            logger.warn("No handler available - ignoring");
            return;
        }

        adyenNotificationHandler.handleNotification(item);
    }

    private AdyenNotificationHandler getAdyenNotificationHandler(final NotificationRequestItem notificationRequestItem) {
        for (final AdyenNotificationHandler handler : notificationHandlers) {
            if (handler.canHandleNotification(notificationRequestItem)) {
                return handler;
            }
        }
        return null;
    }

    private SendNotification parse(final InputStream inputStream) throws ParserConfigurationException, IOException, SAXException, JAXBException {
        final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        final DocumentBuilder builder = builderFactory.newDocumentBuilder();
        final Document document = builder.parse(inputStream);
        final Node sendNotificationNode = document.getElementsByTagNameNS("http://notification.services.adyen.com", "sendNotification").item(0);

        final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        // Use SOAPMessage to handle the envelope
        return (SendNotification) unmarshaller.unmarshal(new DOMSource(sendNotificationNode));
    }
{'fraction_non_alphanumeric': 0.08407983655508408, 'fraction_numerical': 0.002828854314002829, 'mean_word_length': 2.6954123112659696, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4049999', 'n_tokens_mistral': 3029, 'n_tokens_neox': 2927, 'n_words': 717}