summaryrefslogtreecommitdiff
path: root/app/controllers/messages_controller.rb
blob: 7eecc7bbe7dc110792795dcce637acace7d6578e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# Copyright (C) 2007, 2008, 2009, 2010 Heiko Bernloehr (FreeIT.de).
# 
# This file is part of ECS.
# 
# ECS is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# ECS is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public
# License along with ECS. If not, see <http://www.gnu.org/licenses/>.


class MessagesController < ApplicationController

  before_filter :late_initialize
  before_filter :authentication
  before_filter :add_cookie_header
  before_filter :get_record, :only => [:show, :update, :destroy]
  after_filter  :touch_participant_ttl

  def initialize
    super
    @render_cmd=nil
  end

  def index
    index_querystring_list
    @list.each do |li| 
      @body << @ressource_name << "/" << li.id.to_s << "\n"
    end unless @list.empty?
    index_render
  end

  def show
    # TODO Webcache via stale? and fresh_when
    @memberships = Membership.receiver(@participant.id, @record.id)
    case
    when @record.outtimed_auths_resource_by_non_owner?(@app_namespace, @resource_name, @participant)
      raise Ecs::OuttimedAuthsException, 'Authorization token outtimed'
    when (!@memberships.empty? or @participant.sender?(@record))
      @record.filter(__method__, @app_namespace, @ressource_name, params)
      @body = @record.body 
      show_render
      eval(@render_cmd) unless @render_cmd.blank?
    else
      raise Ecs::AuthorizationException, 
            "You are not allowed to access this resource, " +
            "because you are not the original sender or a receiver."
    end
  end


  # Create and save a new message. Then render "Created 201" response.
  # TODO exceptions for: create, constantize
  def create
    @record= Message.create__(request, @app_namespace, @ressource_name, @participant)
    @body = @record.body
    create_render
  end

  def update
    @record.update__(request, @app_namespace, @ressource_name, @participant)
    update_render
  end

  def destroy
    @body = @record.body
    @memberships = Membership.receiver(@participant.id, @record.id)
    show_render
    case
    when @record.outtimed_auths_resource_by_non_owner?(@app_namespace, @resource_name, @participant)
      @record.destroy_as_receiver(@participant)
      raise Ecs::OuttimedAuthsException, 'Authorization token outtimed'
    when (@participant.sender?(@record) and not @participant.receiver?(@record))
      @record.destroy_as_sender
    else
      @record.destroy_as_receiver(@participant)
    end
    eval(@render_cmd) unless @render_cmd.blank?
  end

  def fifo
    queue(:queue_type => :fifo)
  end

  def lifo
    queue(:queue_type => :lifo)
  end

  def details
    details = nil
    no_data_to_render = false
    if params["id"]
      # member subresource
      details = member_subresource_details(params["id"])
      if !details[:receivers] then  no_data_to_render = true end
    else
      index_querystring_list
      # collection subresource
      details ||= []
      @list.each do |li| 
        details << member_subresource_details(li.id)
      end unless @list.empty?
      if details.empty? then  no_data_to_render = true end
    end
    if no_data_to_render
      render :text => "", :content_type => "application/json", :layout => false
    else
      respond_to do |format|
        format.json  { render :json  => JSON.pretty_generate(details) }
        format.xml   { render :xml   => details }
      end
    end
  end

protected

  def member_subresource_details(record_id)
    get_record(record_id)
    if @participant.sender?(@record) or @participant.receiver?(@record)
      receivers=[]
      senders=[]
      Membership.receivers(@record.id).each do |recv|
        receivers << { :pid => recv.participant.id, :mid => recv.id, :cid => recv.community_id,
                       :itsyou => recv.participant_id == @participant.id }
        senders << { :mid => Membership.find_by_participant_id_and_community_id(@record.sender, recv.community_id).id }
      end
      content_type = @record.content_type
      url = @ressource_name + "/" + record_id.to_s
      { :receivers => receivers,
        :senders => senders,
        :content_type => content_type,
        :url => url,
        :owner => { :itsyou => @participant.id == @record.sender,
                    :pid => @record.sender }
      }
    else
      raise Ecs::AuthorizationException, 
            "You are not allowed to access this resource, " +
            "because you are not the original sender or a receiver."
    end
  end

  def index_querystring_list
    header_querystrings = request.headers["X-EcsQueryStrings"]
    if header_querystrings
      hqs = header_querystrings.split(",").map{|s| s.strip}.map{|s| s.split("=").map{|s| s.strip}}
      sender = (m=hqs.assoc("sender")) ? m[1] : nil
      receiver = (m=hqs.assoc("receiver")) ? m[1] : nil
      all = (m=hqs.assoc("all")) ? m[1] : nil
    end
    sender ||= params["sender"] ? params["sender"] : nil
    receiver ||= params["receiver"] ? params["receiver"] : nil
    all ||= params["all"] ? params["all"] : nil
    case
    when sender == "true"
      @list = Message.for_participant_sender(@participant).for_resource(@app_namespace,@ressource_name).for_not_removed.uniq
    when receiver == "true"
      @list = Message.for_participant_receiver(@participant).for_resource(@app_namespace,@ressource_name).for_not_removed.uniq
    when all == "true"
      list1 = Message.for_participant_sender(@participant).for_resource(@app_namespace,@ressource_name).for_not_removed
      list2 = Message.for_participant_receiver(@participant).for_resource(@app_namespace,@ressource_name).for_not_removed
      @list = list1.concat(list2).uniq
    else
      @list = Message.for_participant_receiver(@participant).for_resource(@app_namespace,@ressource_name).for_not_removed.uniq
    end
  end

  def queue(queue_options = {:queue_type => :fifo})
    begin
      Message.transaction do
        # returned record holds a lock (pessimistic locking)
        @record = Message.fifo_lifo_rest(@app_namespace, @ressource_name,@participant.id, queue_options)
        if @record
          @memberships = Membership.receiver(@participant.id, @record.id)
          @body = @record.body 
          if request.post?
            if @record
              show_render
              @record.destroy_as_receiver(@participant)
              eval(@render_cmd) unless @render_cmd.blank?
            else
              raise ActiveRecord::RecordNotFound
            end
          else
            show_render
            eval(@render_cmd) unless @render_cmd.blank?
          end
        else
          empty_render
        end
      end
    rescue ActiveRecord::StaleObjectError, ActiveRecord::RecordNotFound => error
      logger.info "Concurrent access at queue resource"
      raise
    end
  end

  # inititialize instance variables dependent from request object
  def late_initialize
    @app_namespace= request.path.sub(/^\//,'').sub(/\/.*/,'')
    @ressource_name= $&.sub(/\//,'').sub(/\/.*/,'')
    #@ar_model_name= "#{@app_namespace}_#{@ressource_name}".pluralize.classify
    #@ar_model= @ar_model_name.constantize
  end

  # get a record  out of the message table
  def get_record(record_id = params["id"], app_namespace=@app_namespace, ressource_name=@ressource_name)
    @record, @outdated_auth_token = Message.get_record(record_id, app_namespace, ressource_name)
  end
    
  def empty_render
    render :text => "", :content_type => "application/json"
  end

  def index_render
    render :text => @body, :content_type => "text/uri-list"
  end

  def show_render
    #expires_in 3.hours, 'max-stale' => 5.hours, :public => true
    headers["Cache-Control"] = "private, max-age=5"
    x_ecs_receiver_communities= ""
    x_ecs_sender= ""
    @memberships.each do |memb| 
      x_ecs_receiver_communities << memb.community.id.to_s 
      x_ecs_sender << Membership.find_by_participant_id_and_community_id(Participant.find(@record.sender).id, memb.community.id).id.to_s 
      unless @memberships.last == memb
        x_ecs_receiver_communities << ","
        x_ecs_sender << "," 
      end
    end unless @memberships.blank?
    headers["X-EcsReceiverCommunities"]= x_ecs_receiver_communities unless x_ecs_receiver_communities.blank?
    headers["X-EcsSender"]= x_ecs_sender unless x_ecs_sender.blank?
    @render_cmd='render :text => @body, :layout => false, :status => 200, :content_type => Mime::Type.lookup(@record.content_type)'
  end

  def create_render
    location = request.protocol + request.host
    # FIXME request.headers["SERVER_PORT"] is a string compared to integers.
    location += ":" + request.headers["SERVER_PORT"] unless [80,443].include? request.headers["SERVER_PORT"]
    location += request.headers["SCRIPT_NAME"] if request.headers["SCRIPT_NAME"]
    location += request.path.gsub(/\/*$/,'') + "/" + @record.id.to_s
    if @app_namespace == 'sys' and @ressource_name == 'auths'
      render :text => @body, :layout => false, :status => 201, :location => location, :content_type => Mime::Type.lookup_by_extension("json")
    else
      render :text => "", :layout => false, :status => 201, :location => location, :content_type => Mime::Type.lookup(@record.content_type)
    end
  end

  def update_render
    location = request.protocol + request.host
    location += request.headers["SCRIPT_NAME"] if request.headers["SCRIPT_NAME"]
    location += request.path.gsub(/\/*$/,'')
    render :text => "", :layout => false, :status => 200,
           :location => location
  end

  def destroy_render
    render :nothing => true, :layout => false, :status => 200,
           :content_type => "application/json"
  end

end