package org.jasig.portal.channels;

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import com.objectspace.xml.*;
import org.jasig.portal.*;
import org.jasig.portal.layout.*;

import java.net.*;


/**
 * This is a user-defined channel for rendering a web page in an IFrame.
 * For Browsers without support for Inline Frames the channel just presents
 * a link to open in a separate window.
 * 
 */
public class CInlineFrame implements org.jasig.portal.IChannel                            
{ 
  protected String m_sUrl = null;
  private ChannelConfig chConfig = null;

  private static Vector params = null;

  public CInlineFrame()
  {
    params = new Vector();
    params.addElement(new String[] {"URL", "url", "50", "70", "You have chosen to publish a channel that requires you to provide a URL. Please enter the URL for the channel you wish to publish below."} );
    params.addElement(new String[] {"Height", "height", "3", "4", "This channel also requires a height parameter. Please enter the height below."} );
  }

  public void init (ChannelConfig chConfig) {this.chConfig = chConfig;}
  public String getName () {return (String) chConfig.get ("name");}
  public boolean isMinimizable () {return true;}
  public boolean isDetachable () {return true;}
  public boolean isRemovable () {return true;}
  public boolean isEditable () {return false;}
  public boolean hasHelp () {return false;}

  public int getDefaultDetachWidth () {return 0;}
  public int getDefaultDetachHeight () {return 0;}

 

  public Vector getParameters()
  {
    return params;
  }
  
  public void render (HttpServletRequest req, HttpServletResponse res, JspWriter out)
  {    
    try 
    {
      String sBrowser = req.getHeader("User-Agent");
      String sHeight = (String) chConfig.get ("height");
      m_sUrl = (String) chConfig.get ("url");
      String sLine = null;
      URL url = new URL (m_sUrl);
      HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
      BufferedReader theHTML = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
      
      StringBuffer sbHTML = new StringBuffer (1024);
      
      while ((sLine = theHTML.readLine()) != null)
        sbHTML.append (sLine);
      
        String sHTML = sbHTML.toString ();
        if (sHTML != null)
        {
        // This test gets IE 4 and 5 and Netscape 6 into the Iframe world
        // all others get just a link.  This could undoubtedly get refined.
        // IE3 also handled Iframes but I'm not sure of the string it returns.
          if ((sBrowser.indexOf("MSIE 3")>=0)||
              (sBrowser.indexOf(" MSIE 4")>=0) ||
              (sBrowser.indexOf("MSIE 5")>=0) ||
              (sBrowser.indexOf("Mozilla/5")>=0) )
            sHTML = "<IFRAME height=" + sHeight + " width=100% frameborder='no' src='" +
              m_sUrl + "'></IFRAME>";
          else
            sHTML = "<p>This browser does not support inline frames.</p>" +
               "<A href="+ m_sUrl +
               " target='IFrame_Window'>Click this link to view content</A> in a separate window.";
         out.println (sHTML);
      }
      else
      {
        out.println ("<p>The page you chose cannot be rendered within a channel. Please choose another.  <p><i>Note: Pages containing framesets are not allowed.</i>");
      }
    }
    catch (Exception e)
    {
      try
      {
        out.println ("<b>" + m_sUrl + "</b> is currently unreachable.");
        out.println ("Please choose another.");
      }
      catch (Exception ex)
      {
        Logger.log (Logger.ERROR, e);
      }
      
      Logger.log (Logger.ERROR, e);
    }
  }
  
  public void edit (HttpServletRequest req, HttpServletResponse res, JspWriter out)
  {    
    // This channel is not editable
  }
  
  public void help (HttpServletRequest req, HttpServletResponse res, JspWriter out)
  {
    // This channel has no help
  }
  

}
@@ -1002,18 +1002,17 @@
 %224%22, %22Th
-is
+e
  channel
@@ -1016,65 +1016,262 @@
 nel 
-also requires a height parameter. Please enter the height
+width will be determined by its layout but you must specify the height in pixels. %22 +%0A                                    %22Please enter the height below.%22%7D );%0A    params.addElement(new String%5B%5D %7B%22Name%22, %22name%22, %2230%22, %2240%22, %22Please enter a decriptive name
  bel
@@ -1762,17 +1762,16 @@
 rn 0;%7D%0A%0A
- 
 %0A%0A  publ
@@ -1927,17 +1927,16 @@
 %0A    try
- 
 %0A    %7B%0A 

Add the name parameter for publishing