// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http;

import com.yahoo.config.application.api.ApplicationFile;
import com.yahoo.container.jdisc.HttpResponse;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import static com.yahoo.jdisc.http.HttpResponse.Status.OK;

/**
 * Represents a response for a request to read contents of a file.
 *
 * @author Ulf Lilleengen
 */
public class SessionContentReadResponse extends HttpResponse {

    private static final Map<String, String> contentTypeByExtension = loadContentTypeByExtension();

    private final ApplicationFile file;

    public SessionContentReadResponse(ApplicationFile file) {
        super(OK);
        this.file = file;
    }

    @Override
    public void render(OutputStream outputStream) throws IOException {
        try (InputStream inputStream = file.createInputStream()) {
            inputStream.transferTo(outputStream);
        }
    }

    @Override
    public String getContentType() {
        String filename = file.getPath().getName();
        int lastDotIndex = filename.lastIndexOf('.');
        if (lastDotIndex >= 0) {
            String contentType = contentTypeByExtension.get(filename.substring(lastDotIndex + 1));
            if (contentType != null) return contentType;
        }
        return DEFAULT_MIME_TYPE;
    }

    private static Map<String, String> loadContentTypeByExtension() {
        try {
            Pattern whitespace = Pattern.compile("\\s");
            Map<String, String> map = new HashMap<>();
            for (String line : Files.readAllLines(Paths.get("src/main/resources/mime.types"))) {
                if (line.isEmpty() || line.charAt(0) == '#') continue;

                String[] parts = whitespace.split(line);
                for (int i = 1; i < parts.length; i++)
                    map.putIfAbsent(parts[i], parts[0]);
            }

            return map;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

}

@@ -262,16 +262,47 @@
 ponse;%0A%0A
+import java.io.BufferedReader;%0A
 import j
@@ -364,19 +364,18 @@
 java.io.
-Out
+In
 putStrea
@@ -367,32 +367,38 @@
 a.io.InputStream
+Reader
 ;%0Aimport java.io
@@ -398,36 +398,28 @@
 java.io.
-UncheckedIOException
+OutputStream
 ;%0Aimport
@@ -428,50 +428,31 @@
 ava.
-n
 io.
-file.Files;%0Aimport java.nio.file.Paths
+UncheckedIOException
 ;%0Aim
@@ -1712,18 +1712,85 @@
     
-try %7B%0A    
+ClassLoader classLoader = SessionContentReadResponse.class.getClassLoader();%0A
     
@@ -1846,20 +1846,16 @@
         
- 
-   
 Map%3CStri
@@ -1889,24 +1889,25 @@
 ap%3C%3E();%0A
+%0A
         
     for 
@@ -1902,80 +1902,111 @@
     
-    for (String line : Files.readAllLines(Paths.get(%22src/main/resources/
+try (BufferedReader reader = new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream(%22
 mime
@@ -2015,19 +2015,106 @@
 ypes%22)))
+)
  %7B%0A
+            while (reader.ready()) %7B%0A                String line = reader.readLine();%0A
         

Use class loader instead