Package com.draagon.wii.connectors

Source Code of com.draagon.wii.connectors.WIIHttpConnector

/*
* 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.connectors;

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

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

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

//import java.security.Security;

public class WIIHttpConnector implements WIIConnector
{
  private List<NameValuePair> mHeaders = new ArrayList<NameValuePair>();
  private String mContentType = null;
  private WIIRequest mRequest = null;
  private int mStatusCode = 200;

  private static Log log = LogFactory.getLog( WIIHttpConnector.class );

  //static
  //{
    // Configure SSL support
  //  System.setProperty( "java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol" );
  //  Security.addProvider( new com.sun.net.ssl.internal.ssl.Provider() );
  //}

  public WIIHttpConnector()
  {
  }

  public InputStream doRequest( WIIRequest req, List<NameValuePair> headers )
    throws IOException, WIIException
  {
    byte buf[] = new byte[ 1024 ];
    InputStream is = null;

    mRequest = req;

    String base = getExternalURLBase();
    //String content_type = "";

    boolean isPost = false;

    //try
    //{
      if ( mRequest.getMethod().compareToIgnoreCase( "POST" ) == 0 )
        isPost = true;

      // If none exists, then we cannot process the request
      if (base==null || base.equals(""))
          throw new WIIException( "A request path must be provided when calling an external system" );

      // Construct the URL for the external system

      // If a GET was called, check for a query string
      // We need to pass this whether it is a GET or a POST
      // if ( req.getMethod().compareToIgnoreCase( "GET" ) == 0 )
      {
          if ( mRequest.getQueryString() != null )
              base += "?" + mRequest.getQueryString();
      }

      // Open the URL connection
      URL url = new URL( base );
      // URLConnection conn = url.openConnection();
      // System.out.println( "--- create connection: " + url );
      WWWConnection conn = new WWWConnection( url );
      // System.out.println( "--- open connection" );

      conn.open();

      // conn.setDoInput( true );

      // System.out.println( "--- START input parameters" );
      for( NameValuePair pair : headers )
      {
        if ( log.isDebugEnabled() )
          log.debug( "(doRequest) Request header: [" + pair.getName() + "][" + pair.getValue() + "]" );

        conn.setRequestProperty( pair.getName(), pair.getValue() );
      }

      int i = base.indexOf( '/' );
      if ( i > 0 )
      {
        while( base.charAt( i ) == '/' ) i++;

        String tmp = base.substring( i );

        int j = tmp.indexOf( '/' );
        if ( j > 0 ) tmp = tmp.substring( 0, j );

        //ystem.out.println( "HOST: " + tmp );
        conn.setRequestProperty( "host", tmp );
      }

      // System.out.println( "--- END input parameters" );

      // conn.setUseCaches( false );

      // If a POST was called the send that data to the server
      if ( isPost )
      {
          int rc = 0;

          // conn.setDoOutput( true);

          // System.out.println( "--- get input stream" );
          InputStream in = mRequest.getInputStream();
          // System.out.println( "--- get output stream" );
          OutputStream os = conn.getOutputStream();

          // System.out.println( "--- read data" );
          while( ( rc = in.read( buf, 0, 1024 )) >= 0 )
          {
            // System.out.println( "--- write data" );
            os.write( buf, 0, rc );
          }

          // os.close();
      }

      // Return any HTTP Response Headers, including Cookies
      // System.out.println( "--- get input stream" );
      is = conn.getInputStream();
      // System.out.println( "--- get content type" );
      mContentType = conn.getContentType();
      mStatusCode = conn.getStatusCode();

      //while( (head = conn.getHeaderField( i )) != null )
      // System.out.println( "--- Process headers" );
      for ( NameValuePair pair : conn.getHeaders() )
      {
          String name = pair.getName();
          String value = pair.getValue();

          if ( log.isDebugEnabled() )
            log.debug( "(doRequest) Response header: [" + name + "][" + value + "]" );

          if ( name != null
               && name.compareToIgnoreCase( "Keep-Alive" ) != 0
               && name.compareToIgnoreCase( "Connection" ) != 0
               && name.compareToIgnoreCase( "Transfer-Encoding" ) != 0
               && name.compareToIgnoreCase( "Content-Length" ) != 0
               && name.compareToIgnoreCase( "Content-Type" ) != 0
             )
          {
            mHeaders.add( pair );
          }
      }

      return is;
    //}
    //catch( Exception e )
    //{
      // if DEBUG then...
      // e.printStackTrace( System.err );
     // throw new WIIException( e.getMessage() );
    //}
  }

  public List<NameValuePair> getResponseHeaders()
  {
    return mHeaders;
  }

  public String getContentType()
  {
    return mContentType;
  }

  public String getExternalURLBase()
      throws WIIException
  {
    WIISite d = mRequest.getSite();
    if ( d == null ) throw new WIIException( "No site existed to retrieve external URL" );
    return d.getExternalURL( mRequest.getPathInfo() );
  }

  public int getStatusCode()
  {
    return mStatusCode;
  }
}
TOP

Related Classes of com.draagon.wii.connectors.WIIHttpConnector

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.