spring ws dynamic wsdl - no operations, cant change request response element names, what to do? - spring

Problem is, spring ws does not generate operations and others cant generate code over that wsdl. I have it configured as follows:
<sws:dynamic-wsdl id="executeFunnyQuery"
portTypeName="XYExecuteFunnyQuery"
locationUri="/ws/"
targetNamespace="http://www.namespace.eu/sch/xsd/v1/fnmessages"
requestSuffix="XYExecuteFunnyQuery"
responseSuffix="ZYExecuteFunnyQuery"
>
<sws:xsd location="/XYMessagesDictionary/XYExecuteFunnyQuery.xsd"/>
<sws:xsd location="/ZYMessagesDictionary/ZYExecuteFunnyQuery.xsd"/>
I understand everything would be fine if i could change element names. But i cant, they are given to me that way and i cant modify them. Response and request element names only differ by their prefix - the first letter, as XYExecuteFunnyQuery is the request and ZYExecuteFunnyQuery is the response. I tried to put full element names to request and response suffix places...does not seem to be doing the trick, sadly.
Can i some howtell without suffix-prefix mambo-jambo to spring that here is my request and here is the response, now generate please! :)

Related

Spring Boot 2.2.5. How to retrieve a PathVariable parameter inside a Spring Filter

The problem I want to solve.
I need to apply a specific logic to all restful endpoints where the url belongs to a specific sub path: let's say "/api/employee/{id}". This means all the links which start with this path should apply a logic based on the employee ID, which I am trying to apply directly in Spring Boot filter in order to avoid to spread the logic everywhere.
The problem I face.
I am able to get the query parameters from the ServletRequest, but the PathVariables are not available in the Filter.
Any idea how this could be parsed?
Would be much appreciated :)
The PathVariables are simply the URI. You cann call getRequestURI()
From the docs:
java.lang.String getRequestURI()
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:
First line of HTTP request Returned Value
POST /some/path.html HTTP/1.1 /some/path.html
GET http://foo.bar/a.html HTTP/1.0 /a.html
HEAD /xyz?a=b HTTP/1.1 /xyz
https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getRequestURI--

Response Assertion / Regex Extractor Not Working in JMeter

I'm trying to extract the CSRF token so I can log in, and be able to obtain cookies, but I'm not able to.
I'm able to get a 200 response code when accessing the URL that contains the CSRF token, and I'm able to see it on the browser and the console, but my response assertion is not able to assert anything regardless of me changing the apply to, field to test, and pattern matching rules sections. My regular expression extractor isn't able to get anything either. All the headers to get to the URL are there. Any suggestions?
Forgot to mention, I'm able to get it on one server that's exactly (or should be) exactly the same as this one...
EDIT:
I placed it under the HTTP Sampler that has that response, and here is an example of what I get for my response assertion. I've also added various images.
Unfortunately you didn't share your output, so I cannot tell for sure, but although it seems your RegEx is correct in both cases, it could be that it doesn't match due to some extra spacing.
It appears that you are expecting a valid JSON, so instead of RegEx you could use JSON Extractor and/or JSON Assertion, for which extra spacing will not matter.
Example: if Response Data is
{"token":"12345"}
I can specify JSON Extractor as
(most important line is JSON Path: $.token)
and the result will be variable token with value 12345.
Here's a good online JSON Path tester, which can help you to figure out the right JSON Path.
If your goal is to check presence of a JSON Object with name of token and an arbitrary value I would recommend going for JSON Assertion instead.
Add JSON Assertion as a child of the request you would like to assert.
Use the following JSON Path query:
$.token
JSON Assertion is available since JMeter 4.0
If you still want to go for the Response Assertion - configure it as follows:
Pattern Matching Rules: Contains
Patterns to Test: {"token":"(.+?)"}

How to validate request against XSD and return an error object?

My task is to implement a webservice that:
consumes an XML file on a POST endpoint
in happy flow, it returns a DTO as JSON + HTTP 2xx
the incoming XML file is validated against a XSD; if the validation fails, a JSON with a list of all validation errors is returned (including the line, column, error) with HTTP Bad request
the application exposes two endpoints, only one of them should be validated
I have started the implementation with Spring Boot + web, using regular #PostMapping which has "consumes" and "produces" set to application/xml and application/json, respectively. The usual flow works perfectly fine. Now, I stumbled upon the issue of validating the incoming payload. What I figured out:
1) I have to validate the payload before it is converted (marshalled) to an object.
2) Once validated, I have to either:
allow further processing
stop any further processing, write the error object to the response and set the status code to 400 Bad request
My approaches were:
1) using a RequestBodyAdvice, more specifically the beforeBodyRead method implementation. I had the following issue here: I don't know how to write anything to the output in case the validation fails.
2) using a Filter (I've extended OncePerRequestFilter) - fortunately, I can read the request (request.getInputStream()) and write to the response (response.getOutputStream()).
However, how can I do the selective filtering (as mentioned, I only want to validate one single endpoint)?
Are there any other alternatives for placing the incoming request XSD validation? Is spring-web the appropriate choice here? Would you recommend some other library / framework?
To validate xml against xsd schema, my preference is XML Beans. It is very easy to use. Other options are JABX, Castor. Take a look at Java to XML conversions?.
You will need to jar using xsd schmema and will need to put it in the classpath of your application so that it's classes are available for you for validation. Please take a look at this blog.
You can use validation API as mentioned here.
I would prefer to write validation code in the aspect so that it can be reused with other APIs.
If validation fails, throw valid exception from the aspect itself.
If validation is passed, process your input string that you receive.
Please let us know if you need any more information.

RESTful URLs: "Impractical" Requests, and Requiring One of Two Request Parameters

I have a RESTful URL that requires either the offset or the prefix request parameter (but not both).
GET /users?offset=0&count=20
GET /users?prefix=J&count=20
What's the best way to enforce this rule? Spring has the #RequestParam annotation with the 'required' property for optional parameters, but I want to enforce an "either-or" rule on these two parameters. I know I could do it in the code, but is there another way to do it?
Also, what's the proper way to handle "impractical" requests? Say I have 100 million users; the following request, although properly RESTful, is not something I want to support:
GET /users <-- Gets all 100 million users, crashes server and browser!
What should I send back?
You can create two methods and choose one of them with #RequestMapping's params attribute:
#RequestMapping(..., params = {"prefix", "!offset"})
public String usersWithPrefix(#RequestParam("prefix") ...) { ... }
#RequestMapping(..., params = {"offset", "!prefix"})
public String usersWithOffset(#RequestParam("offset") ...) { ... }
what's the proper way to handle "impractical" requests?
The lesser-practiced principles of REST include the requirement that resources be "discoverable". If you are asked for a complete list of 800 million users and you don't want to provide it, you might instead consider serving a page that describes in some way how to filter the collection: for example, an XForms document or HTML containing a FORM element with fields for offset/prefix/count, or a URI template with the appropriate parameters
Or you could just send a "413 Entity too large" error - edit: no you can't. Sorry, I misread the description of whath this code is for
If you decide to go down the route of just sending the first page, I think I would send it as an HTTP redirect to /users?offset=0&count=20 so that the client has a better idea they've not got the full collection (and if your response contains a link to access subsequent pages, even better)

What's the best way to do XMLObject Validation

I'm doing Sprimg WS at my workplace. We run into some strange validation problems, where if a user passes "Dog" for a boolean value. It still accepts it and blows up. I would like to know what's the best way to handle this kind of problem.
Requirement:
Based on the SOAP request, If there are any validation errors, return the set of customized errors back to the user.
Technology used,
XMLObject for XML to Object translation.
Current way to validate (Which I feel can be improved)
Checking if the element is Nil and is Set for each and every element in the XML.
What I tried?
I tried to use XMLObject Validate method, Which I suppose just returns one error at a time.
Which is not feasible for us. I want to send the list of errors which the request forgot to comply with the XML Schema.
Please suggest me some ways to proceed with this , which could be efficient.
You should validate against your XSD schema(s) in your WSDL.
I have written a tutorial with server validation here and a tutorial with client validation here that hopefully gives you some suggestions!

Resources