Search in sources :

Example 11 with ClientException

use of com.microsoft.graph.core.ClientException in project msgraph-sdk-java by microsoftgraph.

the class MockHttpProvider method send.

@Override
public <Result, BodyType> void send(IHttpRequest request, ICallback<Result> callback, Class<Result> resultClass, BodyType serializable) {
    Result result = null;
    try {
        IConnection connection = mConnectionFactory.createFromRequest(request);
        if (connection.getResponseCode() == 200) {
            if (connection.getHeaders().containsKey("Content-Type")) {
                if (connection.getHeaders().get("Content-Type").equals("application/json")) {
                    JsonObject jsonObject = new JsonParser().parse(connection.getResponseMessage()).getAsJsonObject();
                    result = (Result) jsonObject;
                } else if (connection.getHeaders().get("Content-Type").equals("application/octet-stream")) {
                    result = (Result) new BufferedInputStream(connection.getInputStream());
                }
            }
        }
    } catch (Exception ex) {
        final ClientException clientException = new ClientException("Error during http request", ex);
        callback.failure(clientException);
    }
    callback.success(result);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) JsonObject(com.google.gson.JsonObject) ClientException(com.microsoft.graph.core.ClientException) ClientException(com.microsoft.graph.core.ClientException) JsonParser(com.google.gson.JsonParser)

Example 12 with ClientException

use of com.microsoft.graph.core.ClientException in project android-java-snippets-sample by microsoftgraph.

the class DrivesSnippets method getDrivesSnippets.

static DrivesSnippets[] getDrivesSnippets() {
    return new DrivesSnippets[] { // Marker element
    new DrivesSnippets(null) {

        @Override
        public void request(ICallback callback) {
        // No implementation
        }
    }, /* Get the user's drive
                 * GET https://graph.microsoft.com/{version}/me/drive
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/drive_get
                 */
    new DrivesSnippets<JsonObject>(get_me_drive) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getMe().getDrive().buildRequest().get(new ICallback<Drive>() {

                @Override
                public void success(Drive drive) {
                    callback.success(drive.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Get files in the root folder
                 * GET https://graph.microsoft.com/{version}/me/drive/root/children
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_list_children
                 */
    new DrivesSnippets<JsonObject>(get_me_files) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // Get files in root folder
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren().buildRequest().get(new ICallback<IDriveItemCollectionPage>() {

                @Override
                public void success(IDriveItemCollectionPage iDriveItemCollectionPage) {
                    callback.success(iDriveItemCollectionPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Create a file
                 * PUT https://graph.microsoft.com/{version}/me/drive/root/children/{filename}/content
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_post_children
                 */
    new DrivesSnippets<JsonObject>(create_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file
            try {
                String guid = UUID.randomUUID().toString();
                byte[] byteArray = guid.getBytes("UTF-8");
                mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                    @Override
                    public void success(DriveItem driveItem) {
                        callback.success(driveItem.getRawObject());
                    }

                    @Override
                    public void failure(ClientException ex) {
                        callback.failure(ex);
                    }
                });
            } catch (UnsupportedEncodingException uee) {
                uee.printStackTrace();
            }
        }
    }, /*
                 * Download the content of a file
                 * GET https://graph.microsoft.com/{version}/me/drive/items/{filename}/content
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_downloadcontent
                 */
    new DrivesSnippets<JsonObject>(download_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to download
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    // Get the guid that the service assigned to my file
                    String guid = driveItem.id;
                    mGraphServiceClient.getMe().getDrive().getItems().byId(guid).getContent().buildRequest().get(new ICallback<InputStream>() {

                        @Override
                        public void success(InputStream inputStream) {
                            final InputStreamReader inr = new InputStreamReader(inputStream);
                            String text;
                            try {
                                text = CharStreams.toString(inr);
                                JsonObject result = new JsonObject();
                                result.addProperty("value", text);
                                callback.success(result);
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }

                        @Override
                        public void failure(ClientException ex) {
                            callback.failure(ex);
                        }
                    });
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Update the content of a file
                 * PUT https://graph.microsoft.com/{version}/me/drive/items/{filename}/content
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_update
                 */
    new DrivesSnippets<JsonObject>(update_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to update
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    // This is the new content that we use to update the file
                    byte[] byteArray = null;
                    try {
                        byteArray = "A plain text file".getBytes("UTF-8");
                        mGraphServiceClient.getMe().getDrive().getItems().byId(driveItem.id).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                            @Override
                            public void success(DriveItem driveItem) {
                                callback.success(driveItem.getRawObject());
                            }

                            @Override
                            public void failure(ClientException ex) {
                                callback.failure(ex);
                            }
                        });
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Delete the content of a file
                 * DELETE https://graph.microsoft.com/{version}/me/drive/items/{fileId}/
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_delete
                 */
    new DrivesSnippets<JsonObject>(delete_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to delete
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    mGraphServiceClient.getMe().getDrive().getItems().byId(driveItem.id).buildRequest().delete(new ICallback<Void>() {

                        @Override
                        public void success(Void aVoid) {
                            callback.success(null);
                        }

                        @Override
                        public void failure(ClientException ex) {
                            callback.failure(ex);
                        }
                    });
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Renames a file
                 * PATCH https://graph.microsoft.com/{version}/me/drive/items/{fileId}/
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_update
                 */
    new DrivesSnippets<JsonObject>(rename_me_file) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // create a new file to rename
            String guid = UUID.randomUUID().toString();
            byte[] byteArray = null;
            try {
                byteArray = guid.getBytes("UTF-8");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren(guid).getContent().buildRequest().put(byteArray, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    DriveItem renamedDriveItem = new DriveItem();
                    renamedDriveItem.name = "Updated name";
                    mGraphServiceClient.getMe().getDrive().getItems().byId(driveItem.id).buildRequest().patch(renamedDriveItem, new ICallback<DriveItem>() {

                        @Override
                        public void success(DriveItem driveItem) {
                            callback.success(driveItem.getRawObject());
                        }

                        @Override
                        public void failure(ClientException ex) {
                            callback.failure(ex);
                        }
                    });
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Creates a folder
                 * POST https://graph.microsoft.com/me/drive/root/children
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/item_post_children
                 */
    new DrivesSnippets<JsonObject>(create_me_folder) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            String guid = UUID.randomUUID().toString();
            DriveItem driveItem = new DriveItem();
            driveItem.name = guid;
            driveItem.folder = new Folder();
            mGraphServiceClient.getMe().getDrive().getRoot().getChildren().buildRequest().post(driveItem, new ICallback<DriveItem>() {

                @Override
                public void success(DriveItem driveItem) {
                    callback.success(driveItem.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    } };
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) Folder(com.microsoft.graph.extensions.Folder) DriveItem(com.microsoft.graph.extensions.DriveItem) IDriveItemCollectionPage(com.microsoft.graph.extensions.IDriveItemCollectionPage) ICallback(com.microsoft.graph.concurrency.ICallback) Drive(com.microsoft.graph.extensions.Drive) ClientException(com.microsoft.graph.core.ClientException)

Example 13 with ClientException

use of com.microsoft.graph.core.ClientException in project android-java-snippets-sample by microsoftgraph.

the class MeSnippets method getMeSnippets.

static MeSnippets[] getMeSnippets() {
    return new MeSnippets[] { // Marker element
    new MeSnippets(null) {

        @Override
        public void request(ICallback callback) {
        // Not implemented
        }
    }, /* Get information about signed in user
                 * HTTP GET https://graph.microsoft.com/{version}/me
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_get
                 */
    new MeSnippets<JsonObject>(get_me) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getMe().buildRequest().get(new ICallback<User>() {

                @Override
                public void success(User user) {
                    callback.success(user.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get responsibilities of signed in user
                 * HTTP GET https://graph.microsoft.com/{version}/me?$select=AboutMe,Responsibilities,Tags
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_responsibilities) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            final List<Option> options = new LinkedList<>();
            options.add(new QueryOption("$select", "AboutMe,Responsibilities,Tags"));
            mGraphServiceClient.getMe().buildRequest(options).get(new ICallback<User>() {

                @Override
                public void success(User user) {
                    callback.success(user.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the user's manager
                 * HTTP GET https://graph.microsoft.com/{version}/me/manager
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_manager) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getMe().getManager().buildRequest().get(new ICallback<DirectoryObject>() {

                @Override
                public void success(DirectoryObject directoryObject) {
                    callback.success(directoryObject.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the user's direct reports
                 * HTTP GET https://graph.microsoft.com/{version}/me/directReports
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_direct_reports) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getMe().getDirectReports().buildRequest().get(new ICallback<IDirectoryObjectCollectionWithReferencesPage>() {

                @Override
                public void success(IDirectoryObjectCollectionWithReferencesPage iDirectoryObjectCollectionWithReferencesPage) {
                    callback.success(iDirectoryObjectCollectionWithReferencesPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the group membership of the user
                 * HTTP GET https://graph.microsoft.com/{version}/me/memberOf
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_group_membership) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getMe().getMemberOf().buildRequest().get(new ICallback<IDirectoryObjectCollectionWithReferencesPage>() {

                @Override
                public void success(IDirectoryObjectCollectionWithReferencesPage iDirectoryObjectCollectionWithReferencesPage) {
                    callback.success(iDirectoryObjectCollectionWithReferencesPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /* Get the photo of the user
                 * HTTP GET https://graph.microsoft.com/{version}/me/userPhoto
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/resources/user
                 */
    new MeSnippets<JsonObject>(get_me_photo) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getMe().getPhoto().buildRequest().get(new ICallback<ProfilePhoto>() {

                @Override
                public void success(ProfilePhoto profilePhoto) {
                    callback.success(profilePhoto.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    } };
}
Also used : User(com.microsoft.graph.extensions.User) QueryOption(com.microsoft.graph.options.QueryOption) ProfilePhoto(com.microsoft.graph.extensions.ProfilePhoto) LinkedList(java.util.LinkedList) ICallback(com.microsoft.graph.concurrency.ICallback) IDirectoryObjectCollectionWithReferencesPage(com.microsoft.graph.extensions.IDirectoryObjectCollectionWithReferencesPage) QueryOption(com.microsoft.graph.options.QueryOption) Option(com.microsoft.graph.options.Option) ClientException(com.microsoft.graph.core.ClientException) DirectoryObject(com.microsoft.graph.extensions.DirectoryObject)

Example 14 with ClientException

use of com.microsoft.graph.core.ClientException in project android-java-snippets-sample by microsoftgraph.

the class UsersSnippets method getUsersSnippets.

static UsersSnippets[] getUsersSnippets() {
    return new UsersSnippets[] { // Marker element
    new UsersSnippets(null) {

        @Override
        public void request(ICallback callback) {
        // Not implemented
        }
    }, /*
                 * Gets all of the users in your tenant\'s directory.
                 * HTTP GET https://graph.microsoft.com/{version}/myOrganization/users
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_list
                 */
    new UsersSnippets<JsonObject>(get_organization_users) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            mGraphServiceClient.getUsers().buildRequest().get(new ICallback<IUserCollectionPage>() {

                @Override
                public void success(IUserCollectionPage iUserCollectionPage) {
                    callback.success(iUserCollectionPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Gets all of the users in your tenant's directory who are from the United States, using $filter.
                 * HTTP GET https://graph.microsoft.com/{version}/myOrganization/users?$filter=country eq \'United States\'
                 * @see http://graph.microsoft.io/docs/overview/query_parameters
                 */
    new UsersSnippets<JsonObject>(get_organization_filtered_users) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            final List<Option> options = new LinkedList<>();
            options.add(new QueryOption("$filter", "country eq 'United States'"));
            mGraphServiceClient.getUsers().buildRequest(options).get(new ICallback<IUserCollectionPage>() {

                @Override
                public void success(IUserCollectionPage iUserCollectionPage) {
                    callback.success(iUserCollectionPage.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    }, /*
                 * Adds a new user to the tenant's directory
                 * HTTP POST https://graph.microsoft.com/{version}/myOrganization/users
                 * @see https://graph.microsoft.io/docs/api-reference/v1.0/api/user_post_users
                 */
    new UsersSnippets<JsonObject>(insert_organization_user) {

        @Override
        public void request(final ICallback<JsonObject> callback) {
            // Use a random UUID for the user name
            String randomUserName = UUID.randomUUID().toString();
            // create the user
            User user = new User();
            user.accountEnabled = true;
            user.displayName = "SAMPLE " + randomUserName;
            user.mailNickname = randomUserName;
            // get the tenant from preferences
            SharedPreferences prefs = SharedPrefsUtil.getSharedPreferences();
            String tenant = prefs.getString(PREF_USER_TENANT, "");
            user.userPrincipalName = randomUserName + "@" + tenant;
            // initialize a password & say whether or not the user must change it
            PasswordProfile password = new PasswordProfile();
            password.password = UUID.randomUUID().toString().substring(0, 16);
            password.forceChangePasswordNextSignIn = false;
            user.passwordProfile = password;
            mGraphServiceClient.getUsers().buildRequest().post(user, new ICallback<User>() {

                @Override
                public void success(User user) {
                    callback.success(user.getRawObject());
                }

                @Override
                public void failure(ClientException ex) {
                    callback.failure(ex);
                }
            });
        }
    } };
}
Also used : User(com.microsoft.graph.extensions.User) IUserCollectionPage(com.microsoft.graph.extensions.IUserCollectionPage) SharedPreferences(android.content.SharedPreferences) QueryOption(com.microsoft.graph.options.QueryOption) LinkedList(java.util.LinkedList) PasswordProfile(com.microsoft.graph.extensions.PasswordProfile) ICallback(com.microsoft.graph.concurrency.ICallback) QueryOption(com.microsoft.graph.options.QueryOption) Option(com.microsoft.graph.options.Option) ClientException(com.microsoft.graph.core.ClientException)

Example 15 with ClientException

use of com.microsoft.graph.core.ClientException in project msgraph-sdk-java by microsoftgraph.

the class DefaultHttpProviderTests method testPostByte.

@Test
public void testPostByte() throws Exception {
    final String itemId = "itemId";
    final ITestConnectionData data = new ITestConnectionData() {

        @Override
        public int getRequestCode() {
            return 200;
        }

        @Override
        public String getJsonResponse() {
            return "{ \"id\": \"zzz\" }";
        }

        @Override
        public Map<String, String> getHeaders() {
            final HashMap<String, String> map = new HashMap<>();
            map.put("Content-Type", "application/json");
            return map;
        }
    };
    final DriveItem expectedItem = new DriveItem();
    expectedItem.id = itemId;
    setDefaultHttpProvider(expectedItem);
    mProvider.setConnectionFactory(new MockConnectionFactory(new MockConnection(data)));
    final AtomicBoolean progress = new AtomicBoolean(false);
    final AtomicBoolean success = new AtomicBoolean(false);
    final AtomicBoolean failure = new AtomicBoolean(false);
    final IProgressCallback<DriveItem> progressCallback = new IProgressCallback<DriveItem>() {

        @Override
        public void progress(final long current, final long max) {
            progress.set(true);
        }

        @Override
        public void success(final DriveItem item) {
            success.set(true);
        }

        @Override
        public void failure(final ClientException ex) {
            failure.set(true);
        }
    };
    mProvider.send(new MockHttpRequest(), progressCallback, DriveItem.class, new byte[] { 1, 2, 3, 4 });
    assertTrue(progress.get());
    assertTrue(success.get());
    assertEquals(1, mAuthenticationProvider.getInterceptionCount());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DriveItem(com.microsoft.graph.models.extensions.DriveItem) HashMap(java.util.HashMap) ClientException(com.microsoft.graph.core.ClientException) IProgressCallback(com.microsoft.graph.concurrency.IProgressCallback) Test(org.junit.Test)

Aggregations

ClientException (com.microsoft.graph.core.ClientException)16 ICallback (com.microsoft.graph.concurrency.ICallback)10 Test (org.junit.Test)6 InputStream (java.io.InputStream)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 JsonObject (com.google.gson.JsonObject)3 QueryOption (com.microsoft.graph.options.QueryOption)3 HashMap (java.util.HashMap)3 IProgressCallback (com.microsoft.graph.concurrency.IProgressCallback)2 MockExecutors (com.microsoft.graph.concurrency.MockExecutors)2 IDirectoryObjectCollectionWithReferencesPage (com.microsoft.graph.extensions.IDirectoryObjectCollectionWithReferencesPage)2 User (com.microsoft.graph.extensions.User)2 MockLogger (com.microsoft.graph.logger.MockLogger)2 Option (com.microsoft.graph.options.Option)2 MockSerializer (com.microsoft.graph.serializer.MockSerializer)2 BufferedInputStream (java.io.BufferedInputStream)2 IOException (java.io.IOException)2 URL (java.net.URL)2 LinkedList (java.util.LinkedList)2 SharedPreferences (android.content.SharedPreferences)1