Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Url cxn experiment #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions check_code
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ LAYERS = [
'sample',
'client.webApiHttpClient',
'client.okHttpClient',
'client.webApiUrlConnectionClient',
'client',
'client.webApiClients',
'client.structures',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2017, Backblaze Inc. All Rights Reserved.
* License https://www.backblaze.com/using_b2_code.html
*/
package com.backblaze.b2.client;

import com.backblaze.b2.client.exceptions.B2Exception;
import com.backblaze.b2.client.structures.B2AccountAuthorization;
import com.backblaze.b2.client.structures.B2Allowed;
import com.backblaze.b2.client.structures.B2AuthorizeAccountRequest;
import com.backblaze.b2.util.B2Preconditions;

/**
* B2AccountAuthorizerV5AuthImpl is used when the user has authenticated
* with B1.
*/
public class B2AccountAuthorizerV5AuthImpl implements B2AccountAuthorizer {
private final B2AccountAuthorization b2AccountAuthorization;

@Override
public B2AccountAuthorization authorize(B2StorageClientWebifier webifier) throws B2Exception {
return b2AccountAuthorization;
}

public B2AccountAuthorizerV5AuthImpl(B2AccountAuthorization b2AccountAuthorization){
this.b2AccountAuthorization = b2AccountAuthorization;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.backblaze.b2.client.credentialsSources.B2Credentials;
import com.backblaze.b2.client.credentialsSources.B2CredentialsFromEnvironmentSource;
import com.backblaze.b2.client.structures.B2AccountAuthorization;
import com.backblaze.b2.client.structures.B2Allowed;

/**
* Implementations of B2StorageClientFactory can create a B2StorageClient from a B2ClientConfig.
Expand Down Expand Up @@ -56,4 +58,20 @@ default B2StorageClient create(String userAgent) {
final B2Credentials credentials = B2CredentialsFromEnvironmentSource.build().getCredentials();
return create(credentials.getApplicationKeyId(), credentials.getApplicationKey(), userAgent);
}

/**
* If we have authenticated via other means, we can provide the complete authorization
* @param b2AccountAuthorization complete auth info normally returned by authorize_account
* @param userAgent the user agent to use when performing http requests.
* @return a new B2StorageClient or throws a RuntimeException if it can't make one.
*/

default B2StorageClient create(B2AccountAuthorization b2AccountAuthorization,
String userAgent){
final B2AccountAuthorizer accountAuthorizer = new B2AccountAuthorizerV5AuthImpl(b2AccountAuthorization);
final B2ClientConfig config = B2ClientConfig
.builder( accountAuthorizer, userAgent)
.build();
return create(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ public class B2StorageClientFactoryPathBasedImpl implements B2StorageClientFacto
private B2StorageClientFactory factory;

B2StorageClientFactoryPathBasedImpl() {

// register the Apache HttpClient-based implementation:
registerClass("com.backblaze.b2.client.webApiHttpClient.B2StorageHttpClientFactory");

// register the okhttp-based implementation:
registerClass("com.backblaze.b2.client.okHttpClient.B2StorageOkHttpClientFactory");

// register the UrlConnection-based implementation:
registerClass("com.backblaze.b2.client.webApiUrlConnectionClient.B2WebApiUrlConnectionClientFactory");

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2019, Backblaze Inc. All Rights Reserved.
* License https://www.backblaze.com/using_b2_code.html
*/
package com.backblaze.b2.client.webApiUrlConnectionClient;

import com.backblaze.b2.client.B2ClientConfig;
import com.backblaze.b2.client.B2DefaultRetryPolicy;
import com.backblaze.b2.client.B2RetryPolicy;
import com.backblaze.b2.client.B2Sdk;
import com.backblaze.b2.client.B2StorageClient;
import com.backblaze.b2.client.B2StorageClientImpl;
import com.backblaze.b2.client.B2StorageClientWebifier;
import com.backblaze.b2.client.B2StorageClientWebifierImpl;
import com.backblaze.b2.client.webApiClients.B2WebApiClient;

import java.util.function.Supplier;

public class B2StorageUrlConnectionClientBuilder {
private static final String DEFAULT_MASTER_URL = "https://api.backblazeb2.com/";
private final B2ClientConfig config;
private B2WebApiClient webApiClient;
private Supplier<B2RetryPolicy> retryPolicySupplier;

@SuppressWarnings("WeakerAccess")
public static B2StorageUrlConnectionClientBuilder builder(B2ClientConfig config) {
return new B2StorageUrlConnectionClientBuilder(config);
}

private B2StorageUrlConnectionClientBuilder(B2ClientConfig config) {
this.config = config;
}

public B2StorageClient build() {
final B2WebApiClient webApiClient = (this.webApiClient != null) ?
this.webApiClient :
new B2WebApiUrlConnectionClientImpl();
final B2StorageClientWebifier webifier = new B2StorageClientWebifierImpl(
webApiClient,
config.getUserAgent() + " " + B2Sdk.getName() + "/" + B2Sdk.getVersion(),
(config.getMasterUrl() == null) ? DEFAULT_MASTER_URL : config.getMasterUrl(),
config.getTestModeOrNull());
final Supplier<B2RetryPolicy> retryPolicySupplier = (this.retryPolicySupplier != null) ?
this.retryPolicySupplier :
B2DefaultRetryPolicy.supplier();
return new B2StorageClientImpl(
webifier,
config,
retryPolicySupplier);
}

@SuppressWarnings("unused")
public B2StorageUrlConnectionClientBuilder setRetryPolicySupplier(Supplier<B2RetryPolicy> retryPolicySupplier) {
this.retryPolicySupplier = retryPolicySupplier;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019, Backblaze Inc. All Rights Reserved.
* License https://www.backblaze.com/using_b2_code.html
*/
package com.backblaze.b2.client.webApiUrlConnectionClient;


import com.backblaze.b2.client.B2ClientConfig;
import com.backblaze.b2.client.B2StorageClient;
import com.backblaze.b2.client.B2StorageClientFactory;

/**
* Simple factory for the UrlConnection-based B2StorageClient.
*
* THREAD-SAFE.
*/
public class B2WebApiUrlConnectionClientFactory implements B2StorageClientFactory {

@Override
public B2StorageClient create(B2ClientConfig config) {
return B2StorageUrlConnectionClientBuilder.builder(config).build();
}
}
Loading