File Coverage

File:/home/mik/work/module/Tivoli/AccessManager/Admin/Response.pm
Coverage:98.8%

linestmtbrancondsubpodtimecode
1package Tivoli::AccessManager::Admin::Response;
2
15
15
15
155
59
144
use strict;
3
15
15
15
184
58
154
use warnings;
4
15
15
15
188
61
220
use Carp;
5#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6# $Id: Response.pm 305 2006-09-28 19:18:01Z mik $
7#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8$Tivoli::AccessManager::Admin::Response::VERSION = '0.04';
9
15
239
use Inline(C => 'DATA',
10                INC => '-I/opt/PolicyDirector/include',
11                LIBS => ' -lpthread -lpdadminapi -lstdc++',
12                CCFLAGS => '-g -Wall',
13# VERSION => '0.04',
14                NAME => 'Tivoli::AccessManager::Admin::Response',
15
15
15
294
86
          );
16
17my %codes = (
18    INFO => 0,
19    WARNING => 1,
20    ERROR => 2,
21);
22
23sub _get_type {
24
23
148
    my $self = shift;
25
23
126
    my $type = shift;
26
27
23
482
    return 0 unless ( $self->{used} );
28    for ( my $i = 0; $i < $self->response_getcount(); $i++ ) {
29
1
44
        if ( $self->response_getmodifier( $i ) == $codes{$type} ) {
30
1
15
            return 1;
31        }
32
11
67
    }
33
10
256
    return 0;
34}
35
36sub new {
37
1433
1
8618
    my $class = shift;
38
1433
8989
    my $self = {};
39
40
1433
15876
    bless $self, $class;
41
42
1433
15725
    $self->_response();
43
1433
8333
    $self->{isok} = 1;
44
1433
8894
    $self->{iswarning} = 0;
45
1433
6744
    $self->{iserror} = 0;
46
1433
7539
    $self->{used} = 0;
47
1433
12194
    $self->{value}[0] = undef;
48
1433
7173
    $self->{value}[1] = undef;
49
50
1433
12372
    return $self;
51}
52
53sub value {
54
432
1
7638
    my $self = shift;
55
56
432
3064
    return undef unless $self->isok;
57
58
429
2845
    if ( wantarray ) {
59
81
74
759
1672
        return @{$self->{value}[1]} if defined($self->{value}[1]);
60
7
66
        return ();
61    }
62    else {
63
348
7019
        return $self->{value}[0] if defined($self->{value}[0]);
64
13
9
108
154
        return scalar(@{$self->{value}[1]}) if defined($self->{value}[1]);
65
4
49
        return undef;
66    }
67}
68
69sub messages {
70
10
1
149
    my $self = shift;
71
10
60
    my @message = ();
72
73
10
81
    if ( defined $self->{messages} ) {
74
6
6
20
71
        @message = @{$self->{messages}};
75    }
76
77
10
73
    if ($self->{used} ) {
78
6
126
        my $count = $self->response_getcount();
79        for ( my $foo = 0; $foo < $count; $foo++ ) {
80
1
475
            push @message, $self->response_getmessage( $foo );
81
6
24
        }
82    }
83
10
219
    return wantarray ? @message : $message[0];
84}
85
86sub codes {
87
4
1
34
    my $self = shift;
88
4
11
    my @code;
89
90
4
39
    return 0 unless $self->{used};
91
3
21
    if ( wantarray ) {
92        for ( my $i = 0; $i < $self->response_getcount(); $i++ ) {
93
1
46
            push @code, $self->response_getcode($i);
94
2
5
        }
95
2
21
        return @code;
96    }
97    else {
98
1
17
        return $self->response_getcode(0);
99    }
100}
101
102sub isok {
103
2256
1
27685
    my $self = shift;
104
105
2256
19813
    if ( $self->{used} ) {
106
1756
3892014
        return $self->response_getok() && $self->{isok};
107    }
108    else {
109
500
6645
        return $self->{isok};
110    }
111}
112
113sub DESTROY {
114
1433
9413
    my $self = shift;
115
116
1433
18276
    $self->response_free();
117}
118
119sub set_value {
120
752
1
5700
    my $self = shift;
121
122    # I think I want to clear everything out before I start setting it. I
123    # think.
124
752
752
3447
7618
    @{$self->{value}} = ();
125
126    # Pay close attention to the last two conditions -- they are different.
127    # The first condition performs a straight assignment between the values
128    # array and what I was sent. The second one dumps the entire contents of
129    # @_ into value[1]. Very different.
130
752
7812
    if ( @_ == 1 ) {
131
713
3628
        my $foo = shift;
132
713
7674
        $self->{value}[ref($foo) eq 'ARRAY'] = $foo;
133    }
134    elsif ( @_ == 2 and ref($_[1]) eq 'ARRAY' ) {
135
37
37
169
342
        @{$self->{value}} = @_;
136    }
137    elsif ( @_ >= 2 ) {
138
1
1
4
10
        @{$self->{value}[1]} = @_;
139    }
140    else {
141
1
8
        return 0;
142    }
143
751
9449
    return 1;
144}
145
146sub set_message {
147
290
1
1578
    my $self = shift;
148
290
2027
    my @mesgs = @_;
149
150
290
290
1094
4111
    push @{$self->{messages}}, @mesgs;
151}
152
153
265
1
2488
sub set_isok { $_[0]->{isok} = $_[1]; }
154
27
1
294
sub set_iswarning { $_[0]->{iswarning} = $_[1]; }
155
1
1
16
sub set_iserror { $_[0]->{iserror} = $_[1]; }
156
3
1
32
sub iserror { $_[0]->_get_type( "ERROR" ) || $_[0]->{iserror}; }
157
19
1
567
sub iswarning { $_[0]->_get_type( "WARNING" ) || $_[0]->{iswarning}; }
158
1
1
7
sub isinfo { $_[0]->_get_type( "INFO" ); }
159
1601;
161
162 - 372
=head1 NAME

Tivoli::AccessManager::Admin::Response

=head1 SYNOPSIS

    use Tivoli::AccessManager::Admin;

    $resp = Tivoli::AccessManager::Admin::Response->new;

    $resp->iserror and die $resp->messages;

    $resp->set_isok(0);
    $resp->set_message("Line1", "Line2", "Line3");
    $resp->set_iswarning(1);

    $resp->set_value( "foo" );
    print $resp->value;


=head1 DESCRIPTION

L<Tivoli::AccessManager::Admin::Response> is the general purpose object returned by just about
every other method.  It handles the response structures returned from the TAM
API and provides a fair amount of other manipulations.

=head1 CONSTRUCTOR

=head2 new()

Allocates space for the response structure and does the necessary magic to
make it work.

=head3 Parameters

None

=head3 Returns

A fully blessed L<Tivoli::AccessManager::Admin::Response> object.

=head1 METHODS

=head2 value

Retrieves the value stored in the Response object.  It is, as you may suspect,
a read-only method.  As every method in the TAM namespace returns an object of
this type, you will likely only use isok more often.

=head3 Returns

The return should DWYM.  It can return a scalar in scalar context, an array in
list context and undef otherwise.  It tries hard to guess what you meant.  If
you call it in list context and the response object has an array ref, the
right thing happens.

=head2 isok

Indicates if the Response object is .. well, okay.  If the underlying TAM
Response structure has been used, L</"isok"> will return the value of
ivadmin_response_isok logically anded with internal isok value.  If the
structure hasn't been used, L</"isok"> will return the value of its internal isok
flag.

I am still not certain that isok = ! error.

=head3 Returns

True if the flags are so aligned :)

=head2 iserror, iswarning, isinfo

These are read-only methods which return true if the Response object is 
flagged as an error, warning or informational respectively

=head3 Returns

True if the response is an error, a warning or informational. 

=head2 messages

Returns the messages in the Response object.  This is a read-only method.

=head3 Returns

If used in scalar context, only the first message in the message array is
returned.  If used in array context, the full message array will be returned.

=head2 codes

Retrieves the error codes associated with the Response object.  This is a
read-only method.

=head3 Returns

If used in scalar context, only the first code in the code array is
returned.  If used in array context, the full code array will be returned.

=head2 set_value( VALUE | VALUE,ARRAYREF | ARRAY )

Sets the returned value, but does it weirdly.  

If you just send a single parameter, the value will be returned when the
response object is used in scalar context.

If you send a value and an array reference, the value will be returned in
scalar context and the list from the array ref will be returned in list
context.

If you send more than two parameters, the entire list is stored and will be
returned in list context.  Scalar context will give you the number of
elements.

=head2 set_message( STRING[,...] )

Sets the message(s) in the Response object.  You can send any number of
strings to be included in the message.

=head3 Parameters

=over 4

=item STRING[,...]

The message you want to store.

=back

=head3 Returns

I honestly have no idea what it returns.

=head2 set_isok( 0 | 1 )

Sets the isok flag to true or false.

=head3 Parameters

=over 4

=item 0 or 1

Do you want the isok flag to be false or true?

=back

=head3 Returns

The new value of the isok flag although you will likely never need to actually
test the return value 

=head2 set_iswarning( 0|1 )

Sets the iswarning flag to false or true.

=head3 Parameters

=over 4

=item 0 or 1

Do you want the iswarning flag to be false or true?

=back

=head3 Returns

The new value of the iswarning flag, although you will likely never need to
actually test the return value 

=head2 set_iserror( 0|1 )

Sets the iswarning flag to false or true.

=head3 Parameters

=over 4

=item 0 or 1

Do you want the iserror flag to be false or true?

=back

=head3 Returns

The new value of the iserror flag, although you will likely never need to
actually test the return value 

=head1 ACKNOWLEDGEMENTS

See L<Tivoli::AccessManager::Admin> for the list of all the people I am indebted to for their
help while writing these modules.

=head1 BUGS

None known yet.

=head1 AUTHOR

Mik Firestone E<lt>mikfire@gmail.comE<gt>

=head1 COPYRIGHT

Copyright (c) 2004-2011 Mik Firestone.  All rights reserved.  This program is
free software; you can redistibute it and/or modify it under the same terms as
Perl itself.

All references to TAM, Tivoli Access Manager, etc are copyrighted by IBM.

=cut
373