<commit_before>package org.vitrivr.cineast.api.rest.routes;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.Map;
import org.vitrivr.cineast.api.rest.resolvers.ResolutionResult;
import org.vitrivr.cineast.api.rest.resolvers.Resolver;
import spark.Request;
import spark.Response;
import spark.Route;


public class ResolvedContentRoute implements Route {

  private Resolver resolver;

  public ResolvedContentRoute(Resolver resolver){
    this.resolver = resolver;
  }

  @Override
  public Object handle(Request request, Response response) throws Exception {

    Map<String, String> params = request.params();

    String id = null;

    if (params != null && params.containsKey(":id")){
      id = params.get(":id");
    }else{
      response.status(404);
      return 404;
    }

    ResolutionResult rresult = this.resolver.resolve(id);

    if(rresult == null){
      response.status(404);
      return 404;
    }


    response.type(rresult.mimeType);
    response.header("Transfer-Encoding", "identity");
    InputStream inStream = rresult.stream;

    OutputStream out = response.raw().getOutputStream();

    final ReadableByteChannel inputChannel = Channels.newChannel(inStream);
    final WritableByteChannel outputChannel = Channels.newChannel(out);

    fastCopy(inputChannel, outputChannel);

    out.flush();

    return null;
  }

  public static void fastCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

    while(src.read(buffer) != -1) {
      buffer.flip();
      dest.write(buffer);
      buffer.compact();
    }

    buffer.flip();

    while(buffer.hasRemaining()) {
      dest.write(buffer);
    }
<commit_msg>Changed ResolvedContentRoute to behave more like spark static content serving logic<commit_after>package org.vitrivr.cineast.api.rest.routes;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import org.vitrivr.cineast.api.rest.resolvers.ResolutionResult;
import org.vitrivr.cineast.api.rest.resolvers.Resolver;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.utils.GzipUtils;
import spark.utils.IOUtils;

public class ResolvedContentRoute implements Route {

  private Resolver resolver;

  public ResolvedContentRoute(Resolver resolver) {
    this.resolver = resolver;
  }

  @Override
  public Object handle(Request request, Response response) throws Exception {

    Map<String, String> params = request.params();

    String id;

    if (params != null && params.containsKey(":id")) {
      id = params.get(":id");
    } else {
      response.status(400);
      response.raw().getWriter().write("Bad request");
      response.raw().getWriter().flush();
      return 404;
    }

    ResolutionResult rresult = this.resolver.resolve(id);

    if (rresult == null) {
      response.status(400);
      response.raw().getWriter().write("Bad request");
      response.raw().getWriter().flush();
      return 404;
    }

    response.type(rresult.mimeType);

    try (InputStream inputStream = rresult.stream;
        OutputStream wrappedOutputStream =
            GzipUtils.checkAndWrap(request.raw(), response.raw(), false)) {
      IOUtils.copy(inputStream, wrappedOutputStream);
      wrappedOutputStream.flush();
      response.raw().getOutputStream().close();
    }

    return 200;
{'fraction_non_alphanumeric': 0.11669908307863296, 'fraction_numerical': 0.009447068630175049, 'mean_word_length': 3.8064085447263016, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3750530', 'n_tokens_mistral': 1099, 'n_tokens_neox': 1025, 'n_words': 222}