<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF 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.apache.tomcat.websocket;

import java.io.IOException;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.ProtocolHandler;
import javax.servlet.http.WebConnection;
import javax.websocket.Endpoint;

/**
 * Servlet 3.1 HTTP upgrade handler for WebSocket connections.
 */
public class WsProtocolHandler implements ProtocolHandler {

    private final Endpoint ep;
    private final ClassLoader applicationClassLoader;
    private final WsSession wsSession;


    public WsProtocolHandler(Endpoint ep) {
        this.ep = ep;
        applicationClassLoader = Thread.currentThread().getContextClassLoader();
        wsSession = new WsSession(ep);
    }


    @Override
    public void init(WebConnection connection) {
        ServletInputStream sis;
        ServletOutputStream sos;
        try {
            sis = connection.getInputStream();
            sos = connection.getOutputStream();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        WsFrame wsFrame = new WsFrame(sis, wsSession);
        sis.setReadListener(new WsReadListener(this, wsFrame));
        WsRemoteEndpoint wsRemoteEndpoint = new WsRemoteEndpoint(sos);
        wsSession.setRemote(wsRemoteEndpoint);
        sos.setWriteListener(new WsWriteListener(this, wsRemoteEndpoint));

        // Need to call onOpen using the web application's class loader
        Thread t = Thread.currentThread();
        ClassLoader cl = t.getContextClassLoader();
        t.setContextClassLoader(applicationClassLoader);
        try {
            ep.onOpen(wsSession);
        } finally {
            t.setContextClassLoader(cl);
        }
    }


    private void onError(Throwable throwable) {
        // Need to call onError using the web application's class loader
        Thread t = Thread.currentThread();
        ClassLoader cl = t.getContextClassLoader();
        t.setContextClassLoader(applicationClassLoader);
        try {
            ep.onError(throwable);
        } finally {
            t.setContextClassLoader(cl);
        }
    }

    private static class WsReadListener implements ReadListener {

        private final WsProtocolHandler wsProtocolHandler;
        private final WsFrame wsFrame;


        private WsReadListener(WsProtocolHandler wsProtocolHandler,
                WsFrame wsFrame) {
            this.wsProtocolHandler = wsProtocolHandler;
            this.wsFrame = wsFrame;
        }


        @Override
        public void onDataAvailable() {
            try {
                wsFrame.onDataAvailable();
            } catch (IOException e) {
                onError(e);
            }<commit_msg>If the client drops the connection, tell the server the connection is closed.<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF 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.apache.tomcat.websocket;

import java.io.EOFException;
import java.io.IOException;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.ProtocolHandler;
import javax.servlet.http.WebConnection;
import javax.websocket.CloseReason;
import javax.websocket.CloseReason.CloseCodes;
import javax.websocket.Endpoint;

/**
 * Servlet 3.1 HTTP upgrade handler for WebSocket connections.
 */
public class WsProtocolHandler implements ProtocolHandler {

    private final Endpoint ep;
    private final ClassLoader applicationClassLoader;
    private final WsSession wsSession;


    public WsProtocolHandler(Endpoint ep) {
        this.ep = ep;
        applicationClassLoader = Thread.currentThread().getContextClassLoader();
        wsSession = new WsSession(ep);
    }


    @Override
    public void init(WebConnection connection) {
        ServletInputStream sis;
        ServletOutputStream sos;
        try {
            sis = connection.getInputStream();
            sos = connection.getOutputStream();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        WsFrame wsFrame = new WsFrame(sis, wsSession);
        sis.setReadListener(new WsReadListener(this, wsFrame, wsSession));
        WsRemoteEndpoint wsRemoteEndpoint = new WsRemoteEndpoint(sos);
        wsSession.setRemote(wsRemoteEndpoint);
        sos.setWriteListener(new WsWriteListener(this, wsRemoteEndpoint));

        // Need to call onOpen using the web application's class loader
        Thread t = Thread.currentThread();
        ClassLoader cl = t.getContextClassLoader();
        t.setContextClassLoader(applicationClassLoader);
        try {
            ep.onOpen(wsSession);
        } finally {
            t.setContextClassLoader(cl);
        }
    }


    private void onError(Throwable throwable) {
        // Need to call onError using the web application's class loader
        Thread t = Thread.currentThread();
        ClassLoader cl = t.getContextClassLoader();
        t.setContextClassLoader(applicationClassLoader);
        try {
            ep.onError(throwable);
        } finally {
            t.setContextClassLoader(cl);
        }
    }

    private static class WsReadListener implements ReadListener {

        private final WsProtocolHandler wsProtocolHandler;
        private final WsFrame wsFrame;
        private final WsSession wsSession;


        private WsReadListener(WsProtocolHandler wsProtocolHandler,
                WsFrame wsFrame, WsSession wsSession) {
            this.wsProtocolHandler = wsProtocolHandler;
            this.wsFrame = wsFrame;
            this.wsSession = wsSession;
        }


        @Override
        public void onDataAvailable() {
            try {
                wsFrame.onDataAvailable();
            } catch (IOException e) {
                if (e instanceof EOFException){
                    try {
                        wsSession.close(new CloseReason(
                                CloseCodes.CLOSED_ABNORMALLY, e.getMessage()));
                    } catch (IOException e1) {
                        // TODO
                    }
                } else {
                    onError(e);
                }
            }
{'fraction_non_alphanumeric': 0.07075471698113207, 'fraction_numerical': 0.0016573176950535442, 'mean_word_length': 2.5772913816689464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '6983537', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1898, 'n_words': 603}