Search in sources :

Example 6 with ClientException

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

the class BaseRequest method getRequestUrl.

/**
 * Gets the request URL
 *
 * @return the request URL
 */
@Override
public URL getRequestUrl() {
    String requestUrl = addFunctionParameters();
    URI baseUrl = URI.create(requestUrl);
    final UriBuilder uriBuilder = UriBuilder.fromUri(baseUrl);
    for (final QueryOption option : queryOptions) {
        uriBuilder.queryParam(option.getName(), option.getValue().toString());
    }
    try {
        return new URL(uriBuilder.build().toString());
    } catch (final MalformedURLException e) {
        if (this instanceof CustomRequest) {
            this.getClient().getLogger().logError("Invalid custom URL: " + uriBuilder.toString(), e);
        } else {
            throw new ClientException("Invalid URL: " + uriBuilder.toString(), e);
        }
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) QueryOption(com.microsoft.graph.options.QueryOption) ClientException(com.microsoft.graph.core.ClientException) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) URL(java.net.URL)

Example 7 with ClientException

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

the class ChunkedUploadRequest method upload.

/**
 * Upload a chunk with tries.
 *
 * @param responseHandler The handler to handle the HTTP response.
 * @param <UploadType>    The upload item type.
 * @return The upload result.
 */
public <UploadType> ChunkedUploadResult<UploadType> upload(final ChunkedUploadResponseHandler<UploadType> responseHandler) {
    while (this.retryCount < this.maxRetry) {
        try {
            Thread.sleep(RETRY_DELAY * this.retryCount * this.retryCount);
        } catch (final InterruptedException e) {
            throw new ClientException("Exception while waiting to retry file upload.", e);
        }
        ChunkedUploadResult<UploadType> result = null;
        try {
            result = this.baseRequest.getClient().getHttpProvider().send(baseRequest, ChunkedUploadResult.class, this.data, responseHandler);
        } catch (final ClientException e) {
            throw new ClientException("Request failed with error, retry if necessary.", e);
        }
        if (result != null && result.chunkCompleted()) {
            return result;
        }
        this.retryCount++;
    }
    return new ChunkedUploadResult<UploadType>(new ClientException("Upload session failed too many times.", null));
}
Also used : ClientException(com.microsoft.graph.core.ClientException)

Example 8 with ClientException

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

the class GraphServiceClientTest method testOverrideOfDefaultExecutors.

@Test
public void testOverrideOfDefaultExecutors() {
    IExecutors ex = new IExecutors() {

        @Override
        public void performOnBackground(Runnable runnable) {
        // do nothing
        }

        @Override
        public <Result> void performOnForeground(Result result, ICallback<Result> callback) {
        // do nothing
        }

        @Override
        public <Result> void performOnForeground(int progress, int progressMax, IProgressCallback<Result> callback) {
        // do nothing
        }

        @Override
        public <Result> void performOnForeground(ClientException exception, ICallback<Result> callback) {
        // do nothing
        }
    };
    IGraphServiceClient client = // 
    GraphServiceClient.builder().authenticationProvider(// 
    auth).executors(// 
    ex).buildClient();
    assertEquals(ex, client.getExecutors());
    assertEquals(auth, client.getAuthenticationProvider());
    assertNotNull(client.getHttpProvider());
    assertNotNull(client.getLogger());
    assertNotNull(client.getSerializer());
    assertEquals(ex, ((DefaultHttpProvider) client.getHttpProvider()).getExecutors());
}
Also used : ICallback(com.microsoft.graph.concurrency.ICallback) IExecutors(com.microsoft.graph.concurrency.IExecutors) ClientException(com.microsoft.graph.core.ClientException) IProgressCallback(com.microsoft.graph.concurrency.IProgressCallback) IGraphServiceClient(com.microsoft.graph.models.extensions.IGraphServiceClient) Test(org.junit.Test)

Example 9 with ClientException

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

the class BaseStreamRequestTests method testSendWithContentAndCallback.

@Test
public void testSendWithContentAndCallback() {
    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;
        }
    };
    DefaultHttpProvider mProvider = new DefaultHttpProvider(new MockSerializer(null, ""), mAuthenticationProvider, new MockExecutors(), new MockLogger());
    mProvider.setConnectionFactory(new MockConnectionFactory(new MockConnection(data)));
    mBaseClient.setHttpProvider(mProvider);
    final AtomicBoolean success = new AtomicBoolean(false);
    final AtomicBoolean failure = new AtomicBoolean(false);
    final ICallback<InputStream> callback = new ICallback<InputStream>() {

        @Override
        public void success(InputStream inputStream) {
            success.set(true);
        }

        @Override
        public void failure(ClientException ex) {
            failure.set(true);
        }
    };
    final BaseStreamRequest<InputStream> request = new BaseStreamRequest<InputStream>("https://a.b.c", mBaseClient, null, InputStream.class) {
    };
    request.send(new byte[] { 1, 2, 3, 4 }, callback);
    assertTrue(success.get());
    assertFalse(failure.get());
    assertEquals(1, mAuthenticationProvider.getInterceptionCount());
}
Also used : MockSerializer(com.microsoft.graph.serializer.MockSerializer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) MockExecutors(com.microsoft.graph.concurrency.MockExecutors) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ICallback(com.microsoft.graph.concurrency.ICallback) MockLogger(com.microsoft.graph.logger.MockLogger) ClientException(com.microsoft.graph.core.ClientException) Test(org.junit.Test)

Example 10 with ClientException

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

the class BaseStreamRequestTests method testSendWithCallback.

@Test
public void testSendWithCallback() {
    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;
        }
    };
    DefaultHttpProvider mProvider = new DefaultHttpProvider(new MockSerializer(null, ""), mAuthenticationProvider, new MockExecutors(), new MockLogger());
    mProvider.setConnectionFactory(new MockConnectionFactory(new MockConnection(data)));
    mBaseClient.setHttpProvider(mProvider);
    final AtomicBoolean success = new AtomicBoolean(false);
    final AtomicBoolean failure = new AtomicBoolean(false);
    final ICallback<InputStream> callback = new ICallback<InputStream>() {

        @Override
        public void success(InputStream inputStream) {
            success.set(true);
        }

        @Override
        public void failure(ClientException ex) {
            failure.set(true);
        }
    };
    final BaseStreamRequest<InputStream> request = new BaseStreamRequest<InputStream>("https://a.b.c", mBaseClient, null, InputStream.class) {
    };
    request.send(callback);
    assertTrue(success.get());
    assertFalse(failure.get());
    assertEquals(1, mAuthenticationProvider.getInterceptionCount());
}
Also used : MockSerializer(com.microsoft.graph.serializer.MockSerializer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) MockExecutors(com.microsoft.graph.concurrency.MockExecutors) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ICallback(com.microsoft.graph.concurrency.ICallback) MockLogger(com.microsoft.graph.logger.MockLogger) ClientException(com.microsoft.graph.core.ClientException) 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