Send http request with encoded URI - citrus-framework

I m trying to send http request as follows, using citrus test framework http://www.citrusframework.org/
http().client(ENV).post("/xx/v1/ouxxtbound/tel%3A%2B94xxxxxxx")
.header("Authorization", authorization)**strong text**
.header("Accept", "application/json")
.payload(send)
.contentType("application/json");
its is passing a url encoded values, but when it encode again when sending request by Citrus.as tel%253A%252B94xxxxxxx
Is there are way to send encoded URI correctly?

Citrus uses the following method on the Spring RestTemplate
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
...
}
The url is given as String value and Spring will automatically encode this value. When passing in some already encoded String the encoding is done twice. When using non-encoded String value the Spring RestTemplate applies uriVariables logic which is also causing errors.
Citrus should use some other method signature on the RestTemplate that uses the URL object instead of String value. As a temporary workaround you can use a custom RestTemplate subclass that overwrites the methods like this and automatically creates the URL object from String:
#Override
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
return super.exchange(URI.create(url), method, requestEntity, responseType);
}
You can add the custom RestTemplate subclass as Spring bean into the configuration and reference the bean on the Citrus client component by using the attribute rest-template.

Related

Liferay 7.2 Api Rest Session parameter

I have Liferay 7.2.
I created a module rest.
It is possibile to save a variable in session ad use it in other method?
My idea of code is:
#GET
#Path("/save/paramt")
public String jsonSaveParamRequest() throws Exception {
String json = "xx";
//SAVE IN SESSION json
return "ok";
}
#GET
#Path("/get/paramt")
public String jsonGetParamRequest() throws Exception {
String xx= SESSION VARIABLE json
return xx;
}
Saving state in a HTTP session in a REST API is not recommended and should be avoided. Please refer to the discussions here SO:how-to-understand-restful-api-is-stateless.
Technically, I guess this is possible as you can inject the HttpServletRequest as a method param via #Context HttpServletRequest request to your annotated method and use getSession(). But I am not sure if you can rely on that.

Wiremock: choose body file name based on request parameter

I am using wiremock to mock http server and I am returning responses from json files (using withBodyFile method).
Now I want to choose and return response json file based on request parameter. For the example below, I want to define one stub so that the body file is chosen based on request parameter.
myMockServer.stubFor(
get(urlEqualTo(myEndPoint+ "?key=key1"))
.willReturn(aResponse().withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("response_key1.json")
myMockServer.stubFor(
get(urlEqualTo(myEndPoint+ "?key=key2"))
.willReturn(aResponse().withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("response_key2.json")
myMockServer.stubFor(
get(urlEqualTo(myEndPoint+ "?key=key3"))
.willReturn(aResponse().withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("response_key3.json")
Any idea how this would be possible? I tried defining transformer but it was not possible to get Stream Source Path from Response object in overridden method so can't use that appraoch. Thanks a lot..
Body File name can't be parameterized in wiremock as of now. I had a similar requirement, I needed to return the file based on the request URL. So I implemented a transformer as below:
public class BodyFileNameResponseTransformer extends ResponseDefinitionTransformer {
public ResponseDefinition transform(Request request, ResponseDefinition rd,
FileSource fileSource, Parameters parameters) {
if (rd.getBodyFileName().startsWith("{{")) {
return new ResponseDefinitionBuilder().**withBodyFile**(request.getUrl().substring(1))
.withStatus(rd.getStatus())
.withHeaders(rd.getHeaders())
.withTransformers(
rd.getTransformers().toArray(new String[rd.getTransformers().size()]))
.build();
}
return rd;
}
public String getName() {
return "BodyFileNameTransformer";
}
}
you can use request.queryParameter(key) instead of request.getUrl() and form any
file path. Create the file path based on your need and set it as bodyFile on returned ResponseDefinition.
Don't forget to start wiremock with --extensions: Extension class names
More details at Extending Wiremock
This is possible by using the inbuilt template helpers provided by Handlebar.
myMockServer.stubFor(
get(urlEqualTo(myEndPoint+ "?key=key3"))
.willReturn(aResponse().withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("response_{{request.query.key}}.json")
Check for the various models available at http://wiremock.org/docs/response-templating/.

Array Multipart[] file upload using Feign client

I am trying to upload Array of Multipart file object using feign client.
This is the service am trying to call using Feign client.
public ResponseEntity<Object> manageFileUpload(#RequestParam("files") MultipartFile[] files)
I tried using,Feign client Annotation,
#FeignClient(value = "UPLOADUTILITIES", configuration = Upload.MultipartSupportConfig.class, fallback = UploadFallback.class)
My Method,
#RequestMapping(name = "upload", value = "/object", method = RequestMethod.POST)
#Headers("Content-Type: multipart/form-data")
ResponseEntity<Object> manageFileUpload(#Param("files") MultipartFile[] files);
I was rewarded by the error,
"message": "Type definition error: [simple type, class java.io.FileDescriptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile[0]->org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile[\"inputStream\"]->java.io.FileInputStream[\"fd\"])",
Then by referring this link.I tried in my client side, the blow code.
public class MultipartSupportConfig {
#Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
#Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
Then by the code example, i changed my MultiPart File object into File Object.Now my request got fired, but i got Not a multipart request.
I tried this https://github.com/pcan/feign-client-test#feign-client-test,
I created a class and used the encoder class, and changed my encoder as FeignSpringFormEncoder,
Still I am getting No serializer found error.
Could anyone share a simple client, server example with Array of Multipart file request, using feign cleint. Thanks!

Spring RestTemplate Row Mapper

Java - spring RestTemplate
using following code to get rest response
PropsNotMatchClass notMatchClass;
ResponseEntity<PerfectPropsMatchClass> resp= restTemplate.getForEntity(url, PerfectPropsMatchClass.class)
PerfectPropsMatchClass props = resp.getBody();
i had to create PerfectPropsMatchClass class b/c my PropsNotMatchClass didn't had exact names as in returned response. is there a efficient way i can iterate on each row in response and create objects of PropsNotMatchClass.
You can use ResponseExtractor.
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/ResponseExtractor.html
It is used in RestTemplate as such.
<T> T execute(String url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor<T> responseExtractor, Map<String,?> uriVariables)

Async REST calls with JAX-RS

I need to create a RESTful service which should support async calls in follwing way. When user calls some method he got the http '202' code and url to poll where he can see the status of his request. Currently I use JAX-RS and its annoations:
#Path("")
public interface MyService {
#POST
#Path("/myService/{name}")
#Consumes({APPLICATION_XML, APPLICATION_JSON})
void postSomething(#PathParam("name") String name, MyObject data);
}
Such mapping would expose MyService's postSomething() method by url /myService/{name} which serves POST requests, get 'name' parameter from url and 'data' from request body.
I want that after making this PUT request client get 202 http code and some callback url to poll to get the result once method will be executed.
So the question is:
1. How to make JAX-RS return 202 code?
2. How to pass callback url to the client?
Have the postSomething method return a Response object:
public Response postSomething(#PathParam("name") String name, MyObject data) {
return Response.status(Status.ACCEPTED).build();
}
If you want the callback URI as plain-text in the HTTP Body you could do something like this:
public Response postSomething(#PathParam("name") String name, MyObject data) {
return Response.status(Status.ACCEPTED).entity("http://google.com").build();
}
For generating URIs by resource classes, have a look at UriBuilder
Use #Context HttpServletResponse servletResponse to get direct control over the servlet's response mechanism.
#PUT
#Path("/myService/{name}")
#Consumes({APPLICATION_XML, APPLICATION_JSON})
void postSomething(#PathParam("name") String name, #Context HttpServletResponse response, MyObject data) {
// ...
response.setStatus(HttpServletResponse.SC_ACCEPTED);
response.setHeader("Location", myURL);
// ...
}