ibm mobilefirst - call javascript adapter from java adapter - ibm-mobilefirst

Anyone knows - How to call javascript adapter from java adapter?
I have created a javascript adapter that will invoke a soap service, then my restful service are Java Adapters. How to invoke a method in javascript adapter from the java Adapter.

You can use createJavascriptAdapterRequest on AdaptersAPIto call
a JavaScript adapter procedure from a Java adapter
HttpUriRequest req = adaptersAPI.createJavascriptAdapterRequest(AdapterName, ProcedureName, [parameters]);
org.apache.http.HttpResponse response = adaptersAPI.executeAdapterRequest(req);
JSONObject jsonObj = adaptersAPI.getResponseAsJSON(response);
Reference:
Advanced Adapter Usage and Mashup

Related

JX-RS 2.0 client API with Apache CFX 3.1.8 client implementation Thread Safety concern

I am writing REST client using JX-RS 2.0 client API and have Apache CXF 3.1.8 client as implementation in classpath.
My gradle dependency
compile group: 'org.apache.cxf', name: 'cxf-rt-rs-client', version: '3.1.8'
I plan to client the Client instance with root url only once and use it to multiple create WebTarget at runtime as needed by the application. I want to reuse Client object because https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/Client.html this says it expensive operation to create and destroy client object.
String baseUrl = "http://localhost:9081/rest-service/rest/";
Client client = ClientBuilder.newClient();
client.register(JacksonJsonProvider.class);
client.register(CustomObjectMapperProvider.class);
Use the client object to create WebTarget
WebTarget webTarget = client.target(baseUrl + path);
I understand that under the cover org.apache.cxf.jaxrs.client.WebClient (the CXF's client implementation) is used. http://cxf.apache.org/docs/jax-rs-client-api.html#JAX-RSClientAPI-ThreadSafety link says that both CXF WebClient and ProxyClient are not thread safe by default. As per this link there a threadSafe boolean flag that we can set to true while creating CXFs WebClient or ProxyClient.
In my code I want to use standard JX-RS 2.0 client APIs/interfaces and not CXF specific classes like WebClient or ProxyClient.
Questions -
If I instantiate the standard Client object using code above, is
it thread safe? Is it ok to create it once and use it multiple time
to WebTarget of different resources?
List item If it is not thread safe how to achieve thread safety where I want to reuse client object?

Accessing Soap service using java adapter in mobilefirst

I have written a SOAP WSDL application using mobilefirst by:
1) Using the Discover backend services option
2) Writing my own JavaScript adapter code to follow the documentation provided by IBM.
Now my final R&D is to consume this soap service using a java adapter.
I have gone through many StackOverflow topics and samples but I haven't been able to find anything related. Can anyone explain the steps to invoke a SOAP-based webservice request using java adapter?
The following video blog post details the following about Java adapers: https://www.youtube.com/watch?v=cKM5480-6wI
Creating Java Adapter
Understanding Java Adapter structure
Implementing simple sayHello procedure for HTTP GET method
Implementing several procedures for different HTTP methods
Working with various types of request parameters
Using Java code to access MobileFirst server functionality and Java servlet functionality
Debugging Java Adapters
Communicating with a simple backend using using Apache HTTP Client
Leveraging WSDL files and communicating with a SOAP based webservice
Using WLResourceRequest in client applications to communicate with Java Adapters
Using Postman REST client for communicating with Java Adapters
Create HTTP Adapter
In xml add WebService Host and Port in Domain/Port elements
Assemble soapXMLRequest
Use HTTP Invoke to call server
ex.
var input = {
method : 'post',
returnedContentType : 'xml',
headers : {
SOAPAction : SOAP_FULL_URL
},
path : WEBSERVICE_PATH,
body : {
content : soapXMLRequest,
contentType : 'text/xml; charset=utf-8'
}
};
var response = WL.Server.invokeHttp(input);

MobileFirst SAP Adapter http POST requests

How do we pass header such as "content-type" in a SAP NW Adapter? I have to implement a $batch request from a MobileFirst hybrid app and for this need to set the content-type from the request.
How is that possible? I have searched through adapter documentation and nothing seems to help.
It's not possible to pass any headers to SAP NW adapters. If you're using v7.0, you might be able to make use of the RESTful Adapter Wizard along with Java adapters to pass the headers you're referring to: https://www-01.ibm.com/support/knowledgecenter/SSHS8R_7.0.0/com.ibm.worklight.dev.doc/devref/t_impl_java_adapter_JAXRS.html

How to invoke Java adapter from HTTP adapter?

Is there any API to invoke a Java adapter from a HTTP adapter in IBM MobileFirst Platform Foundation 7.0?
I have tried with WL.Server.invokeHttp(input), but received a 404 response. What should be the value for 'path' in the input?
It is currently not possible to directly call a Java adapter from a JavaScript adapter.
You are encouraged to submit a feature request: https://developer.ibm.com/mobilefirstplatform/help
Alternatives:
Implement a JavaScript adapter that invokes Java code. See the tutorial Using Java in JavaScript adapters
Use another JavaScript adater - set the Java adapter as the backend it connects to and call it from the requesting JavaScript adapter
Rough steps to follow:
You have JavaScript adapter A
This adapter calls a procedure in another JavaScript adapter, adapter B (adapter mashup)
JavaScript adapter B will set in its XML the Java adapter properties as the backend (localhost, MFP server port)
Requests from this adapter should then be sent to /the-server-context-root/adapters/the-java-adapter/*
The Java adapter should have its security disabled, otherwise an access token would need to be forwarded in an header from the client in the request
As you can see the second option is less trivial to implement, and is less supported. I would investigate option 1...

How to invoke Java adapter procedure from JS adapter procedure in MobileFirst Platform 7.0?

Our team is migrating to MobileFirst Platform 7.0 because of Java adapters introduced in this version. And after having a quick look I haven't found how to invoke procedures in Java adapter from procedures in JavaScript adapters. Looks like Java adapter doesn't define procedures and has only REST endpoints. How to access them from JS adapter?
In general the new RESTful JAX-RS (Java-based) adapters in version 7.0 operate independently of the old JavaScript-style adapters. I'm not aware of an in-process API to call them, so you would have to call them like any other RESTful service using WL.Server.invokeHttp (i.e. like how you might typically do this using the HTTP adapter).
However, don't confuse the new RESTful JAX-RS (Java-based) adapters with using custom Java code in older-style JavaScript adapters, which you can also do (and were able to do prior to 7.0). See this tutorial for more information on the latter.