Apache2-REST

 view release on metacpan or  search on metacpan

lib/Apache2/REST/Handler.pm  view on Meta::CPAN

=cut

sub buildNext{
    my ( $self , $frag , $req ) = @_ ;
    ## default implementation
    
    my $newC = $self->class().'::'.$frag ;
    eval "require $newC;";
    if ( $@ ){
        warn "Class $newC not found: $@\n" ;
        return undef ;
    }
    return $newC->new($self) ;
}


=head2 isAuth

Given a method and a request, returns true if this method is allowed.

The default implementation delegates to the parent.

Nothing is allowed by default. So you need to override this method at least once.

It is called by the framework like this (for instance):

$this->isAuth('GET' , $req) ;

=cut

sub isAuth{
    my ( $self , $method , $req  ) = @ _;
    if ( $self->parent()){
        return $self->parent()->isAuth($method , $req ) || 0 ;
    }
    return 0 ;
}

=head2 new

You can override this in subclasses. Do not forget to call $class->SUPER::new() ;

=cut

sub new{
    my ( $class , $parent ) = @_ ;
    my $self = {
        'parent' => $parent ,
    };
    # Enforce the presence of the _conf attribute.
    if ( $parent ){
        $self->{'_conf'} = $parent->conf() ;
    }else{
        $self->{'_conf'} = Apache2::REST::Conf->new()  ;
    }
    return bless $self , $class ;
}

=head2 conf

Get/Sets the configuration attached to this handler.
Or the parent one if no one is defined.

=cut

sub conf{
    my ( $self , $newC ) = @_ ;
    if ( defined $newC ){
        $self->{'_conf'} = $newC ;
    }
    return $self->{'_conf'} || $self->parent()->conf() ;
}

=head2 isTopLevel

Returns true if this handler handles the application top level.

Usage:
    
    if ( $this->isTopLevel() ){ .. }

=cut

sub isTopLevel{
    my ( $self ) = @_ ;
    return ! $self->parent() ;
}


=head2 rootHandler

Returns the root handler processing this request.

=cut

sub rootHandler{
    my ( $self ) = @_ ;
    if ( $self->isTopLevel() ){ return $self ;}
    return $self->parent()->rootHandler() ;
}


=head2 seekByClass
    
Seek for a handler of the given class along the parent path.
Returns the first handler found or undef if nothing found.
    
usage:
    
    my $handler = $self->seekByClass('My::REST::API::myclass') ;

=cut

sub seekByClass{
    my ($self ,$className) = @_ ;
    if ( $self->class() eq $className ){
        return $self ;
    }
    if ( $self->isTopLevel()){ return undef ;}
    return $self->parent()->seekByClass($className) ;
}



( run in 1.755 second using v1.01-cache-2.11-cpan-e1769b4cff6 )