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.File;
027import java.io.IOException;
028import java.net.URLDecoder;
029import java.nio.charset.Charset;
030import java.util.Locale;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.apache.http.HttpEntity;
035import org.apache.http.HttpEntityEnclosingRequest;
036import org.apache.http.HttpException;
037import org.apache.http.HttpRequest;
038import org.apache.http.HttpResponse;
039import org.apache.http.HttpStatus;
040import org.apache.http.MethodNotSupportedException;
041import org.apache.http.entity.ContentType;
042import org.apache.http.entity.FileEntity;
043import org.apache.http.entity.StringEntity;
044import org.apache.http.protocol.HttpContext;
045import org.apache.http.protocol.HttpRequestHandler;
046import org.apache.http.util.EntityUtils;
047
048public class SynchroBrowserHandler implements HttpRequestHandler {
049    /* Logger */
050    private static final Log log = LogFactory.getLog(SynchroBrowserHandler.class);
051
052    private final File dataDirectory;
053
054    public SynchroBrowserHandler(final File dataDirectory) {
055        super();
056        this.dataDirectory = dataDirectory;
057    }
058
059    public void handle(
060            final HttpRequest request,
061            final HttpResponse response,
062            final HttpContext context) throws HttpException, IOException {
063
064        String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
065        if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
066            throw new MethodNotSupportedException(method + " method not supported");
067        }
068        String target = request.getRequestLine().getUri();
069
070        if (request instanceof HttpEntityEnclosingRequest) {
071            HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
072            byte[] entityContent = EntityUtils.toByteArray(entity);
073            log.debug("Incoming entity content (bytes): " + entityContent.length);
074        }
075
076        final File file = new File(this.dataDirectory, URLDecoder.decode(target, "UTF-8"));
077        if (!file.exists()) {
078
079            response.setStatusCode(HttpStatus.SC_NOT_FOUND);
080            StringEntity entity = new StringEntity(
081                    "<html><body><h1>File" + file.getPath() +
082                            " not found</h1></body></html>",
083                    ContentType.create("text/html", "UTF-8"));
084            response.setEntity(entity);
085            log.warn("File " + file.getPath() + " not found");
086
087        } else if (!file.canRead() || file.isDirectory()) {
088
089            response.setStatusCode(HttpStatus.SC_FORBIDDEN);
090            StringEntity entity = new StringEntity(
091                    "<html><body><h1>Access denied</h1></body></html>",
092                    ContentType.create("text/html", "UTF-8"));
093            response.setEntity(entity);
094            log.warn("Cannot read file " + file.getPath());
095
096        } else {
097
098            response.setStatusCode(HttpStatus.SC_OK);
099            FileEntity body = new FileEntity(file, ContentType.create("text/html", (Charset) null));
100            response.setEntity(body);
101            log.debug("Serving file " + file.getPath());
102        }
103    }
104
105}