/**
 * Licensed under the Apache License, Version 2.0 (the "License") under
 * one or more contributor license agreements. See the NOTICE file
 * distributed with this work for information regarding copyright
 * ownership. 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 com.msiops.jaxrs.loadbalancer;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;

/**
 * A pre-matching filter that detects load balancer scheme change and sets the
 * incoming scheme accordingly.
 */
@PreMatching()
public class ForwardedProtocolFilter implements ContainerRequestFilter {

    @Override
    public void filter(final ContainerRequestContext requestContext)
            throws IOException {

        final String fproto = requestContext
                .getHeaderString("x-forwarded-proto");
        final String fport = requestContext.getHeaderString("x-forwarded-port");

        final URI in = requestContext.getUriInfo().getRequestUri();

        if (!in.isAbsolute()) {
            /*
             * no point dealing with a non-absolute uri
             */
            return;
        }

        if (in.isOpaque()) {
            /*
             * no point dealing with something definitely not a url
             */
            return;
        }

        final boolean haveHttps;
        if (in.getScheme().equalsIgnoreCase("http")) {
            haveHttps = false;
        } else if (in.getScheme().equalsIgnoreCase("https")) {
            haveHttps = true;
        } else {
            /*
             * not an HTTP(S) request URI, do nothing
             */
            return;
        }

        final boolean wantHttps;
        if (fproto == null) {
            wantHttps = haveHttps;
        } else if (fproto.equalsIgnoreCase("http")) {
            wantHttps = false;
        } else if (fproto.equalsIgnoreCase("https")) {
            wantHttps = true;
        } else {
            /*
             * not an HTTP(S) request URI, do nothing
             */
            return;
        }

        final int havePort;
        if (in.getPort() == -1) {
            havePort = haveHttps ? 443 : 80;
        } else {
            havePort = in.getPort();
        }

        final int wantPort;
        if (fport == null) {
            wantPort = havePort;
        } else {
            try {
                wantPort = Integer.parseInt(fport.trim());
            } catch (final Exception x) {
                // error parsing port, change nothing
                return;
            }
        }

        final String newScheme = haveHttps == wantHttps ? in.getScheme()
                : (wantHttps ? "https" : "http");
        final int stdPort = wantHttps ? 443 : 80;
        final int newPort = wantPort == stdPort ? -1 : wantPort;

        final URI out;
        try {
            out = new URI(newScheme, in.getUserInfo(), in.getHost(), newPort,
                    in.getPath(), in.getQuery(), in.getFragment());
        } catch (final URISyntaxException e) {
            // any problem and we just don't modify the url
            return;
        }
        if (!in.equals(out)) {
            requestContext.setRequestUri(out);
        }

    }
}

@@ -981,16 +981,74 @@
 ching;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 /**%0A * A
@@ -1243,24 +1243,126 @@
 stFilter %7B%0A%0A
+    private static Logger LOG = LoggerFactory%0A            .getLogger(ForwardedProtocolFilter.class);%0A%0A
     @Overrid
@@ -1644,24 +1644,83 @@
 ed-port%22);%0A%0A
+        LOG.debug(%22fproto %7B%7D,  fport %7B%7D%22, fproto, fport);%0A%0A
         fina
@@ -1772,24 +1772,57 @@
 uestUri();%0A%0A
+        LOG.debug(%22in %7B%7D%22, in);%0A%0A
         if (
@@ -3357,32 +3357,173 @@
    %7D%0A        %7D%0A%0A
+        LOG.debug(%22haveHttps %7B%7D,  havePort %7B%7D%22, haveHttps, havePort);%0A        LOG.debug(%22wantHttps %7B%7D,  wantPort %7B%7D%22, wantHttps, wantPort);%0A%0A
         final St
@@ -4069,38 +4069,127 @@
   %7D%0A
-        if (!in.equals(out)) %7B
+%0A        LOG.debug(%22out %7B%7D%22, out);%0A%0A        if (!in.equals(out)) %7B%0A            LOG.debug(%22set request uri to %7B%7D%22, out);
 %0A   

debug logging in filter