mentawai 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +4 -9
- data/Manifest.txt +1 -0
- data/bin/menta +19 -104
- data/lib/mentawai/application.rb +5 -15
- data/lib/mentawai/core/action.rb +4 -0
- data/lib/mentawai/core/app_manager.rb +13 -9
- data/lib/mentawai/core/controller.rb +7 -13
- data/lib/mentawai/core/forward.rb +3 -3
- data/lib/mentawai/handler/menta_handler.rb +36 -48
- data/lib/mentawai/i18n/i18n_file.rb +81 -0
- data/lib/mentawai/i18n/loc_manager.rb +30 -3
- data/lib/mentawai/page/methods/messages.rb +39 -8
- data/lib/mentawai/server.rb +14 -50
- data/lib/mentawai/util/properties.rb +3 -1
- data/lib/mentawai/version.rb +1 -1
- metadata +3 -2
data/History.txt
CHANGED
@@ -1,10 +1,5 @@
|
|
1
|
-
== 0.
|
1
|
+
== 0.7.0 2008-05-07
|
2
2
|
|
3
|
-
*
|
4
|
-
|
5
|
-
*
|
6
|
-
* ERB pages can be anywere now
|
7
|
-
* Everything inside /WEB-INF directory is protected from external access
|
8
|
-
* You can run different applications in the same instance of MentaOnRuby ('/' = /ROOT directory like Java servlets)
|
9
|
-
* You can use the new page method content_type to set the content-type of a erb template (ex: text/xml)
|
10
|
-
* Many other improvements and bug fixes
|
3
|
+
* Forget about different applications running inside the same server
|
4
|
+
(Ruby does not have a classloader like Java)
|
5
|
+
* i18n is working as well as page methods (master file inside /WEB-INF/i18n)
|
data/Manifest.txt
CHANGED
@@ -21,6 +21,7 @@ lib/mentawai/core/invocation_chain.rb
|
|
21
21
|
lib/mentawai/core/output.rb
|
22
22
|
lib/mentawai/core/session.rb
|
23
23
|
lib/mentawai/handler/menta_handler.rb
|
24
|
+
lib/mentawai/i18n/i18n_file.rb
|
24
25
|
lib/mentawai/i18n/loc_manager.rb
|
25
26
|
lib/mentawai/loader.rb
|
26
27
|
lib/mentawai/page/methods/content_type.rb
|
data/bin/menta
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
def version
|
4
4
|
require 'mentawai/version'
|
5
5
|
puts "mentawai #{Mentawai::VERSION::STRING}" +
|
6
|
-
" - Mentawai Web
|
6
|
+
" - Mentawai Web Framework in Ruby"
|
7
7
|
puts "More information on http://www.mentaframework.org"
|
8
8
|
end
|
9
9
|
|
@@ -12,7 +12,7 @@ def help
|
|
12
12
|
puts
|
13
13
|
puts "-v,--version\t\tShow version"
|
14
14
|
puts "-h,--help\t\tShow this help"
|
15
|
-
puts "-c,--create <dirname>\tCreate web
|
15
|
+
puts "-c,--create <dirname>\tCreate web application (default dirname = 'mentawai')"
|
16
16
|
end
|
17
17
|
|
18
18
|
START_FILE = <<EOF
|
@@ -25,31 +25,17 @@ server = Server.new
|
|
25
25
|
server.host = '127.0.0.1'
|
26
26
|
server.port = 8081
|
27
27
|
|
28
|
-
# Configure the parameters of your web application... (
|
28
|
+
# Configure the parameters of your web application... (optional)
|
29
29
|
|
30
|
-
|
31
|
-
:app_context => '/', # default = '/'
|
30
|
+
server.options = {
|
32
31
|
:app_manager_file => 'app_manager.rb', # default = 'app_manager.rb'
|
33
32
|
:app_manager_class => 'AppManager', # default = 'AppManager'
|
34
33
|
:action_extension => 'mtw', # default = 'mtw'
|
35
|
-
:listing_allowed =>
|
34
|
+
:listing_allowed => true, # default = false
|
36
35
|
:default_page => 'index.erb', # default = 'index.erb'
|
37
36
|
:dirs_allowed_for_listing => [] # default = []
|
38
37
|
}
|
39
38
|
|
40
|
-
server.add(root_app_options)
|
41
|
-
|
42
|
-
# Configure the parameters of a second web application... (/myapp context)
|
43
|
-
|
44
|
-
myapp_app_options = {
|
45
|
-
:app_context => '/myapp',
|
46
|
-
:action_extension => 'menta',
|
47
|
-
:listing_allowed => true,
|
48
|
-
:dirs_allowed_for_listing => ['img']
|
49
|
-
}
|
50
|
-
|
51
|
-
server.add(myapp_app_options)
|
52
|
-
|
53
39
|
# Start mongrel server to serve your Mentawai applications...
|
54
40
|
|
55
41
|
server.start
|
@@ -57,7 +43,7 @@ server.start
|
|
57
43
|
|
58
44
|
EOF
|
59
45
|
|
60
|
-
|
46
|
+
APP_MANAGER_FILE = <<EOF
|
61
47
|
include Mentawai::Core
|
62
48
|
|
63
49
|
class AppManager < ApplicationManager
|
@@ -74,23 +60,7 @@ class AppManager < ApplicationManager
|
|
74
60
|
end
|
75
61
|
EOF
|
76
62
|
|
77
|
-
|
78
|
-
include Mentawai::Core
|
79
|
-
|
80
|
-
class AppManager < ApplicationManager
|
81
|
-
|
82
|
-
def init(appContext)
|
83
|
-
|
84
|
-
# Define an action...
|
85
|
-
action(:action_name => "/Hello", :class_name => "MyApp2::Hello").on(:success => fwd("hello.erb"))
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
EOF
|
91
|
-
|
92
|
-
|
93
|
-
ACTION_FILE_1 = <<EOF
|
63
|
+
ACTION_FILE = <<EOF
|
94
64
|
|
95
65
|
module MyApp1
|
96
66
|
|
@@ -128,33 +98,7 @@ module MyApp1
|
|
128
98
|
end
|
129
99
|
EOF
|
130
100
|
|
131
|
-
|
132
|
-
|
133
|
-
module MyApp2
|
134
|
-
|
135
|
-
class Hello < Mentawai::Core::Action
|
136
|
-
|
137
|
-
def sayHi
|
138
|
-
|
139
|
-
output['username'] = "Sergio"
|
140
|
-
|
141
|
-
:success
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
def sayHello
|
146
|
-
|
147
|
-
output['username'] = "Rubem"
|
148
|
-
|
149
|
-
:success
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
end
|
154
|
-
end
|
155
|
-
EOF
|
156
|
-
|
157
|
-
VIEW_FILE_1 = <<EOF
|
101
|
+
VIEW_FILE = <<EOF
|
158
102
|
<html>
|
159
103
|
<body>
|
160
104
|
<title>ROOT ('/') Mentawai Application</title>
|
@@ -166,62 +110,33 @@ VIEW_FILE_1 = <<EOF
|
|
166
110
|
</html>
|
167
111
|
EOF
|
168
112
|
|
169
|
-
VIEW_FILE_2 = <<EOF
|
170
|
-
<html>
|
171
|
-
<body>
|
172
|
-
<title>myapp ('/myapp') Mentawai Application</title>
|
173
|
-
<!-- using menta method m_out -->
|
174
|
-
<h3>Hi1: <%= m_out('username') %></h3>
|
175
|
-
</body>
|
176
|
-
</html>
|
177
|
-
EOF
|
178
|
-
|
179
|
-
|
180
113
|
def create_app(app)
|
181
114
|
Dir.mkdir(app)
|
182
115
|
|
183
|
-
Dir.mkdir(app + '/
|
184
|
-
Dir.mkdir(app + '/
|
185
|
-
Dir.mkdir(app + '/
|
186
|
-
Dir.mkdir(app + '/ROOT/WEB-INF/lib')
|
187
|
-
|
188
|
-
Dir.mkdir(app + '/myapp')
|
189
|
-
Dir.mkdir(app + '/myapp/WEB-INF')
|
190
|
-
Dir.mkdir(app + '/myapp/WEB-INF/src')
|
191
|
-
Dir.mkdir(app + '/myapp/WEB-INF/lib')
|
116
|
+
Dir.mkdir(app + '/WEB-INF')
|
117
|
+
Dir.mkdir(app + '/WEB-INF/src')
|
118
|
+
Dir.mkdir(app + '/WEB-INF/lib')
|
192
119
|
|
193
120
|
f = File.open(app + '/start.rb', 'w')
|
194
121
|
f.print START_FILE
|
195
122
|
f.close
|
196
123
|
|
197
|
-
f = File.open(app + '/
|
198
|
-
f.print
|
199
|
-
f.close
|
200
|
-
|
201
|
-
f = File.open(app + '/ROOT/WEB-INF/src/hello.rb', 'w')
|
202
|
-
f.print ACTION_FILE_1
|
203
|
-
f.close
|
204
|
-
|
205
|
-
f = File.open(app + '/ROOT/hello.erb', 'w')
|
206
|
-
f.print VIEW_FILE_1
|
207
|
-
f.close
|
208
|
-
|
209
|
-
f = File.open(app + '/myapp/WEB-INF/app_manager.rb', 'w')
|
210
|
-
f.print APP_MANAGER_FILE_2
|
124
|
+
f = File.open(app + '/WEB-INF/app_manager.rb', 'w')
|
125
|
+
f.print APP_MANAGER_FILE
|
211
126
|
f.close
|
212
127
|
|
213
|
-
f = File.open(app + '/
|
214
|
-
f.print
|
128
|
+
f = File.open(app + '/WEB-INF/src/hello.rb', 'w')
|
129
|
+
f.print ACTION_FILE
|
215
130
|
f.close
|
216
131
|
|
217
|
-
f = File.open(app + '/
|
218
|
-
f.print
|
132
|
+
f = File.open(app + '/hello.erb', 'w')
|
133
|
+
f.print VIEW_FILE
|
219
134
|
f.close
|
220
135
|
|
221
|
-
puts "Mentawai web
|
136
|
+
puts "Mentawai web application created: #{app}"
|
222
137
|
puts "To run: ruby start.rb inside \"#{app}\""
|
223
138
|
puts
|
224
|
-
puts "Successfuly created
|
139
|
+
puts "Successfuly created mentawai application!"
|
225
140
|
end
|
226
141
|
|
227
142
|
if %w(-v --version).include? ARGV.first
|
data/lib/mentawai/application.rb
CHANGED
@@ -3,16 +3,14 @@ module Mentawai
|
|
3
3
|
|
4
4
|
class Application
|
5
5
|
|
6
|
-
attr_reader :appContext, :
|
6
|
+
attr_reader :appContext, :appManagerFilename, :appManagerClass, :appManagerFile, :appManager, :extension, :default_page, :listing_allowed, :dirs_allowed_for_listing
|
7
7
|
|
8
|
-
def initialize(params)
|
8
|
+
def initialize(params = {})
|
9
9
|
|
10
10
|
@appContext = Hash.new
|
11
11
|
|
12
|
-
@contextPath = params[:app_context] || '/'
|
13
|
-
|
14
12
|
@appManagerFilename = params[:app_manager_file] || 'app_manager.rb'
|
15
|
-
@appManagerFilename = "
|
13
|
+
@appManagerFilename = "./WEB-INF/#{@appManagerFilename.sub(/^\//, "")}"
|
16
14
|
raise "Application manager does not exist: " + @appManagerFilename if not File.exists?(@appManagerFilename)
|
17
15
|
|
18
16
|
@appManagerClass = params[:app_manager_class] || 'AppManager'
|
@@ -33,16 +31,8 @@ module Mentawai
|
|
33
31
|
|
34
32
|
end
|
35
33
|
|
36
|
-
def contextPathDir
|
37
|
-
if contextPath == '/'
|
38
|
-
"/ROOT"
|
39
|
-
else
|
40
|
-
contextPath
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
34
|
def to_s
|
45
|
-
"#{
|
35
|
+
"#{appManagerFilename}/#{appManagerClass}/#{extension}"
|
46
36
|
end
|
47
37
|
|
48
38
|
def reloadAppManager(force = false)
|
@@ -53,7 +43,7 @@ module Mentawai
|
|
53
43
|
puts "Reloading app manager: " + @appManagerFilename
|
54
44
|
end
|
55
45
|
load @appManagerFilename
|
56
|
-
@appManager = eval(appManagerClass + ".new
|
46
|
+
@appManager = eval(appManagerClass + ".new")
|
57
47
|
@appManager.init(@appContext)
|
58
48
|
@appManagerFileLastModified = @appManagerFile.mtime
|
59
49
|
end
|
data/lib/mentawai/core/action.rb
CHANGED
@@ -4,34 +4,37 @@ module Mentawai
|
|
4
4
|
module Core
|
5
5
|
|
6
6
|
class ApplicationManager
|
7
|
+
|
8
|
+
def self.instance
|
9
|
+
@@instance
|
10
|
+
end
|
7
11
|
|
8
|
-
def initialize
|
12
|
+
def initialize
|
9
13
|
|
10
14
|
puts "Initializing application manager..."
|
11
15
|
|
12
|
-
@app = app
|
13
|
-
|
14
16
|
@actions = Hash.new
|
15
17
|
@innerActions = Hash.new
|
16
18
|
|
17
19
|
@globalFilters = Array.new
|
18
20
|
@globalConsequences = Hash.new
|
19
21
|
|
20
|
-
@loader = Loader.new("
|
22
|
+
@loader = Loader.new("./WEB-INF")
|
21
23
|
@loader.reloadFiles
|
22
24
|
|
23
25
|
@pageMethods = initPageMethods
|
24
26
|
@customPageMethods = Hash.new
|
27
|
+
|
28
|
+
# Set a singleton here so that the whole application can
|
29
|
+
# have a quick access to the ApplicationManager class...
|
30
|
+
@@instance = self
|
31
|
+
|
25
32
|
end
|
26
33
|
|
27
34
|
def init(application)
|
28
35
|
# should be overriden...
|
29
36
|
end
|
30
37
|
|
31
|
-
def app
|
32
|
-
@app
|
33
|
-
end
|
34
|
-
|
35
38
|
def pageMethods
|
36
39
|
@pageMethods
|
37
40
|
end
|
@@ -119,7 +122,7 @@ module Mentawai
|
|
119
122
|
# Methods to create consequences...
|
120
123
|
|
121
124
|
def fwd(page)
|
122
|
-
Forward.new(
|
125
|
+
Forward.new(page)
|
123
126
|
end
|
124
127
|
|
125
128
|
private
|
@@ -134,6 +137,7 @@ module Mentawai
|
|
134
137
|
h[:m_has_errors] = %w{ Mentawai::Page::Method::Message hasErrors }
|
135
138
|
h[:m_has_error] = %w{ Mentawai::Page::Method::Message hasError }
|
136
139
|
h[:m_has_msgs] = %w{ Mentawai::Page::Method::Message hasMessages }
|
140
|
+
h[:m_has_field_errors] = %w{ Mentawai::Page::Method::Message hasFieldErrors }
|
137
141
|
h[:m_has_msg] = h[:m_has_msgs]
|
138
142
|
h[:m_errors] = %w{ Mentawai::Page::Method::Message errors }
|
139
143
|
h[:m_error] = %w{ Mentawai::Page::Method::Message error }
|
@@ -12,7 +12,6 @@ module Mentawai
|
|
12
12
|
@@forward_cache.extend(MonitorMixin)
|
13
13
|
|
14
14
|
def initialize(app)
|
15
|
-
@contextPath = app.contextPath
|
16
15
|
@appContext = app.appContext
|
17
16
|
@appManager = app.appManager
|
18
17
|
@extension = app.extension
|
@@ -20,7 +19,7 @@ module Mentawai
|
|
20
19
|
@action = nil
|
21
20
|
end
|
22
21
|
|
23
|
-
attr_reader :appManager, :
|
22
|
+
attr_reader :appManager, :extension
|
24
23
|
|
25
24
|
def action
|
26
25
|
@action
|
@@ -119,15 +118,13 @@ module Mentawai
|
|
119
118
|
# Use conventions to find a view
|
120
119
|
dest = '/' + actionName + "/" + (innerAction ? innerAction : 'index') + '.erb'
|
121
120
|
|
122
|
-
full_dest = @appManager.app.contextPathDir + dest
|
123
|
-
|
124
121
|
# Cache forward consequence...
|
125
122
|
@@forward_cache.synchronize {
|
126
|
-
conseq = @@forward_cache[
|
123
|
+
conseq = @@forward_cache[dest]
|
127
124
|
if not conseq then
|
128
125
|
# Check if file exists
|
129
|
-
if File.exists?('.' +
|
130
|
-
@@forward_cache[
|
126
|
+
if File.exists?('.' + dest) then
|
127
|
+
@@forward_cache[dest] = conseq = Forward.new(dest)
|
131
128
|
end
|
132
129
|
end
|
133
130
|
}
|
@@ -145,8 +142,6 @@ module Mentawai
|
|
145
142
|
|
146
143
|
# Accessing a page file directly
|
147
144
|
|
148
|
-
full_url = @appManager.app.contextPathDir + req.fullpath
|
149
|
-
|
150
145
|
url = req.fullpath.dup
|
151
146
|
|
152
147
|
fix_url(url)
|
@@ -159,8 +154,8 @@ module Mentawai
|
|
159
154
|
|
160
155
|
# Cache forward consequence...
|
161
156
|
@@forward_cache.synchronize {
|
162
|
-
forward = @@forward_cache[
|
163
|
-
@@forward_cache[
|
157
|
+
forward = @@forward_cache[url]
|
158
|
+
@@forward_cache[url] = forward = Forward.new(url) if not forward
|
164
159
|
}
|
165
160
|
|
166
161
|
@consequence = forward
|
@@ -172,7 +167,6 @@ module Mentawai
|
|
172
167
|
protected
|
173
168
|
|
174
169
|
def fix_url(url)
|
175
|
-
url.gsub!(/^#{@contextPath}/, "") # remove context path
|
176
170
|
url.gsub!(/\?.*$/, "") # remove query string (if present)
|
177
171
|
url.gsub!(/^\//, "") # remove a '/' from begining
|
178
172
|
url.gsub!(/\/$/, "") # remove a '/' from ending
|
@@ -192,7 +186,7 @@ module Mentawai
|
|
192
186
|
action.application = @appContext
|
193
187
|
action.page = Hash.new
|
194
188
|
locales = get_locales(request.env['HTTP_ACCEPT_LANGUAGE'])
|
195
|
-
action.locale = LocaleManager.instance.
|
189
|
+
action.locale = LocaleManager.instance.get_locale(locales)
|
196
190
|
else
|
197
191
|
raise "Not an action: " + action.to_s
|
198
192
|
end
|
@@ -17,7 +17,7 @@ module Mentawai
|
|
17
17
|
@@types.key?(extension)
|
18
18
|
end
|
19
19
|
|
20
|
-
def initialize(
|
20
|
+
def initialize(page)
|
21
21
|
@page = page
|
22
22
|
|
23
23
|
extension = nil
|
@@ -33,9 +33,9 @@ module Mentawai
|
|
33
33
|
@contentType = @@types[extension]
|
34
34
|
|
35
35
|
if page !~ /^\//
|
36
|
-
@filename = "
|
36
|
+
@filename = "./#{page}"
|
37
37
|
else
|
38
|
-
@filename = ".#{
|
38
|
+
@filename = ".#{page}"
|
39
39
|
end
|
40
40
|
|
41
41
|
raise "Cannot find page: " + @filename if not File.exists?(@filename)
|
@@ -6,23 +6,14 @@ module Mentawai
|
|
6
6
|
|
7
7
|
class MentaHandler < Rack::Handler::Mongrel
|
8
8
|
|
9
|
-
def initialize(server,
|
9
|
+
def initialize(server, rack_app)
|
10
10
|
|
11
|
-
@
|
11
|
+
@menta_app = server.app
|
12
12
|
|
13
|
-
|
13
|
+
@dir_handler = Mongrel::DirHandler.new('.', @menta_app.listing_allowed, @menta_app.default_page)
|
14
14
|
|
15
|
-
|
16
|
-
@server.apps.each do |k,v|
|
17
|
-
if v.listing_allowed
|
18
|
-
listing_allowed = true
|
19
|
-
break
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
@dir_handler = Mongrel::DirHandler.new('.', listing_allowed)
|
15
|
+
super rack_app
|
24
16
|
|
25
|
-
super app
|
26
17
|
end
|
27
18
|
|
28
19
|
def self.mime_types
|
@@ -42,6 +33,8 @@ module Mentawai
|
|
42
33
|
|
43
34
|
def process(request, response)
|
44
35
|
|
36
|
+
# Construct Rack request...
|
37
|
+
|
45
38
|
env = {}.replace(request.params)
|
46
39
|
env["SCRIPT_NAME"] = "" if env["SCRIPT_NAME"] == "/"
|
47
40
|
env["QUERY_STRING"] ||= ""
|
@@ -51,13 +44,7 @@ module Mentawai
|
|
51
44
|
|
52
45
|
url = req.fullpath
|
53
46
|
|
54
|
-
|
55
|
-
|
56
|
-
app = @server.apps[context_path]
|
57
|
-
|
58
|
-
context_path_dir = app.contextPathDir
|
59
|
-
|
60
|
-
ru = RequestURL.new(context_path, context_path_dir, url, app.default_page)
|
47
|
+
ru = RequestURL.new(url, @menta_app.default_page, request)
|
61
48
|
|
62
49
|
# Block forbidden directory...
|
63
50
|
if ru.forbidden? then
|
@@ -68,7 +55,7 @@ module Mentawai
|
|
68
55
|
end
|
69
56
|
|
70
57
|
# Block if no listing is allowed and this is a directory...
|
71
|
-
if ru.is_dir? and not
|
58
|
+
if ru.is_dir? and not @menta_app.listing_allowed
|
72
59
|
response.start(403) do |head,out|
|
73
60
|
out << "Listing is not unabled for this application!"
|
74
61
|
end
|
@@ -76,13 +63,13 @@ module Mentawai
|
|
76
63
|
end
|
77
64
|
|
78
65
|
# Block if listing is allowed but directory is not allowed...
|
79
|
-
if ru.is_dir? and
|
66
|
+
if ru.is_dir? and @menta_app.listing_allowed and not @menta_app.dirs_allowed_for_listing.empty?
|
80
67
|
|
81
68
|
allowed = false
|
82
69
|
|
83
|
-
|
70
|
+
@menta_app.dirs_allowed_for_listing.each do |dir|
|
84
71
|
dir = '/' + dir if dir !~ /^\//
|
85
|
-
if ru.
|
72
|
+
if ru.url =~ /^#{dir}\/?$/
|
86
73
|
allowed = true
|
87
74
|
break
|
88
75
|
end
|
@@ -96,16 +83,21 @@ module Mentawai
|
|
96
83
|
end
|
97
84
|
end
|
98
85
|
|
99
|
-
if url =~ /\.#{
|
86
|
+
if ru.url =~ /\.#{@menta_app.extension}\??$/ or ru.url =~ /\.#{@menta_app.extension}\?.+$/
|
100
87
|
# We need to process this inside the Mentawai controller... (action request)
|
101
88
|
super
|
102
|
-
elsif (url =~ /(\.\w+)\??$/ or url =~ /(\.\w+)\?.+$/) and Mentawai::Core::Forward.is_registered?($1)
|
89
|
+
elsif (ru.url =~ /(\.\w+)\??$/ or ru.url =~ /(\.\w+)\?.+$/) and Mentawai::Core::Forward.is_registered?($1)
|
90
|
+
|
91
|
+
if not ru.exists?
|
92
|
+
response.start(404) do |head,out|
|
93
|
+
out << "Page not found!!"
|
94
|
+
end
|
95
|
+
return
|
96
|
+
end
|
97
|
+
|
103
98
|
# We need to process this inside the Mentawai controller... (ERB page request)
|
104
99
|
super
|
105
100
|
else
|
106
|
-
# Change Mongrel request to reflect the context path dir...
|
107
|
-
request.params["PATH_INFO"] = ru.url_with_context_dir
|
108
|
-
|
109
101
|
# Let Mongrel serve this request...
|
110
102
|
@dir_handler.process(request, response)
|
111
103
|
end
|
@@ -115,53 +107,49 @@ module Mentawai
|
|
115
107
|
|
116
108
|
class RequestURL
|
117
109
|
|
118
|
-
def initialize(
|
110
|
+
def initialize(url, default_page, request)
|
119
111
|
|
120
|
-
|
121
|
-
|
122
|
-
@url_file_path = '.' + context_path_dir + url
|
123
|
-
@url_with_context_dir = context_path_dir + url
|
124
|
-
@url_without_context_dir = url
|
125
|
-
else
|
126
|
-
@url_file_path = '.' + url
|
127
|
-
@url_with_context_dir = url
|
128
|
-
@url_without_context_dir = url.sub(/#{context_path_dir}/, "")
|
129
|
-
@url_without_context_dir = '/' if @url_without_context_dir == ""
|
130
|
-
end
|
131
|
-
|
112
|
+
@url = url.dup
|
113
|
+
@url_file_path = '.' + url
|
132
114
|
@is_dir = File.directory?(@url_file_path)
|
115
|
+
@exists = File.exists?(@url_file_path)
|
133
116
|
|
134
117
|
# Check if we need to append the default_page
|
135
|
-
if @is_dir
|
118
|
+
if @is_dir and default_page
|
136
119
|
url_file_path_with_default_page = append_filename(@url_file_path, default_page)
|
137
120
|
if File.exists?(url_file_path_with_default_page)
|
138
121
|
@is_dir = false
|
139
|
-
@
|
140
|
-
@url_with_context_dir = append_filename(@url_with_context_dir, default_page)
|
122
|
+
@url = append_filename(@url, default_page)
|
141
123
|
@url_file_path = url_file_path_with_default_page
|
124
|
+
@exists = File.exists?(@url_file_path)
|
125
|
+
request.params["PATH_INFO"] = @url.dup # change here too
|
142
126
|
end
|
143
127
|
end
|
144
128
|
|
145
129
|
# Check if this is a forbidden URL
|
146
|
-
if @
|
130
|
+
if @url =~ /^\/start\.rb/ or @url =~ /^\/WEB-INF/
|
147
131
|
@forbidden = true
|
148
132
|
else
|
149
133
|
@forbidden = false
|
150
134
|
end
|
151
135
|
end
|
152
136
|
|
153
|
-
attr_reader :
|
137
|
+
attr_reader :url, :url_file_path
|
154
138
|
|
155
139
|
def is_dir?
|
156
140
|
@is_dir
|
157
141
|
end
|
158
142
|
|
143
|
+
def exists?
|
144
|
+
@exists
|
145
|
+
end
|
146
|
+
|
159
147
|
def forbidden?
|
160
148
|
@forbidden
|
161
149
|
end
|
162
150
|
|
163
151
|
def to_s
|
164
|
-
"RequestURL:
|
152
|
+
"RequestURL: url=#{url} path=#{url_file_path} forbidden=#{forbidden?} is_dir=#{is_dir?}"
|
165
153
|
end
|
166
154
|
|
167
155
|
private
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'mentawai/util/properties'
|
3
|
+
|
4
|
+
include Mentawai::Util
|
5
|
+
|
6
|
+
module Mentawai
|
7
|
+
module I18N
|
8
|
+
|
9
|
+
class I18NFile
|
10
|
+
|
11
|
+
def initialize(fullpath)
|
12
|
+
|
13
|
+
@fullpath = fullpath
|
14
|
+
|
15
|
+
@prop = Properties.loadProp(@fullpath)
|
16
|
+
|
17
|
+
if @prop
|
18
|
+
@file = File.new(@fullpath)
|
19
|
+
@last_modified = @file.mtime
|
20
|
+
else
|
21
|
+
@file = @last_modified = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
@last_check = Time.now
|
25
|
+
@lock = Mutex.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def [](key)
|
29
|
+
|
30
|
+
prop = get_prop
|
31
|
+
|
32
|
+
if prop
|
33
|
+
if prop.key?(key)
|
34
|
+
# Return the key value...
|
35
|
+
prop[key]
|
36
|
+
else
|
37
|
+
# Key is not in the file
|
38
|
+
'!' + key + '!'
|
39
|
+
end
|
40
|
+
else
|
41
|
+
# If file does not exist, return the key...
|
42
|
+
key
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def get_prop
|
49
|
+
|
50
|
+
if Time.now - @last_check > 5
|
51
|
+
|
52
|
+
@lock.synchronize {
|
53
|
+
|
54
|
+
@last_check = Time.now
|
55
|
+
|
56
|
+
if not @prop
|
57
|
+
@prop = Properties.loadProp(@fullpath)
|
58
|
+
return nil if not @prop
|
59
|
+
@file = File.new(@fullpath)
|
60
|
+
@last_modified = @file.mtime
|
61
|
+
end
|
62
|
+
|
63
|
+
if @last_modified != @file.mtime
|
64
|
+
|
65
|
+
@last_modified = @file.mtime
|
66
|
+
|
67
|
+
@prop = Properties.loadProp(@fullpath)
|
68
|
+
|
69
|
+
end
|
70
|
+
}
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
@prop
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'singleton'
|
2
|
+
require 'mentawai/i18n/i18n_file'
|
2
3
|
|
3
4
|
module Mentawai
|
4
5
|
module I18N
|
@@ -6,27 +7,53 @@ module Mentawai
|
|
6
7
|
class LocaleManager
|
7
8
|
include Singleton
|
8
9
|
|
10
|
+
MASTER_FILE = './WEB-INF/i18n/master'
|
11
|
+
DEFAULT_LOCALE = 'en_US'
|
12
|
+
|
9
13
|
def initialize
|
10
14
|
@locales = Array.new
|
15
|
+
@i18n = Hash.new
|
11
16
|
end
|
12
17
|
|
13
18
|
def add(loc)
|
19
|
+
# Add locale to the list...
|
14
20
|
@locales.add(loc)
|
21
|
+
|
22
|
+
# Add master file...
|
23
|
+
@i18n[loc] = I18NFile.new(MASTER_FILE + '_' + loc + '.i18n')
|
15
24
|
end
|
16
25
|
|
17
26
|
def locales
|
18
27
|
@locales
|
19
28
|
end
|
20
29
|
|
30
|
+
def i18n
|
31
|
+
@i18n
|
32
|
+
end
|
33
|
+
|
21
34
|
def default
|
22
35
|
if @locales.empty?
|
23
|
-
|
36
|
+
DEFAULT_LOCALE
|
24
37
|
else
|
25
38
|
@locales[0]
|
26
39
|
end
|
27
40
|
end
|
28
41
|
|
29
|
-
def
|
42
|
+
def get_i18n(locale, return_default = true)
|
43
|
+
if i18n.key?(locale)
|
44
|
+
i18n[locale]
|
45
|
+
elsif return_default
|
46
|
+
if i18n.key?(default)
|
47
|
+
i18n[default]
|
48
|
+
else
|
49
|
+
i18n[default] = I18NFile.new(MASTER_FILE + '_' + default + '.i18n')
|
50
|
+
end
|
51
|
+
else
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_locale(locale, return_default = true)
|
30
57
|
|
31
58
|
if locale.nil?
|
32
59
|
return return_default ? default : nil
|
@@ -35,7 +62,7 @@ module Mentawai
|
|
35
62
|
# If array, try to match any of them
|
36
63
|
if locale.is_a?(Array)
|
37
64
|
locale.each do |loc|
|
38
|
-
l =
|
65
|
+
l = get_locale(loc, false) # recursive no default here...
|
39
66
|
return l if l
|
40
67
|
end
|
41
68
|
return return_default ? default : nil
|
@@ -6,25 +6,56 @@ module Mentawai
|
|
6
6
|
|
7
7
|
class Message < Mentawai::Page::PageMethod
|
8
8
|
|
9
|
-
def initialize(
|
9
|
+
def initialize(controller, parameters)
|
10
10
|
super
|
11
11
|
@errors = output['errors']
|
12
12
|
@messages = output['messages']
|
13
13
|
@fieldErrors = output['fieldErrors']
|
14
14
|
@fieldName = params[:field]
|
15
|
+
@i18n = controller.action.i18n
|
15
16
|
end
|
16
17
|
|
17
|
-
attr_reader :errors, :messages, :fieldErrors
|
18
|
-
|
19
18
|
def get_default_key
|
20
19
|
:field
|
21
20
|
end
|
22
21
|
|
22
|
+
def errors
|
23
|
+
return nil if @errors.nil? or not @errors.is_a?(Array) or @errors.empty?
|
24
|
+
res = ""
|
25
|
+
@errors.each do |err|
|
26
|
+
res += @i18n[err] + "<br/>"
|
27
|
+
end
|
28
|
+
res
|
29
|
+
end
|
30
|
+
|
31
|
+
def messages
|
32
|
+
return nil if @messages.nil? or not @messages.is_a?(Array) or @messages.empty?
|
33
|
+
res = ""
|
34
|
+
@messages.each do |msg|
|
35
|
+
res += @i18n[msg] + "<br/>"
|
36
|
+
end
|
37
|
+
res
|
38
|
+
end
|
39
|
+
|
40
|
+
def fieldErrors
|
41
|
+
return nil if @fieldErrors.nil? or not @fieldErrors.is_a?(Hash) or @fieldErrors.empty?
|
42
|
+
res = ""
|
43
|
+
@fieldErrors.each do |k, msg|
|
44
|
+
res += @i18n[msg] + "<br/>"
|
45
|
+
end
|
46
|
+
res
|
47
|
+
end
|
48
|
+
|
23
49
|
def hasErrors
|
24
|
-
return false if @errors
|
50
|
+
return false if not @errors or not @errors.is_a?(Array)
|
25
51
|
!@errors.empty?
|
26
52
|
end
|
27
53
|
|
54
|
+
def hasFieldErrors
|
55
|
+
return false if @fieldErrors.nil? or not @fieldErrors.is_a?(Hash)
|
56
|
+
!@fieldErrors.empty?
|
57
|
+
end
|
58
|
+
|
28
59
|
def hasError
|
29
60
|
if @fieldName
|
30
61
|
hasFieldError
|
@@ -53,8 +84,8 @@ module Mentawai
|
|
53
84
|
|
54
85
|
def fieldError
|
55
86
|
raise "Missing field for field error!" if @fieldName.nil?
|
56
|
-
return nil if @fieldErrors.nil?
|
57
|
-
@fieldErrors[@fieldName]
|
87
|
+
return nil if @fieldErrors.nil? or not @fieldErrors.is_a?(Hash)
|
88
|
+
@i18n[@fieldErrors[@fieldName]]
|
58
89
|
end
|
59
90
|
|
60
91
|
def error
|
@@ -62,13 +93,13 @@ module Mentawai
|
|
62
93
|
fieldError
|
63
94
|
else
|
64
95
|
return nil if @errors.nil? || !@errors.is_a?(Array) || @errors.empty?
|
65
|
-
@errors[0]
|
96
|
+
@i18n[@errors[0]]
|
66
97
|
end
|
67
98
|
end
|
68
99
|
|
69
100
|
def message
|
70
101
|
return nil if @messages.nil? || !@messages.is_a?(Array) || @messages.empty?
|
71
|
-
@messages[0]
|
102
|
+
@i18n[@messages[0]]
|
72
103
|
end
|
73
104
|
|
74
105
|
end
|
data/lib/mentawai/server.rb
CHANGED
@@ -7,14 +7,15 @@ module Mentawai
|
|
7
7
|
|
8
8
|
class Server
|
9
9
|
|
10
|
-
attr_accessor :host, :port
|
10
|
+
attr_accessor :host, :port, :options
|
11
|
+
|
12
|
+
attr_reader :app
|
11
13
|
|
12
14
|
def initialize(path = nil)
|
13
15
|
|
14
16
|
@host = "127.0.0.1"
|
15
17
|
@port = 8081
|
16
|
-
|
17
|
-
@apps = Hash.new
|
18
|
+
@options = {}
|
18
19
|
|
19
20
|
if not path
|
20
21
|
@loader = nil
|
@@ -24,30 +25,6 @@ module Mentawai
|
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
27
|
-
attr_reader :apps
|
28
|
-
|
29
|
-
def add_application(app_options)
|
30
|
-
|
31
|
-
app = Application.new(app_options)
|
32
|
-
|
33
|
-
raise "Context already exists: " + app.contextPath if @apps.has_key?(app.contextPath)
|
34
|
-
|
35
|
-
# add /WEB-INF/src and lib directory to the load path...
|
36
|
-
$:.unshift(".#{app.contextPathDir}/WEB-INF/lib")
|
37
|
-
$:.unshift(".#{app.contextPathDir}/WEB-INF/src")
|
38
|
-
|
39
|
-
@apps[app.contextPath] = app
|
40
|
-
|
41
|
-
app.reloadAppManager
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
alias add add_application
|
46
|
-
|
47
|
-
def contexts
|
48
|
-
@apps.keys
|
49
|
-
end
|
50
|
-
|
51
28
|
def call(env)
|
52
29
|
|
53
30
|
# force reload of any mentawai file if modified...
|
@@ -56,36 +33,23 @@ module Mentawai
|
|
56
33
|
req = Rack::Request.new(env)
|
57
34
|
res = Rack::Response.new(env)
|
58
35
|
|
59
|
-
contextPath = find_context_path(req.fullpath)
|
60
|
-
|
61
|
-
env['menta.contextPath'] = contextPath
|
62
|
-
|
63
|
-
app = @apps[contextPath]
|
64
36
|
app.reloadAppManager(filesLoaded > 0)
|
65
37
|
|
66
38
|
controller = Mentawai::Core::Controller.new(app)
|
39
|
+
|
67
40
|
controller.service(env, req, res)
|
68
|
-
end
|
69
|
-
|
70
|
-
def find_context_path(fullpath)
|
71
|
-
# Extract context path from URL...
|
72
|
-
if fullpath =~ /(\/[^\/]*)/ then
|
73
|
-
contextPath = $1
|
74
|
-
else
|
75
|
-
raise "Cannot get context path: " + env.fullpath
|
76
|
-
end
|
77
41
|
|
78
|
-
# Decide about context path...
|
79
|
-
if @apps.has_key?(contextPath) then
|
80
|
-
contextPath
|
81
|
-
elsif @apps.has_key?('/') then
|
82
|
-
'/'
|
83
|
-
else
|
84
|
-
raise "Cannot find context: " + contextPath
|
85
|
-
end
|
86
42
|
end
|
87
43
|
|
88
|
-
def start
|
44
|
+
def start()
|
45
|
+
|
46
|
+
@app = Application.new(options)
|
47
|
+
|
48
|
+
# add /WEB-INF/src and lib directory to the load path...
|
49
|
+
$:.unshift("./WEB-INF/lib")
|
50
|
+
$:.unshift("./WEB-INF/src")
|
51
|
+
|
52
|
+
@app.reloadAppManager
|
89
53
|
|
90
54
|
session_config = { :cookie_expire_after => 0, :session_expire_after => 10 }
|
91
55
|
session_server = Mentawai::Session::MentaSession.new(self, session_config)
|
data/lib/mentawai/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mentawai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergio Oliveira Jr.
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-05-07 00:00:00 -03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- lib/mentawai/core/output.rb
|
51
51
|
- lib/mentawai/core/session.rb
|
52
52
|
- lib/mentawai/handler/menta_handler.rb
|
53
|
+
- lib/mentawai/i18n/i18n_file.rb
|
53
54
|
- lib/mentawai/i18n/loc_manager.rb
|
54
55
|
- lib/mentawai/loader.rb
|
55
56
|
- lib/mentawai/page/methods/content_type.rb
|