Skip to content

Commit 0cc0cd1

Browse files
authored
Add support for the license server (#21)
* Add license server url to all API models * Update HelperMethods.java * Update
1 parent 111f8cb commit 0cc0cd1

File tree

12 files changed

+100
-16
lines changed

12 files changed

+100
-16
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,40 @@ public static void main(String args[]) {
123123
}
124124
}
125125
```
126+
127+
### Calling through the license server
128+
If you would like to re-route the requests through our [license-server](https://github.com/cryptolens/license-server) that is installed on the client site, you can specify its url using `LicenseServerUrl`
129+
parameter. All API models expose this parameter.
130+
131+
For example, let's suppose that your client runs the license server on `http://10.1.1.6:8080` and you want to call `Key.GetKey()`. In this case, we first define all parameters for the request and then
132+
modify the license server url:
133+
134+
```java
135+
GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
136+
model.LicenseServerUrl = "http://10.1.1.6:8080";
137+
```
138+
139+
We do this because there is currently no overload method that accepts `LicenseServerUrl` parameter.
140+
141+
The entire code is shown below:
142+
143+
```java
144+
String RSAPubKey = "<RSAKeyValue><Modulus>sGbvxwdlDbqFXOMlVUnAF5ew0t0WpPW7rFpI5jHQOFkht/326dvh7t74RYeMpjy357NljouhpTLA3a6idnn4j6c3jmPWBkjZndGsPL4Bqm+fwE48nKpGPjkj4q/yzT4tHXBTyvaBjA8bVoCTn+LiC4XEaLZRThGzIn5KQXKCigg6tQRy0GXE13XYFVz/x1mjFbT9/7dS8p85n8BuwlY5JvuBIQkKhuCNFfrUxBWyu87CFnXWjIupCD2VO/GbxaCvzrRjLZjAngLCMtZbYBALksqGPgTUN7ZM24XbPWyLtKPaXF2i4XRR9u6eTj5BfnLbKAU5PIVfjIS+vNYYogteQ==<Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
145+
146+
String auth = APIKey.get("getkeyactivate");
147+
148+
APIError error = new APIError();
149+
150+
GetKeyModel model = new GetKeyModel(3349, "ICVLD-VVSZR-ZTICT-YKGXL");
151+
model.LicenseServerUrl = "http://10.1.1.6:8080";
152+
153+
LicenseKey license = Key.GetKey(auth, RSAPubKey, model , error);
154+
155+
if (license == null) {
156+
System.out.println("The license does not work.");
157+
System.out.println("Error: " + error.message);
158+
} else {
159+
System.out.println("The license is valid!");
160+
System.out.println("It will expire: " + license.Expires);
161+
}
162+
```

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.cryptolens.legacy.RequestHandler;
66
import io.cryptolens.models.APIError;
77
import io.cryptolens.models.ErrorType;
8+
import io.cryptolens.models.RequestModel;
89

910
import java.lang.reflect.Field;
1011
import java.lang.reflect.Type;
@@ -16,22 +17,29 @@
1617

1718
public class HelperMethods {
1819

19-
public static <T extends BasicResult> T SendRequestToWebAPI(String method, Object model, Map<String,String> extraParams, Class<T> clazz) {
20+
public static <T extends BasicResult> T SendRequestToWebAPI(String method, RequestModel model, Map<String,String> extraParams, Class<T> clazz) {
2021
return SendRequestToWebAPI(method, model, extraParams, clazz, null);
2122
}
2223

23-
public static <T extends BasicResult> T SendRequestToWebAPI(String method, Object model, Map<String,String> extraParams, Class<T> clazz, APIError error) {
24+
public static <T extends BasicResult> T SendRequestToWebAPI(String method, RequestModel model, Map<String,String> extraParams, Class<T> clazz, APIError error) {
2425

2526
Map<String,String> params = new HashMap<>();
2627
List<Field> allFields = new ArrayList<>();
2728
getAllFields(allFields, model.getClass());
2829

30+
String licenseServerUrl = "https://app.cryptolens.io";
31+
2932
for(Field field : allFields) {
3033
field.setAccessible(true);
3134
try {
3235
Object value = field.get(model);
3336
if(value != null) {
34-
params.put(field.getName(), value.toString());
37+
38+
if(field.getName() == "LicenseServerUrl") {
39+
licenseServerUrl = value.toString();
40+
} else {
41+
params.put(field.getName(), value.toString());
42+
}
3543
}
3644
} catch (Exception ex) {
3745
if(error != null) {
@@ -48,7 +56,7 @@ public static <T extends BasicResult> T SendRequestToWebAPI(String method, Objec
4856

4957
try {
5058

51-
String response = requestHandler.makePostRequest("https://app.cryptolens.io/api/" + method, params);
59+
String response = requestHandler.makePostRequest(licenseServerUrl + "/api/" + method, params);
5260

5361
Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
5462
@Override

src/main/java/io/cryptolens/legacy/HttpsURLConnectionRequestHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
public class HttpsURLConnectionRequestHandler implements RequestHandler {
1010
public String makePostRequest(String url, Map<String,String> params) throws Exception {
1111
URL url_ = new URL(url);
12-
HttpsURLConnection connection = (HttpsURLConnection)url_.openConnection();
12+
13+
// HttpsURLConnection extends HttpURLConnection
14+
HttpURLConnection connection = (HttpURLConnection)url_.openConnection();
1315
connection.setRequestMethod("POST");
1416

1517
connection.setDoOutput(true);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.cryptolens.internal.*;
44
import io.cryptolens.models.GetProductsResult;
5+
import io.cryptolens.models.RequestModel;
6+
57
import java.util.*;
68

79
/**
@@ -19,6 +21,6 @@ public static GetProductsResult GetProducts (String token) {
1921
Map<String, String> extraParams = new HashMap<>();
2022
extraParams.put("token", token);
2123

22-
return HelperMethods.SendRequestToWebAPI("product/GetProducts", new Object(), extraParams, GetProductsResult.class);
24+
return HelperMethods.SendRequestToWebAPI("product/GetProducts", new RequestModel(), extraParams, GetProductsResult.class);
2325
}
2426
}

src/main/java/io/cryptolens/models/ActivateModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* The parameters that are used when calling the Key.Activate method.
55
*/
6-
public class ActivateModel {
6+
public class ActivateModel extends RequestModel {
77

88
/**
99
* The product id, which can be found on the product page.
@@ -48,7 +48,7 @@ public class ActivateModel {
4848
public int MaxOverdraft;
4949

5050
/**
51-
* Allows you to specify a friendy name for the activated device, for example the employee's email.
51+
* Allows you to specify a friendly name for the activated device, for example the employee's email.
5252
* Friendly name does not impact the number of active machine codes / seats, but it offers an easy way
5353
* of linking a machine/seat with a user. For added security, you can HMAC hash this value.
5454
* <br/>

src/main/java/io/cryptolens/models/CreateTrialKeyModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.cryptolens.models;
22

3-
public class CreateTrialKeyModel {
3+
public class CreateTrialKeyModel extends RequestModel {
44
public int ProductId;
55
public String MachineCode;
66

src/main/java/io/cryptolens/models/DeactivateModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.cryptolens.models;
22

3-
public class DeactivateModel {
3+
public class DeactivateModel extends RequestModel {
44

55
public int ProductId;
66
public String Key;

src/main/java/io/cryptolens/models/GetKeyModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* The parameters that are used when calling the Key.GetKey method.
55
*/
66

7-
public class GetKeyModel {
7+
public class GetKeyModel extends RequestModel {
88
/**
99
* The product id, which can be found on the product page.
1010
*/

src/main/java/io/cryptolens/models/ProductAndKeyModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.cryptolens.models;
22

3-
public abstract class ProductAndKeyModel {
3+
public abstract class ProductAndKeyModel extends RequestModel{
44
/**
55
* The product id.
66
*/

src/main/java/io/cryptolens/models/RecordUsageModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.cryptolens.models;
22

3-
public class RecordUsageModel {
3+
public class RecordUsageModel extends RequestModel {
44
public int ProductId;
55
public String Key;
66
public int Amount;

0 commit comments

Comments
 (0)