Question

Is there a way to query the worklight server URL information from inside of a hybrid application? I have a need when I build an app for a remote server to establish credentials with the server that WL is installed on before I can call adapter procedures from that server. Currently I'm doing this by performing an dojo.xhrGet on the console. The URL is hardcoded in the app at this time. For many reasons I would like be able to query this information at run time. In iOS this information is stored in worklight.plist and in android it is in assets/wlclient.properties.

I'm running WL 6.1 and I've tried calling get properties as below

WL.Client.getAppProperty(WL.AppProp.WORKLIGHT_ROOT_URL);
WL.Client.getAppProperty(WL.AppProp.APP_SERVICES_URL);

but all they return is

/MyApp/apps/services/api/simpleApp/common/
/MyApp/apps/services/

I need to get to the host information like the following found in the android wlclient.propeties

wlServerProtocol = https
wlServerHost = zzzz.aaa.bb.com
wlServerPort = 15024
wlServerContext = /

or in iOS worklight.plist

<key>protocol</key> 
<string>https</string>
<key>host</key>
<string>zzzz.aaa.bb.com</string>
<key>port</key>
<string>15024</string>
<key>wlServerContext</key>
<string>/</string>

Any help will be greatly welcomed.

Was it helpful?

Solution 2

I did a little more investigation and was able to write a cordova plugin for android and ios to read in the value from the prospective property files that WL generates. Below are the different sources. See WorkLight Getting Started Section 6 for more information on writing a cordova plugin. It was actually fairly easy. Once you get past how... different Objective-C is, it took just a few minutes.


Android code

WorklightPropertiesPlugin.java

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.res.AssetManager;
import android.content.res.Resources;

public class WorklightPropertiesPlugin extends CordovaPlugin {
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        boolean handled = false;
        String responseText = "";
        if(action.equalsIgnoreCase("serverURL")) {
            try {
                AssetManager am = this.cordova.getActivity().getApplicationContext().getAssets();
                InputStream is = am.open("wlclient.properties");
                Properties prop = new Properties();
                prop.load(is);
                String wlServerProtocol = prop.getProperty("wlServerProtocol");
                String wlServerHost = prop.getProperty("wlServerHost");
                String wlServerPort = prop.getProperty("wlServerPort");
                String wlServerContext = prop.getProperty("wlServerContext");
                responseText = wlServerProtocol + "://" + wlServerHost + ":" + wlServerPort + wlServerContext;
                handled = true;
            } catch(IOException e) {
                callbackContext.error("Error loading properties " + e.getLocalizedMessage());
            }
        }
        if(handled) {
            callbackContext.success(responseText);
        }
        return handled;
    }
}

iOS code

WorklightPropertiesPlugin.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>

@interface WorklightPropertiesPlugin : CDVPlugin

- (void) serverURL:(CDVInvokedUrlCommand*) command;

@end

WorklightPropertiesPlugin.m

#import "WorklightPropertiesPlugin.h"

@implementation WorklightPropertiesPlugin

- (void) serverURL:(CDVInvokedUrlCommand*)command{
    NSString * response = nil;
    CDVPluginResult *pluginResult = nil;

    NSString* plistLoc =[[NSBundle mainBundle]pathForResource:@"worklight" ofType:@"plist"];
    if(plistLoc == nil){
        [NSException raise:@"[Remote Load Initialization Error]" format:@"Unable to locate worklight.plist"];
    }

    NSDictionary* wlProps = [NSDictionary dictionaryWithContentsOfFile:plistLoc];
    NSString* proto = [wlProps valueForKey:@"protocol"];
    NSString* host = [wlProps valueForKey:@"host"];
    NSString* port = [wlProps valueForKey:@"port"];
    NSString* wlServerContext = [wlProps valueForKey:@"wlServerContext"];

    if(proto == nil || host == nil || port == nil){
        [NSException raise:@"[Remote Load Initialization Error]" format:@"host, port and protocol are all required keys in worklight.plist"];
    }

    response = [NSString stringWithFormat:@"%@://%@:%@%@", proto, host, port, wlServerContext];

    pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:response];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top