From 6c16d562bcdf7313ba2cb95e5664fbfbf13a6de6 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Tue, 14 Oct 2014 14:07:21 -0400 Subject: [PATCH] restructure package for trial Original commit: elastic/x-pack-elasticsearch@0ae53fab2e4f0e6a608d67bf22b9ed68c4d9ff1e --- .../license/plugin/action/Utils.java | 103 ---------- .../plugin/action/get/GetLicenseResponse.java | 9 +- .../license/plugin/core/LicensesMetaData.java | 10 +- .../license/plugin/core/LicensesService.java | 6 +- .../plugin/core/trial/TrialLicenseUtils.java | 181 ++++++++++++++++++ .../core/{ => trial}/TrialLicenses.java | 2 +- .../{ => trial}/TrialLicensesBuilder.java | 67 +------ .../plugin/rest/RestGetLicenseAction.java | 4 +- 8 files changed, 201 insertions(+), 181 deletions(-) create mode 100644 src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java rename src/main/java/org/elasticsearch/license/plugin/core/{ => trial}/TrialLicenses.java (93%) rename src/main/java/org/elasticsearch/license/plugin/core/{ => trial}/TrialLicensesBuilder.java (68%) diff --git a/src/main/java/org/elasticsearch/license/plugin/action/Utils.java b/src/main/java/org/elasticsearch/license/plugin/action/Utils.java index db599f2086e..71f98b715ca 100644 --- a/src/main/java/org/elasticsearch/license/plugin/action/Utils.java +++ b/src/main/java/org/elasticsearch/license/plugin/action/Utils.java @@ -11,18 +11,12 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.LicenseBuilders; -import org.elasticsearch.license.plugin.core.TrialLicenses; -import org.elasticsearch.license.plugin.core.TrialLicensesBuilder; import java.io.IOException; import java.util.*; import static org.elasticsearch.license.core.ESLicenses.*; import static org.elasticsearch.license.manager.Utils.getESLicenseFromSignature; -import static org.elasticsearch.license.plugin.core.TrialLicenses.TrialLicense; -import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.fromEncodedTrialLicense; -import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.toEncodedTrialLicense; -import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.trialLicensesBuilder; public class Utils { @@ -40,41 +34,6 @@ public class Utils { } } - public static TrialLicenses readTrialLicensesFromMetaData(StreamInput in) throws IOException { - boolean exists = in.readBoolean(); - return exists ? fromEncodedTrialLicenses(in.readStringArray()) : null; - - } - - public static void writeTrialLicensesToMetaData(TrialLicenses trialLicenses, StreamOutput out) throws IOException { - if (trialLicenses == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeStringArray(toEncodedTrialLicenses(trialLicenses)); - } - } - - public static String[] toEncodedTrialLicenses(TrialLicenses trialLicenses) { - Set encodedTrialLicenses = new HashSet<>(); - for (TrialLicense trialLicense : trialLicenses) { - encodedTrialLicenses.add(toEncodedTrialLicense(trialLicense)); - } - return encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()]); - } - - public static TrialLicenses fromEncodedTrialLicenses(String[] encodedTrialLicenses) { - final TrialLicensesBuilder trialLicensesBuilder = trialLicensesBuilder(); - for (String encodedTrialLicense : encodedTrialLicenses) { - trialLicensesBuilder.license(fromEncodedTrialLicense(encodedTrialLicense)); - } - return trialLicensesBuilder.build(); - } - - public static TrialLicenses fromEncodedTrialLicenses(Set encodedTrialLicenses) { - return fromEncodedTrialLicenses(encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()])); - } - public static String[] toSignatures(ESLicenses esLicenses) { Set signatures = new HashSet<>(); for (ESLicense esLicense : esLicenses) { @@ -120,57 +79,6 @@ public class Utils { } } - public static TrialLicenses readTrialLicensesFrom(StreamInput in) throws IOException { - final TrialLicensesBuilder licensesBuilder = TrialLicensesBuilder.trialLicensesBuilder(); - boolean exists = in.readBoolean(); - if (exists) { - int size = in.readVInt(); - for (int i = 0; i < size; i++) { - licensesBuilder.license(trialLicenseFromMap(in.readMap())); - } - return licensesBuilder.build(); - } - return null; - } - - public static void writeTrialLicensesTo(TrialLicenses trialLicenses, StreamOutput out) throws IOException { - if (trialLicenses == null) { - out.writeBoolean(false); - return; - } - out.writeBoolean(true); - out.writeVInt(trialLicenses.trialLicenses().size()); - for (TrialLicense trialLicense : trialLicenses) { - out.writeMap(trialLicenseAsMap(trialLicense)); - } - } - - // TODO: make sure field order is preserved - public static Map trialLicenseAsMap(TrialLicense trialLicense) { - ImmutableMap.Builder builder = ImmutableMap.builder(); - builder.put(TrialLicenseFields.UID, trialLicense.uid()); - builder.put(TrialLicenseFields.TYPE, Type.TRIAL.string()); - builder.put(TrialLicenseFields.SUBSCRIPTION_TYPE, SubscriptionType.NONE.string()); - builder.put(TrialLicenseFields.ISSUE_DATE, trialLicense.issueDate()); - builder.put(TrialLicenseFields.FEATURE, trialLicense.feature().string()); - builder.put(TrialLicenseFields.EXPIRY_DATE, trialLicense.expiryDate()); - builder.put(TrialLicenseFields.MAX_NODES, trialLicense.maxNodes()); - builder.put(TrialLicenseFields.ISSUED_TO, trialLicense.issuedTo()); - return builder.build(); - } - - public static TrialLicense trialLicenseFromMap(Map map) { - return TrialLicensesBuilder.trialLicenseBuilder() - .uid((String) map.get(TrialLicenseFields.UID)) - .issuedTo((String) map.get(TrialLicenseFields.ISSUED_TO)) - .maxNodes((int) map.get(TrialLicenseFields.MAX_NODES)) - .feature(FeatureType.fromString((String) map.get(TrialLicenseFields.FEATURE))) - .issueDate((long) map.get(TrialLicenseFields.ISSUE_DATE)) - .expiryDate((long) map.get(TrialLicenseFields.EXPIRY_DATE)) - .build(); - - } - // TODO: make sure field order is preserved public static Map licenseAsMap(ESLicense esLicense) { ImmutableMap.Builder builder = ImmutableMap.builder(); @@ -201,17 +109,6 @@ public class Utils { } - final static class TrialLicenseFields { - private final static String UID = "uid"; - private final static String TYPE = "type"; - private final static String SUBSCRIPTION_TYPE = "subscription_type"; - private final static String ISSUE_DATE = "issue_date"; - private final static String FEATURE = "feature"; - private final static String ISSUED_TO = "issued_to"; - private final static String MAX_NODES = "max_nodes"; - private final static String EXPIRY_DATE = "expiry_date"; - } - final static class LicenseFields { private final static String UID = "uid"; private final static String TYPE = "type"; diff --git a/src/main/java/org/elasticsearch/license/plugin/action/get/GetLicenseResponse.java b/src/main/java/org/elasticsearch/license/plugin/action/get/GetLicenseResponse.java index 63e0f864807..6469808de1f 100644 --- a/src/main/java/org/elasticsearch/license/plugin/action/get/GetLicenseResponse.java +++ b/src/main/java/org/elasticsearch/license/plugin/action/get/GetLicenseResponse.java @@ -10,8 +10,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.core.LicenseBuilders; -import org.elasticsearch.license.plugin.core.TrialLicenses; -import org.elasticsearch.license.plugin.core.TrialLicensesBuilder; +import org.elasticsearch.license.plugin.core.trial.TrialLicenseUtils; +import org.elasticsearch.license.plugin.core.trial.TrialLicenses; +import org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder; import java.io.IOException; @@ -42,14 +43,14 @@ public class GetLicenseResponse extends ActionResponse { public void readFrom(StreamInput in) throws IOException { super.readFrom(in); licenses = readGeneratedLicensesFrom(in); - trialLicenses = readTrialLicensesFrom(in); + trialLicenses = TrialLicenseUtils.readTrialLicensesFrom(in); } @Override public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); writeGeneratedLicensesTo(licenses, out); - writeTrialLicensesTo(trialLicenses, out); + TrialLicenseUtils.writeTrialLicensesTo(trialLicenses, out); } } \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java b/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java index 90bf5b6a3a2..a5589c84723 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/LicensesMetaData.java @@ -12,6 +12,8 @@ import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.license.core.ESLicenses; +import org.elasticsearch.license.plugin.core.trial.TrialLicenseUtils; +import org.elasticsearch.license.plugin.core.trial.TrialLicenses; import java.io.IOException; import java.util.*; @@ -73,7 +75,7 @@ public class LicensesMetaData implements MetaData.Custom { TrialLicenses trialLicenses = null; if (in.readBoolean()) { esLicenses = readGeneratedLicensesFromMetaData(in); - trialLicenses = readTrialLicensesFromMetaData(in); + trialLicenses = TrialLicenseUtils.readTrialLicensesFromMetaData(in); } return new LicensesMetaData(esLicenses, trialLicenses); } @@ -88,7 +90,7 @@ public class LicensesMetaData implements MetaData.Custom { } else { out.writeBoolean(true); writeGeneratedLicensesToMetaData(licensesMetaData.getLicenses(), out); - writeTrialLicensesToMetaData(licensesMetaData.getTrialLicenses(), out); + TrialLicenseUtils.writeTrialLicensesToMetaData(licensesMetaData.getTrialLicenses(), out); } } @@ -120,7 +122,7 @@ public class LicensesMetaData implements MetaData.Custom { } } - return new LicensesMetaData(fromSignatures(signatures), fromEncodedTrialLicenses(encodedTrialLicenses)); + return new LicensesMetaData(fromSignatures(signatures), TrialLicenseUtils.fromEncodedTrialLicenses(encodedTrialLicenses)); } /** @@ -130,7 +132,7 @@ public class LicensesMetaData implements MetaData.Custom { public void toXContent(LicensesMetaData licensesMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(); builder.array(Fields.LICENSES, toSignatures(licensesMetaData.getLicenses())); - builder.array(Fields.TRIAL_LICENSES, toEncodedTrialLicenses(licensesMetaData.getTrialLicenses())); + builder.array(Fields.TRIAL_LICENSES, TrialLicenseUtils.toEncodedTrialLicenses(licensesMetaData.getTrialLicenses())); builder.endObject(); } diff --git a/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java b/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java index 4a82241b651..fa6556863e5 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/LicensesService.java @@ -21,13 +21,15 @@ import org.elasticsearch.license.core.LicenseBuilders; import org.elasticsearch.license.manager.ESLicenseManager; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequest; import org.elasticsearch.license.plugin.action.put.PutLicenseRequest; +import org.elasticsearch.license.plugin.core.trial.TrialLicenses; +import org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder; import java.util.HashSet; import java.util.Set; import static org.elasticsearch.license.core.ESLicenses.FeatureType; -import static org.elasticsearch.license.plugin.core.TrialLicenses.TrialLicense; -import static org.elasticsearch.license.plugin.core.TrialLicensesBuilder.EMPTY; +import static org.elasticsearch.license.plugin.core.trial.TrialLicenses.TrialLicense; +import static org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder.EMPTY; /** * Service responsible for maintaining and providing access to licenses on nodes. diff --git a/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java new file mode 100644 index 00000000000..098d101211e --- /dev/null +++ b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenseUtils.java @@ -0,0 +1,181 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.license.plugin.core.trial; + +import org.apache.commons.codec.binary.Base64; +import org.elasticsearch.common.collect.ImmutableMap; +import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.license.core.ESLicenses; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder.trialLicensesBuilder; + +public class TrialLicenseUtils { + + public static TrialLicenses.TrialLicense fromEncodedTrialLicense(String encodedTrialLicense) { + byte[] encodedBytes = Base64.decodeBase64(encodedTrialLicense); + ByteBuffer byteBuffer = ByteBuffer.wrap(encodedBytes); + + int uidLen = byteBuffer.getInt(); + byte[] uidBytes = new byte[uidLen]; + byteBuffer.get(uidBytes); + String uid = new String(uidBytes, Charset.forName("UTF-8")); + + int issuedToLen = byteBuffer.getInt(); + byte[] issuedToBytes = new byte[issuedToLen]; + byteBuffer.get(issuedToBytes); + String issuedTo = new String(issuedToBytes, Charset.forName("UTF-8")); + + int featureLen = byteBuffer.getInt(); + byte[] featureBytes = new byte[featureLen]; + byteBuffer.get(featureBytes); + String feature = new String(featureBytes, Charset.forName("UTF-8")); + + int maxNodes = byteBuffer.getInt(); + long issueDate = byteBuffer.getLong(); + long expiryDate = byteBuffer.getLong(); + + return TrialLicensesBuilder.trialLicenseBuilder() + .uid(uid) + .issuedTo(issuedTo) + .feature(ESLicenses.FeatureType.fromString(feature)) + .maxNodes(maxNodes) + .issueDate(issueDate) + .expiryDate(expiryDate) + .build(); + } + + public static String toEncodedTrialLicense(TrialLicenses.TrialLicense trialLicense) { + byte[] uidBytes = trialLicense.uid().getBytes(Charset.forName("UTF-8")); + String feature = trialLicense.feature().string(); + byte[] featureBytes = feature.getBytes(Charset.forName("UTF-8")); + byte[] issuedToBytes = trialLicense.issuedTo().getBytes(Charset.forName("UTF-8")); + + // uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate + int len = 4 + uidBytes.length + issuedToBytes.length + featureBytes.length + 4 + 8 + 8; + final byte[] encodedLicense = new byte[len]; + ByteBuffer byteBuffer = ByteBuffer.wrap(encodedLicense); + + byteBuffer.putInt(uidBytes.length); + byteBuffer.put(uidBytes); + + byteBuffer.putInt(issuedToBytes.length); + byteBuffer.put(issuedToBytes); + + byteBuffer.putInt(featureBytes.length); + byteBuffer.put(featureBytes); + + byteBuffer.putInt(trialLicense.maxNodes()); + byteBuffer.putLong(trialLicense.issueDate()); + byteBuffer.putLong(trialLicense.expiryDate()); + + return Base64.encodeBase64String(encodedLicense); + } + + public static TrialLicenses readTrialLicensesFromMetaData(StreamInput in) throws IOException { + boolean exists = in.readBoolean(); + return exists ? fromEncodedTrialLicenses(in.readStringArray()) : null; + + } + + public static void writeTrialLicensesToMetaData(TrialLicenses trialLicenses, StreamOutput out) throws IOException { + if (trialLicenses == null) { + out.writeBoolean(false); + } else { + out.writeBoolean(true); + out.writeStringArray(toEncodedTrialLicenses(trialLicenses)); + } + } + + public static String[] toEncodedTrialLicenses(TrialLicenses trialLicenses) { + Set encodedTrialLicenses = new HashSet<>(); + for (TrialLicenses.TrialLicense trialLicense : trialLicenses) { + encodedTrialLicenses.add(toEncodedTrialLicense(trialLicense)); + } + return encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()]); + } + + public static TrialLicenses fromEncodedTrialLicenses(String[] encodedTrialLicenses) { + final TrialLicensesBuilder trialLicensesBuilder = trialLicensesBuilder(); + for (String encodedTrialLicense : encodedTrialLicenses) { + trialLicensesBuilder.license(fromEncodedTrialLicense(encodedTrialLicense)); + } + return trialLicensesBuilder.build(); + } + + public static TrialLicenses fromEncodedTrialLicenses(Set encodedTrialLicenses) { + return fromEncodedTrialLicenses(encodedTrialLicenses.toArray(new String[encodedTrialLicenses.size()])); + } + + public static TrialLicenses readTrialLicensesFrom(StreamInput in) throws IOException { + final TrialLicensesBuilder licensesBuilder = TrialLicensesBuilder.trialLicensesBuilder(); + boolean exists = in.readBoolean(); + if (exists) { + int size = in.readVInt(); + for (int i = 0; i < size; i++) { + licensesBuilder.license(trialLicenseFromMap(in.readMap())); + } + return licensesBuilder.build(); + } + return null; + } + + public static void writeTrialLicensesTo(TrialLicenses trialLicenses, StreamOutput out) throws IOException { + if (trialLicenses == null) { + out.writeBoolean(false); + return; + } + out.writeBoolean(true); + out.writeVInt(trialLicenses.trialLicenses().size()); + for (TrialLicenses.TrialLicense trialLicense : trialLicenses) { + out.writeMap(trialLicenseAsMap(trialLicense)); + } + } + + // TODO: make sure field order is preserved + public static Map trialLicenseAsMap(TrialLicenses.TrialLicense trialLicense) { + ImmutableMap.Builder builder = ImmutableMap.builder(); + builder.put(TrialLicenseFields.UID, trialLicense.uid()); + builder.put(TrialLicenseFields.TYPE, ESLicenses.Type.TRIAL.string()); + builder.put(TrialLicenseFields.SUBSCRIPTION_TYPE, ESLicenses.SubscriptionType.NONE.string()); + builder.put(TrialLicenseFields.ISSUE_DATE, trialLicense.issueDate()); + builder.put(TrialLicenseFields.FEATURE, trialLicense.feature().string()); + builder.put(TrialLicenseFields.EXPIRY_DATE, trialLicense.expiryDate()); + builder.put(TrialLicenseFields.MAX_NODES, trialLicense.maxNodes()); + builder.put(TrialLicenseFields.ISSUED_TO, trialLicense.issuedTo()); + return builder.build(); + } + + public static TrialLicenses.TrialLicense trialLicenseFromMap(Map map) { + return TrialLicensesBuilder.trialLicenseBuilder() + .uid((String) map.get(TrialLicenseFields.UID)) + .issuedTo((String) map.get(TrialLicenseFields.ISSUED_TO)) + .maxNodes((int) map.get(TrialLicenseFields.MAX_NODES)) + .feature(ESLicenses.FeatureType.fromString((String) map.get(TrialLicenseFields.FEATURE))) + .issueDate((long) map.get(TrialLicenseFields.ISSUE_DATE)) + .expiryDate((long) map.get(TrialLicenseFields.EXPIRY_DATE)) + .build(); + + } + + final static class TrialLicenseFields { + private final static String UID = "uid"; + private final static String TYPE = "type"; + private final static String SUBSCRIPTION_TYPE = "subscription_type"; + private final static String ISSUE_DATE = "issue_date"; + private final static String FEATURE = "feature"; + private final static String ISSUED_TO = "issued_to"; + private final static String MAX_NODES = "max_nodes"; + private final static String EXPIRY_DATE = "expiry_date"; + } +} diff --git a/src/main/java/org/elasticsearch/license/plugin/core/TrialLicenses.java b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenses.java similarity index 93% rename from src/main/java/org/elasticsearch/license/plugin/core/TrialLicenses.java rename to src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenses.java index c34329120fb..11c8243572b 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/TrialLicenses.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicenses.java @@ -3,7 +3,7 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.license.plugin.core; +package org.elasticsearch.license.plugin.core.trial; import java.util.Collection; diff --git a/src/main/java/org/elasticsearch/license/plugin/core/TrialLicensesBuilder.java b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicensesBuilder.java similarity index 68% rename from src/main/java/org/elasticsearch/license/plugin/core/TrialLicensesBuilder.java rename to src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicensesBuilder.java index bef9ed9c0ee..3d7f88aaab6 100644 --- a/src/main/java/org/elasticsearch/license/plugin/core/TrialLicensesBuilder.java +++ b/src/main/java/org/elasticsearch/license/plugin/core/trial/TrialLicensesBuilder.java @@ -3,18 +3,15 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.license.plugin.core; +package org.elasticsearch.license.plugin.core.trial; -import org.apache.commons.codec.binary.Base64; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.license.core.DateUtils; import org.elasticsearch.license.core.ESLicenses; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; import java.util.*; -import static org.elasticsearch.license.plugin.core.TrialLicenses.TrialLicense; +import static org.elasticsearch.license.plugin.core.trial.TrialLicenses.TrialLicense; public class TrialLicensesBuilder { @@ -29,66 +26,6 @@ public class TrialLicensesBuilder { } - public static TrialLicense fromEncodedTrialLicense(String encodedTrialLicense) { - byte[] encodedBytes = Base64.decodeBase64(encodedTrialLicense); - ByteBuffer byteBuffer = ByteBuffer.wrap(encodedBytes); - - int uidLen = byteBuffer.getInt(); - byte[] uidBytes = new byte[uidLen]; - byteBuffer.get(uidBytes); - String uid = new String(uidBytes, Charset.forName("UTF-8")); - - int issuedToLen = byteBuffer.getInt(); - byte[] issuedToBytes = new byte[issuedToLen]; - byteBuffer.get(issuedToBytes); - String issuedTo = new String(issuedToBytes, Charset.forName("UTF-8")); - - int featureLen = byteBuffer.getInt(); - byte[] featureBytes = new byte[featureLen]; - byteBuffer.get(featureBytes); - String feature = new String(featureBytes, Charset.forName("UTF-8")); - - int maxNodes = byteBuffer.getInt(); - long issueDate = byteBuffer.getLong(); - long expiryDate = byteBuffer.getLong(); - - return trialLicenseBuilder() - .uid(uid) - .issuedTo(issuedTo) - .feature(ESLicenses.FeatureType.fromString(feature)) - .maxNodes(maxNodes) - .issueDate(issueDate) - .expiryDate(expiryDate) - .build(); - } - - public static String toEncodedTrialLicense(TrialLicense trialLicense) { - byte[] uidBytes = trialLicense.uid().getBytes(Charset.forName("UTF-8")); - String feature = trialLicense.feature().string(); - byte[] featureBytes = feature.getBytes(Charset.forName("UTF-8")); - byte[] issuedToBytes = trialLicense.issuedTo().getBytes(Charset.forName("UTF-8")); - - // uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate - int len = 4 + uidBytes.length + issuedToBytes.length + featureBytes.length + 4 + 8 + 8; - final byte[] encodedLicense = new byte[len]; - ByteBuffer byteBuffer = ByteBuffer.wrap(encodedLicense); - - byteBuffer.putInt(uidBytes.length); - byteBuffer.put(uidBytes); - - byteBuffer.putInt(issuedToBytes.length); - byteBuffer.put(issuedToBytes); - - byteBuffer.putInt(featureBytes.length); - byteBuffer.put(featureBytes); - - byteBuffer.putInt(trialLicense.maxNodes()); - byteBuffer.putLong(trialLicense.issueDate()); - byteBuffer.putLong(trialLicense.expiryDate()); - - return Base64.encodeBase64String(encodedLicense); - } - public static TrialLicenses merge(TrialLicenses trialLicenses, TrialLicenses mergeTrialLicenses) { if (trialLicenses == null && mergeTrialLicenses == null) { throw new IllegalArgumentException("both licenses can not be null"); diff --git a/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java b/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java index 88569e5b0ad..162d09997d1 100644 --- a/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java +++ b/src/main/java/org/elasticsearch/license/plugin/rest/RestGetLicenseAction.java @@ -13,12 +13,12 @@ import org.elasticsearch.license.core.ESLicenses; import org.elasticsearch.license.plugin.action.get.GetLicenseAction; import org.elasticsearch.license.plugin.action.get.GetLicenseRequest; import org.elasticsearch.license.plugin.action.get.GetLicenseResponse; -import org.elasticsearch.license.plugin.core.TrialLicenses; +import org.elasticsearch.license.plugin.core.trial.TrialLicenses; import org.elasticsearch.rest.*; import org.elasticsearch.rest.action.support.RestBuilderListener; import static org.elasticsearch.license.plugin.action.Utils.licenseAsMap; -import static org.elasticsearch.license.plugin.action.Utils.trialLicenseAsMap; +import static org.elasticsearch.license.plugin.core.trial.TrialLicenseUtils.trialLicenseAsMap; import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestStatus.OK;