Class: JRuby::Rack::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby/rack/grizzly_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(arr) ⇒ Response

Returns a new instance of Response.



41
42
43
# File 'lib/jruby/rack/grizzly_helper.rb', line 41

def initialize(arr)
  @status, @headers, @body = *arr
end

Instance Method Details

#getBodyObject



53
54
55
56
57
58
59
60
61
# File 'lib/jruby/rack/grizzly_helper.rb', line 53

def getBody
  b = ""
  begin
  @body.each {|part| b << part }
  ensure
    @body.close  if @body.respond_to? :close
  end
  b
end

#getHeadersObject



49
50
51
# File 'lib/jruby/rack/grizzly_helper.rb', line 49

def getHeaders
  @headers
end

#getStatusObject



45
46
47
# File 'lib/jruby/rack/grizzly_helper.rb', line 45

def getStatus
  @status
end

#respond(response) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/jruby/rack/grizzly_helper.rb', line 63

def respond(response)
  if fwd = @headers["Forward"]
    fwd.call(response)
  else
      write_status(response)
      write_headers(response)
      write_body(response)
  end
end

#rstrip_newline(v) ⇒ Object

Strips off any r or n from the end of the v MUST not be null



102
103
104
105
106
107
108
109
110
# File 'lib/jruby/rack/grizzly_helper.rb', line 102

def rstrip_newline(v)
  len = v.size
  (v.size-1).downto(0) do |i|
    if(v[i] == 10 || v[i] == 13)
      len -= 1
    end
  end
  len
end

#write_body(response) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/jruby/rack/grizzly_helper.rb', line 113

def write_body(response)
  stream = response.output_stream
  begin
    @body.each do |el|
      stream.write(el.to_java_bytes)
    end
  ensure
    @body.close  if @body.respond_to? :close
  end          
end

#write_headers(response) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jruby/rack/grizzly_helper.rb', line 77

def write_headers(response)
  mime_headers = response.getResponse().getMimeHeaders()

  @headers.each do |k,v|
    case k
    when /^Content-Type$/i
      response.content_type = v.to_s
    when /^Content-Length$/i
      response.content_length = v.to_i
    else
        if v.respond_to?(:each)                              
          v.each do |val|
            len = rstrip_newline(val)
            mime_headers.addValue(k.to_java_bytes,0, k.length).setBytes(val.to_java_bytes, 0, len)
          end
        else
          len = rstrip_newline(v)
          mime_headers.addValue(k.to_java_bytes,0, k.length).setBytes(val.to_java_bytes, 0, val.length)
        end
    end
  end
end

#write_status(response) ⇒ Object



73
74
75
# File 'lib/jruby/rack/grizzly_helper.rb', line 73

def write_status(response)
  response.setStatus(@status.to_i)
end