Retrieve HTTP response code in WSO2 ESB/API Manager - wso2

I'm using WSO2 API Manager and I want access to details of the client's incoming API request (into API Manager, for example, the HTTP method) as well the response from my API endpoint. I've followed the approach in the following document to write a custom mediator class which gets invokes on both the "In" (for the request) and "Out" (for the response) flows:
https://docs.wso2.org/display/AM160/Adding+a+Mediation+Extension
It seems I can get various bits of data that I need from the MessageContext that is passed into my mediator, but I'm struggling with getting the response code from my API endpoint. Is there a way to get access to the HTTP response itself (and all it's headers and other elements) from the MessageContext? I stumbled across the PassThroughTransportUtils class which has a determineHttpStatusCode method which I could call but I'm not sure this is the best way of doing it.

"HTTP_SC" property is stored in Axis2MessageContext, which can be accessed as below:
org.apache.axis2.context.MessageContext msgContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
String httpStatusCode = (String) msgContext.getProperty(NhttpConstants.HTTP_SC);
can be null, if not set.

Related

AWS Lambda function - accessing Path and request parameter value

For my POC, created simple lambda function , which will give emp information through rest api.
Created lambda function and access all the emp data using API gateway.
Facing some challenges while accessing particular data.
i am looking for
emp/1 - to retrieve emp id
emp/_search?name="apple" - search name contains apple.
Question is how to retrieve path and request parameters in java code.
public class TestAwsLambdaFunction implements RequestHandler<Map<String, Object>, String> {
#Override
public String handleRequest(Map<String, Object> input, Context context) {
String empID= null;
try {
#SuppressWarnings("unchecked")
Map<String, String> pathParameters = (Map<String, String>) input.get("querystring");
empID= pathParameters.get("id");
System.out.println(empID);
// TO-Do Business logic -
} catch (Exception e) {
// TODO: handle exception
}
return "Hello from Lambda!" + empID;
}
}
What is the best way to expose my data in Rest api call. Bit confused with Lambda or serverless .
have any option to show the data via page wise. Since i am new to AWS. Please guide me
Question is how to retrieve path and request parameters in java code.
You can use mapping template to send $input.params('name') property in the request body to your Lambda function.
What is the best way to expose my data in Rest api call
Use the proxy integration with these guidelines:
Avoid greedy path variables, except perhaps for a catch-all 404.
Avoid using the ANY method.
Define request models and enable request validation (remember it’s off by default).
In your Lambda, check that the content-type header matches one of your request models, and return a 415 Unsupported Media Type status code if it doesn’t (the proxy integration uses the WHEN_NO_MATCH passthrough behavior). After this check, your Lambda can assume the request validation is fully enforced.
By Ben Kehoe
https://read.acloud.guru/how-you-should-and-should-not-use-the-api-gateway-proxy-integration-f9e35479b993
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-java
https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-call-api.html
https://github.com/vaquarkhan/Serverless-AWS-Lambda-guide/blob/master/01-aws-lambda-serverless-framework/02-building-rest-api-in-nodejs-with-lambda-gateway.md
https://www.puresec.io/blog/aws-security-best-practices-for-api-gateway
https://www.stackery.io/blog/serverless-function-architecture-principles/
https://technology.finra.org/code/enjoying-auto-scaling-integrated-authentication-low-host-cost.html
You need to choose Lambda Proxy Integrations when you set up your API Gateway. Here's official document Set up Lambda Proxy Integrations in API Gateway.
In this case, API Gateway will pass the whole request data to Lambda, including the request headers, query string parameters, URL path variables and so on. Then you can parse the data using your Java code.

How to send json payload as HTTP OPTIONS response body?

The HTTP OPTIONS method is supposedly used to determine what other methods the server supports on a given resource. Given that, I have questions:
I have a GET API having certain parameters and I want to provide some information about the parameters (ex:their usage,type) in the preflight request.
Suppose, I have the following endpoint:
/v1/pets/?limit=10&page=1&birth_year=2013
and I want to provide the usage information (format,type,etc) about the parameters limit,page and birth_year in the OPTIONS response body.
Questions
Is it possible to send a json payload in response to OPTIONS request?
How can that be achieved using AWS API Gateway?

How to achieve the dynamic integration pattern in WSO2 ESB

My requirement is I will expose the ESB integration implementation as a REST API.
1) In the implementation part whenever they will call This ESB API it has to route based on content in the post request sent to ESB(this can be achieved by content based routing pattern ).
2) But on the Endpoint side Which is to be called by the ESB while routing based on content may vary means, if today there is 2 end point to call tomorrow it may rise to 5 like that how to apply changes are is there any solution. So, how to achieve this dynamic integration.
I have 4 ways to achieve this integration pattern.
1) Deploy files in the registry which has the endpoint in it and having the file name which is same as the content name(regex) sent in the payload for routing. Then by storing that name in property mediator and use concat function to read that file in another property mediator, and by pattern matching read the endpoint. Thus you can use that endpoint and achieve the dynamic content based routing.
2) Have separate sequence each one to call one endpoint and have the same name as the content name. The main sequence will call the sequence based on the content.
3)Store the content on which to be routed and endpoint in the RDBMS database then use DB lookup mediator to retrieve the endpoint based on this you can route.
4) I think this is best and suitable for integration scenario where tomorrow the endpoint may change( that is when the number of the endpoint is not fixed ) and each endpoint wants a different transformation of the payload(I mean XML to json or JSON to XML etc..).
First, use one API and store the content in property mediator and use that mediator to call another API which has the implementation to call the endpoint.
For example, if the content based on which routing has to happen has the payload like this
{ "content":"c1" }
Store the content in property mediator using name uri.var.address. Then create another API's which will have the implementation to call endpoint(For each endpoint create separate API's) and URI-Template of the API's should have content stored in the property mediator(same as payload request). While using send mediator to call these ENDPOINT implemented API's use http request having url of the API with /{uri.var.address} because to match the URI-template details.

SpringBoot SOAP webservice Dynamic WSDL generation not working If remove Request suffix from RequestPayload element

I am creating SOAP web service using Spring Boot SOAP Webservice Sample project. If I use following code dynamically generated WSDL shows Operations.
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "AvailNotifRequest")
#ResponsePayload
public OTAHotelAvailNotifRS getAvailNotif(#RequestPayload AvailNotifRequest request) {
But I need request element to change like this.
#PayloadRoot(namespace = NAMESPACE_URI, localPart = "OTAHotelAvailNotifRQ")
#ResponsePayload
public OTAHotelAvailNotifRS getOTAHotelAvailNotifRQ(#RequestPayload OTAHotelAvailNotifRQ request) {
I found a similar question on this link Spring web service dynamic wsdl not generating message for a schema element answer says we need to add suffix Request after request element like AvailNotifRequest but I want to use OTAHotelAvailNotifRQ as my request input.
How can I use this because I am not getting operations in wsdl when I change request input like this.
According to official Spring-WS documentation:
The <dynamic-wsdl> builds a WSDL from a XSD schema by using conventions. It iterates over all element elements found in the schema, and creates a message for all elements. Next, it creates WSDL operation for all messages that end with the defined request or response suffix. The default request suffix is Request; the default response suffix is Response, though these can be changed by setting the requestSuffix and responseSuffix attributes on <dynamic-wsdl />, respectively.
In other words you can use the setRequestSuffix and setResponseSuffix on DefaultWsdl11Definition in order to specify a request and response suffix different from the default one. In the above case that could for example be:
wsdl11Definition.setRequestSuffix("RQ");
wsdl11Definition.setResponseSuffix("RS");

Passing #FormParam values to a rest service without using an HTML form

We have an existing rest web service that does a certain online transaction. It was created to receive input of #FormParam type. When we call this web service, we initially just passed the values by appending it to the url
e.g.
/sometransaction?creditCardNumber=123
Problem is, since the number is appended to the url, this gets logged in the web server http requests logs. This cant be since this is sensitive information. We need to pass this the same way a HTML form does a POST submit, it order for the parameters not to be appended to the url and get logged by the web server. Problem is, we don't have a UI page to do this. This is just basically a web service calling another web service.
How can we achieve this?
Code:
#POST
#Path("/dotransaction")
Public Response doTransaction(#BeanParam TxnParams) {
}
Its a rest web service the the params class TxnParams have #FormParam attributes
Ensure the Content-Type is set to application/x-www-form-urlencoded and send the data in the request payload.
Use & to separate the parameters and use = to associate the parameter with its value.
That's what the request will be like:
POST /sometransaction HTTP/1.1
Host: example.org
Content-Type: application/x-www-form-urlencoded
creditCardNumber=4111111111111111&expirationDate=09-2016
And always use HTTPS when sending sensitive information over the wire.