SOAPjr::request - the SOAPjr request object


SOAPjr documentation Contained in the SOAPjr distribution.

Index


Code Index:

NAME

Top

SOAPjr::request - the SOAPjr request object

VERSION

Top

Version 1.0.3

SYNOPSIS

Top

    See perldoc SOAPjr for more info.

AUTHOR

Top

Rob Manson, <robman[at]cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-soapjr at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SOAPjr. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc SOAPjr




You can also look for information at:

* SOAPjr.org

http://SOAPjr.org

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=SOAPjr

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/SOAPjr

* CPAN Ratings

http://cpanratings.perl.org/d/SOAPjr

* Search CPAN

http://search.cpan.org/dist/SOAPjr/

ACKNOWLEDGEMENTS

Top

See http://SOAPjr.org/specs.html for further information on related RFC's and specifications.

COPYRIGHT & LICENSE

Top


SOAPjr documentation Contained in the SOAPjr distribution.
package SOAPjr::request;

use strict;
use warnings;
use File::Basename;
use File::Temp;
use File::Copy;
use URI::Escape;

our $VERSION = "1.0.3";

use base qw(SOAPjr::message);
use Carp;

sub _init {
    my $self = shift;
    $self->{server} = shift;
    my $query = shift;
    $self = $self->SUPER::_init(@_);
    my $update_count = $self->set($query);
    return $self;
}

sub set {
    my $self  = shift;
    my $query = shift;
    my $cgi_query;
    my $count = 0;
    my $json;
    if (ref($query) ne 'HASH' && $query->can("param")) {
        # Make a copy
        $cgi_query = $query;
        my @names = $query->param;
        my %params = ( map { $_ => $query->param($_) } @names );
        $query = { params => \%params };
    }
    if (exists $query->{params}) {
        if (exists $query->{params}->{json} ) {
            my $url_decoded_json = uri_unescape($query->{params}->{json});
            if ($self->{json}->can("decode")) {
                # Modern-ish 2.x JSON API
                $json = $self->{json}->decode( $url_decoded_json );
            } elsif ($self->{json}->can("jsonToObj")) {
                # Olde Version 1.x JSON API
                $json = $self->{json}->jsonToObj( $url_decoded_json );
            } else {
                # TODO: handle unknown JSON API
                carp "WARNING: unknown JSON API";
            }
            if ( $json->{HEAD} ) {
                $self->{_data}->{HEAD} = $json->{HEAD};
            } else {
                carp "WARNING: HEAD missing";
            }
            if ( $json->{BODY} ) {
                $self->{_data}->{BODY} = $json->{BODY};
            } else {
                carp "WARNING: BODY missing";
            }
            # TODO: what about json_type

            # Check for "RELATED" components
            if (exists $json->{HEAD}->{related}) {
                while (my ($k, $v) = each %{$json->{HEAD}->{related}}) {
                    # TODO: handle other types of related content
                    next unless ($v eq 'binary');
                    # Append file data
                    unless ($cgi_query) {
                        carp "WARNING: related item is a file but query not a CGI object";
                    }
                    my $filename = $cgi_query->param($k);
                    my $fh = $cgi_query->upload($k);
                    # Save CGI tmp file into our own tmp file (for lifecycle 
                    # reasons)
                    my $tmp_fh = File::Temp->new(UNLINK => 0);
                    my $tmp_file = $tmp_fh->filename;
                    copy ($fh, $tmp_file) or die $!;
                    close $tmp_fh;
                    $self->{_data}->{BODY}->{$k}->{filepath} = $tmp_file;
                }
            }
        }
    }

    return $self->SUPER::set( $query, $count );
}

1;