package MasonX::ApacheHandler::LastFilter;

# $Id: WithCallbacks.pm,v 1.26 2003/02/14 22:49:07 david Exp $

use strict;
use HTML::Mason qw(1.10);
use HTML::Mason::ApacheHandler ();
use HTML::Mason::Exceptions ();
#use MasonX::CallbackHandle;
use Apache::Constants qw(HTTP_OK OK);
#use Params::Validate ();
use Apache::Filter;

use Exception::Class ( 'HTML::Mason::Exception::Callback::InvalidKey' =>
		       { isa => 'HTML::Mason::Exception',
			 description => 'No callback found for callback key',
			 fields => [ 'callback_key' ] },

                       'HTML::Mason::Exception::Callback::Execution' =>
		       { isa => 'HTML::Mason::Exception',
			 description => 'Error thrown by callback',
			 fields => [ 'callback_error' ] },
		     );

use HTML::Mason::MethodMaker( read_only => [qw(default_priority
                                               default_pkg_key
                                               redirected)] );

use vars qw($VERSION @ISA);
@ISA = qw(HTML::Mason::ApacheHandler);

$VERSION = '0.91';

sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_);

   use Sys::Syslog;
    openlog( "masonrequest", "nowait", "user" );
      my ($package, $filename, $line) = caller;
       syslog( "info", "MASONX-new pid %s from %s,%s,%s ", $$, $package, $filename, $line);
       syslog( "info", "MASONX-new self is type %s", ref $self);
    closelog;


    return $self;
}

sub request_args {
    my $self = shift;
    my ($args, $r, $q) = $self->SUPER::request_args(@_);

    return ($args, $r, $q);
}

sub prepare_request {
    my $self = shift;
    my $m = $self->SUPER::prepare_request(@_);
    return $m;
}

sub handler { 
	my $self = shift;

	my $r = Apache::Request->instance($self)->filter_register();

	my ($comp_source, $buffer, $comp, $interp, $fh);

	# read in stuff from previous filter
	$fh = $r->filter_input();
	while (<$fh>) {
		$comp_source .= $_;
	}

	# start a new Interp, using Resolver::Null
	$interp = HTML::Mason::Interp->new(
		resolver_class => 'HTML::Mason::Resolver::Null',
		data_dir => '/tmp'
	);

	# make the component
	$comp = $interp->make_component( comp_source => $comp_source);

	# dump that evaled code into $buffer
	$interp->make_request( out_method => \$buffer , comp => $comp)->exec();

	# finally send it to the client
	# work on code to check for headers, its in AH
	$r->send_http_header('text/plain');
	print $buffer;
	return OK;

} 

1;
