<commit_before>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed 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.openehealth.ipf.platform.camel.ihe.mllp.core;


import org.apache.camel.CamelException;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.component.mina2.Mina2Consumer;
import org.apache.camel.component.mina2.Mina2Helper;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.apache.camel.component.mina2.Mina2Constants.MINA_CLOSE_SESSION_WHEN_COMPLETE;
import static org.apache.camel.component.mina2.Mina2PayloadHelper.getIn;
import static org.apache.camel.component.mina2.Mina2PayloadHelper.getOut;
import static org.apache.camel.util.ExchangeHelper.isOutCapable;

/**
 * This {@link IoFilterAdapter} is used to catch all exceptions occurred inside the {@link MllpConsumer} filterChain,
 * before the message reaches the CamelRoute, and send appropriate response created by defined exceptionHandler
 * either over RouteBuilder exception handling using "consumer.bridgeErrorHandler=true" or directly by
 * "consumer.exceptionHandler=#myExceptionHandlerBean"
 *
 * @author <NAME>
 *
 */
public class MllpExceptionIoFilter extends IoFilterAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(MllpExceptionIoFilter.class);

    private final Mina2Consumer mina2Consumer;

    public MllpExceptionIoFilter(Mina2Consumer mina2Consumer) {
        this.mina2Consumer = mina2Consumer;
    }

    @Override
    public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
        Exchange exchange = mina2Consumer.getEndpoint().createExchange();
        CamelException exception = new CamelException(cause.getMessage());
        exchange.setException(exception);
        mina2Consumer.getExceptionHandler().handleException("", exchange, exception);
        sendResponse(session, exchange);
    }

    private void sendResponse(IoSession session, Exchange exchange) throws Exception {
        boolean disconnect = mina2Consumer.getEndpoint().getConfiguration().isDisconnect();
        Object response = exchange.hasOut()? getOut(mina2Consumer.getEndpoint(), exchange):
                                             getIn(mina2Consumer.getEndpoint(), exchange);
        if (response != null) {
            LOG.debug("Writing body: {}", response);
            Mina2Helper.writeBody(session, response, exchange);
        } else {
            LOG.debug("Writing no response");
            disconnect = Boolean.TRUE;
        }

        // should session be closed after complete?
        Message message = isOutCapable(exchange)? exchange.getOut() : exchange.getIn();
        Boolean close = message.getHeader(MINA_CLOSE_SESSION_WHEN_COMPLETE, Boolean.class);

        // should we disconnect, the header can override the configuration
        if (close != null) {
            disconnect = close;
        }
        if (disconnect) {
            LOG.debug("Closing session when complete at address: {}", mina2Consumer.getAcceptor().getLocalAddress());
            session.close(true);
        }
    }
}<commit_msg>exclude SSL exceptions from NAK response<commit_after>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed 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.openehealth.ipf.platform.camel.ihe.mllp.core;


import org.apache.camel.CamelException;
import org.apache.camel.Exchange;
import org.apache.camel.component.mina2.Mina2Consumer;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLException;

import static java.util.concurrent.TimeUnit.*;
import static org.apache.camel.component.mina2.Mina2Constants.MINA_CLOSE_SESSION_WHEN_COMPLETE;
import static org.apache.camel.component.mina2.Mina2PayloadHelper.getIn;
import static org.apache.camel.component.mina2.Mina2PayloadHelper.getOut;

/**
 * This {@link IoFilterAdapter} is used to catch all exceptions occurred inside the {@link MllpConsumer} filterChain,
 * before the message reaches the CamelRoute, and send appropriate response created by defined exceptionHandler
 * either over RouteBuilder exception handling using "consumer.bridgeErrorHandler=true" or directly by
 * "consumer.exceptionHandler=#myExceptionHandlerBean"
 *
 * @author <NAME>
 *
 */
public class MllpExceptionIoFilter extends IoFilterAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(MllpExceptionIoFilter.class);

    private final Mina2Consumer mina2Consumer;

    public MllpExceptionIoFilter(Mina2Consumer mina2Consumer) {
        this.mina2Consumer = mina2Consumer;
    }

    @Override
    public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
        if (sendResponse(cause)){
            Exception exception = new CamelException(cause.getMessage());
            Exchange exchange = createExchange(exception);
            mina2Consumer.getExceptionHandler().handleException("", exchange, exception);
            sendResponse(session, exchange);
        } else {
            nextFilter.exceptionCaught(session, cause);
        }
    }

    private void sendResponse(IoSession session, Exchange exchange) throws Exception {
        boolean disconnect = mina2Consumer.getEndpoint().getConfiguration().isDisconnect();
        Object response = exchange.hasOut()? getOut(mina2Consumer.getEndpoint(), exchange):
                                             getIn(mina2Consumer.getEndpoint(), exchange);
        if (response != null) {
            WriteFuture future = session.write(response);
            LOG.trace("Waiting for write to complete for body: {} using session: {}", response, session);
            if (!future.awaitUninterruptibly(10, SECONDS)) {
                LOG.warn("Cannot write body: " + response + " using session: " + session);
            }
        } else {
            LOG.debug("Writing no response");
            disconnect = Boolean.TRUE;
        }

        // should session be closed after complete?
        Boolean close = (Boolean)session.getAttribute(MINA_CLOSE_SESSION_WHEN_COMPLETE);
        if (close != null) {
            disconnect = close;
        }
        if (disconnect) {
            LOG.debug("Closing session when complete at address: {}", mina2Consumer.getAcceptor().getLocalAddress());
            session.closeNow();
        }
    }

    private Exchange createExchange(Throwable cause){
        Exchange exchange = mina2Consumer.getEndpoint().createExchange();
        exchange.setException(cause);
        return exchange;
    }

    private boolean sendResponse(Throwable cause){
        return !(cause instanceof SSLException);
    }
}
{'fraction_non_alphanumeric': 0.0893574297188755, 'fraction_numerical': 0.008157630522088354, 'mean_word_length': 3.5407407407407407, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '382838', 'n_tokens_mistral': 2093, 'n_tokens_neox': 2047, 'n_words': 582}