Search in sources :

Example 36 with OAuthException

use of org.eclipse.smarthome.core.auth.client.oauth2.OAuthException in project openhab-core by openhab.

the class ConsoleOAuthCommandExtension method execute.

@Override
public void execute(String[] args, Console console) {
    this.console = console;
    if (args.length < 2) {
        console.println("Argument expected.  Please check usage.");
        return;
    }
    AbstractTestAgent agent = getTestAgent(args[0]);
    if (agent == null) {
        console.println("Unexpected test agent:" + args[0]);
        return;
    }
    AccessTokenResponse response;
    try {
        switch(args[1]) {
            case "create":
                OAuthClientService newService = agent.testCreateClient();
                console.println("handle: " + agent.handle + ", service: " + newService);
                break;
            case "getAccessTokenByResourceOwnerPassword":
                response = agent.testGetAccessTokenByResourceOwnerPasswordCredentials();
                consolePrintAccessToken(response);
                break;
            case "getClient":
                OAuthClientService service = agent.testGetClient(args[2]);
                console.println("OAuthClientService: " + service);
                break;
            case "refresh":
                response = agent.testRefreshToken();
                consolePrintAccessToken(response);
                break;
            case "getAccessTokenByCode":
                console.println("using authorization code: " + args[2]);
                response = agent.testGetAccessTokenByAuthorizationCode(args[2]);
                consolePrintAccessToken(response);
                break;
            case "getAuthorizationUrl":
                String authURL;
                if (args.length >= 3) {
                    authURL = agent.testGetAuthorizationUrl(args[2]);
                    console.println("Authorization URL: " + authURL + " state: " + args[2]);
                } else {
                    authURL = agent.testGetAuthorizationUrl(null);
                    console.println("Authorization URL: " + authURL + " state: null");
                }
                break;
            case "getCachedAccessToken":
                response = agent.testGetCachedAccessToken();
                consolePrintAccessToken(response);
                break;
            case "close":
                console.println("Closing test agent client service...");
                agent.close();
                break;
            case "delete":
                console.println("Delete by handle: " + args[2]);
                agent.delete(args[2]);
                break;
            default:
                console.println("Commands are case-sensitive.  Unknown command: " + args[1]);
                break;
        }
    } catch (OAuthException | IOException | OAuthResponseException e) {
        console.print(String.format("%s %s, cause %s", e.getClass(), e.getMessage(), e.getCause()));
    }
}
Also used : OAuthResponseException(org.openhab.core.auth.client.oauth2.OAuthResponseException) OAuthClientService(org.openhab.core.auth.client.oauth2.OAuthClientService) AbstractTestAgent(org.openhab.core.auth.oauth2client.test.internal.AbstractTestAgent) OAuthException(org.openhab.core.auth.client.oauth2.OAuthException) IOException(java.io.IOException) AccessTokenResponse(org.openhab.core.auth.client.oauth2.AccessTokenResponse)

Aggregations

OAuthException (org.openhab.core.auth.client.oauth2.OAuthException)25 IOException (java.io.IOException)19 AccessTokenResponse (org.openhab.core.auth.client.oauth2.AccessTokenResponse)19 OAuthResponseException (org.openhab.core.auth.client.oauth2.OAuthResponseException)17 AccessTokenResponse (org.eclipse.smarthome.core.auth.client.oauth2.AccessTokenResponse)9 OAuthException (org.eclipse.smarthome.core.auth.client.oauth2.OAuthException)8 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)7 ExecutionException (java.util.concurrent.ExecutionException)5 NonNullByDefault (org.eclipse.jdt.annotation.NonNullByDefault)5 Nullable (org.eclipse.jdt.annotation.Nullable)5 Request (org.eclipse.jetty.client.api.Request)5 OAuthClientService (org.openhab.core.auth.client.oauth2.OAuthClientService)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 List (java.util.List)4 TimeoutException (java.util.concurrent.TimeoutException)4 Configuration (org.openhab.core.config.core.Configuration)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 ZonedDateTime (java.time.ZonedDateTime)3 ArrayList (java.util.ArrayList)3