/** * Copyright (c) 2013 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See license text file delivered with this project for more information. */ package com.nokia.example.statusshout.engine; import javax.microedition.content.Invocation; import javax.microedition.content.Registry; import javax.microedition.content.ResponseListener; import javax.microedition.midlet.MIDlet; /** * Common base class for service utilising OAuth authentication. */ public abstract class OAuthService implements ResponseListener { // Constants private static final String TAG = "OAuthService."; private static final String BROWSER_ID = "com.nokia.browser"; // Members protected MIDlet midlet; protected ShareListener listener; /** * Constructor. * * @param listener The listener for events. * @throws NullPointerException If the given MIDlet instance is null. */ protected OAuthService(MIDlet midlet, ShareListener listener) throws NullPointerException { if (midlet == null) { throw new NullPointerException(TAG + "OAuthService(): MIDlet is null!"); } this.midlet = midlet; this.listener = listener; } /** * Authenticates the user. */ public abstract void authenticate(); /** * Checks whether the given token is valid or not. * * @param token The token to check. * @return True if the token is valid, false otherwise. */ public abstract boolean isValid(final String token); /** * Adds the given service as a listener for the content handler. There can * be only one listener at a time so this needs to be called, if the service * needs to get callbacks to ResponseListener.invocationResponseNotify(). * * @param service An instance deriving from this class. */ protected void setAsListener(OAuthService service) { Registry.getRegistry(midlet.getClass().getName()).setListener(service); } /** * Creates OAuth invocation. * * @param loginUrl The URL to navigate to. * @param redirectUrl The URL where browser redirects after a successful * login. */ protected void createInvocation(final String loginUrl, final String redirectUrl) { System.out.println(TAG + "createInvocation(): " + loginUrl + ", " + redirectUrl); Invocation invocation = new Invocation(); invocation.setURL(loginUrl); invocation.setID(BROWSER_ID); invocation.setArgs(new String[] { "mode=proxy", "redirect_intercept=" + redirectUrl }); invocation.setResponseRequired(true); try { Registry.getRegistry(midlet.getClass().getName()).invoke(invocation); } catch (Exception e) { System.out.println(TAG + "createInvocation(): " + e.toString()); if (listener != null) { listener.onError(e.toString()); } } } }