2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
21 import java.io.IOException;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
27 import org.eclipse.jetty.http.HttpStatus;
28 import org.eclipse.jetty.server.HttpChannel;
29 import org.eclipse.jetty.server.HttpConfiguration;
30 import org.eclipse.jetty.server.HttpConnection;
31 import org.eclipse.jetty.server.Request;
32 import org.eclipse.jetty.util.URIUtil;
35 * Secured Redirect Handler
37 * Using information present in the {@link HttpConfiguration}, will attempt to redirect to the {@link HttpConfiguration#getSecureScheme()} and
38 * {@link HttpConfiguration#getSecurePort()} for any request that {@link HttpServletRequest#isSecure()} == false.
40 public class SecuredRedirectHandler extends AbstractHandler
43 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
45 HttpChannel<?> channel = baseRequest.getHttpChannel();
46 if (baseRequest.isSecure() || (channel == null))
52 HttpConfiguration httpConfig = channel.getHttpConfiguration();
53 if (httpConfig == null)
55 // no config, show error
56 response.sendError(HttpStatus.FORBIDDEN_403,"No http configuration available");
60 if (httpConfig.getSecurePort() > 0)
62 String scheme = httpConfig.getSecureScheme();
63 int port = httpConfig.getSecurePort();
65 String url = URIUtil.newURI(scheme,baseRequest.getServerName(),port,baseRequest.getRequestURI(),baseRequest.getQueryString());
66 response.setContentLength(0);
67 response.sendRedirect(url);
71 response.sendError(HttpStatus.FORBIDDEN_403,"Not Secure");
74 baseRequest.setHandled(true);