Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web.portunif.util;
 
Utility class used to redirect an HTTP(s) request to another protocol and endpoint. The following scenarios are supported: (1) http://host:port1 to https://host:port1 (2) https://host:port1 to http://host:port1 (3) http://host:port2 to https://host:port1 (4) https://host:port2 to https://host:port1 (5) http://host:port2 to http://host:port1 (6) https://host:port2 to http://host:port1 This class internally start an NIO Selector listening on an 'external' port to a 'redirect' port. All requests to the external port, independently of the protocol are redirected to the 'redirect' port.

Author(s):
Jeanfrancois Arcand
 
 public class Redirector {
    
    
HTTP end line.
 
     private final static String NEWLINE = "\r\n";
    
    
The IP address the server is running on.
 
     private static String ipAddress = "127.0.0.1";
 
     
     static {
         try{
              = InetAddress.getLocalHost().getHostName();
         } catch (Throwable t){
             ;
         }
     }
    
    
    
Header String.
 
     private final static String headers =  + "Connection:close" + 
                 + "Cache-control: private" + 
                 + ;
    
    
    
HTTP SC_FOUND header.
    private final static ByteBuffer SC_FOUND =
            ByteBuffer.wrap( ("HTTP/1.1 302 Moved Temporarily" 
                + ).getBytes());
    
    
    // --------------------------------------------------------- Constructor --/
    
    
    public Redirector(){
    }

    
    
Redirect a secure request (https) to http or https.

Parameters:
protocolInfo the ProtocolInfo that contains the information about the current protocol state.
    public final void redirectSSL(ProtocolInfo protocolInfothrows IOException{
        String host = parseHost(protocolInfo.byteBuffer);
        if (host == null){
            host =  + ":" + 
                    protocolInfo.socketChannel.socket().getLocalPort();
        }      
        redirectSSL(protocolInfo,
                protocolInfo.isRequestedTransportSecure ? 
                    new String("Location: https://" + host): 
                    new String("Location: http://" + host));
    }
    
    
    
Redirect a secure request (https) to http or https.

Parameters:
protocolInfo the ProtocolInfo that contains the information about the current protocol state.
byteBuffer the bytes response.
    private final void redirectSSL(ProtocolInfo protocolInfo
            String httpHeadersthrows IOException{
        SSLOutputWriter.flushChannel(protocolInfo.socketChannel,
                .slice(),
                protocolInfo.outputBB,
                protocolInfo.sslEngine);
        SSLOutputWriter.flushChannel(protocolInfo.socketChannel,
                ByteBuffer.wrap((httpHeaders + protocolInfo.requestURI + )
                    .getBytes()),
                protocolInfo.outputBB,
                protocolInfo.sslEngine);
    }
   
    
    
Redirect a un-secure request (http) to http or https.

Parameters:
protocolInfo the ProtocolInfo that contains the information about the current protocol state.
    public final void redirect(ProtocolInfo protocolInfo)
            throws IOException{
        String host = parseHost(protocolInfo.byteBuffer);
        if (host == null){
            host =  + ":" + 
                    protocolInfo.socketChannel.socket().getLocalPort();
        }
        redirect(protocolInfo,protocolInfo.isRequestedTransportSecure 
                    ? new String("Location: https://" + host)
                    : new String("Location: http://" + host));
    }
    
    
    
Redirect a secure request (http) to http or https.

Parameters:
protocolInfo the ProtocolInfo that contains the information about the current protocol state.
byteBuffer the bytes response.
    private final void redirect(ProtocolInfo protocolInfo,String httpHeaders)
            throws IOException{
        OutputWriter.flushChannel(protocolInfo.socketChannel,.slice());
        OutputWriter.flushChannel(protocolInfo.socketChannel,
                ByteBuffer.wrap((httpHeaders 
                    + protocolInfo.requestURI + ).getBytes()));
    }
 
    
    
Return the host value, or null if not found.

Parameters:
byteBuffer the request bytes.
    private String parseHost(ByteBuffer byteBuffer){
         boolean isFound = false;
                          
        int curPosition = byteBuffer.position();
        int curLimit = byteBuffer.limit();
        // Rule a - If nothing, return to the Selector.
        if (byteBuffer.position() == 0)
            return ;
       
        byteBuffer.position(0);
        byteBuffer.limit(curPosition);
        
        int state =0;
        int start =0;
        int end = 0;        
        
        try {                         
            byte c;            
            
            // Rule b - try to determine the host header
            while(byteBuffer.hasRemaining()) {
                c = (byte)Ascii.toLower(byteBuffer.get());
                switch(state) {
                    case 0: // Search for first 'h'
                        if (c == 0x68){
                            state = 1;      
                        } else {
                            state = 0;
                        }
                        break;
                    case 1: // Search for next 'o'
                        if (c == 0x6f){
                            state = 2;
                        } else {
                            state = 0;
                        }
                        break;
                    case 2: // Search for next 's'
                        if (c == 0x73){
                            state = 3;
                        } else {
                            state = 0;
                        }
                        break;
                    case 3: // Search for next 't'
                        if (c == 0x74){
                            state = 4;
                        } else {
                            state = 0;
                        }
                        break
                    case 4: // Search for next ':'
                        if (c == 0x3a){
                            state = 5;
                        } else {
                            state = 0;
                        }
                        break;     
                    case 5: // Get the Host                  
                        StringBuilder sb = new StringBuilder();
                        while (c != 0x0d && c != 0x0a) {
                            sb.append((charc);
                            c = byteBuffer.get();
                        }
                        return sb.toString().trim();                        
                    default:
                        throw new IllegalArgumentException("Unexpected state");
                }      
            }
            return null;
        } catch (BufferUnderflowException bue) {
            return null;
        } finally {     
            if ( end > 0 ){
                byteBuffer.position(start);
                byteBuffer.limit(end);
            } else {
                byteBuffer.limit(curLimit);
                byteBuffer.position(curPosition);                               
            }
        }       
    }
    
    
    
Log an exception.
    private void log(Throwable ex){
        Logger logger = SSLSelectorThread.logger();
        if ( logger.isLoggable(.) ){
            logger.log(.,"Redirector",ex);
        }
    }
New to GrepCode? Check out our FAQ X