<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Norwegian Defence Research Establishment / NTNU
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package no.ntnu.okse.protocol.wsn;

import com.google.common.io.ByteStreams;
import org.apache.log4j.Logger;
import org.ntnunotif.wsnu.base.net.XMLParser;
import org.ntnunotif.wsnu.base.util.InternalMessage;
import org.ntnunotif.wsnu.base.util.Utilities;
import org.xmlsoap.schemas.soap.envelope.Body;
import org.xmlsoap.schemas.soap.envelope.Envelope;
import org.xmlsoap.schemas.soap.envelope.Header;
import org.xmlsoap.schemas.soap.envelope.ObjectFactory;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by <NAME> (myth) on 3/19/15.
 * <p>
 * okse is licenced under the MIT licence.
 */
public class WSNRequestParser {

    private static Logger log;

    public WSNRequestParser() {
        log = Logger.getLogger(WSNRequestParser.class.getName());
    }

    public WSNInternalMessage parseMessage(WSNInternalMessage message, OutputStream streamToRequestor) {
        // TODO: Write method to parse and locate which service the message is destined for.

        // We set up the initial returnmessage as having no destination, so we can just return it
        // if we cannot locate where it should go, even though it might be syntactically correct.
        WSNInternalMessage returnMessage = new WSNInternalMessage(InternalMessage.STATUS_FAULT |
                InternalMessage.STATUS_FAULT_INVALID_DESTINATION, null);

        boolean foundRecipient = true;

        // Is it just a request message and has no content
        if ((message.statusCode & InternalMessage.STATUS_HAS_MESSAGE) == 0) {
            log.info("Forwarding request-message...");

            if (foundRecipient) {
                // returnMessage = someService.accept(message);
            } else {
                /*
                for (Service s: services) {
                    returnMessage = s.accept(message);
                    if (returnMessage.statusCode & STATUS_FAULT_INVALID_DESTINATION > 0) {
                        continue;
                    } else if (returnMessage.statusCode & STATUS_OK > 0) {
                        break;
                    } else if (returnMessage.statusCode & STATUS_FAULT_INTERNAL_ERROR > 0) {
                        break;
                    }
                    break;
                }
                 */
            }
        }

        // There is content that should be dealt with
        else {
            log.info("Forwarding message with content...");

            try {

                XMLParser.parse(message);

                try {

                    JAXBElement msg = (JAXBElement) message.getMessage();
                    Class messageClass = msg.getDeclaredType();

                    if (messageClass.equals(org.w3._2001._12.soap_envelope.Envelope.class)) {
                        message.setMessage(msg.getValue());
                    }
                } catch (ClassCastException classcastex) {
                    log.error("Failed to cast a message to a SOAP envelope");
                    return new WSNInternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_FAULT_INVALID_PAYLOAD, null);
                }
            } catch (JAXBException initialJaxbParsingException) {
                log.error("Parsing error: " + initialJaxbParsingException.getLinkedException().getMessage());

                try {
                    XMLParser.writeObjectToStream(Utilities.createSoapFault("Client", "Invalid formatted message"), streamToRequestor);
                    return new WSNInternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_FAULT_INVALID_PAYLOAD, null);<commit_msg>Fix a bug that failed to check if a linked exception was present or not in requestparser<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Norwegian Defence Research Establishment / NTNU
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package no.ntnu.okse.protocol.wsn;

import com.google.common.io.ByteStreams;
import org.apache.log4j.Logger;
import org.ntnunotif.wsnu.base.net.XMLParser;
import org.ntnunotif.wsnu.base.util.InternalMessage;
import org.ntnunotif.wsnu.base.util.Utilities;
import org.xmlsoap.schemas.soap.envelope.Body;
import org.xmlsoap.schemas.soap.envelope.Envelope;
import org.xmlsoap.schemas.soap.envelope.Header;
import org.xmlsoap.schemas.soap.envelope.ObjectFactory;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.stream.XMLStreamException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by <NAME> (myth) on 3/19/15.
 * <p>
 * okse is licenced under the MIT licence.
 */
public class WSNRequestParser {

    private static Logger log;

    public WSNRequestParser() {
        log = Logger.getLogger(WSNRequestParser.class.getName());
    }

    public WSNInternalMessage parseMessage(WSNInternalMessage message, OutputStream streamToRequestor) {
        // TODO: Write method to parse and locate which service the message is destined for.

        // We set up the initial returnmessage as having no destination, so we can just return it
        // if we cannot locate where it should go, even though it might be syntactically correct.
        WSNInternalMessage returnMessage = new WSNInternalMessage(InternalMessage.STATUS_FAULT |
                InternalMessage.STATUS_FAULT_INVALID_DESTINATION, null);

        boolean foundRecipient = true;

        // Is it just a request message and has no content
        if ((message.statusCode & InternalMessage.STATUS_HAS_MESSAGE) == 0) {
            log.info("Forwarding request-message...");

            if (foundRecipient) {
                // returnMessage = someService.accept(message);
            } else {
                /*
                for (Service s: services) {
                    returnMessage = s.accept(message);
                    if (returnMessage.statusCode & STATUS_FAULT_INVALID_DESTINATION > 0) {
                        continue;
                    } else if (returnMessage.statusCode & STATUS_OK > 0) {
                        break;
                    } else if (returnMessage.statusCode & STATUS_FAULT_INTERNAL_ERROR > 0) {
                        break;
                    }
                    break;
                }
                 */
            }
        }

        // There is content that should be dealt with
        else {
            log.info("Forwarding message with content...");

            try {

                XMLParser.parse(message);

                try {

                    JAXBElement msg = (JAXBElement) message.getMessage();
                    Class messageClass = msg.getDeclaredType();

                    if (messageClass.equals(org.w3._2001._12.soap_envelope.Envelope.class)) {
                        message.setMessage(msg.getValue());
                    }
                } catch (ClassCastException classcastex) {
                    log.error("Failed to cast a message to a SOAP envelope");
                    return new WSNInternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_FAULT_INVALID_PAYLOAD, null);
                }
            } catch (JAXBException initialJaxbParsingException) {
                if (initialJaxbParsingException.getLinkedException() != null) {
                    log.error("Parsing error: " + initialJaxbParsingException.getLinkedException().getMessage());
                } else {
                    log.error("Parsing error: " + initialJaxbParsingException);
                }

                try {
                    XMLParser.writeObjectToStream(Utilities.createSoapFault("Client", "Invalid formatted message"), streamToRequestor);
                    return new WSNInternalMessage(InternalMessage.STATUS_FAULT | InternalMessage.STATUS_FAULT_INVALID_PAYLOAD, null);
{'fraction_non_alphanumeric': 0.07722200079713033, 'fraction_numerical': 0.004184934236747709, 'mean_word_length': 2.4349760438056127, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 13}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7058169', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2479, 'n_words': 812}