Class: VertxApex::RoutingContext

Inherits:
Object
  • Object
show all
Defined in:
/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb

Overview

Represents the context for the handling of a request in Apex.

A new instance is created for each HTTP request that is received in the VertxApex::Router#accept of the router.

The same instance is passed to any matching request or failure handlers during the routing of the request or failure.

The context provides access to the Vertx::HttpServerRequest and Vertx::HttpServerResponse and allows you to maintain arbitrary data that lives for the lifetime of the context. Contexts are discarded once they have been routed to the handler for the request.

The context also provides access to the Session, cookies and body for the request, given the correct handlers in the application.

Instance Method Summary (collapse)

Instance Method Details

- (Fixnum) add_body_end_handler { ... }

Add a handler that will be called just before the response body has been completely written. This gives you a hook where you can write any extra data to the response before it has ended when it will be too late.

Yields:

  • the handler

Returns:

  • (Fixnum)
    the id of the handler. This can be used if you later want to remove the handler.

Raises:

  • (ArgumentError)


289
290
291
292
293
294
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 289

def add_body_end_handler
  if block_given?
    return @j_del.java_method(:addBodyEndHandler, [Java::IoVertxCore::Handler.java_class]).call(Proc.new { yield })
  end
  raise ArgumentError, "Invalid arguments when calling add_body_end_handler()"
end
Add a cookie. This will be sent back to the client in the response. The context must have first been routed to a CookieHandler for this to work.

Parameters:

Returns:

  • (self)

Raises:

  • (ArgumentError)


160
161
162
163
164
165
166
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 160

def add_cookie(cookie=nil)
  if cookie.class.method_defined?(:j_del) && !block_given?
    @j_del.java_method(:addCookie, [Java::IoVertxExtApex::Cookie.java_class]).call(cookie.j_del)
    return self
  end
  raise ArgumentError, "Invalid arguments when calling add_cookie(cookie)"
end

- (Fixnum) add_headers_end_handler { ... }

Add a handler that will be called just before headers are written to the response. This gives you a hook where you can write any extra headers before the response has been written when it will be too late.

Yields:

  • the handler

Returns:

  • (Fixnum)
    the id of the handler. This can be used if you later want to remove the handler.

Raises:

  • (ArgumentError)


270
271
272
273
274
275
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 270

def add_headers_end_handler
  if block_given?
    return @j_del.java_method(:addHeadersEndHandler, [Java::IoVertxCore::Handler.java_class]).call(Proc.new { yield })
  end
  raise ArgumentError, "Invalid arguments when calling add_headers_end_handler()"
end
@return the number of cookies. The context must have first been routed to a CookieHandler for this to work.

Returns:

  • (Fixnum)

Raises:

  • (ArgumentError)


180
181
182
183
184
185
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 180

def cookie_count
  if !block_given?
    return @j_del.java_method(:cookieCount, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling cookie_count()"
end

- (Set<::VertxApex::Cookie>) cookies

@return a set of all the cookies. The context must have first been routed to a CookieHandler for this to be populated.

Returns:

Raises:

  • (ArgumentError)


189
190
191
192
193
194
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 189

def cookies
  if !block_given?
    return ::Vertx::Util::Utils.to_set(@j_del.java_method(:cookies, []).call()).map! { |elt| ::VertxApex::Cookie.new(elt) }
  end
  raise ArgumentError, "Invalid arguments when calling cookies()"
end

- (::VertxApex::Route) current_route

@return the current route this context is being routed through.

Returns:

Raises:

  • (ArgumentError)


122
123
124
125
126
127
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 122

def current_route
  if !block_given?
    return ::VertxApex::Route.new(@j_del.java_method(:currentRoute, []).call())
  end
  raise ArgumentError, "Invalid arguments when calling current_route()"
end

- (void) fail(statusCode = nil)

This method returns an undefined value.

Fail the context with the specified status code.

This will cause the router to route the context to any matching failure handlers for the request. If no failure handlers match a default failure response will be sent.

Parameters:

  • statusCode (Fixnum) (defaults to: nil)
    the HTTP status code

Raises:

  • (ArgumentError)


77
78
79
80
81
82
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 77

def fail(statusCode=nil)
  if statusCode.class == Fixnum && !block_given?
    return @j_del.java_method(:fail, [Java::int.java_class]).call(statusCode)
  end
  raise ArgumentError, "Invalid arguments when calling fail(statusCode)"
end

- (true, false) failed?

@return true if the context is being routed to failure handlers.

Returns:

  • (true, false)

Raises:

  • (ArgumentError)


306
307
308
309
310
311
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 306

def failed?
  if !block_given?
    return @j_del.java_method(:failed, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling failed?()"
end

- (Set<::VertxApex::FileUpload>) file_uploads

@return a set of fileuploads (if any) for the request. The context must have first been routed to a BodyHandler for this to work.

Returns:

Raises:

  • (ArgumentError)


228
229
230
231
232
233
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 228

def file_uploads
  if !block_given?
    return ::Vertx::Util::Utils.to_set(@j_del.java_method(:fileUploads, []).call()).map! { |elt| ::VertxApex::FileUpload.new(elt) }
  end
  raise ArgumentError, "Invalid arguments when calling file_uploads()"
end

- (Object) get(key = nil)

Get some data from the context. The data is available in any handlers that receive the context.

Parameters:

  • key (String) (defaults to: nil)
    the key for the data

Returns:

  • (Object)
    the data

Raises:

  • (ArgumentError)


97
98
99
100
101
102
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 97

def get(key=nil)
  if key.class == String && !block_given?
    return ::Vertx::Util::Utils.from_object(@j_del.java_method(:get, [Java::java.lang.String.java_class]).call(key))
  end
  raise ArgumentError, "Invalid arguments when calling get(key)"
end

- (String) get_acceptable_content_type

If the route specifies produces matches, e.g. produces `text/html` and `text/plain`, and the `accept` header matches one or more of these then this returns the most acceptable match.

Returns:

  • (String)
    the most acceptable content type.

Raises:

  • (ArgumentError)


260
261
262
263
264
265
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 260

def get_acceptable_content_type
  if !block_given?
    return @j_del.java_method(:getAcceptableContentType, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling get_acceptable_content_type()"
end

- (::Vertx::Buffer) get_body

@return Get the entire HTTP request body as a Vertx::Buffer. The context must have first been routed to a BodyHandler for this to be populated.

Returns:

  • (::Vertx::Buffer)

Raises:

  • (ArgumentError)


219
220
221
222
223
224
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 219

def get_body
  if !block_given?
    return ::Vertx::Buffer.new(@j_del.java_method(:getBody, []).call())
  end
  raise ArgumentError, "Invalid arguments when calling get_body()"
end

- (Hash{String => Object}) get_body_as_json

@return Get the entire HTTP request body as a Vertx::JsonObject. The context must have first been routed to a BodyHandler for this to be populated.

Returns:

  • (Hash{String => Object})

Raises:

  • (ArgumentError)


210
211
212
213
214
215
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 210

def get_body_as_json
  if !block_given?
    return @j_del.java_method(:getBodyAsJson, []).call() != nil ? JSON.parse(@j_del.java_method(:getBodyAsJson, []).call().encode) : nil
  end
  raise ArgumentError, "Invalid arguments when calling get_body_as_json()"
end

- (String) get_body_as_string(encoding = nil)

Get the entire HTTP request body as a string, assuming the specified encoding. The context must have first been routed to a BodyHandler for this to be populated.

Parameters:

  • encoding (String) (defaults to: nil)
    the encoding, e.g. "UTF-16"

Returns:

  • (String)
    the body

Raises:

  • (ArgumentError)


199
200
201
202
203
204
205
206
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 199

def get_body_as_string(encoding=nil)
  if !block_given? && encoding == nil
    return @j_del.java_method(:getBodyAsString, []).call()
  elsif encoding.class == String && !block_given?
    return @j_del.java_method(:getBodyAsString, [Java::java.lang.String.java_class]).call(encoding)
  end
  raise ArgumentError, "Invalid arguments when calling get_body_as_string(encoding)"
end
Get the cookie with the specified name. The context must have first been routed to a CookieHandler for this to work.

Parameters:

  • name (String) (defaults to: nil)
    the cookie name

Returns:

Raises:

  • (ArgumentError)


150
151
152
153
154
155
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 150

def get_cookie(name=nil)
  if name.class == String && !block_given?
    return ::VertxApex::Cookie.new(@j_del.java_method(:getCookie, [Java::java.lang.String.java_class]).call(name))
  end
  raise ArgumentError, "Invalid arguments when calling get_cookie(name)"
end

- (String) mount_point

@return the mount point for this router. It will be null for a top level router. For a sub-router it will be the path at which the subrouter was mounted.

Returns:

  • (String)

Raises:

  • (ArgumentError)


114
115
116
117
118
119
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 114

def mount_point
  if !block_given?
    return @j_del.java_method(:mountPoint, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling mount_point()"
end

- (void) next

This method returns an undefined value.

Tell the router to route this context to the next matching route (if any). This method, if called, does not need to be called during the execution of the handler, it can be called some arbitrary time later, if required.

If next is not called for a handler then the handler should make sure it ends the response or no response will be sent.

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 65

def next
  if !block_given?
    return @j_del.java_method(:next, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling next()"
end

- (String) normalised_path

Return the normalised path for the request.

The normalised path is where the URI path has been decoded, i.e. any unicode or other illegal URL characters that were encoded in the original URL with `%` will be returned to their original form. E.g. `%20` will revert to a space. Also `+` reverts to a space in a query.

The normalised path will also not contain any `..` character sequences to prevent resources being accessed outside of the permitted area.

It's recommended to always use the normalised path as opposed to Vertx::HttpServerRequest#path if accessing server resources requested by a client.

Returns:

  • (String)
    the normalised path

Raises:

  • (ArgumentError)


140
141
142
143
144
145
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 140

def normalised_path
  if !block_given?
    return @j_del.java_method(:normalisedPath, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling normalised_path()"
end

- (self) put(key = nil, obj = nil)

Put some arbitrary data in the context. This will be available in any handlers that receive the context.

Parameters:

  • key (String) (defaults to: nil)
    the key for the data
  • obj (Object) (defaults to: nil)
    the data

Returns:

  • (self)

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 87

def put(key=nil,obj=nil)
  if key.class == String && (obj.class == String  || obj.class == Hash || obj.class == Array || obj.class == NilClass || obj.class == TrueClass || obj.class == FalseClass || obj.class == Fixnum || obj.class == Float) && !block_given?
    @j_del.java_method(:put, [Java::java.lang.String.java_class,Java::java.lang.Object.java_class]).call(key,::Vertx::Util::Utils.to_object(obj))
    return self
  end
  raise ArgumentError, "Invalid arguments when calling put(key,obj)"
end

- (true, false) remove_body_end_handler?(handlerID = nil)

Remove a body end handler

Parameters:

  • handlerID (Fixnum) (defaults to: nil)
    the id as returned from io.vertx.ext.apex.RoutingContext#addBodyEndHandler(io.vertx.core.Handler).

Returns:

  • (true, false)
    true if the handler existed and was removed, false otherwise

Raises:

  • (ArgumentError)


298
299
300
301
302
303
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 298

def remove_body_end_handler?(handlerID=nil)
  if handlerID.class == Fixnum && !block_given?
    return @j_del.java_method(:removeBodyEndHandler, [Java::int.java_class]).call(handlerID)
  end
  raise ArgumentError, "Invalid arguments when calling remove_body_end_handler?(handlerID)"
end
Remove a cookie. The context must have first been routed to a CookieHandler for this to work.

Parameters:

  • name (String) (defaults to: nil)
    the name of the cookie

Returns:

Raises:

  • (ArgumentError)


171
172
173
174
175
176
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 171

def remove_cookie(name=nil)
  if name.class == String && !block_given?
    return ::VertxApex::Cookie.new(@j_del.java_method(:removeCookie, [Java::java.lang.String.java_class]).call(name))
  end
  raise ArgumentError, "Invalid arguments when calling remove_cookie(name)"
end

- (true, false) remove_headers_end_handler?(handlerID = nil)

Remove a headers end handler

Parameters:

  • handlerID (Fixnum) (defaults to: nil)
    the id as returned from io.vertx.ext.apex.RoutingContext#addHeadersEndHandler(io.vertx.core.Handler).

Returns:

  • (true, false)
    true if the handler existed and was removed, false otherwise

Raises:

  • (ArgumentError)


279
280
281
282
283
284
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 279

def remove_headers_end_handler?(handlerID=nil)
  if handlerID.class == Fixnum && !block_given?
    return @j_del.java_method(:removeHeadersEndHandler, [Java::int.java_class]).call(handlerID)
  end
  raise ArgumentError, "Invalid arguments when calling remove_headers_end_handler?(handlerID)"
end

- (::Vertx::HttpServerRequest) request

@return the HTTP request object

Returns:

  • (::Vertx::HttpServerRequest)

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 38

def request
  if !block_given?
    if @cached_request != nil
      return @cached_request
    end
    return @cached_request = ::Vertx::HttpServerRequest.new(@j_del.java_method(:request, []).call())
  end
  raise ArgumentError, "Invalid arguments when calling request()"
end

- (::Vertx::HttpServerResponse) response

@return the HTTP response object

Returns:

  • (::Vertx::HttpServerResponse)

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 49

def response
  if !block_given?
    if @cached_response != nil
      return @cached_response
    end
    return @cached_response = ::Vertx::HttpServerResponse.new(@j_del.java_method(:response, []).call())
  end
  raise ArgumentError, "Invalid arguments when calling response()"
end

- (::VertxApex::Session) session

Get the session. The context must have first been routed to a SessionHandler for this to be populated. Sessions live for a browser session, and are maintained by session cookies.

Returns:

Raises:

  • (ArgumentError)


238
239
240
241
242
243
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 238

def session
  if !block_given?
    return ::VertxApex::Session.new(@j_del.java_method(:session, []).call())
  end
  raise ArgumentError, "Invalid arguments when calling session()"
end

- (void) set_acceptable_content_type(contentType = nil)

This method returns an undefined value.

Set the acceptable content type. Used by

Parameters:

  • contentType (String) (defaults to: nil)
    the content type

Raises:

  • (ArgumentError)


333
334
335
336
337
338
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 333

def set_acceptable_content_type(contentType=nil)
  if contentType.class == String && !block_given?
    return @j_del.java_method(:setAcceptableContentType, [Java::java.lang.String.java_class]).call(contentType)
  end
  raise ArgumentError, "Invalid arguments when calling set_acceptable_content_type(contentType)"
end

- (void) set_body(body = nil)

This method returns an undefined value.

Set the body. Used by the BodyHandler. You will not normally call this method.

Parameters:

  • body (::Vertx::Buffer) (defaults to: nil)
    the body

Raises:

  • (ArgumentError)


315
316
317
318
319
320
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 315

def set_body(body=nil)
  if body.class.method_defined?(:j_del) && !block_given?
    return @j_del.java_method(:setBody, [Java::IoVertxCoreBuffer::Buffer.java_class]).call(body.j_del)
  end
  raise ArgumentError, "Invalid arguments when calling set_body(body)"
end

- (void) set_session(session = nil)

This method returns an undefined value.

Set the session. Used by the SessionHandler. You will not normally call this method.

Parameters:

Raises:

  • (ArgumentError)


324
325
326
327
328
329
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 324

def set_session(session=nil)
  if session.class.method_defined?(:j_del) && !block_given?
    return @j_del.java_method(:setSession, [Java::IoVertxExtApex::Session.java_class]).call(session.j_del)
  end
  raise ArgumentError, "Invalid arguments when calling set_session(session)"
end

- (Fixnum) status_code

If the context is being routed to failure handlers after a failure has been triggered by calling #fail then this will return that status code. It can be used by failure handlers to render a response, e.g. create a failure response page.

Returns:

  • (Fixnum)
    the status code used when signalling failure

Raises:

  • (ArgumentError)


248
249
250
251
252
253
254
255
256
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 248

def status_code
  if !block_given?
    if @cached_status_code != nil
      return @cached_status_code
    end
    return @cached_status_code = @j_del.java_method(:statusCode, []).call()
  end
  raise ArgumentError, "Invalid arguments when calling status_code()"
end

- (::Vertx::Vertx) vertx

@return the Vert.x instance associated to the initiating VertxApex::Router for this context

Returns:

  • (::Vertx::Vertx)

Raises:

  • (ArgumentError)


105
106
107
108
109
110
# File '/Users/julien/java/vertx-aggregator/modules/vertx-apex/src/main/resources/vertx-apex/routing_context.rb', line 105

def vertx
  if !block_given?
    return ::Vertx::Vertx.new(@j_del.java_method(:vertx, []).call())
  end
  raise ArgumentError, "Invalid arguments when calling vertx()"
end