Designing HTTP Bulk Request - spring-mvc

I am designing a REST Bulk Request that will take following array of following element:
{
"method": <POST | GET | PATCH>,
"path": <Relative URL of API to execute>
}
All these Bulk elements would execute the API on the same server.
Is there a way I can call dispatcher servlet to execute this internally rather than calling methods backed by API ? I also want to read response for every request operation in Bulk request and accumulate response that will returned.
Currently I am calling methods directly with if elses doing request mapping style of work.
Please let me know if I am missing any details on expressing my problem definition. Request redirect/forward wont work in my case as I need to be in control to execute every operation in Bulk request and collect and accumulate response.
I am using Spring Boot MVC
Anand

You can consider implementing "dummy" HttpServletRequest and HttpServletResponse.
Autowire the DispatcherServlet in your bulk request controller.
In your controller method which handle the bulk request, while looping through the individual request, instantiate your own implementation of HttpServletRequest and HttpServletResponse and call the
dispatcherServlet.service(request, response);
You can read the response status via response.getStatus() and content via response.getOutputStream() or response.getWriter(). See java-printwriter-vs-servletoutputstream/.
By doing so, you don't have to manually call your controller method manually.
That's how I implemented the bulk requests.

Related

Grpc Writing Interceptors for Specific Endpoint

I implemented a grpc server in Golang. This server has multiple endpoints, but for one of the endpoints I want to implement an interceptor that will check the validity of an authentication token before proceeding with the request. I know how to implement an interceptor that will run when a request reaches any of the grpc endpoints, but how can I make so that my interceptor only runs for one specific endpoint?
For those interested, I was able to find the method of the request by inspecting the grpc.UnaryServerInfo param of the interceptor. There is an attribute called FullMethod that gives you the the endpoint of the request.
You can get the method name and compare when you are getting a request through your interceptor. Your custom interceptor will have one component called ServerInfo which will help you to filter from which method you are getting called. Based on that, you can filter out your authentication endpoint

How to get Http header values in Apache Camel- Jersey Rest API

I have an application which uses Apache Camel to build an API. It basically uses blueprint.xml to define routes and processing is done by a bean(please note its not any processor bean. Just a plain Java bean). It uses Jersey client to invoke the backend system Rest API.
My requirement is to get the http headers in the code to be able to send them to our custom logging system.
a) I tried #httpHeaders annotation but this does not inject the headers on my code.
b) Since its not using any BeanProcessor i dont have an Exchange object from where i can get the header values.
Please help with a way to get header values on the code.
Add the request context to your class
#Context
private HttpServletRequest request;
and get the headers in your endpoint using request.getHeader
Returns the value of the specified request header as a String.

(REST ) Client - set the javax.servlet.http.HttpServletRequest into request How to?

A lot of web applications must be used the specified REST service. This REST service uses currently the informations only from javax.servlet.http.HttpServletRequest. This HttpServletRequest may be contain the specific cookie and other parameters.
I'm try to use the (REST) client API form JERSEY to realize a client, that will build the REST requests. This client will be called within custom web filter, that will be registered by web.xml of each web application.
Currently i have the following problem: i'm don't know, howto transmitt the HttpServletRequest during call of the REST Service....
Thx for your help....
So if I understand correctly, you want receive a request in any of the web applications, and then you want to forward this request to the REST web service?
If so, you will need to serialize the request to make it transportable. You could also create a class that is able to hold the data that you need from the javax.servlet.http.HttpServletRequest and then serialize it into XML or JSON.
If you have the request in a more transportable format, you can pass it to your REST service via a #HeaderParam or as the request body (I would prefer the latter).

Get controller method caller (3rd party webservice)

Is it possible to retrieve the URL of a 3rd party webservice calling my controller method?
Things like Request.Current.Url refer to my own URL (/someController/someAction/).
The 3rd party webservice is sending HTTP POST data to my /someController/someAction and I wish to send back a HTTP POST message to the caller of the method, the 3rd party webservice, without using
return Content["some response"]
which will force me to exit the method. Since the answer is plain text I would like to send it using HTTP Post.
What I actually try to do is respond to the calling webservice without exiting my method (return Content() will exit) so I can call other methods to process the data send to me by the webservice. I want to first tell the webservice I received their stuff and than process, in this way when a processing error occurs the webservice at least will not resend old data. Is there another way to do this than building your own HTTP post?
You can rely on Request.UrlReferer, but your idea seems not that good. The best solution would be propably to start new thread for data processing and stick to return Content.

Access Response Headers from ASP.Net PageMethod Call

When using ASP.Net Ajax to call PageMethods, how can I access the Http response headers from the "success" method?
For example:
PageMethods.DoSomething(
function(result){successMethod(result)},
function(error){errorMethod(error)}
);
function successMethod(result){
//------how can I access the Http response headers from here? ------
}
Thanks for any help
In your example, PageMethods.DoSomething should have a return value equal to WebRequest if it's an asp.net web service proxy. This is provided so that you can manipulate the request after you've initiated it (i.e. cancel it etc).
With this class you have an add_completed method which you can use to add a handler for when the web request completes. The signature for the callback is function OnWebRequestCompleted(executor, eventArgs), and the executor parameter in this enables you to get hold of extra response information. For example, you can get hold of the response headers with executor.getAllResponseHeaders(); which should be a map (named collection) of header names and values.
So if you add a handler to the web request's completed event immediately after making the service method call, it should work (there's no web service in the world that can respond faster than two consecutive lines of code!).
The previous hyperlink to WebRequest contains a full example of how wire this up. Notice, however, that this code uses the WebRequest directly.
Asp.Net Ajax Web Service proxy classes use the WebServiceProxy class, and each proxy method ultimately call its invoke method, which returns the WebRequest instance.
A web request has a headers collection
http://msdn.microsoft.com/en-us/library/bb383774.aspx
The webrequestmanager is a static object that you may be able to extract this information from:
http://msdn.microsoft.com/en-us/library/bb397435.aspx
Hopefully, between the two links, it makes sense :-;
I'm not saying recode to use this necessarily, but page methods is a wrapper and as such I think it would access information from a web request, which can be affected from the WebRequestManager...

Resources