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

BACK-44 java-sdk init with domain #23

Open
wants to merge 2 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
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ android.useAndroidX=true
android.nonTransitiveRClass=true
android.disableAutomaticComponentCreation=true

version.java-sdk-common = 7.0.8
version.java-client-sdk = 7.0.7
version.android-client-sdk = 7.0.8
version.backendless-commons = 6.7.5.0
version.java-sdk-common = 7.1.0
version.java-client-sdk = 7.1.0
version.android-client-sdk = 7.1.0
version.backendless-commons = 6.7.5.1
version.weborbclient = 5.2.0.8
version.io-socket-client = 2.1.0
version.projectlombok = 1.18.24
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static void initApp( Object context, final String customDomain )

public static void initApp( String applicationId, String apiKey )
{
BackendlessInternal.initApp( applicationId, apiKey );
BackendlessInternal.initApp( (Object) null, applicationId, apiKey );
}

public static void initApp( Object context, final String applicationId, final String apiKey )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.backendless;


import com.backendless.exceptions.ExceptionMessage;
import com.backendless.files.BackendlessFile;
import com.backendless.files.BackendlessFileFactory;
Expand Down Expand Up @@ -57,9 +58,12 @@
import weborb.writer.MessageWriter;
import weborb.writer.amf.AmfV3Formatter;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -143,7 +147,12 @@ public boolean isReferenceableType()
private BackendlessInternal()
{
}


static boolean isInitialized()
{
return initialized;
}

/**
* Initializes the Backendless API and all Backendless dependencies. This is the first step in using the client API.
* <p>
Expand All @@ -155,9 +164,17 @@ private BackendlessInternal()
*/
static void initApp( String applicationId, String apiKey )
{
initApp( BackendlessInjector.getInstance().getContextHandler().getAppContext(), applicationId, apiKey );
setAppIdAndApiKey( applicationId, apiKey );
initApp();
}


static void initApp( Object context, final String applicationId, final String apiKey )
{
setAppIdAndApiKey(applicationId, apiKey);
BackendlessInjector.getInstance().getContextHandler().setContext(context);
initApp();
}

/**
* Initializes the Backendless API and all Backendless dependencies. This is the first step in using the client API.
* <p>
Expand All @@ -168,52 +185,62 @@ static void initApp( String applicationId, String apiKey )
*/
static void initApp( String customDomain )
{
initApp( BackendlessInjector.getInstance().getContextHandler().getAppContext(), customDomain );
}

static boolean isInitialized()
{
return initialized;
initApp( (Object) null, customDomain );
}

static void initApp( Object context, final String customDomain )
{
if( customDomain == null || customDomain.trim().isEmpty() )
throw new IllegalArgumentException( "Custom domain cant be null or empty" );

if( customDomain.startsWith( "http" ) )
setUrl( customDomain );
else
setUrl( "http://" + customDomain );

URI uri;
try
{
uri = new URI( getUrl() );

final URI uri;
try {
uri = customDomain.startsWith("http") ? new URI(customDomain) : new URI("http://" + customDomain);
}
catch( URISyntaxException e )
{
throw new RuntimeException( e );
throw new IllegalArgumentException( "Domain passed in wrong form.", e );
}

prefs.setCustomDomain( uri.getHost() );
BackendlessInjector.getInstance().getContextHandler().setContext( context );

StringBuilder response;
try {
URL apiInfoUrl = new URL( uri + "/api/info" );
HttpURLConnection urlConnection = (HttpURLConnection) apiInfoUrl.openConnection();
if ( urlConnection.getResponseCode() != 200 )
throw new IllegalStateException( "Server returned " + urlConnection.getResponseCode() + " " + urlConnection.getResponseMessage() );

response = new StringBuilder(10 * 1024);
int read;
byte[] fbuf = new byte[ 2048 ];
try ( BufferedInputStream bufInStream = new BufferedInputStream( urlConnection.getInputStream() )) {
while( (read = bufInStream.read( fbuf )) != -1 )
response.append( new String( fbuf, 0, read ) );
}
}
catch (IOException e) {
throw new IllegalStateException( "Cannot retrieve app info from Backendless server using domain '" + getUrl() + "'.", e );
}

Map<String, Object> appInfo = JSONUtil.getJsonConverter().readObject( response.toString(), Map.class );

prefs.setCustomDomain(uri.getHost());
setAppIdAndApiKey((String) appInfo.get("appId"), (String) appInfo.get("apiKey"));
setUrl(uri.toString());
BackendlessInjector.getInstance().getContextHandler().setContext(context);
initApp();
}

static void initApp( Object context, final String applicationId, final String apiKey )
private static void setAppIdAndApiKey( final String applicationId, final String apiKey )
{
if( applicationId == null || applicationId.equals( "" ) )
if( applicationId == null || applicationId.isEmpty())
throw new IllegalArgumentException( ExceptionMessage.NULL_APPLICATION_ID );

if( apiKey == null || apiKey.equals( "" ) )
if( apiKey == null || apiKey.isEmpty())
throw new IllegalArgumentException( ExceptionMessage.NULL_API_KEY );

prefs.initPreferences( applicationId, apiKey );
BackendlessInjector.getInstance().getContextHandler().setContext( context );
initApp();
}

private static void initApp()
{
if( BackendlessInjector.getInstance().isAndroid() && BackendlessInjector.getInstance().getContextHandler().getAppContext() == null )
Expand Down