-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7eb5f3
commit 800822f
Showing
19 changed files
with
604 additions
and
311 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 150 additions & 78 deletions
228
app/src/main/java/com/acuant/sampleapp/MainActivity.kt
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
app/src/main/java/com/acuant/sampleapp/backgroundtasks/AcuantTokenService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.acuant.sampleapp.backgroundtasks | ||
|
||
import android.os.AsyncTask | ||
import com.acuant.acuantcommon.model.Credential | ||
import android.util.Base64 | ||
import org.json.JSONObject | ||
import java.io.BufferedReader | ||
import java.io.InputStreamReader | ||
import java.io.OutputStreamWriter | ||
import java.io.PrintWriter | ||
import java.net.HttpURLConnection | ||
import java.net.URL | ||
import java.util.* | ||
import javax.net.ssl.HttpsURLConnection | ||
|
||
class AcuantTokenService(private val credential: Credential, | ||
private val listener: AcuantTokenServiceListener) : AsyncTask<Any?, Any?, Any?>() { | ||
private var responseText: String? = null | ||
private var responseCode = -1 | ||
private var token = "" | ||
|
||
override fun doInBackground(objects: Array<Any?>): Any? { | ||
try { | ||
val formatter = Formatter() | ||
val urlEnd = "%s/oauth/token" | ||
val urlString = formatter.format( | ||
urlEnd, credential.endpoints.acasEndpointTrimmed).toString() | ||
val url = URL(urlString) | ||
val conn = url.openConnection() as HttpURLConnection | ||
try { | ||
conn.requestMethod = "POST" | ||
val sub = if (credential.subscription != null && credential.subscription != "") "${credential.subscription};" else "" | ||
val auth = Base64.encodeToString("$sub${credential.username}:${credential.password}".toByteArray(), Base64.NO_WRAP) | ||
conn.setRequestProperty("Authorization", "Basic $auth") | ||
conn.setRequestProperty("Accept", "application/json") | ||
conn.addRequestProperty("Cache-Control", "no-cache") | ||
conn.addRequestProperty("Cache-Control", "max-age=0") | ||
conn.addRequestProperty("Content-Type", "application/json") | ||
conn.useCaches = false | ||
val webservicesTimeout = 60000 | ||
conn.readTimeout = webservicesTimeout | ||
conn.connectTimeout = webservicesTimeout | ||
val outputStream = conn.outputStream | ||
val charset = "UTF-8" | ||
PrintWriter(OutputStreamWriter(outputStream, charset), true) | ||
val settingJsonObject = JSONObject() | ||
settingJsonObject.put("grant_type", "client_credentials") | ||
val dataBytes = settingJsonObject.toString().toByteArray() | ||
outputStream.write(dataBytes, 0, dataBytes.size) | ||
outputStream.flush() | ||
responseCode = conn.responseCode | ||
responseText = conn.responseMessage | ||
val response: StringBuffer | ||
if (responseCode == HttpsURLConnection.HTTP_OK) { | ||
val `in` = BufferedReader( | ||
InputStreamReader(conn.inputStream)) | ||
var output: String? | ||
response = StringBuffer() | ||
while (`in`.readLine().also { output = it } != null) { | ||
response.append(output) | ||
} | ||
`in`.close() | ||
responseText = response.toString() | ||
val json = JSONObject(responseText) | ||
if (json.has("access_token")) { | ||
token = json.getString("access_token") | ||
} | ||
} | ||
} catch (e: Exception) { | ||
throw e | ||
} finally { | ||
conn.disconnect() | ||
} | ||
} catch (e: Exception) { | ||
responseText = e.message | ||
listener.onFail(responseCode) | ||
} | ||
return null | ||
} | ||
|
||
override fun onPostExecute(o: Any?) { | ||
super.onPostExecute(o) | ||
if (token != "") | ||
listener.onSuccess(token) | ||
else | ||
listener.onFail(-2) | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
app/src/main/java/com/acuant/sampleapp/backgroundtasks/AcuantTokenServiceListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.acuant.sampleapp.backgroundtasks | ||
|
||
interface AcuantTokenServiceListener { | ||
fun onSuccess(token: String) | ||
fun onFail(responseCode: Int) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.