001package fr.ifremer.adagio.synchro.socket;
002
003/*
004 * #%L
005 * SIH-Adagio :: Synchronization
006 * $Id:$
007 * $HeadURL:$
008 * %%
009 * Copyright (C) 2012 - 2014 Ifremer
010 * %%
011 * This program is free software: you can redistribute it and/or modify
012 * it under the terms of the GNU Affero General Public License as published by
013 * the Free Software Foundation, either version 3 of the License, or
014 * (at your option) any later version.
015 * 
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Affero General Public License
022 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
023 * #L%
024 */
025
026import java.io.IOException;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.apache.http.ConnectionClosedException;
031import org.apache.http.HttpException;
032import org.apache.http.HttpServerConnection;
033import org.apache.http.protocol.BasicHttpContext;
034import org.apache.http.protocol.HttpContext;
035import org.apache.http.protocol.HttpService;
036
037public class SynchroWorkerThread extends Thread {
038    /* Logger */
039    private static final Log log = LogFactory.getLog(SynchroWorkerThread.class);
040
041    private final HttpService httpservice;
042    private final HttpServerConnection conn;
043
044    public SynchroWorkerThread(
045            final HttpService httpservice,
046            final HttpServerConnection conn) {
047        super();
048        this.httpservice = httpservice;
049        this.conn = conn;
050    }
051
052    @Override
053    public void run() {
054        log.debug("New connection thread");
055        HttpContext context = new BasicHttpContext(null);
056        try {
057            while (!Thread.interrupted() && this.conn.isOpen()) {
058                this.httpservice.handleRequest(this.conn, context);
059            }
060        } catch (ConnectionClosedException ex) {
061            log.warn("Client closed connection");
062        } catch (IOException ex) {
063            log.error("I/O error: " + ex.getMessage());
064        } catch (HttpException ex) {
065            log.error("Unrecoverable HTTP protocol violation: " + ex.getMessage());
066        } finally {
067            try {
068                this.conn.shutdown();
069            } catch (IOException ignore) {
070            }
071        }
072    }
073
074}