/*
 * Copyright 2015 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.glowroot.ui;

import java.io.File;

import javax.annotation.Nullable;

import com.google.common.base.Charsets;
import com.google.common.io.CharSource;
import com.google.common.io.Files;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpUtil;

import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;

class GlowrootLogHttpService implements UnauthenticatedHttpService {

    private final File logDir;

    GlowrootLogHttpService(File logDir) {
        this.logDir = logDir;
    }

    @Override
    public @Nullable FullHttpResponse handleRequest(ChannelHandlerContext ctx, HttpRequest request)
            throws Exception {

        File glowrootLogFile = new File(logDir, "glowroot.log");
        CharSource glowrootLogCharSource = Files.asCharSource(glowrootLogFile, Charsets.UTF_8);

        HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
        response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
        boolean keepAlive = HttpUtil.isKeepAlive(request);
        if (keepAlive && !request.protocolVersion().isKeepAliveDefault()) {
            response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        }
        HttpServices.preventCaching(response);
        ctx.write(response);
        ChannelFuture future =
                ctx.write(ChunkedInputs.from(ChunkSource.from(glowrootLogCharSource)));
        HttpServices.addErrorListener(future);
        if (!keepAlive) {
            HttpServices.addCloseListener(future);
        }
        // return null to indicate streaming
        return null;
    }
}

@@ -13,16 +13,21 @@
 ght 2015
+-2016
  the ori
@@ -51,16 +51,16 @@
 uthors.%0A
-
  *%0A * Li
@@ -655,16 +655,69 @@
 io.File;
+%0Aimport java.util.Collections;%0Aimport java.util.List;
 %0A%0Aimport
@@ -785,16 +785,56 @@
 arsets;%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -1803,57 +1803,335 @@
 n %7B%0A
-%0A        File glowrootLogFile = new File(logDir, 
+        String%5B%5D list = logDir.list();%0A        if (list == null) %7B%0A            throw new IllegalStateException(%0A                    %22Could not list directory: %22 + logDir.getAbsolutePath());%0A        %7D%0A        List%3CString%3E filenames = Lists.newArrayList();%0A        for (String filename : list) %7B%0A            if (filename.matches(
 %22glo
@@ -2140,46 +2140,330 @@
 oot.
+*%5C%5C.
 log%22)
-;%0A        CharSource glowrootLogC
+) %7B%0A                filenames.add(filename);%0A            %7D%0A        %7D%0A        Collections.sort(filenames);%0A        List%3CCharSource%3E charSources = Lists.newArrayList();%0A        for (String filename : filenames) %7B%0A            File file = new File(logDir, filename);%0A            if (file.isFile()) %7B%0A                c
 harS
@@ -2467,19 +2467,22 @@
 arSource
- = 
+s.add(
 Files.as
@@ -2496,20 +2496,9 @@
 rce(
-glowrootLogF
+f
 ile,
@@ -2513,18 +2513,42 @@
 s.UTF_8)
+)
 ;%0A
+            %7D%0A        %7D
 %0A       
@@ -3206,20 +3206,27 @@
 rom(
-glowrootLogC
+CharSource.concat(c
 harS
@@ -3230,16 +3230,18 @@
 arSource
+s)
 )));%0A   

Fix /log url now that log files are daily