package net.nitram509.controller;

import org.glassfish.jersey.server.ContainerRequest;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.net.URI;

@Provider
public class RedirectHttpsFilter implements ContainerRequestFilter {

  public static final String X_FORWARDED_PROTO = "X-Forwarded-Proto";

  @Context
  HttpServletResponse response;

  @Override
  public void filter(ContainerRequestContext ctx) throws IOException {
    String x_forwarded_proto_header = ctx.getHeaderString(X_FORWARDED_PROTO);
    System.out.println(x_forwarded_proto_header);
    if (x_forwarded_proto_header != null && !x_forwarded_proto_header.startsWith("https")) {
      ContainerRequest request = (ContainerRequest) ctx.getRequest();
      String path = (request.getPath(false) != null) ? request.getPath(false) : "";
      URI baseUri = request.getBaseUri();
      String host_port = baseUri.getHost() + ":" + baseUri.getPort();
      response.sendRedirect("https://" + host_port + "/" + path);
    }
  }

}
@@ -86,202 +86,8 @@
 t;%0A%0A
-import javax.servlet.FilterChain;%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.ServletRequest;%0Aimport javax.servlet.ServletResponse;%0Aimport javax.servlet.http.HttpServletRequest;%0A
 impo
@@ -712,58 +712,8 @@
 O);%0A
-    System.out.println(x_forwarded_proto_header);%0A
     

optimize imports