Class: ViewAction

Inherits:
WebAction show all
Includes:
Assert
Defined in:
lib/ribit/action.rb

Direct Known Subclasses

CancelAction, DefaultAction, StyleSheetAction

Constant Summary collapse

ID =
'view'

Instance Attribute Summary

Attributes inherited from WebAction

#id

Instance Method Summary collapse

Methods included from Assert

assert, #assert, assert_nil, #assert_nil, #assert_not_nil, assert_not_nil, raise_exception

Methods inherited from WebAction

#add_headers

Constructor Details

#initialize(ribitData, ribitConfig, pageCache) ⇒ ViewAction

Returns a new instance of ViewAction.



402
403
404
405
406
407
408
409
410
411
# File 'lib/ribit/action.rb', line 402

def initialize( ribitData, ribitConfig, pageCache )
  assert_not_nil( ribitData, 'RibitData is nil' )
  assert_not_nil( ribitConfig )
  
  @id = ID
  @ribitData = ribitData
  @ribitConfig = ribitConfig
  @pageCache = pageCache
  @logger = RibitLogger.new( ViewAction )
end

Instance Method Details

#decorate_page(page) ⇒ Object



414
415
416
417
418
# File 'lib/ribit/action.rb', line 414

def decorate_page( page )
  # set the base url for whole page (all links are relative to this)
  add_headers( page )
  return false
end

#get_cached_page(pageFullId) ⇒ Object



490
491
492
493
494
495
496
# File 'lib/ribit/action.rb', line 490

def get_cached_page( pageFullId )
  if ( @pageCache == nil )
    return nil
  end
  
  return @pageCache.get( pageFullId )
end

#get_page_id(pageRequest) ⇒ Object



485
486
487
# File 'lib/ribit/action.rb', line 485

def get_page_id( pageRequest )
   return pageRequest.get_requested_page_id
end

#run(pageRequest, pageResponse) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/ribit/action.rb', line 421

def run( pageRequest, pageResponse )
  super( pageRequest, pageResponse )
  
  # get page id and show the page
  pageID = get_page_id( pageRequest )
  
  if ( pageID == nil )
    raise "No page ID defined"
  end
  
  page = @ribitData.get_page_by_id( pageID )
  # check that there exists a page
  if ( page == nil )
    raise "No page for ID=#{pageID}"
  end
  
  # check first from cache
  cachedPage = get_cached_page( page.full_id )
  
  if ( cachedPage != nil )
    @logger.debug( 'Found page from cache for pageId=' + page.full_id )
    
    lastModified = pageRequest.get_header_field( 'If-Modified-Since' )
    if ( lastModified != nil )
      @logger.debug( "Found If-Modified-Since field from HTTP headers" )
      time = Time.httpdate( lastModified )
      
      # NOTE: MODIFIED time contains also fraction of seconds but httpdate
      #       doesn't have them => we have to compare full seconds
      if ( cachedPage.is_time_same_or_after( lastModified ) )
        @logger.debug( "Cached page is up to date" )
        pageResponse.status = 304
        return
      end
    end
  
    pageResponse.set_body( cachedPage.data )
    pageResponse.set_content_type( cachedPage.contentType )
    
    pageResponse.set_header_field( 'Cache-Control', 'public, must-revalidate, proxy-revalidate' )
    pageResponse.set_header_field( 'Last-Modified', cachedPage.lastModfiedHttpdate )
  
  else
    @logger.debug( "Building page for ID=#{page.full_id}" )
    textPage = TextPage.new( @ribitData, page, @ribitConfig )
    messagesInserted = decorate_page( textPage )
      
    textPage.output_page( pageResponse )
    
    # store page to cache if there wasn't any messages
    if ( @pageCache != nil and not messagesInserted )
      # we can safely insert data to cache
      @logger.debug( "Inserting page #{page.full_id} data to cache" )
  
      cachedPage = CachedPage.new( pageResponse.get_body, pageResponse.get_content_type )       
      @pageCache.add( page.full_id, cachedPage )
      
      pageResponse.set_header_field( 'Cache-Control', 'public, must-revalidate, proxy-revalidate' )
      pageResponse.set_header_field( 'Last-Modified', cachedPage.lastModfiedHttpdate )
    end
  end
end