Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 Sonatype, Inc. All rights reserved. This program is licensed to you under the Apache License Version 2.0, and you may not use this file except in compliance with the Apache License Version 2.0. You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the Apache License Version 2.0 is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 
 package org.sonatype.sisu.jetty.custom;
 
 import java.net.URL;
 
 
Simple rule to perform a HTTP to HTTPS redirect. Usable for testing and such, but Jetty has real solutions and handlers to perform this.

Author(s):
jdcasey
 
 public class RedirectToHttpsRule
     extends Rule
 {
     private static final Logger logger = Log.getLoggerRedirectToHttpsRule.class.getName() );
 
     private Integer httpsPort;
 
     public RedirectToHttpsRule()
     {
         setTerminatingtrue );
     }
 
     public int getHttpsPort()
     {
         return ;
     }
 
     public void setHttpsPortint httpsPort )
     {
         .info"HTTPS port set to: {}"httpsPortnull );
 
         this. = httpsPort;
     }
 
     @Override
     public String matchAndApplyString targetHttpServletRequest requestHttpServletResponse response )
         throws IOException
     {
         StringBuffer requestURL = request.getRequestURL();
         .info"Original URL: {}"requestURLnull );
 
         if ( !requestURL.toString().startsWith"https" ) )
         {
             if ( "POST".equalsrequest.getMethod() ) )
             {
                 response.sendError."POST to HTTP not supported. Please use HTTPS"
                     + (  == null ? "" : " (Port: " +  + ")" ) + " instead." );
                 return target;
             }
 
             URL url = new URLrequestURL.toString() );
 
             StringBuilder result = new StringBuilder();
             result.append"https://" ).appendurl.getHost() );
 
             if (  != null )
             {
                 result.append':' ).append );
             }
 
             result.appendurl.getPath() );
 
             String queryString = request.getQueryString();
             if ( queryString != null )
             {
                 .info"Adding query string to redirect: {}"queryStringnull );
                 result.append'?' ).appendqueryString );
             }
 
             .info"Redirecting to URL: {}"resultnull );
             response.sendRedirectresult.toString() );
             return target;
         }
         else
         {
             .info"NOT redirecting. Already HTTPS: {}"requestURLnull );
             return null;
         }
     }
New to GrepCode? Check out our FAQ X