Class: Dispatcher

Inherits:
Object show all
Defined in:
lib/lapillus/dispatcher.rb

Constant Summary collapse

HTTPHEADER =
{ 'status'=> 'OK', 'Content-Type' => 'text/html; charset=utf-8'}

Class Method Summary collapse

Class Method Details

.dispatch(cgi, application) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lapillus/dispatcher.rb', line 11

def Dispatcher.dispatch(cgi, application)
  #    @@guard.synchronize do
  request_method = cgi.env_table['REQUEST_METHOD']
#    puts "Dispatcher.dispatch(#{cgi},#{application}): request_method=#{request_method}"
#    puts "cgi=#{cgi.to_yaml}"
  case request_method
  when "GET"
    Dispatcher.get(cgi, application)
  when "POST"
    Dispatcher.post(cgi, application)
  else
    raise "unexpected request method: "+request_method
  end
  #    end
end

.get(cgi, application) ⇒ Object

TODO: remove duplication between get and post TODO: set request cycle to nil when done TODO: remove duplication between new page and respond to event



30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lapillus/dispatcher.rb', line 30

def Dispatcher.get(cgi, application)
  #    log = WEBrick::Log.new
  #    log.info("URI path= #{cgi.env_table['PATH_INFO']}")
  
  #puts;puts cgi.env_table.each {|k,v| puts k.to_s+":"+v.to_s};puts    
  
  session = Dispatcher.get_session(cgi)
  
  request_cycle = RequestCycle.new(session)
  RequestCycle.set(request_cycle)
  path_info = cgi.env_table['PATH_INFO']
  
  if cgi.has_key?'listener'
    puts "AJAXREQUEST!   "+cgi['listener'].to_s
    
    pathparts=cgi.env_table['REQUEST_URI'].split('/')
    if pathparts.length==2 
      page_identifier = application.homepage.to_s
    else
      pagepath = pathparts[2]
      page_identifier = application.bookmarkable_pages[pagepath].to_s
    end
    
    page = restore_page_from_session(page_identifier, path_info, session)
    path_to_listener = cgi['listener']
    listener = page[path_to_listener]
    #TODO: this should be moved to the listener
    #TODO: we should always store the session whether we
    #rerender something or not
    puts "!!"+listener.class.to_s
    path_to_component = cgi['component']
    #NOTICE: non existing cgi variables are return as empty string
    if cgi.has_key?'menuitem'
      listener.on_menu(cgi['menuitem'])
      render_page_and_store_in_session(request_cycle, page, session, cgi, path_info)
      return
    else
      if path_to_component!=""
        component = page[path_to_component]
        listener.on_drop(component)
      elsif cgi.has_key? 'event'
        parameters=Hash.new
        unnecessary_keys=['listener','event']
        cgi.keys.each{|key|
          parameters[key]=cgi[key] unless unnecessary_keys.include? key
        }
        listener.send(cgi['event'], parameters)        
      elsif cgi.has_key? 'periodic_update'
        #puts "[#{Time.new}] has it changed yet?"
      else
        listener.on_click
      end
      store_page_in_session(page,path_info,session)
      
      cgi.out(HTTPHEADER) { listener.render_component }
      #puts "Dispatcher.get: listener.render_component=#{listener.render_component}"
      return
    end
  end
  
  pathparts=path_info.split('/')
  if pathparts.length<=2 
    page_identifier = application.homepage.to_s
  else
    pagepath = pathparts[2]
    page_identifier = application.bookmarkable_pages[pagepath].to_s
  end
  
  pathparts=path_info.split('/')
#    puts "pathparts.length #{pathparts.length}"
#    puts pathparts.join("::")
  if (pathparts.length<2)
    page = application.homepage.new
  else
    page_params = Hash.new
    pagepath = pathparts[1]
    (2..pathparts.length-1).step(2){|i| page_params[pathparts[i]]=pathparts[i+1]}
#      puts pagepath
    page = application.bookmarkable_pages[pagepath].new(page_params)
  end
  
  render_page_and_store_in_session(request_cycle, page, session, cgi, path_info)
end

.post(cgi, application) ⇒ Object

TODO: add test!



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lapillus/dispatcher.rb', line 115

def Dispatcher.post(cgi, application)
  session = Dispatcher.get_session(cgi)
  
  request_cycle = RequestCycle.new(session)
  RequestCycle.set(request_cycle)
#    puts "cgi_page=#{cgi['page'].string}"
  page = restore_page_from_session(cgi['page'].string, cgi.env_table['PATH_INFO'], session)
  
  values = Hash.new()
  cgi.keys.each { |key| values[key] = cgi[key]}
  page.post(values)
  path_info = cgi.env_table['PATH_INFO']
  render_page_and_store_in_session(request_cycle, page, session, cgi, path_info)
end