SOAPServer.pm 4.18 KB
Newer Older
1 2 3 4
## @file
# SOAP support for Lemonldap::NG::Common::CGI

## @class
5 6 7 8
# Extend SOAP::Transport::HTTP::Server to be able to use posted datas catched
# by CGI module.
# All Lemonldap::NG cgi inherits from CGI so with this library, they can
# understand both browser and SOAP requests.
9 10
package Lemonldap::NG::Common::CGI::SOAPServer;
use SOAP::Transport::HTTP;
11
use base qw(SOAP::Transport::HTTP::Server);
12
use bytes;
13

14
our $VERSION = '1.0.0';
15

Xavier Guimard's avatar
Xavier Guimard committed
16 17
## @method protected void DESTROY()
# Call SOAP::Trace::objects().
18 19
sub DESTROY { SOAP::Trace::objects('()') }

20 21 22
## @cmethod Lemonldap::NG::Common::CGI::SOAPServer new(@param)
# @param @param SOAP::Transport::HTTP::Server::new() parameters
# @return Lemonldap::NG::Common::CGI::SOAPServer object
23 24 25 26 27 28 29 30 31 32 33
sub new {
    my $self = shift;
    return $self if ref $self;

    my $class = ref($self) || $self;
    $self = $class->SUPER::new(@_);
    SOAP::Trace::objects('()');

    return $self;
}

34 35 36 37
## @method void handle(CGI cgi)
# Build SOAP request using CGI->param('POSTDATA') and call
# SOAP::Transport::HTTP::Server::handle() then return the result to the client.
# @param $cgi CGI object
38 39 40 41 42
sub handle {
    my $self = shift->new;
    my $cgi  = shift;

    my $content = $cgi->param('POSTDATA');
43
    my $length  = bytes::length($content);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    if ( !$length ) {
        $self->response( HTTP::Response->new(411) )    # LENGTH REQUIRED
    }
    elsif ( defined $SOAP::Constants::MAX_CONTENT_SIZE
        && $length > $SOAP::Constants::MAX_CONTENT_SIZE )
    {
        $self->response( HTTP::Response->new(413) )   # REQUEST ENTITY TOO LARGE
    }
    else {
        $self->request(
            HTTP::Request->new(
                'POST' => $ENV{'SCRIPT_NAME'},
                HTTP::Headers->new(
                    map {
                        (
                              /^HTTP_(.+)/i
                            ? ( $1 =~ m/SOAPACTION/ )
                                  ? ('SOAPAction')
                                  : ($1)
                            : $_
                          ) => $ENV{$_}
                      } keys %ENV
                ),
                $content,
            )
        );
71
        $self->SUPER::handle();
72 73 74 75 76 77 78 79 80
    }

    print $cgi->header(
        -status => $self->response->code . " "
          . HTTP::Status::status_message( $self->response->code ),
        -type           => $self->response->header('Content-Type'),
        -Content_Length => $self->response->header('Content-Length'),
        -SOAPServer     => 'Lemonldap::NG CGI',
    );
81
    binmode( STDOUT, ":bytes" );
82 83 84 85 86 87
    print $self->response->content;
}

1;
__END__

Xavier Guimard's avatar
POD  
Xavier Guimard committed
88 89
=head1 NAME

Xavier Guimard's avatar
Xavier Guimard committed
90 91
=encoding utf8

Xavier Guimard's avatar
POD  
Xavier Guimard committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
Lemonldap::NG::Common::CGI::SOAPServer - Extends L<SOAP::Lite> to be compatible
with L<CGI>.

=head1 SYNOPSIS

  use CGI;
  use Lemonldap::NG::Common::CGI::SOAPServer;
  
  my $cgi = CGI->new();
  Lemonldap::NG::Common::CGI::SOAPServer->dispatch_to('same as SOAP::Lite')
     ->handle($cgi)

=head1 DESCRIPTION

This extension just extend L<SOAP::Lite> handle() method to load datas from
a L<CGI> object instead of STDIN.

=head1 SEE ALSO

111
L<http://lemonldap-ng.org/>, L<Lemonldap::NG::Common::CGI>
Xavier Guimard's avatar
POD  
Xavier Guimard committed
112 113 114

=head1 AUTHOR

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
=over

=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>

=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=back

=head1 BUG REPORT

Use OW2 system to report bug or ask for features:
L<http://jira.ow2.org>

=head1 DOWNLOAD

Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>
Xavier Guimard's avatar
POD  
Xavier Guimard committed
132 133 134

=head1 COPYRIGHT AND LICENSE

135 136 137 138 139 140 141
=over

=item Copyright (C) 2008, 2009, 2010 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>

=item Copyright (C) 2010, 2012 by Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>

=back
Xavier Guimard's avatar
POD  
Xavier Guimard committed
142 143

This library is free software; you can redistribute it and/or modify
144 145 146 147 148 149 150 151 152 153 154
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see L<http://www.gnu.org/licenses/>.
Xavier Guimard's avatar
POD  
Xavier Guimard committed
155 156

=cut