view Lib/IMPL/Web/Handler/RestController.pm @ 198:2ffe6f661605

Implemented IMPL::Web::Handler::RestController fixes in IMPL::Serialization completed IMPL::Web::Application::RestResource added IMPL::Web::Handler::JSONView added IMPL::Web::RestContract
author cin
date Fri, 20 Apr 2012 16:06:36 +0400
parents 6b1dda998839
children e743a8481327
line wrap: on
line source

package IMPL::Web::Handler::RestController;
use strict;

use IMPL::lang qw(:declare :constants);

use IMPL::declare {
	require => {
		HttpException => 'IMPL::Web::Exception',
        NotFoundException => 'IMPL::Web::NotFoundException'
	},
	base => {
		'IMPL::Object' => undef,
		'IMPL::Object::Autofill' => '@_',
		'IMPL::Object::Serializable' => undef
	}	
};

BEGIN {
	public property root => PROP_GET | PROP_OWNERSET;
	public property contract => PROP_GET | PROP_OWNERSET;
}

sub Invoke {
	my ($this,$action) = @_;
	
	my $query = $action->query;
	
	my $method = $query->request_method;
	
	#TODO: path_info is broken for IIS
	my $pathInfo = $query->path_info;
	
	my @segments = split /\//, $pathInfo;
	
	# remove first segment since it's always empty
	shift @segments;
	
	my ($obj,$view) = (pop(@segments) =~ m/(.*?)(?:\.(\w+))?$/);
	
	$action->context->{view} = $view;
	
	my $res = $this->contract->Transform($this->root);
	
	while(@segments) {
		$res = $this->contract->Transform( $res->InvokeHttpMethod('GET',shift @segments,$action) );
		
		die NotFoundException->new() unless $res;
	}
	
	$res = $res->InvokeHttpMethod($method,$obj,$action);
}

1;

__END__

=pod

=head1 NAME

=head1 SYNOPSIS

=head1 DESCRIPTION

Использует C<$ENV{PATH_INFO}> для получения ресурса и вызова метода.


=cut