WebServiceTemplate.sendSourceAndReceive specify action - spring-ws

I am calling a webservice using WebServiceTemplate.sendSourceAndReceive.
As argument, I am passing a the soap xml as source that actually contains soap header and body.
But the server complains that, invalid action name.
So how to specify action name in the WebServiceTemplate.sendSourceAndReceive method?
Thanks in advance.

You should use one of those sendSourceAndReceive which applies a WebServiceMessageCallback arg. And use SoapActionCallback:
Result result = new DOMResult();
template.sendSourceAndReceiveToResult(
new StringSource("<content xmlns=\"http://tempuri.org\"/>"),
new SoapActionCallback("http://tempuri.org/SOAPAction"),
result);

Related

Zend Apigility : DELETE HTTP method Validation

I have recently explored Apigility I want to use HTTP DELETE method to delete some entity but before deleting I need to validate "entityId" must be given and must be Digit and trim.
Problem is documentation mentions that:
Content Validation currently only works for POST, PATCH, and PUT requests. If you need to validate query string parameters, you will need to write your own logic for those tasks.
https://apigility.org/documentation/content-validation/intro
I have make some custome modification in config file as bellow:
'NetworkingNightAPI\\V1\\Rpc\\DeleteSlotByLoginUser\\Controller' => [
'DELETE' => 'NetworkingNightAPI\\V1\\Rpc\\AssignTimeSlotToLoginUser\\Validator',
],
As I have mention DELETE method to validate same as NetworkingNightAPI\V1\Rpc\AssignTimeSlotToLoginUser\Validator but the issue is it always return 'Value could not be empty' even I have added valid row JSON values using PostMan
Thanks!
Thank you for your reply
What I have found is Apigility uses 'zf-content-validation' module for validating the input data (https://github.com/zfcampus/zf-content-validation)
This module dose not restrict such HTTP Methods you can apply validation to DELETE method as well Like it says that
"In the above example, the Application\Controller\HelloWorld\Validator service will be selected for PATCH, PUT, or DELETE requests, while the Application\Controller\HelloWorld\CreationValidatorwill be selected for POST requests."
So you just need to add manual entry for DELETE method in config file as below:
'NetworkingNightAPI\\V1\\Rpc\\DeleteSlotByLoginUser\\Controller' => [
'input_filter' => 'NetworkingNightAPI\\V1\\Rpc\\DeleteSlotByLoginUser\\Validator',
'DELETE' => 'NetworkingNightAPI\\V1\\Rpc\\DeleteSlotByLoginUser\\Validator',
],
In addition HTTP DELETE method will not validate using JSON row body from POSTMAN you have to pass query parameters and in your controller you need to get validated data using plugin like below:
$recruiterId = $this->getInputFilter()->getValues()['recruiterId'];
$timeSlotId = $this->getInputFilter()->getValues()['timeSlotId'];
If you want to delete a resource your should use the url that includes the route to that entity. This means the id would be in your route parameters, not in your query parameters. So the id is a route parameter/identifier and the RestController will search your entity using the identifier in the fetch($id) method of your resource listener. The listener should return a not found (404) response in case the entity with that identifier doesn't exist.
The content validation you mention in your question is for validating POST/GET parameters. So there is no need for such validator in case of a delete request.
So say for example you want to delete a Slot you would have a route:
api/v1/slots/[slot_id]
And if you want to delete Slot with id 1 you would send a delete request to:
DELETE
api/v1/slots/1
Your listener should simply return a 404 response in case a Slot with slot_id 1 doesn't exist.
I see you're using RPC Rather than Rest style - if you're passing the parameter using the query string you will have to validate it yourself inside the controller, for example:
public function someActionMethod()
{
$id = $this->getRequest()->getQuery('id');
$validator = new Input('id');
$validator->getValidatorChain()
->attach(new \Zend\Validator\NotEmpty())
;
$validator->getFilterChain()
->attach(new StringToUpper())
;
$inputFilter = new InputFilter();
$inputFilter
->add($validator)
->setData($this->getRequest()->getQuery())
;
if( ! $inputFilter->isValid()) {
return new \ZF\ApiProblem\ApiProblemResponse(
new ApiProblem(400, $inputFilter)
);
}
}
Apigility won't use any of the config generated using the UI to validate those fields for you wuen passed via query string as it says in the docs - they will be ignored. You would need to generate the valaidator yourself.
You could set it up to generate the validation using a config if you wished and then load the validator inside the controller to save writing boiler plate code as above.

Calling a web api method with custom object

Usually I send my post request with a custom parameter and a custom return and object using
HttpClientExtension.PostAsJsonAsync<T>
This allows my to call a post method with a custom object.
Now, I want to be able to send my custom object as a parameter and return value to a GET Method.
Lets say my method signature is
[HttpGet]
public MyMethodResponse MyMethod(MyMethodRequest request)
How can I send a request when I have an instance of MyMethodRequest ?
Thanks.
You need to encode MyMethodRequest onto the query string. You can either encode it as separate query string parameters or as a single one. You have handle the encoding yourself on the client side, remembering to URI-encode the parameters. Decoding is done using a custom ModelBinder or TypeConverter respectively. This article shows examples of binding a complex object on the query string.

How to bind JSON to model within a method?

I understand that MVC4 can automatically bind json to type models.
For example, take an HTMLItem model.
In part of a method I retrieve the HTMLItem model data in json format from an external site using HttpRequest and StreamReader. I grab this as a string and then want to pass it into another method that takes HTMLItem as a parameter.
How do I ensure that the receiving method handles this as the type I require (HTMLItem)? It currently doesn't recognise it as such.
I tried assigning the string to the model in the originating method, but the IDE gives me the red squiggly for assigning a string to another type.
I don't want to have to go through the json string and assign each field manually if possible.
Any help, as always, much appreciated. Thanks.
You could deserialize the JSON to your model like:
using System.Web.Script.Serialization;
...
JavaScriptSerializer serializer = new JavaScriptSerializer();
YourModelType model = serializer.Deserialize<YourModelType>(yourJSON);

JSON string from client to Struts 2 Action. How to handle just using struts.xml configuration

I ve a question. I have a JSON string ready to send to an Struts2 action from a ajax javascript function. Action is called properly, but i dont know how to get the JSON parameter from its method.
Is any struts.xml action configuration that makes me able to put automatically the information in the object, just as ..attributeClass?
In a similar way that i send from the server to the jqGrid (a javascript object to make grids with data inside, just with struts configuration result type=json and the attributes of the object that i want to send as a JSON string to the client web)?
Or maybe the only way is forget the struts.xml configuration and "hard programming" HttpServletRequest parameter, that has JSON string?
Thank you!
Both the JSON plugin and REST plugin can do what you want. Which makes more sense to use depends on the application.
You could do the work manually, either by accessing the request directly (not recommended), or by manually parsing a string parameter.

Can I have an MVC Action as a ServiceMethod for an AutoCompleteExtender?

Assuming Tools is my Controller, I specify '/Tools' in the ServicePath, and the action name 'GetToolsList' in the ServiceMethod. Can I fire an AutoCompleteExtender this way? How would I be able to pass the prefixText to the Action here? Or is it not possible?
Did it. Created an action that returned void, and JSON-serialized the output and wrote it into the response object so that it's read and shown in an AutoCompleteExtender.

Resources