IBM Worklight: Calling Push Notification Adapter from backend - ibm-mobilefirst

I have implemented a Push Notification adpater and would like backend system to call the procedure in order to submit notification.
With reference to this post, http://server:port/dev/invoke?adapter=adapterName&procedure=procedureName&parameters=1,2,3 can be used but it works in development environment only.
At the end of the post it is mentioned that http://server:port/context/invoke?adapter=adapterName&procedure=procedureName&parameters=1,2,3 can be used to invoke the adapter.
(1) May I know whether it is supported in production environment only? Because it returned HTTP method GET is not supported by this URL in development environment.
(2) Noted that we might need to define securityTest="wl_unprotected" for the adapter and thus the adapter is not protected. What will be the best way to achieve the goal for backend sytem to call the adapter with security control?

In Worklight 5, Worklight Studio uses Jetty within and the usage of a context root is not required. Only once moving your project to an application server such as Tomcat, WebSphere, Liberty, you have to specify it (in worklight.properties and application-descriptor.xml).
So,
in development - no.
in production - yes.
In Worklight 6 where Jetty has been replaced with Liberty, context root is now part of your project whether you are in development or production.
You will have to add the "wl_unprotected" securityTest to your procedure in the adapter XML.
As for protecting the requests, my guess is that your IT department can isolate and protect specific URLs and not allow using them in certain scenarios. You already have the URL structure for them to use.

Related

How to change MobileFirst 8 server URL dynamically from client (Mobile Application)?

I am working on ionic 3 with MobileFirst 8.x in it. To connect server I have configured URL in config.xml file.
Is there any way to change server URL dynamically while using application?
URL change occurs only once and after that we will connect the services which are secure under any scopes.
MobileFirst server version :- 8.0.0.00-20170911-123510
MobileFirst Cordova(plugin) version :- 8.0.2017102403
I can see that provision was there in MobileFirst 7.x :-
https://mobilefirstplatform.ibmcloud.com/blog/2015/02/02/changing-server-url-runtime/
Yes. Use the WL.App.setServerURL to achieve this. Usage details here
You have not mentioned why you intend to use the API. This API comes handy in development environments. For example, switching between dev or staging server instances.
Important points to note:
1) As this API points the client application to a new server, client id associated with the earlier server is reset. The application will register fresh to the new server.
2) Authentication contexts created by the earlier server is reset. You will be challenged again by the new server.
You should use this API judiciously and with caution.

How to get Hostname and Port of IBM MobileFirst server where a Java Adapter is deployed?

I am using IBM MobileFirst Platform Foundation 8.0.0. I have a Java Adapter that is deployed on the mobilefirst server that is calling an external service. After creating a resource on the external system, I return the created id on “location” response header. It gives me the complete url of the created resource of the external system like http://example.com:1234/resource_name/1234
I don’t want to expose the external system’s url to the caller / client of my adapter. I want to return something like this - http://mobilefirstserver.com:6789/mfp/api/adapters/MyAdapter/resource_name/1234
where "mobilefirstserver" is the server on which MyAdapter is running. I am unable to find any method from mfp libraries where it can retrieve “mobilefirstserver” and “6789”. I can retrieve the adapter’s target URI, which is “example” and “1234” from adapter.xml, but not the host and port details of the mobilefirst server.
Some options:
Use the configuration API to obtain it: ConfigurationAPI.getServerJNDIProperty("mfpadmin/mfp.admin.‌​authorization.server‌​.url") - this will be something like this: http://localhost:9080/mfp/api. You will need to extract the host and port from it.
You could use, on the client-side, the WL.App.getServerUrl API and use it as a parameter in your call to the adapter. But that little sense for your scenario (although I didn't fully 'get' what is the scenario).
Since the server is yours, you can simply hard-code these values in your adapter implementation.

Worklight Apps pointing to different Worklight Servers

I want to distribute my Worklight App via the App Store. The problem is, users will have to point to different Worklight Servers depending on what company they belong to, but I don't want my users to have visibility to the Worklight Server URL or have the ability to change it themselves (meaning I want to set worklightSettings in the application-descriptor.xml to false so "Custom Server URL" does not appear in the settings).
So my idea is to initially point all my users to a default Worklight Server (in which they would only connect to the first time the start the app) which would be responsible for identifying which server they should point to based of some credentials. Once the user gives the server their credentials, it will respond with the Worklight Server URL they should connect to and then the application will set it so they will now always connect to the proper server for their company.
I know in the iPhone for example, the URL lives in worklight.plist and before I build it, I can change that value to set my url, but can I change that url after the .ipa is built without having the user manually change the "Custom Server URL" in the settings? Perhaps there's a way to change the "Custom Server URL" value programatically and always have "Use Custom URL" true, but hide it from the user in the settings?
Though not supported (at all) by Worklight, this can be accomplished. See if Anton's answer for the following question is of help:
Re-route/Divert some WL.Client Adapter Invocation traffic to WL Server through different URL (for PCI payment and security requirements)?

5.0.6 - Changing Adapter domain and port configuration after deploy through configuration - for QA and Testing team in development process

I have seen some questions about dynamic adapter configuration at run-time
how to pass domain,port number,username and password from user input to worklight http adapter
With a Worklight Adapter can I change the domain and port for a procedure at runtime?
but my question is about adapter configuration after deploy on WL server 5.0.6 from external - not at runtime from code.
Our scenario is that in our enterprise development and testing process requires the QA and Testing team to change the destination host and port for the adapters from mockup-backend to testing-backend to production-backend regularly during testing.
of course, this should happen through configuration and not re-compile and re-deploy.
Is there a way to change the domain and port values for adapters that are deployed on the WL server 5.0.6?
Can this be changed in the WL database tables?
Can this be changed in the WL server filesystem somewhere?
since we did not find any solution to that yet, the developer team is already thinking about using the Adapter only as dumb facade and implement all connection to the backend-services in Adapter-side Java using the Java URL and Connection classes.
Then it would be easy to read a configuration file from the file system in Java to change the backend destination URL and ports in this file.
It would even be easy to read the URL and port from a property in the worklight.properties file in JavaScript and pass it to Java to connect.
I would like to avoid having developing all backend connection code in Java. I would rather see the project use the actual Adapter connectivity features, but I am also not sure how to convince the team since it is quite easy with just 4 lines of code in Java. The advantage of using the Adapter connectivity that can not be configured is hard to see over a own Java implementation in Adapter-Java code.
Thank you.
Is there a way to change the domain and port values for adapters that are deployed on the WL server 5.0.6? Can this be changed in the WL database tables? Can this be changed in the WL server filesystem somewhere?
In Worklight 5.0.6, as written in the questions you've linked to in the question, an adapter's connectivity settings cannot be altered. They are stored in the packaged adapter and cannot be read from anywhere else during runtime nor after deployment.

Detect Worklight Server Hostname/IP Address from Worklight Client code

For a variety of reasons, I need programatic JavaScript access the the hostname or IP address of the Worklight server from the client code.
For example:
- I want to pull scripts and resources from the same host as the Worklight server
- I have an out-of-band service on the Worklight host and I want to connect to it
#IdanAdar has said that the address does not exist. I've search the WL object and cannot find it either. See Can we get the app version and IP address info using worklight api?.
Note that document.location.hostname is null when running as a native app.
My current workaround is to set the IP Address / hostname as a global variable in my top-level html file. This is not ideal because it causes my sourcecode to have to vary by implementation.
Worklight already has this value externalized in the application-descriptor.xml
The server side API has this exposed in WL.Server.configuration["local.IPAddress"]. I think the client should expose an API similar to this as well.
Although not the best option you can develop an adapter that returns the WL.Server.configuration["local.IPAddress"] if available, also could be possible to acces to the IP through the http request.
Is it possible to access to the HTTPRequest in the worklight adapter implementation?
I'm not sure if you can invoke adapters before wlCommonInit, if you can not and you need the ip before the common init then this approach is useless.