Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scalatra - how do we do an internal redirect / forward of request

I want to call another internal url from my scalatra 'controller'. I can't do a simple redirect, as there's some security settings that mean a user has access only to the first url.

Is there a way to do this?

like image 881
Louis Sayers Avatar asked Jul 22 '11 06:07

Louis Sayers


2 Answers

get("/foo") {
  servletContext.getRequestDispatcher("/bar").forward(request, response)
} 
like image 90
Ali Salehi Avatar answered Oct 27 '22 09:10

Ali Salehi


The get() method is defined as (similar to POST, et al):

def get(transformers : org.scalatra.RouteTransformer*)(action : => scala.Any) : org.scalatra.Route

Depends on what you mean by internal redirect, I presume you just want to execute another route's action. You have a few options of what you can do. This seems to be working for me:

val canonicalEndpoint = get("/first/route") {
  //do things in here    
}

Then you could subsequently do:

get("/second/route")( canonicalEndpoint.action )

And I think you would get your desired response.

I like saving the whole Route response of the get() as you may also want to use that with scalatra's url() function in routing.

like image 21
trevorgrayson Avatar answered Oct 27 '22 08:10

trevorgrayson