Access request parameters in Freemarker templates with JForum - freemarker

I'm using JForum and I wanted to access request parameters in Freemarker template files.
I googled a bit and tried the followings without any luck.
${RequestParameters.paramName}
${RequestParameters['paramName']}
${args['paramName']}
${param.paramName}
They all return RequestParameters is undefined or args is undefined sort of errors.
I saw some questions like "How do you access request parameters in Freemarker templates using Spring MVC?" but I could not get help from those questions and answers.
Followup
So as far as I've found out, Freemarker does not reveal those information. You need to add those info by yourself.
I got some help from javaranch.com. Here is the link, if it would help anyone who's asking the same question: Access request parameters in Freemarker templates.

Maybe JForum doesn't expose the request parameters at all. FreeMarker is not like JSP, it's not bound to Servlet-s. The library/framework that embeds FreeMarker decides if it will expose the request (and the session, etc.), and how. Cleaner applications won't do that. It breaks the separation of concerns (and pure MVC).

Related

AWS APIGW - Access static HTTP Headers for Integration Request in Mapping Template for Lambda

I am trying to modify an API GW config similar to what is found here https://oozio.medium.com/serverless-discord-bot-55f95f26f743
What I need to do is pass to lambda a custom static HTTP Header. I have defined in APIGW -> /{resource} -> Integration Request -> HTTP Headers: a header like {"Name": "application", "Mapped from": "'discord'", "Caching": false} (with single ticks as noted in the documentation for a static value)
This header never appears in the list of headers from the mapping template. I've spent the better part of a day trying to solve this.
Simplified question:
How do you access a static HTTP Header defined in Integration Request, in the Mapping Template?
TIA
After working with AWS support, this is apparently not possible.
Many Thanks for your kind patience. After our call I started to look for a possible
solution to fetch the statically set HTTP header in the mapping template, but
unfortunately could not find any solution.
I then discussed this scenario with my colleagues and also reached out to the
back-end API Gateway Service Team. It seems that this is something that is not
possible currently.
We cannot access the static header value defined in the integration request, in
the request body mapping template. If your use case needs to send the static header
in body for non proxy integration then it is recommended that you may define it
statically in the body mapping template directly.
I understand as you explained to me how you need to access the header in the mapping
template but unfortunately this does not seem to have a solution. Although the
statically set header is seen to be passed in the API Gateway logs, there is no
mechanism in the lambda service to fetch these headers.
Currently, I can ask the Service Team to consider the same as a feature request but
I wont be having a ETA available and also would need a solid reasoning since the
same can be done in the mapping template.
The workaround at this time (March 2021) has to be expressing it directly in the mapping template itself, or using Proxy for Lambda.
The following was provided as justification for the feature request.
When creating APIGW via a templating process (Such as terraform), being able to
express 'API Wide' variables is a useful tool for sending contextual information
to a backend integration. For example, a Lambda that can process requests from
multiple sources, but it needs to know which source should be used. While the
value can be statically expressed in the mapping template, having it be a
variable as part of the API allows for better visibility and management as its
an interact-able component, where as the mapping template is effectively a
complex string. This also does not need to be a header, it could simply be
"integration variables" that allow you to define simple k:v pairs that are either
passed as part of context, or accessible in $context within the mapping template.
While statically expressing it in the mapping template is a workaround, it is
only that.

asp.net-web-api using culture for returning errors in different languages

I'm using ASP.NET Web API 2,
I have resx files for errors, I need to return the error in the correct language (by user culture).
My solution is
1)I created BaseApiController that all the other controllers would inherit.
2)In BaseApiController I changed the Thread.CurrentThread.CurrentCulture for each request.
My question is if this is the correct way for doing it?
Thanks a lot!
There are lots of way doing this. It actually depends on your architecture. Your way is also acceptable. You will implement ResourceManager if you use your way. Let me give some other examples:
You can keep language code in request header and you don't need to
change Thread.CurrentThread.CurrentCulture.
You can store errors in database with language code and you can get
corresponding error with the active culture when the operation is
failed.
You can store errors in cache with language code and you can get
corresponding error with the active culture when the operation is
failed.
As you can see, there are lots of ways. As I said it depends on your architecture.
Good luck

Redirecting back to Portlet from ResourceMapping in Spring 3 portlets

I am trying to work out a way to provide a CSV download through a Spring 3 Portlet. I have a method that uses the #ResourceMapping annotation to define a handler that takes some report params in the form of a #ModelAttribute, builds the report, and returns it. The catch-22 I am running into is validating the parameters being send in from the client form.
If I make the handler a #ResourceMapping, I can set the headers and write out the report as using the ResourceResponse, but I can't seem to figure out how to redirect the user back to the Portlet view with errors when their input fails validation. However, if I make it an #ActionMapping, I can then check the BindingResults and forward them back to the form as needed, but the ActionResponse doesn't allow me to set the Content-Disposition header nor write out the CSV bytes, which is sort of critical for sending the report back.
I am at a total loss here, as I don't even know what my options are. Is it even possible to do what I am trying to do with a Portlet? Are there other examples I could look at for a possible work-around?
I suggest you to use both #ActionMapping and #ResourceMapping to fulfill your requirement.
As you said you were able to handle the validation errors using the #ActionResponse, I'll tell you how to handle the Resource Streaming.
As you know every #ActionResponse is followed by a #RenderResponse, just return the same view but, with a hidden iframe this time whose src points to the ResourceURL.
Now the Request you receive in #ResourceMapping is something which is already Validated. So, you can now serve your CSV.
I dont know how complex is your UI and if you are using jsp as views in your application. If nicely managed, Validation can be handled by #ResourceMapping.
Thank you

Access request parameters from a JSP View in Spring Web MVC without putting them in a model

I'd like to be able to access some HTTP GET parameters directly in a JSP, without having to pass them through a Controller+Model, but at the same time still use the dispatcher/controller/model/view mechanism for other parameters and logic.
This is because I have many HTTP GET parameters that are generated by Javascript and used also only in Javascript. My Controllers don't need them at all.
I tried ${arg}, ${request.arg}, ${requestScope.arg}, nothing seems to work.
If I bypass the dispatcher, ${requestScope.arg} works.
But is there a way to make it work with the dispatcher?
Thanks!
If that's request parameters that you want to access (and not request attributes like the title says), then the syntax is ${param.parameterName}.
If it's request attributes, then it's ${requestScope.attributeName}.
See http://java.sun.com/products/jsp/syntax/2.0/syntaxref207.html#1010522 for a quick reference.

Getting request attributes in freemarker

How do I check a value from the request attribute in freemarker?
I tried <#if *${RequestParameters['servicesettings']} ??> but getting errors ->
Encountered "*" at line
Can anyone help?
It depends on the Web application framework, because FreeMarker itself doesn't expose the request parameters. (Well, except if the framework uses freemareker.ext.servlet.FreemarkerServlet which is kind of an extension to FreeMarker.) Also, usually you shouldn't access request parameters directly from an MVC template, or anything that is HTTP/Servlet specific.
As of the error message, what you have written has a few syntax errors... probably you meant <#if RequestParameters.servicesettings??> (it's not JSP - don't use ${...}-s inside FreeMarker tags). This will require that you have RequestParameters in the data-model, that I can't know for sure...
We should write like this:
${Request.requestattribute}
You can use
${requestParameters.servicesettings}.
According to the JavaDoc of the FreemarkerServlet:
It makes all request, request parameters, session, and servlet context attributes available to templates through Request, RequestParameters, Session, and Application variables.
The scope variables are also available via automatic scope discovery. That is, writing Application.attrName, Session.attrName, Request.attrName is not mandatory; it's enough to write attrName, and if no such variable was created in the template, it will search the variable in Request, and then in Session, and finally in Application.
You can simply write:
${attrName}
to get the value of a request attribute (that you might have set in a servlet request filter using request.setAttribute('attrName', 'value')
Worked for me with Freemarker 2.3.27-incubating

Resources