Cache-Control: private in Spring-MVC - spring

WebContentInterceptor is nice, but I can't find how to make it add the "private" directive to the CacheControl HTTP header.
I either need to subclass it, or use response.setHeader in my controllers.
Is there any other convenient way to do this?
Preferably something annotation based :-)

Try https://github.com/foo4u/spring-mvc-cache-control. And it's annotation based :D.
It seems that Spring is taking into account adding this in a future version: https://jira.springsource.org/browse/SPR-7129.
Meantime, it seems that it's included in v4.2.

Related

Response Cache Attribute isn't override

I am using aspnetboilerplate version 5.13.0. I want response cache but it didn't work no matter what i tried. Response Header has always cache-control: no-cache, no-store.
I tried the following;
I just added the response cache attribute.
Response Cache middleware and cache profile.
I defined DefaultResponseCacheAttributeForControllers in WebCoreModule PreInitialize
Offical Documentation;
Abp provides a convenient way for you to configure the default Cache-Control header for all ApplicationService and Controller via IAbpAspNetCoreConfiguration
DefaultResponseCacheAttributeForAppServices: Used if Controller class does not define Microsoft.AspNetCore.Mvc.ResponseCacheAttribute
DefaultResponseCacheAttributeForControllers: Used if ApplicationService class does not define Microsoft.AspNetCore.Mvc.ResponseCacheAttribute
Note: Cache-Control is not configured by default. You may configure for all ApplicationService and Controller then use Microsoft.AspNetCore.Mvc.ResponseCacheAttribute at method/action level to override it.
But it is a bit difficult for me to understand. Does anyone have a code sample?

Using RequestScope without ServletModule

I was exploring #RequestScoped and was wondering if there's way to use it without installing ServletModule. I am using Guice 3.0 + Jersey 1.17 and probably don't want to use GuiceContainer & GuiceServletContextListener.
I want object creation(injections) per request depending on some user input in the Jersey request. Is it possible? What can be performance & security considerations of using GuiceContainer if I had to replace my existing ServletContextListener with that of Guice?
If there's a way of using RequestScope as per my needs, can you give me some references for the same?
It is possible to bind a custom Scope implementation to a predefined scoping annotation like #RequestScoped. It does mean that then you cannot use ServletModule, since you can't bind two different implementations to the same scoping annotation.
See the documentation on Custom Scopes for details. You will need to write code to determine what constitutes a "request" for purposes of scoping, and trigger entering and exiting the scope as necessary.
For example, in the normal Guice implementation, ServletScopes.RequestScope uses a ThreadLocal initialized in GuiceFilter to keep track of what the current request is.

Possible to use #RepositoryRestResource with #PathVariable instead of #Param?

I have a RestRepositoryResource which is working properly. However, I would prefer to structure URLs using path variables instead of query parameters. The goal would be this:
http://localhost/persons/findByLastName/Smith/
instead of this:
http://localhost/persons/findByLastName?lastName=Smith
I have played around with various annotations but not achieved this using RestRepositoryResource. Is this possible or does this have to be done with a Controller resource mapping?
Spring data repositories don't support the #PathVariable annotation right now. However a simple workaround for this problem might be to use URLRewriteFilter internally re-route the request for http://localhost/persons/findByLastName/Smith/ to http://localhost/persons/findByLastName?lastName=Smith without the user noticing.

Enunciate validation of Jersey API fails with #POST #FormParam + String data

I'm evaluating enunciate to document our REST APIs and I'm having an issue with the validation step:
Validation result has errors.
my.java: error: [core] An entity parameter must be of type MultivaluedMap<String, String> if there is another parameter annotated with #FormParam.
#FormParam("my-param") String myParam, String data)
This construct of accepting the POST data as a String entity in addition to #FormParam bindings is supported by Jersey, so not sure why enunciate is choking on it? Is this not JAX-RS compliant?
This is a really useful to capture the full post data for auditing purposes if something went wrong. Is there a way to configure enunciate to ignore this argument?
If not, is there some other way to capture the post data in a way that would keep enunciate happy? I'm reluctant to go to MultivaluedMap as the stringification process may not result in exactly the String which was passed in...
Thanks!
It may be that the validation checks Enunciate performs are outdated. You might consider requesting a change by submitting a JIRA issue.
One thing you could try as a workaround is to create your own custom parameter annotation and configure Enunciate consider it as a custom resource parameter.
<enunciate>
...
<services>
<rest>
<custom-resource-parameter-annotation qualifiedName="org.myco.CustomResourceParam"/>
</rest>
</services>
...
</enunciate>
However, if the purpose of the parameter is for auditing, I'd really recommend using a Jersey filter. That way you don't litter your API code with auditing concerns.

Inject cookies into controller

Is there any way to inject the cookie dependecy to a controller? Or do i have to write my own interface and wrapper class around the Cookie collection class?
I think you're asking about whether you can get a Cookie as a parameter to an Action. I don't believe you can do this, so you'll have to hit the Cookie class directly.
What we do in this case (when cookie based data is required by most of the actions in an application) is put a utility method in a Controller base class and then have all our controllers descend from that. Makes it very easy to use the Cookie in an Action, and centralizes the code for extracting it.
Since no better answered surfaced I just implemented a interface and injected that for concrete scenario and Mocked it for test

Resources