create variable can get accessed trough the request in spring boot - spring

I have a case that a variable needs to be accessed trough the request, the flow goes like thist
custom Filter (retrieve request, doing stuff, set XXX variable)
controller (access XXX Variable)
custom lib (access XXX Variable)
repo (access XXX Variable)
i've read about requestcontext but i heard it'll become a problem when it comes to multithread, so what can you guys recommend to me ?
Thanks

Related

Do I have to store sentry public key in an .env file

Everything is more or less said in the title. When I start the sentry sdk I have a dsn string that I have to write in my code to initialize it. I was wondering if I can hardcode this string or if I have to store it somewhere else. I wonder if it is dangerous or not. Thanks in advance.
Example of initialization: Sentry.init("https://examplePublicKey#o0.ingest.sentry.io/0")
This is a public address. The DSN string only allows events to be sent to a Sentry project. It doesn't give access to retrieve any data from it nor it can be used to execute any operation on your behalf. If for some reason it gets abused (bogus events start coming in), you can easily discard that DSN and create a new one.

ValueError: The 'field_name' attribute has no file associated with it - only when updating?

I use DRF and django-storages to upload files to a S3 bucket.
I can create from the admin or from a POST to the API endpoint any new entry with a file.
But for some reason, as soon as I try to update the instance (be it from the admin or from the API endpoint), I get a ValueError that seems to indicate that DRF can't find the file.
But when I use Django's admin or shell, I see that the file has been correctly saved (FieldFile on instance) and that the file has been saved in the bucket.
I don't override anything: not the admin forms, not the serializer's update method, not the view's retrieve method, etc.
I have no idea how that could even be possible.
Any suggestion?
I ended up finding the solution. Somewhere in my code one of the serializer's fields had a to_representation() method that was returning the instance instead of the instance.url. I hope that this will help anyone.

C# - Services how to set the start parameters

I am currently working on a windows service (which starts so that is a good thing).
The big question is how can I get parameters in the start parameter field (without doing it manual ofcourse).
So what I would like to see is the following. Upon installation of the service I would like it if the following happens.
The services gets installed and the start parameters are set.
How would one do such a thing (already been browsing StackOverflow but it doesn't comply with what I want)
The reason I ask the question is the following: The service is part of a communication layer between GUI and a receiving backend. If the backend location differs (e.g. another IP address) the service needs to have the new address accordingly.
If you would like to have some more info please ask (don't down the post if something is not in order 'just ask :)')
Thanks in advance
After the update of your question, I understand what you are trying to accomplish. As far as I currently know, it is not possible to set these start parameters without using the registry. You'll have to do it manually from the services console or by using an installer. When you look at the MSDN page covering ServiceBase.OnStart (MSDN ServiceBase.OnStart method) it clearly states:
Process initialization arguments for the service in the OnStart method, not in the Main method. The arguments in the args parameter array can be set manually in the properties window for the service in the Services console. The arguments entered in the console are not saved; they are passed to the service on a one-time basis when the service is started from the control panel. Arguments that must be present when the service is automatically started can be placed in the ImagePath string value for the service's registry key (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\). You can obtain the arguments from the registry using the GetCommandLineArgs method, for example: string[] imagePathArgs = Environment.GetCommandLineArgs();
Thing is that you will still have to keep track of these registry settings when removing the service.
Therefore the link provided here ("Am I running as a service") might help out as well.
use sc.exe:
c:\>sc config <myservice> binPath= "\path\to\myservice.exe -param -param"
In your OnStart() or your service thread use something like:
string myArg = ConfigurationManager.AppSettings["MyArg"]
where in your App.Config you've added
<appSettings>
<!-- My keys -->
<add key="MyArg" value="xxx"/>
</appSettings>
Start a Windows Service passing arguments by using the System.ServiceProcess ServiceController Start(String[]) method like this:
ServiceController sc = new ServiceController("BDESVC");
sc.Start(new string[] { "argValue" });

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

Using Session Vars in a MVC Domain Model library

I have a IQueryable function. In that function, I need to store and retrieve data to Session; can you guys point me in the right direction.
I've looked at the HttpSessionStatBase where the session is usually taken from HttpContext.Current but this doesnt seem possible to do in the library. Am I missing something?
Thanks in advance.
I would avoid having a dependency on the static HttpContext. My preferred strategy would be to extract the information from the session in the controller and pass it as parameters (or set as properties) on your data access layer/repository. If you feel that you must use the Session directly, then I would provide it to the DAL/repository in the same manner -- as a property or as a parameter. Note, however, that you are increasing the coupling between your DAL/repository and the controller. This will make it much more difficult to re-use in a non-web setting, i.e., you'd have to create a fake session just to interact with the DAL/repository if you ever needed to work with it from a windows service or console app, for example.

Resources