Package com.draagon.wii.headers

Source Code of com.draagon.wii.headers.HttpHeaders

/*
* Copyright 2000 Draagon Software, Inc. All Rights Reserved.
*
* This software is the proprietary information of Draagon Software, Inc.
* Use is subject to license terms.
*
*/

package com.draagon.wii.headers;

import com.draagon.wii.*;
import com.draagon.wii.util.NameValuePair;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* The HttpHeaders Request Header object copies specific HTTP request headers
* from the requesting browser and passes them to the external system.  It
* will also pass the X-WII-BASE header, which specifies the base URL
* to use when the external application desires to create a fully qualified
* path.
* <p>
* NOTE: This will need to be modified to support new HTTP request headers
* that are created in new versions of the HTTP protocol.
*
* @author  Doug Mealing
* @version 1.1, 12/17/00
* @see     com.draagon.wii.WIIRequestHeaders
* @since   JDK1.2
*/
public class HttpHeaders implements WIIRequestHeaders, WIIResponseHeaders
{
  @SuppressWarnings("unused")
  private static Log log = LogFactory.getLog( HttpHeaders.class );

  /**
   * Empty constructor
   */
  public HttpHeaders()
  {
  }

  /**
   * Retrieves the new headers based on the headers from the HTTP request.
   * It will also add additional Draagon Web Integrator headers as needed
   *
   * @param req The WIIRequest object representing the client request which can
   *              be used to retrieve the client's request headers
   * @param headers The headers to be sent to the external system
   */
  public void processRequestHeaders( WIIRequest req, List<NameValuePair> headers )
    throws WIIException
  {
    // Remove any Connection & acccept-encoding headers
    for( NameValuePair pair : new ArrayList<NameValuePair>( headers ))
    {
      String name = pair.getName();
      if ( name.compareToIgnoreCase( "connection" ) == 0 )
        headers.remove( pair );
      if ( name.compareToIgnoreCase( "accept-encoding" ) == 0 )
        headers.remove( pair );
    }   

    // Always close the connection
    headers.add( new NameValuePair( "Connection", "close" ));
    headers.add( new NameValuePair( "Accept-Encoding", "" ));

    // WII HTML Specific
    headers.add( new NameValuePair( "X-WII-URL", req.getWIIURL() ));
    headers.add( new NameValuePair( "X-WII-Site-URL", req.getSiteURL() ));
  }

  /**
   * Processes the HTTP response headers that need to be modified
   *
   * @param req  The WII Request object
   * @param res  The WII Response object
   * @param headers  The vector of HTTP response headers
   */
  public void processResponseHeaders( WIIRequest req, WIIResponse res, List<NameValuePair> headers )
    throws WIIException
  {
    for( NameValuePair pair : headers )
    {
      String name = pair.getName();
      String value = pair.getValue();

      // Adjust the Location response header
      if ( name.compareToIgnoreCase( "location" ) == 0 )
      {
        try
        {
          // System.out.println( "LOCATION: " + value );

          WIISite d = req.getSite();
          String external = null;
          if ( d != null ) external = d.getExternalURL( "/" );

          // System.out.println( "EXT URL:  " + external );
          // System.out.println( "WII URL:  " + req.getWIIURL() );
          // System.out.println( "SITE URL: " + req.getSiteURL() );

          if ( value != null && value.length() > 0 && value.charAt( 0 ) == '/' &&
              !value.toLowerCase().startsWith( req.getWIIURL().toLowerCase() ))
          {
            // Redirecting, but to the wrong location
            value = req.getSiteURL() + value;
            pair.setValue( value );
          }
          else if ( external != null && value != null &&
              value.toLowerCase().startsWith( external.toLowerCase() ))
          {
            int x = external.length();
            value = req.getSiteURL() + "/" + value.substring( x );
            // System.out.println( "NEW LOC:  " + value );
            pair.setValue( value );
          }
        }
        catch ( WIIException e ) {}
      }
      else if ( name.compareToIgnoreCase( "set-cookie" ) == 0 )
      {
        int j = value.indexOf( '=' );
        if ( j > 0 )
        {
          String n = value.substring( 0, j );
          String v = value.substring( j + 1 );

          String vv = v.toLowerCase();
          int ii = vv.indexOf( "path" );
          if ( ii > 0 )
          {
            // Look for a preceding ';'
            while ( ii >= 0 && vv.charAt( ii - 1 ) == ' '
                || vv.charAt( ii - 1 ) == ';' ii--;

            // Rip out the path statement for the cookie
            int jj = vv.indexOf( ';', ii + 1 );
            if ( jj > 0 )
              v = v.substring( 0, ii ) + v.substring( jj );
            else
              v = v.substring( 0, ii );

            // After removing the path, add it back
            value = n + "=" + v;
            pair.setValue( value );
          } // end if path found
        } // end if = found
      } // end if set-cookie
    } // end of for loop
  }
}
TOP

Related Classes of com.draagon.wii.headers.HttpHeaders

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.