Apache2-REST

 view release on metacpan or  search on metacpan

lib/Apache2/REST/Writer/json.pm  view on Meta::CPAN

Returns the response as json UTF8 bytes for output.

=cut

sub asBytes{
    my ($self,  $resp ) = @_ ;
    
    #Shallow unblessed copy of response
    # JSON wont output blessed object not implementing the TO_JSON request
    my %resp = %$resp ;
    my $coder = JSON::XS->new->allow_blessed(0)->utf8;
    ## These are bytes. This is correct.
    return $coder->encode(\%resp) ;
    
}

1;

lib/Apache2/REST/Writer/xml.pm  view on Meta::CPAN


Returns the response as xml UTF8 bytes for output.

=cut

sub asBytes{
    my ($self,  $resp ) = @_ ;
    my $xmlString =  XMLout($resp , RootName => 'response' ) ;
    # xmlString is a string, not bytes
    # return bytes.
    return Encode::encode_utf8($xmlString) ;
}

1;

lib/Apache2/REST/Writer/xml_stream.pm  view on Meta::CPAN

sub getPreambleBytes{
    my ($self,  $resp ) = @_ ;
    ## To check: escape_value exists in XML::Simple object.
    my $xmlString = q|<?xml version="1.0" encoding="UTF-8" ?>
<response type="streamed" message="|.$self->{xml_simple}->escape_value($resp->message()).q|" status="|.$self->{xml_simple}->escape_value($resp->status()).q|">
|;
    
    $xmlString .=  XMLout($resp->data() , RootName => 'data' ) ;
    # xmlString is a string, not bytes
    # return bytes.
    return Encode::encode_utf8($xmlString) ;
}

sub getPostambleBytes{
    my ($self, $resp) = @_;
    ## Just close the response.
    return Encode::encode_utf8(q|
</response>
|);
}

sub getNextBytes{
    my ($self , $resp) = @_;
    my $nextChunk = $resp->stream->nextChunk();
    unless( defined $nextChunk ){ return undef;}
    unless( ref $nextChunk ){
	confess($resp->stream()."->nextChunk MUST return a chunk of data as a reference, not a binary string");
    }
    my $xmlString = XMLout($nextChunk , RootName => 'chunk' );
    return Encode::encode_utf8($xmlString);
}

1;

lib/Apache2/REST/Writer/yaml.pm  view on Meta::CPAN

Returns the response as yaml UTF8 bytes for output.

=cut

sub asBytes{
    my ($self,  $resp ) = @_ ;
    ## Shallow unblessed copy
    my %resp = %$resp ;
    my $yaml = Dump(\%resp) ;
    ## yaml is a perl string, not bytes.
    return Encode::encode_utf8($yaml) ;
}

1;

lib/Apache2/REST/Writer/yaml_multipart.pm  view on Meta::CPAN

    my ($self,  $resp) = @_;
    my $nextChunk = $resp->multipart_stream->nextChunk();
    unless( defined $nextChunk ){ return undef;}
    unless( ref $nextChunk ){
        confess($resp->stream()."->nextChunk MUST return a chunk of data as a reference, not a binary string");
    }
    # shallow unblessed copy
    my %resp = %$nextChunk;
    my $yaml = Dump(\%resp) ;
    ## yaml is a perl string, not bytes.
    my $data = Encode::encode_utf8($yaml) ;

    # Now, for multipart stuff we return a content type and data
    return {'mimetype' => $PART_MIME_TYPE, 'data' => $data};
}

1;

lib/Apache2/REST/Writer/yaml_stream.pm  view on Meta::CPAN

    return 'text/yaml';
}

=head2 getPreambleBytes

Returns the response preamble - nothing interesting here

=cut

sub getPreambleBytes{
    return Encode::encode_utf8("") ;
}


=head2 getPreambleBytes

Returns the response postamble - nothing interesting here

=cut
sub getPostambleBytes{
    my ($self, $resp) = @_;
    ## Just close the response.
    return Encode::encode_utf8("");
}

=head2 getNextBytes

Returns the next chunk of data as yaml bytes

=cut

sub getNextBytes {
    my ($self,  $resp) = @_;
    my $nextChunk = $resp->stream->nextChunk();
    unless( defined $nextChunk ){ return undef;}
    unless( ref $nextChunk ){
        confess($resp->stream()."->nextChunk MUST return a chunk of data as a reference, not a binary string");
    }
    # shallow unblessed copy
    my %resp = %$nextChunk;
    my $yaml = Dump(\%resp) ;
    ## yaml is a perl string, not bytes.
    return Encode::encode_utf8($yaml) ;
}

1;

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

comes with it's own headers.

It is called by the framework like this ($resp is a Apache2::REST::Response):

    $this->getPreambleBytes($resp) ;

=cut

sub getPreambleBytes{
    my ($self,  $resp ) = @_ ;
    return Encode::encode_utf8("");
}


=head2 getNextPart

Returns the next part of the multipart response, or undef at the end of the stream.
The chunk should be a hash containing 'mimetype' and 'data'.  This allows
the subclass to dictate the mimetype of every chunk and, thus, they
can all be different if desired (an xml doc, then an audio file for ex.).

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

boundary string.

Called by the framework like that:

$this->getPostambleBytes($response);

=cut

sub getPostambleBytes{
    my ($self, $resp) = @_;
    return Encode::encode_utf8("");
}

=head2 handleModPerlResponse

Handles writing this response in a mod perl request object at response time.
This also handles the additional work of crafting the correct multipart
boundaries, etc.

Beware, this method switches STDOUT to binmode.



( run in 1.180 second using v1.01-cache-2.11-cpan-49f99fa48dc )