Skip to content

Commit 487a07e

Browse files
committed
Add error messages in all methods of the Key class
1 parent 4c253de commit 487a07e

File tree

5 files changed

+93
-17
lines changed

5 files changed

+93
-17
lines changed

src/main/java/io/cryptolens/internal/HelperMethods.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.google.gson.*;
44
import io.cryptolens.legacy.HttpsURLConnectionRequestHandler;
55
import io.cryptolens.legacy.RequestHandler;
6+
import io.cryptolens.models.APIError;
7+
import io.cryptolens.models.ErrorType;
68

79
import java.lang.reflect.Field;
810
import java.lang.reflect.Type;
@@ -15,6 +17,10 @@
1517
public class HelperMethods {
1618

1719
public static <T extends BasicResult> T SendRequestToWebAPI(String method, Object model, Map<String,String> extraParams, Class<T> clazz) {
20+
return SendRequestToWebAPI(method, model, extraParams, clazz, null);
21+
}
22+
23+
public static <T extends BasicResult> T SendRequestToWebAPI(String method, Object model, Map<String,String> extraParams, Class<T> clazz, APIError error) {
1824

1925
Map<String,String> params = new HashMap<>();
2026
List<Field> allFields = new ArrayList<>();
@@ -28,7 +34,10 @@ public static <T extends BasicResult> T SendRequestToWebAPI(String method, Objec
2834
params.put(field.getName(), value.toString());
2935
}
3036
} catch (Exception ex) {
31-
System.err.println(ex.getStackTrace());
37+
if(error != null) {
38+
error.errorType = ErrorType.LibraryError;
39+
error.message = ex.getMessage();
40+
}
3241
}
3342
}
3443

@@ -55,7 +64,10 @@ public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializatio
5564
return res;
5665

5766
} catch (Exception ex) {
58-
System.err.println(ex.getStackTrace());
67+
if(error != null) {
68+
error.errorType = ErrorType.LibraryError;
69+
error.message = ex.toString();
70+
}
5971
}
6072

6173
return null;

src/main/java/io/cryptolens/methods/Key.java

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel
5454
* <code>
5555
* APIError error = new APIError();<br>
5656
* LicenseKey license = Key.Activate(auth, RSAPubKey, new ActivateModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL", Helpers.GetMachineCode()), error);<br>
57-
* System.out.println(error.Message);
57+
* System.out.println(error.message);
5858
* </code>
5959
* @param token The access token with 'Activate' permission.
6060
* @param RSAPubKey Your RSA Public Key, which can be found at https://app.cryptolens.io/docs/api/v3/QuickStart.
@@ -73,16 +73,18 @@ public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel
7373
extraParams.put("SignMethod", "1");
7474
extraParams.put("token", token);
7575

76-
ActivateResult result = HelperMethods.SendRequestToWebAPI("key/activate", model, extraParams, ActivateResult.class);
76+
ActivateResult result = HelperMethods.SendRequestToWebAPI("key/activate", model, extraParams, ActivateResult.class, error);
7777

7878
if(result == null || result.result == 1) {
7979
if(result != null) {
8080
if (error != null) {
81-
error.Message = result.message;
81+
error.message = result.message;
82+
error.errorType = ErrorType.WebAPIError;
8283
}
83-
System.err.println("The server returned an error: " + result.message);
8484
} else {
85-
System.err.println("The server returned an error.");
85+
if (error != null) {
86+
error.errorType = ErrorType.WebAPIError;
87+
}
8688
}
8789
return null;
8890
}
@@ -99,17 +101,37 @@ public static LicenseKey Activate (String token, String RSAPubKey, ActivateModel
99101
* @return True if deactivation succeeded and false otherwise.
100102
*/
101103
public static boolean Deactivate (String token, DeactivateModel model) {
104+
return Deactivate(token, model, null);
105+
}
106+
107+
/**
108+
* This method will 'undo' a key activation with a certain machine code.
109+
* The key should not be blocked, since otherwise this method will throw an error.
110+
* More info: https://app.cryptolens.io/docs/api/v3/Deactivate
111+
* @param token The access token with 'Deactivate' permission.
112+
* @param model Method parameters.
113+
* @param error The error object whose Message field will be populated if an error has occurred. Please initialize
114+
* this parameter, i.e. define <code>APIError error = new APIError();</code> and then pass
115+
* <code>error</code> into this parameter.
116+
* @return True if deactivation succeeded and false otherwise.
117+
*/
118+
public static boolean Deactivate (String token, DeactivateModel model, APIError error) {
102119

103120
Map<String,String> extraParams = new HashMap<>();
104121
extraParams.put("token", token);
105122

106-
BasicResult result = HelperMethods.SendRequestToWebAPI("key/deactivate", model, extraParams, BasicResult.class);
123+
BasicResult result = HelperMethods.SendRequestToWebAPI("key/deactivate", model, extraParams, BasicResult.class, error);
107124

108125
if(result == null || result.result == 1) {
109126
if(result != null) {
110-
System.err.println("The server returned an error: " + result.message);
127+
if (error != null) {
128+
error.message = result.message;
129+
error.errorType = ErrorType.WebAPIError;
130+
}
111131
} else {
112-
System.err.println("The server returned an error.");
132+
if (error != null) {
133+
error.errorType = ErrorType.WebAPIError;
134+
}
113135
}
114136
return false;
115137
}
@@ -132,9 +154,30 @@ public static boolean Deactivate (String token, DeactivateModel model) {
132154
* @return A new license key or error message.
133155
*/
134156
public static CreateKeyResult CreateTrialKey(String token, CreateTrialKeyModel model) {
157+
return CreateTrialKey(token, model, null);
158+
}
159+
160+
/**
161+
* <p>This method creates a license key that is time-limited, <a href="https://help.cryptolens.io/licensing-models/node-locked" target="_blank">node-locked</a>
162+
* and with the "Time-Limited" and "Trial" features set to true (which can be set by editing the
163+
* <a href="https://help.cryptolens.io/web-interface/feature-definitions" target="_blank">feature definitions</a> on the product page).
164+
* Note, by default, the trial will work for 15 days. To change this limit, you can set the <b>Feature Lock</b>
165+
* to the desired value, when creating the access token.</p>
166+
*
167+
* <p>If a trial key was already created for a certain machine code, this method will try to find the license
168+
* key and return it instead. However, this will only occur if the license key is still a trial key (based
169+
* on feature definitions) and is not blocked.</p>
170+
* @param token The access token with 'CreateTrialKey' permission.
171+
* @param model Method parameters.
172+
* @param error The error object whose Message field will be populated if an error has occurred. Please initialize
173+
* this parameter, i.e. define <code>APIError error = new APIError();</code> and then pass
174+
* <code>error</code> into this parameter.
175+
* @return A new license key or error message.
176+
*/
177+
public static CreateKeyResult CreateTrialKey(String token, CreateTrialKeyModel model, APIError error) {
135178
Map<String,String> extraParams = new HashMap<>();
136179
extraParams.put("token", token);
137180

138-
return HelperMethods.SendRequestToWebAPI("key/createtrialkey", model, extraParams, CreateKeyResult.class);
181+
return HelperMethods.SendRequestToWebAPI("key/createtrialkey", model, extraParams, CreateKeyResult.class, error);
139182
}
140183
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package io.cryptolens.models;
22

33
/**
4-
* Used to store the error message obtained from the Web API.
4+
* Used to store the error message obtained from the Web API or the client library
5+
* (eg. in case there is a network error).
56
*/
67
public class APIError {
7-
public String Message;
88

9-
public APIError() { Message = ""; }
9+
public ErrorType errorType;
10+
public String message;
1011

11-
public APIError(String message) {
12-
Message = message;
12+
public APIError() { message = ""; }
13+
14+
public APIError(String message, ErrorType errorType) {
15+
this.message = message;
16+
this.errorType = errorType;
1317
}
1418
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.cryptolens.models;
2+
3+
public enum ErrorType {
4+
NotSpecified (0),
5+
WebAPIError (1),
6+
LibraryError (2);
7+
8+
public final int error;
9+
10+
ErrorType (final int errorValue) {
11+
error = errorValue;
12+
}
13+
14+
public int getError() {
15+
return error;
16+
}
17+
}

src/test/java/io/cryptolens/KeyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void testApp() throws Exception {
4848

4949
if (license == null || !Helpers.IsOnRightMachine(license)) {
5050
System.out.println("The license does not work.");
51-
System.out.println("Error: " + error.Message);
51+
System.out.println("Error: " + error.message);
5252
} else {
5353
System.out.println("The license is valid!");
5454
System.out.println("It will expire: " + license.Expires);

0 commit comments

Comments
 (0)