aboutsummaryrefslogtreecommitdiff
path: root/java/android/OlmLibSdk/olm-sdk/src/main/java
diff options
context:
space:
mode:
authorpedroGitt <pedro.contreiras@amdocs.com>2016-10-14 15:27:20 +0200
committerpedroGitt <pedro.contreiras@amdocs.com>2016-10-14 15:27:20 +0200
commit102809955026d92a3f5cf29e05998d91a992de9c (patch)
treeca8dae7dada59e283d0ccc85f2b299b49d53f4a3 /java/android/OlmLibSdk/olm-sdk/src/main/java
parent57ec6fff885e25b32e749d3e63788ff010b1fdfd (diff)
- Add inbound and outbound group sessions
- Modify constructors for inbound and outbound group sessions - Add new Ecxception class
Diffstat (limited to 'java/android/OlmLibSdk/olm-sdk/src/main/java')
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java33
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java87
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java35
3 files changed, 115 insertions, 40 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java
index 53ef7a3..aa15c32 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java
@@ -34,20 +34,34 @@ public class OlmInboundGroupSession implements Serializable {
*/
private long mNativeOlmInboundGroupSessionId;
-
- public OlmInboundGroupSession() {
- initNewSession();
- }
-
/**
* Getter on the native inbound group session ID.
* @return native inbound group session ID
*/
- public long getOlmInboundGroupSessionId(){
+ public long getOlmInboundGroupSessionId() {
return mNativeOlmInboundGroupSessionId;
}
/**
+ * Constructor.<br>
+ * Create and save a new native session instance ID and start a new inbound group session.
+ * The session key parameter is retrieved from a outbound group session
+ * See {@link #initNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)}
+ * @param aSessionKey session key
+ * @throws OlmUtilsException
+ */
+ public OlmInboundGroupSession(String aSessionKey) throws OlmUtilsException {
+ if(initNewSession()) {
+ if( 0 != initInboundGroupSessionWithSessionKey(aSessionKey)) {
+ releaseSession();// prevent memory leak before throwing
+ throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION);
+ }
+ } else {
+ throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE);
+ }
+ }
+
+ /**
* Release native session and invalid its JAVA reference counter part.<br>
* Public API for {@link #releaseSessionJni()}.
* To be called before any other API call.
@@ -88,12 +102,13 @@ public class OlmInboundGroupSession implements Serializable {
private native long initNewSessionJni();
/**
- * Creates a new inbound group session.<br>
- * The session key parameter is retrieved from a outbound group session.
+ * Start a new inbound group session.<br>
+ * The session key parameter is retrieved from a outbound group session
+ * see {@link OlmOutboundGroupSession#sessionKey()}
* @param aSessionKey session key
* @return 0 if operation succeed, -1 otherwise
*/
- public int initInboundGroupSessionWithSessionKey(String aSessionKey) {
+ private int initInboundGroupSessionWithSessionKey(String aSessionKey) {
int retCode = -1;
if(TextUtils.isEmpty(aSessionKey)){
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java
index 63c0c36..bbe2718 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java
@@ -28,16 +28,30 @@ public class OlmOutboundGroupSession {
*/
private long mNativeOlmOutboundGroupSessionId;
- public OlmOutboundGroupSession() {
- initNewSession();
- }
-
/**
* Getter on the native outbound group session ID.
* @return native outbound group session ID
*/
public long getOlmInboundGroupSessionId(){
- return mNativeOlmInboundGroupSessionId;
+ return mNativeOlmOutboundGroupSessionId;
+ }
+
+ /**
+ * Constructor.<br>
+ * Create and save a new session native instance ID and
+ * initialise a new outbound group session.<br>
+ * See {@link #initNewSession()} and {@link #initOutboundGroupSession()}
+ * @throws OlmUtilsException
+ */
+ public OlmOutboundGroupSession() throws OlmUtilsException {
+ if(initNewSession()) {
+ if( 0 != initOutboundGroupSession()) {
+ releaseSession();// prevent memory leak before throwing
+ throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION);
+ }
+ } else {
+ throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE);
+ }
}
/**
@@ -45,9 +59,8 @@ public class OlmOutboundGroupSession {
* Public API for {@link #releaseSessionJni()}.
* To be called before any other API call.
*/
- public void releaseSession(){
- releaseSessionJni();
-
+ public void releaseSession() {
+ releaseSessionJni();
mNativeOlmOutboundGroupSessionId = 0;
}
@@ -80,54 +93,66 @@ public class OlmOutboundGroupSession {
*/
private native long initNewSessionJni();
-
/**
- * Creates a new outbound group session.<br>
- * The session key parameter is retrieved from a outbound group session.
+ * Start a new outbound group session.<br>
* @return 0 if operation succeed, -1 otherwise
*/
- public int initOutboundGroupSession() {
+ private int initOutboundGroupSession() {
return initOutboundGroupSessionJni();
}
- public native int initOutboundGroupSessionJni();
-
-
-
+ private native int initOutboundGroupSessionJni();
+ /**
+ * Get a base64-encoded identifier for this session.
+ * @return session identifier if operation succeed, null otherwise.
+ */
public String sessionIdentifier() {
String retValue = null;
- //retValue = sessionIdentifierJni();
+ retValue = sessionIdentifierJni();
return retValue;
}
- public native String sessionIdentifierJni();
-
+ private native String sessionIdentifierJni();
-
-
- public long messageIndex() {
- long retValue =0;
- //retValue = messageIndexJni();
+ /**
+ * Get the current message index for this session.<br>
+ * Each message is sent with an increasing index, this
+ * method returns the index for the next message.
+ * @return current session index
+ */
+ public int messageIndex() {
+ int retValue =0;
+ retValue = messageIndexJni();
return retValue;
}
- private native long messageIndexJni();
-
-
-
+ private native int messageIndexJni();
+ /**
+ * Get the base64-encoded current ratchet key for this session.<br>
+ * Each message is sent with a different ratchet key. This method returns the
+ * ratchet key that will be used for the next message.
+ * @return outbound session key
+ */
public String sessionKey() {
String retValue = null;
- //retValue = sessionKeyJni();
+ retValue = sessionKeyJni();
return retValue;
}
private native String sessionKeyJni();
-
+ /**
+ * Encrypt some plain-text message.<br>
+ * @param aClearMsg message to be encrypted
+ * @return the encrypted message if operation succeed, null otherwise
+ */
public String encryptMessage(String aClearMsg) {
String retValue = null;
- //retValue = encryptMessageJni(aClearMsg);
+
+ if(!TextUtils.isEmpty(aClearMsg)) {
+ retValue = encryptMessageJni(aClearMsg);
+ }
return retValue;
}
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java
new file mode 100644
index 0000000..f0cdc83
--- /dev/null
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2016 OpenMarket Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.matrix.olm;
+
+public class OlmUtilsException extends Exception {
+ // exception codes
+ public static final int EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE = 0;
+ public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 1;
+ public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 2;
+
+ private final int mCode;
+
+ public OlmUtilsException(int aExceptionCode) {
+ super();
+ mCode = aExceptionCode;
+ }
+
+ public int getExceptionCode() {
+ return mCode;
+ }
+}