This repository has been archived by the owner on May 12, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Dimdron/refactor_clients
Refactor SM and SS examples
- Loading branch information
Showing
9 changed files
with
226 additions
and
272 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -35,4 +35,4 @@ | |
</activity> | ||
</application> | ||
|
||
</manifest> | ||
</manifest> |
5 changes: 0 additions & 5 deletions
5
...id-example/app/src/main/java/com/cossacklabs/themis/android/example/CallbackListener.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
android-example/app/src/main/java/com/cossacklabs/themis/android/example/HttpCallback.java
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,8 @@ | ||
package com.cossacklabs.themis.android.example; | ||
|
||
public interface HttpCallback { | ||
|
||
void onSuccess(byte[] response); | ||
|
||
void onFail(Exception ex); | ||
} |
97 changes: 97 additions & 0 deletions
97
android-example/app/src/main/java/com/cossacklabs/themis/android/example/HttpClient.java
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,97 @@ | ||
package com.cossacklabs.themis.android.example; | ||
|
||
import android.util.Log; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.BufferedWriter; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Future; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
|
||
class HttpClient { | ||
|
||
private final ExecutorService executorService; | ||
|
||
HttpClient(ExecutorService executorService) { | ||
this.executorService = executorService; | ||
} | ||
|
||
@SuppressWarnings("WeakerAccess") | ||
byte[] sendMessage(String endpoint, String message, Charset messageCharset) throws IOException, HttpException { | ||
String query = "message=" + URLEncoder.encode(message, messageCharset.name()); | ||
|
||
java.net.URL url = new URL(endpoint); | ||
|
||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); | ||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Content-length", String.valueOf(query.length())); | ||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); | ||
connection.setDoOutput(true); | ||
connection.setDoInput(true); | ||
|
||
OutputStream outputStream = new BufferedOutputStream(connection.getOutputStream()); | ||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)); | ||
writer.write(query); | ||
writer.flush(); | ||
writer.close(); | ||
outputStream.close(); | ||
|
||
connection.connect(); | ||
|
||
Log.d("SMC", "getResponseMessage = " + connection.getResponseMessage()); | ||
final int responseCode = connection.getResponseCode(); | ||
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) { | ||
throw new HttpException(responseCode, new String(readResponse(connection.getErrorStream()))); | ||
} | ||
return readResponse(connection.getInputStream()); | ||
} | ||
|
||
@SuppressWarnings("UnusedReturnValue") | ||
Future<?> sendMessageAsync(final String endpoint, final String message, final Charset messageCharset, final HttpCallback httpCallback) { | ||
return executorService.submit(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
final byte[] response = sendMessage(endpoint, message, messageCharset); | ||
httpCallback.onSuccess(response); | ||
} catch (Exception e) { | ||
httpCallback.onFail(e); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private byte[] readResponse(InputStream inputStream) throws IOException { | ||
final ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
final byte[] buffer = new byte[1024]; | ||
int readDataLen; | ||
while ((readDataLen = inputStream.read(buffer)) != -1) { | ||
os.write(buffer, 0, readDataLen); | ||
} | ||
return os.toByteArray(); | ||
} | ||
|
||
@SuppressWarnings("WeakerAccess") | ||
public static class HttpException extends Exception { | ||
|
||
public final int code; | ||
public final String response; | ||
|
||
public HttpException(int code, String response) { | ||
super(String.format("Request failed with code %s (%s)", code, response)); | ||
this.code = code; | ||
this.response = response; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.