1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Initialize commands from HTTP requests
17 # FIXME: this module is pretty tied up to the nitwed routes.
18 # To be more generic, param names should be extracted as variables.
21 import commands_catalog
27 import nitcorn
::vararg_routes
29 redef class DocCommand
30 # Init the command from an HTTPRequest
31 fun http_init
(req
: HttpRequest): CmdMessage do
32 var filter
= cmd_filter
33 var opt_vis
= req
.visibility_arg
("min-visibility")
34 if opt_vis
!= null then filter
.min_visibility
= opt_vis
35 var opt_fictive
= req
.bool_arg
("no-fictive")
36 if opt_fictive
!= null then filter
.accept_fictive
= not opt_fictive
37 var opt_test
= req
.bool_arg
("no-test")
38 if opt_test
!= null then filter
.accept_test
= not opt_test
39 var opt_redef
= req
.bool_arg
("no-redef")
40 if opt_redef
!= null then filter
.accept_redef
= not opt_redef
41 var opt_extern
= req
.bool_arg
("no-extern")
42 if opt_extern
!= null then filter
.accept_extern
= not opt_extern
43 var opt_example
= req
.bool_arg
("no-example")
44 if opt_example
!= null then filter
.accept_example
= not opt_example
45 var opt_attr
= req
.bool_arg
("no-attribute")
46 if opt_attr
!= null then filter
.accept_attribute
= not opt_attr
47 var opt_doc
= req
.bool_arg
("no-empty-doc")
48 if opt_doc
!= null then filter
.accept_empty_doc
= not opt_doc
49 var opt_inh
= req
.mentity_arg
(model
, "inherit")
50 if opt_inh
!= null then filter
.accept_inherited
= opt_inh
51 var opt_match
= req
.string_arg
("match")
52 if opt_match
!= null then filter
.accept_full_name
= opt_match
59 redef fun http_init
(req
) do
60 var name
= req
.param
("id")
61 if name
!= null then name
= name
.from_percent_encoding
62 self.mentity_name
= name
69 redef fun http_init
(req
) do
70 var opt_limit
= req
.int_arg
("l")
71 if opt_limit
!= null then limit
= opt_limit
72 var opt_page
= req
.int_arg
("p")
73 if opt_page
!= null then page
= opt_page
82 redef class CmdMessage
83 # HTTP code to return for this message
84 var http_status_code
= 200
88 redef var http_status_code
= 400
91 redef class CmdWarning
92 redef var http_status_code
= 404
95 redef class ErrorMEntityNoName
96 redef var http_status_code
= 400
99 redef class ErrorMEntityNotFound
100 redef var http_status_code
= 404
103 redef class ErrorMEntityConflict
104 redef var http_status_code
= 300
109 redef class CmdComment
110 redef fun http_init
(req
) do
111 var opt_full_doc
= req
.bool_arg
("full_doc")
112 if opt_full_doc
!= null then full_doc
= opt_full_doc
113 var opt_fallback
= req
.bool_arg
("fallback")
114 if opt_fallback
!= null then fallback
= opt_fallback
115 var opt_format
= req
.string_arg
("format")
116 if opt_format
!= null then format
= opt_format
121 redef class CmdEntityLink
122 redef fun http_init
(req
) do
123 var opt_text
= req
.string_arg
("text")
124 if opt_text
!= null then text
= opt_text
125 var opt_title
= req
.string_arg
("title")
126 if opt_title
!= null then title
= opt_title
131 redef class CmdAncestors
132 redef fun http_init
(req
) do
133 var opt_parents
= req
.bool_arg
("parents")
134 if opt_parents
!= null then parents
= opt_parents
139 redef class CmdDescendants
140 redef fun http_init
(req
) do
141 var opt_children
= req
.bool_arg
("children")
142 if opt_children
!= null then children
= opt_children
147 redef class CmdEntityList
148 # FIXME avoid linearization conflict
149 redef fun http_init
(req
) do return super
152 redef class CmdSearch
153 redef fun http_init
(req
) do
154 query
= req
.string_arg
("q")
159 redef class CmdModelEntities
160 redef fun http_init
(req
) do
161 var opt_kind
= req
.string_arg
("kind")
162 if opt_kind
!= null then kind
= opt_kind
168 redef fun http_init
(req
) do
169 var opt_format
= req
.string_arg
("format")
170 if opt_format
!= null then format
= opt_format
175 redef class CmdEntityCode
176 # FIXME avoid linearization conflict
177 redef fun http_init
(req
) do
178 var name
= req
.param
("id")
179 if name
!= null then name
= name
.from_percent_encoding
182 var opt_format
= req
.string_arg
("format")
183 if opt_format
!= null then format
= opt_format
191 redef fun http_init
(req
) do
192 var opt_format
= req
.string_arg
("format")
193 if opt_format
!= null then format
= opt_format
198 redef class CmdInheritanceGraph
199 redef fun http_init
(req
) do
200 var opt_pdepth
= req
.int_arg
("pdepth")
201 if opt_pdepth
!= null then pdepth
= opt_pdepth
202 var opt_cdepth
= req
.int_arg
("cdepth")
203 if opt_cdepth
!= null then cdepth
= opt_cdepth
210 redef class CmdCatalogTag
211 redef fun http_init
(req
) do
212 var tag
= req
.param
("tid")
213 if tag
!= null then tag
= tag
.from_percent_encoding
219 redef class CmdCatalogPerson
220 redef fun http_init
(req
) do
221 var name
= req
.param
("pid")
222 if name
!= null then name
= name
.from_percent_encoding
223 self.person_name
= name
230 redef class HttpRequest
232 # Map String visiblity name to MVisibility object
233 var allowed_visibility
: HashMap[String, MVisibility] is lazy
do
234 var res
= new HashMap[String, MVisibility]
235 res
["public"] = public_visibility
236 res
["protected"] = protected_visibility
237 res
["private"] = private_visibility
241 # Get arg as a MVisibility
243 # Return `null` if no option with that `key` or if the value is not in
244 # `allowed_visibility`.
245 fun visibility_arg
(key
: String): nullable MVisibility do
246 var value
= string_arg
(key
)
247 if value
== null then return null
248 if not allowed_visibility
.keys
.has
(key
) then return null
249 return allowed_visibility
[value
]
252 # Get arg as a MEntity
254 # Lookup first by `MEntity::full_name` then by `MEntity::name`.
255 # Return `null` if the mentity name does not exist or return a conflict.
256 private fun mentity_arg
(model
: Model, key
: String): nullable MEntity do
257 var value
= string_arg
(key
)
258 if value
== null or value
.is_empty
then return null
260 var mentity
= model
.mentity_by_full_name
(value
)
261 if mentity
!= null then return mentity
263 var mentities
= model
.mentities_by_name
(value
)
264 if mentities
.is_empty
or mentities
.length
> 1 then return null
265 return mentities
.first