How do I access the request object inside my java controller in the play framework?
The Scala tutorial-page references some sort of "request" object, but I don't see how to access that in the Java tutorial-page.
I'm using Play Framework 2.5
The answer is really clear and straightforward:
When you call request() you access to the requests object, like headers and body, example request().body()
Much more here: https://www.playframework.com/documentation/2.5.x/JavaBodyParsers
Just add explicitly type: Action { request: Request[_] => ... }
Related
In API Gateway I have a GET endpoint like following (with some request headers too)
http://awesomedomain/v1/myspecialkey/find?a=b
Is there a way the Lambda (Authorizer) code can read "myspecialkey"?
Thanks in advance
Yes it is possible, when you are building lambda authorizer you can choose Lambda Payload Type to be Request.
Assuming that you have named your first lambda parameter events, then inside of the lambda, you will have access to your parameter values via
event.pathParameters
as well as access to your query string via
event.queryStringParameters
And other request information if needed, such as authorization token which you can extract from event.headers.
the above code uses NodeJs syntax, the same logic holds true for Java but you will need to modify it according to Java syntax
I am still learning Play framework for Java. I am trying to create a test case where I am mocking server. In there, I am defining url and response using RoutingDSL. However, I can't seem to find any way to access request headers (actually I am trying to see if client is sending proper cookies).
Here is the snippet of code:
server = Server.forRouter(20000, components ->
RoutingDsl.fromComponents(components)
.GET("/demo").routeTo(() ->
// Here I need something to get request headers or cookies
new Result(HttpStatus.SEE_OTHER_303)).build());
Solution:
After looking through the documentation (it was mentioned briefly here: https://www.playframework.com/documentation/2.6.x/JavaRoutingDsl):
Since you will be implementing actions, you may want to import the
static methods from Controller, which includes factory methods for
creating results, accessing the request, response and session.
Basically, idea was to use static methods from play.mvc.Controller class, where that information is held:
server = Server.forRouter(20000, components ->
RoutingDsl.fromComponents(components)
.GET("/demo").routeTo(() ->
doSomethingWithRequest(play.mvc.Controller.request())
new Result(HttpStatus.SEE_OTHER_303)).build());
I've read https://www.playframework.com/documentation/2.5.x/ScalaHttpFilters#more-powerful-filters, but I still don't understand how to access the request body inside the filter chain. I'm trying to make an accumulator but I'm not sure how to access the nextFilter in the apply method of EssentialAction. If anyone knows how to actually access the request body inside the filter chain let me know! I'm working in java
In order to parse the body of the request, you can use Action composition in your filters. You must actually stream / parse the body in the filter manually, and then let the framework parse it again as it passes to your controller.
Here is a good article on how this can be achieved.
https://www.javacodegeeks.com/2013/02/understanding-the-play-filter-api.html
I have a basic service in Java, for example:
public interface FolderService {
void deleteFolder(String path);
void createFolder(String path, String folderName);
void moveFolder(String oldPath, String newPath);
}
which has multiple implementations. How can I map this service on AWS Lambda and API Gateway ?
I am expecting the API to have the format
POST {some_url}/folderService/createFolder
or
GET {some_url}/folderService/createFolder?path=/home/user&folderName=test
First, design your API mapping each HTTP method to a Java method.
DELETE /{path}
POST /{path}/{folderName}
PUT /{oldPath}?to={newPath} or PUT /{newPath}?from={oldPath}
Second, create the API Gateway Mapping. Each HTTP method has its own mapping. Define a constant value with the name of the method. Ex.
"action" : "deleteFolder"
Create three lambda functions. Each function, in the function handler, reads the "action" attribute and call the correct method.
or
Create one single lambda function that reads the action and calls the respective Java method.
API Gateway Mapping Template
Lambda Function Handler (Java)
You already have experience with AWS Lambda? The mapping part can be tricky. Feel free to ask for more details.
I came for ruby/rails background. I am facing issues to get header attributes of rest call.
In case of rails, I used to write below code to list all requested header attributes.
puts request.headers.inspect
Could any body please suggest me what is the equivalent for Grails ?
The request object is an instance of the Servlet API's HttpServletRequest interface, so you can use the getHeader and getHeaderNames methods
This is an example of how to print all the headers - add it where you can access the request object (e.g. inside a controller method):
request.getHeaderNames().each {
println(it + ":" + request.getHeader(it))
}
Below is the code, which list all header attributes.
request.headerNames.each{
println it
}
attributes
accept
accept-encoding
content-type
api-key
time-stamp
signature
user-agent
host
Take a look here. On this page you can find "The request object is an instance of the Servlet API's HttpServletRequest interface". You can use getHeader and getHeaderNames methods
Also, remember in Groovy/Grails any getXXX method will treat XXX as a property. So, getHeader and getHeaderNames can be abbreviated to request.header 'someheadername' or request.headerNames