package net.nitram509.controller;

import org.glassfish.jersey.server.ContainerRequest;

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);
    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);
    }
  }

}
@@ -1010,21 +1010,16 @@
  String 
-host_
 port = b
@@ -1032,23 +1032,38 @@
 .get
-Hos
+Por
 t() 
-+ %22:%22 + 
+%3E 0 ? Integer.toString(
 base
@@ -1075,16 +1075,22 @@
 etPort()
+) : %22%22
 ;%0A      
@@ -1128,13 +1128,34 @@
 %22 + 
-host_
+baseUri.getHost() + %22:%22 + 
 port

fix -1 issue with port