Skip to content

Commit d4dd645

Browse files
committed
make sure internal servlet attributes are retrieved on env['internal.attr'] (fixes #173)
1 parent 0ebde8e commit d4dd645

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed

src/main/ruby/rack/handler/servlet/default_env.rb

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,32 @@ def load_header(env, key)
167167
def load_variable(env, key)
168168
return nil if @servlet_env.nil?
169169
case key
170-
when 'CONTENT_TYPE'
171-
content_type = @servlet_env.getContentType
172-
env[key] = content_type if content_type
173-
when 'CONTENT_LENGTH'
174-
content_length = @servlet_env.getContentLength
175-
env[key] = content_length.to_s if content_length >= 0
176-
when 'PATH_INFO' then env[key] = @servlet_env.getPathInfo
177-
when 'QUERY_STRING' then env[key] = @servlet_env.getQueryString || ''
178-
when 'REMOTE_ADDR' then env[key] = @servlet_env.getRemoteAddr || ''
179-
when 'REMOTE_HOST' then env[key] = @servlet_env.getRemoteHost || ''
180-
when 'REMOTE_USER' then env[key] = @servlet_env.getRemoteUser || ''
181-
when 'REQUEST_METHOD' then env[key] = @servlet_env.getMethod || 'GET'
182-
when 'REQUEST_URI' then env[key] = @servlet_env.getRequestURI
183-
when 'SCRIPT_NAME' then env[key] = @servlet_env.getScriptName
184-
when 'SERVER_NAME' then env[key] = @servlet_env.getServerName || ''
185-
when 'SERVER_PORT' then env[key] = @servlet_env.getServerPort.to_s
186-
when 'SERVER_SOFTWARE' then env[key] = rack_context.getServerInfo
187-
else
188-
nil
170+
when 'CONTENT_TYPE'
171+
content_type = @servlet_env.getContentType
172+
env[key] = content_type if content_type
173+
when 'CONTENT_LENGTH'
174+
content_length = @servlet_env.getContentLength
175+
env[key] = content_length.to_s if content_length >= 0
176+
when 'PATH_INFO' then env[key] = @servlet_env.getPathInfo
177+
when 'QUERY_STRING' then env[key] = @servlet_env.getQueryString || ''
178+
when 'REMOTE_ADDR' then env[key] = @servlet_env.getRemoteAddr || ''
179+
when 'REMOTE_HOST' then env[key] = @servlet_env.getRemoteHost || ''
180+
when 'REMOTE_USER' then env[key] = @servlet_env.getRemoteUser || ''
181+
when 'REQUEST_METHOD' then env[key] = @servlet_env.getMethod || 'GET'
182+
when 'REQUEST_URI' then env[key] = @servlet_env.getRequestURI
183+
when 'SCRIPT_NAME' then env[key] = @servlet_env.getScriptName
184+
when 'SERVER_NAME' then env[key] = @servlet_env.getServerName || ''
185+
when 'SERVER_PORT' then env[key] = @servlet_env.getServerPort.to_s
186+
when 'SERVER_SOFTWARE' then env[key] = rack_context.getServerInfo
187+
else
188+
# NOTE: even though we allowed for overrides and loaded all attributes
189+
# up front (looping thru getAttributeNames) container "hidden" attribs
190+
# might still get resolved e.g. 'org.apache.tomcat.sendfile.support'
191+
if hidden_attr = @servlet_env.getAttribute(key)
192+
env[key] = hidden_attr
193+
else
194+
nil
195+
end
189196
end
190197
end
191198

src/spec/ruby/rack/handler/servlet_spec.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
#--
2-
# Copyright (c) 2010-2012 Engine Yard, Inc.
3-
# Copyright (c) 2007-2009 Sun Microsystems, Inc.
4-
# This source code is available under the MIT license.
5-
# See the file LICENSE.txt for details.
6-
#++
7-
8-
require File.expand_path('spec_helper', File.dirname(__FILE__) + '/../..')
1+
require File.expand_path('../../spec_helper', File.dirname(__FILE__))
2+
93
require 'rack/handler/servlet'
104
require 'stringio'
115

@@ -341,6 +335,34 @@
341335
expect( env['jruby.rack.context'] ).to be @rack_context
342336
end
343337

338+
it "retrieves hidden attribute" do
339+
servlet_request_class = Class.new(org.jruby.rack.mock.MockHttpServletRequest) do
340+
341+
def getAttributeNames
342+
names = super.to_a.reject { |name| name.start_with?('org.apache') }
343+
return java.util.Collections.enumeration(names)
344+
end
345+
346+
end
347+
servlet_request = servlet_request_class.new(servlet_context)
348+
servlet_request.setAttribute('current_page', 'index.html'.to_java)
349+
servlet_request.setAttribute('org.answer.internal', 4200.to_java)
350+
servlet_request.setAttribute('org.apache.internal', true.to_java)
351+
352+
servlet_env = org.jruby.rack.servlet.ServletRackEnvironment.new(
353+
servlet_request, servlet_response, @rack_context
354+
)
355+
356+
env = servlet.create_env servlet_env
357+
358+
expect( env.keys ).to include 'current_page'
359+
expect( env.keys ).to include 'org.answer.internal'
360+
expect( env.keys ).to_not include 'org.apache.internal'
361+
362+
expect( env['org.answer.internal'] ).to be 4200
363+
expect( env['org.apache.internal'] ).to be true
364+
end
365+
344366
end
345367

346368
shared_examples "(eager)rack-env" do

0 commit comments

Comments
 (0)