Retrieving HTTP parameters in Grails - grails

the request was sent from an HTML form, the request body contains the form data,how can i retrieve that data in my Grails application.Below is the URL from where i need to retrieve data.I'm a Rookie in Grails so please help me with this.
hxxp//localhost:8080/copypolicyNumber/loginaction.jsp?fname=Roger&lname=wallace&city=Des+Moines&pnum=123456&submit=Submit

As you just started with Grails, I suggest you look at the screencasts available in the Grails website, and also check the free e-book Getting Started with Grails (need registration).
Grails works with the params map for both GET and POST requests. Also, it uses an special url mapping that you need to be aware of.
So, assuming that you have a login controller with the action login and considering that you called the url: myapp/login/login?fname=Roger&lname=wallace&city=Des+Moines&pnum=123456
class LoginController {
def login() {
println params.fname //Roger
println params.lname //wallace
println params.city //Des Moines
println params.pnum //123456
}
}

Related

Send POST request in ATG with checkFormRedirect method

I have requirement of changing GET to POST redirection to external URL.
Currently, we are using checkFormRedirect(url,req,res) to redirect to external URL which by default uses GET as per my understanding. I want to change this request to POST.
One way is we can use HTTPClient API for re-direction.
Is there any way ATG out of box provide some thing to POST redirection. Please help.
If you submitted a form in JSP as you are using checkFormRedirect(). It is already a POST request and you can get data in your handlerXXX method.
You can use this method to control redirects. The API call of this method looks somewhat like:-
public boolean checkFormRedirect(pSuccessURL, pFailureURL, pRequest, pResponse);
Now, this method redirects to pSuccessURL if no form errors are found in the form. Otherwise, it redirects to pFailureURL.

Web page without real files corresponding to URLs?

geniuses!
I need to make a demo page acting like DBpedia (http://dbpedia.org).
Two pages from different URLs,
http://dbpedia.org/page/Barack_Obama and
http://dbpedia.org/page/Lionel_Messi,
show different content.
I cannot really think DBpedia has million pages for all individual entities (E.g., Barack Obama and Lionel Messi).
How can I handle such URL request?
I know a bit about GET request but example URLs above do not seem like to use GET method.
Thank you in advance!
ps. Please teach me the process. Something like:
1. A user enters URL on a browser.
2. ...
When visiting http://dbpedia.org/page/Barack_Obama, your browser does send a GET request, e.g.:
GET /page/Barack_Obama HTTP/1.1
Host: dbpedia.org
The server (dbpedia.org) receives this GET request and then decides what to do. From the outside, you can’t know (for sure) how the server does something. The two common cases are:
Static web page: a file gets served that exists somewhere on the server. The URL path is often mapped to the server’s file system, but that’s not necessarily the case.
Dynamic web page: a file gets served that is generated on the fly. The content often comes from a database, but that’s not necessarily the case.
After trying some solutions, I'm now using Spring Web MVC framework.
Maybe Dynamic web page solution mentioned in unor's answer.
#Controller
public class SimpleDisplayController {
#RequestMapping("/page/{symbolicName:[!-z]+}")
public String displayEntity(HttpServletRequest hsr, Model model) {
String reqPath = (String) hsr.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String entityLb = reqPath.substring(reqPath.lastIndexOf("/"));
model.addAttribute("label", entityLb);
return "entity";
}
}
I could get request using regex as you can see at the 4th line: #RequestMapping("/page/{symbolicName:[!-z]+}").
The function above returns the string 'entity' which is the name of a HTML file serving as a template.
The following code is a body part of the example HTML template.
<body>
<p th:text="'About entity ' + ${label} + '...'" />
</body>
Since I add an attribute with the key 'label' in the controller above, the template can process ${label}.
In the example HTML template, th:text is a snytax of Thymeleaf (Java library to make an XML/XHTML/HTML5 template) which is supported by Spring.

I need referrer URI in Grails 2.x

I'm using Grails 2.4.4 and trying to get the browser url which loaded my grails app (referrer url) in my controller. I have searched SO and google already, looked in documentation but it seems like i'm searching for a mythical creature.
// inside my before interception
String referer = request.getHeader("Referer");
println "referer= "+referer // null
Anyone done this?

Redirect to error controller in web Api

I write project WEB API 2 and I'd like to show json result in my custom format when I user wrong url like "localhost/api/v1/" or "localhost/api", for this I tryed to create ErrorController with get request which will return message about error. I add route config to WebApiConfig:
config.Routes.MapHttpRoute(
"ErrorInApi",
"v{version}",
new {controller ="Error" }
);
How I can write config to redirect in error controller in all wrong url cases?
Most of the times you use exception filters to deal with exceptions in web api, however you can use them to deal with exceptions during routing, which is the case OP asks. So you may use two new things web api offers you: IExceptionLogger and IExceptionHandler which receive instance of ExceptionContext. Linked page provide sample implementation of such hander and logger.

Determine the url (or controller and action names) of a request which is unauthorized with Shiro Grails plugin

I would like to be able to log the requests that my app receives that are unauthorized. Because the Shiro plugin uses an HTTP redirect to send the user to auth/unauthorized the request object is a fresh one and I can't get the original URL; controller/action name; or request parameters from it.
Is there a way to determine either the original url, or the controller and action names (and request params if possible) inside the AuthController unauthorized action?
I am looking at http://plugins.grails.org/grails-shiro/tags/RELEASE_1_1_3/ShiroGrailsPlugin.groovy as a reference of the plugin source.
Details:
Grails 1.3.7
Shiro Grails plugin 1.1.3
I had the same problem... my solution is not perfect:
a browser sends the so called referer in one of the headers which you can get through
request?.getHeader('Referer')
But the referer is nothing you really can rely on -- but most of the browsers send it.
Another solution could be the filter: try to write the current url to another variable before you call accessControl() in ShiroSecurityFilters.groovy. You can get the current URL through request.forwardURI.
Update: just verified my last assumption - this seems the cleanest solution to me:
In ShiroSecurityFilters.groovy, replace
// Access control by convention.
accessControl()
with
// Access control by convention.
if (!accessControl()) {
session.deniedUrl = request.forwardURI
return false
}
which enables you to access the url as session.deniedUrl in your auth/unauthorized controller.

Resources