Page 1 of 1

Use API outside web application

Posted: Tue Dec 17, 2013 5:02 am
by ecircle
Hi,

we want to use the WURFL/ScientiaMobile API within a plain Java application, without ServletContext and without Spring. We use the API as follows to build the WURFLEngine manually:

Code: Select all

        // setup WURFL engine
        InputStream in = WurflRawClassifier.class.getResourceAsStream("/path/to/wurfl.xml");
        WURFLResource resource = new XMLResource(in, "wurfl.xml");
        GeneralWURFLEngine engine = new GeneralWURFLEngine(resource);
        engine.setEngineTarget(EngineTarget.accuracy);
        engine.setCacheProvider(new LRUMapCacheProvider());

        // get device information
        Device device = engine.getDeviceForRequest("Some user agent string");
        String deviceId = device.getId();
        String deviceRootId = device.getDeviceRootId();
        String matchType = device.getMatchType().name();
        Map<String, String> capabilities = device.getCapabilities();
        Map<String, String> virtualCapabilities = device.getVirtualCapabilities();
We want to know if that is the right way to manually build and use the WURFLEngine and/or if something is missing?

Thanks,
Stefan

Re: Use API outside web application

Posted: Tue Dec 17, 2013 11:08 am
by support-all
Hello Stefan!

Regarding your question: unless you need to get some more detail, the WURFL Engine setup could be done in a single line in a shorter way, i.e.

Code: Select all

WURFLEngine engine = new GeneralWURFLEngine("/path/to/wurfl.xml");

As for the

Code: Select all

engine.setEngineTarget(EngineTarget.accuracy);
it's okay and regarding your last line,

Code: Select all

engine.setCacheProvider(new LRUMapCacheProvider());

mind that WURFL gets initialized by default with a better cache option. So, unless you're aiming to use this specific option, this line is not needed.

Same goes for the device information: I see you're not willing to use ServletContext and Spring, however it is more efficient to pass a HttpServletRequest to getDeviceForRequest, instead of "Some UA string".

(above mentioned line is:)

Code: Select all

Device device = engine.getDeviceForRequest("Some user agent string");

and mind that the latest two lines

Code: Select all

Map<String, String> capabilities = device.getCapabilities();
Map<String, String> virtualCapabilities = device.getVirtualCapabilities();

do require a lot of computation. Unless it is needed, it is better to get only the caps and vcaps that are really needed, in order to improve the performances. you may want to take a look at the capability filter feature :)


I hope this does answer your question :)


Thanks,

Francesca

Re: Use API outside web application

Posted: Wed Dec 18, 2013 4:14 am
by ecircle
Thanks Francesca for the immediate repsonse.

We choosed the more complex way to setup GeneralWURFLEngine because the wurfl.xml is packaged within a JAR and not directly available in the file system.

We choosed the LRUMapCacheProvider because it was also configured in http://wurfl.sourceforge.net/njava_index.php, but we'll remove it.

We retrive all the capabilites only during evaluation phase, once we identified the interesting capablites we'll use the filter feature, thanks for the hint :)

Kind Regards,
Stefan