6

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?

Louis Sayers
  • 2,162
  • 3
  • 30
  • 53

2 Answers2

3
get("/foo") {
  servletContext.getRequestDispatcher("/bar").forward(request, response)
} 
Community
  • 1
  • 1
Ali Salehi
  • 6,899
  • 11
  • 49
  • 75
1

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.

trevorgrayson
  • 1,806
  • 1
  • 21
  • 29