Package com.draagon.wii.connectors

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

/*
* 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.NameValuePair;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

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

  public HttpConnector()
  {
  }

  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 ( req.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 ( req.getQueryString() != null )
              base += "?" + req.getQueryString();
      }

      // Open the URL connection
      URL url = new URL( base );
      URLConnection conn = url.openConnection();

      // conn.setDoInput( true );

      //System.out.println( "--- START input parameters" );
      for( NameValuePair pair : headers )
      {
        //System.out.println( ">>> request header [" + pair.getName() +"][" + pair.getValue() + "]" );
        conn.setRequestProperty( pair.getName(), pair.getValue() );
      }
      //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);

          InputStream in = req.getInputStream();
          OutputStream os = conn.getOutputStream();

          while( ( rc = in.read( buf, 0, 1024 )) >= 0 )
          {
              os.write( buf, 0, rc );

              // System.out.print( new String( buf, 0, rc ));
          }

          os.close();
      }

      // Return any HTTP Response Headers, including Cookies
      String value = null;
      int i = 0;

      while (( value = conn.getHeaderField( i )) != null )
      {
          String name = conn.getHeaderFieldKey( i );
          // String value = conn.getHeaderField( i );

          // if ( isPost )
          //System.out.println( "<<< response header: [" + name + "][" + value + "]" );

          if ( name == null )
          {
            // WARNING!  Process the response code here!
            // mHeaders.addElement( new NameValuePair( null, value ));
            String code = null;
            int j = value.indexOf( ' ' );
            if ( j == -1 ) code = value;
            else code = value.substring( j ).trim();

            j = code.indexOf( ' ' );
            if ( j > 0 ) code = code.substring( 0, j );

            try {
              mStatusCode = Integer.parseInt( code );
            }
            catch( NumberFormatException e ) {
              throw new IOException( "Invalid Status Code [" + code + "]" );
            }
          }
          else 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( new NameValuePair( name, value ));
          }

          i++;
      }

      is = conn.getInputStream();
      mContentType = conn.getContentType();

      return is;
    //}
    //catch( Exception e )
    //{
    //  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.HttpConnector

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.