Search in sources :

Example 1 with ResourceException

use of com.okta.sdk.resource.ResourceException in project okta-sdk-java by okta.

the class Quickstart method main.

public static void main(String[] args) {
    try {
        // Instantiate a builder for your Client. If needed, settings like Proxy and Caching can be defined here.
        ClientBuilder builder = Clients.builder();
        // No need to define anything else; build the Client instance. The ClientCredential information will be automatically found
        // in pre-defined locations: i.e. ~/.okta/okta.yaml
        Client client = builder.build();
        // Create a group
        Group group = GroupBuilder.instance().setName("my-user-group-" + UUID.randomUUID().toString()).setDescription("Quickstart created Group").buildAndCreate(client);
        println("Group: '" + group.getId() + "' was last updated on: " + group.getLastUpdated());
        // Create a User Account
        String email = "joe.coder+" + UUID.randomUUID().toString() + "@example.com";
        char[] password = { 'P', 'a', 's', 's', 'w', 'o', 'r', 'd', '1' };
        User user = UserBuilder.instance().setEmail(email).setFirstName("Joe").setLastName("Coder").setPassword(password).setSecurityQuestion("Favorite security question?").setSecurityQuestionAnswer("None of them!").putProfileProperty("division", // key/value pairs predefined in the user profile schema
        "Seven").setActive(true).buildAndCreate(client);
        // add user to the newly created group
        user.addToGroup(group.getId());
        String userId = user.getId();
        println("User created with ID: " + userId);
        // You can look up user by ID
        println("User lookup by ID: " + client.getUser(userId).getProfile().getLogin());
        // or by Email
        println("User lookup by Email: " + client.getUser(email).getProfile().getLogin());
        // get the list of users
        UserList users = client.listUsers();
        // get the first user in the collection
        println("First user in collection: " + users.iterator().next().getProfile().getEmail());
    // or loop through all of them (paging is automatic)
    // int ii = 0;
    // for (User tmpUser : users) {
    // println("["+ ii++ +"] User: " + tmpUser.getProfile().getEmail());
    // }
    } catch (ResourceException e) {
        // we can get the user friendly message from the Exception
        println(e.getMessage());
        // and you can get the details too
        e.getCauses().forEach(cause -> println("\t" + cause.getSummary()));
        throw e;
    }
}
Also used : ResourceException(com.okta.sdk.resource.ResourceException) ClientBuilder(com.okta.sdk.client.ClientBuilder) UserList(com.okta.sdk.resource.user.UserList) Clients(com.okta.sdk.client.Clients) GroupBuilder(com.okta.sdk.resource.group.GroupBuilder) User(com.okta.sdk.resource.user.User) Group(com.okta.sdk.resource.group.Group) Client(com.okta.sdk.client.Client) UUID(java.util.UUID) UserBuilder(com.okta.sdk.resource.user.UserBuilder) Group(com.okta.sdk.resource.group.Group) User(com.okta.sdk.resource.user.User) ResourceException(com.okta.sdk.resource.ResourceException) Client(com.okta.sdk.client.Client) UserList(com.okta.sdk.resource.user.UserList) ClientBuilder(com.okta.sdk.client.ClientBuilder)

Example 2 with ResourceException

use of com.okta.sdk.resource.ResourceException in project okta-sdk-java by okta.

the class DefaultDataStore method execute.

private Response execute(Request request) throws ResourceException {
    applyDefaultRequestHeaders(request);
    Response response = this.requestExecutor.executeRequest(request);
    log.trace("Executed HTTP request.");
    if (requestLog.isTraceEnabled()) {
        requestLog.trace("Executing request: method: '{}', url: {}", request.getMethod(), request.getResourceUrl());
    }
    if (response.isError()) {
        Map<String, Object> body = getBody(response);
        String requestId = response.getHeaders().getOktaRequestId();
        if (Strings.hasText(requestId)) {
            body.put(DefaultError.ERROR_ID.getName(), requestId);
        }
        throw new ResourceException(new DefaultError(body).setHeaders(response.getHeaders().getXHeaders()).setStatus(response.getHttpStatus()));
    }
    return response;
}
Also used : Response(com.okta.sdk.impl.http.Response) ResourceException(com.okta.sdk.resource.ResourceException) QueryString(com.okta.sdk.impl.http.QueryString) DefaultError(com.okta.sdk.impl.error.DefaultError)

Aggregations

ResourceException (com.okta.sdk.resource.ResourceException)2 Client (com.okta.sdk.client.Client)1 ClientBuilder (com.okta.sdk.client.ClientBuilder)1 Clients (com.okta.sdk.client.Clients)1 DefaultError (com.okta.sdk.impl.error.DefaultError)1 QueryString (com.okta.sdk.impl.http.QueryString)1 Response (com.okta.sdk.impl.http.Response)1 Group (com.okta.sdk.resource.group.Group)1 GroupBuilder (com.okta.sdk.resource.group.GroupBuilder)1 User (com.okta.sdk.resource.user.User)1 UserBuilder (com.okta.sdk.resource.user.UserBuilder)1 UserList (com.okta.sdk.resource.user.UserList)1 UUID (java.util.UUID)1