unify encoding; minor fixes & clean up imports

Original commit: elastic/x-pack-elasticsearch@0d00a7b177
This commit is contained in:
Areek Zillur 2014-10-22 12:17:13 -04:00
parent 3c551ab6d9
commit 1f153402ef
15 changed files with 49 additions and 44 deletions

View File

@ -7,10 +7,11 @@ package org.elasticsearch.license.core;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.*; import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.HashMap;
import java.util.Map;
public class ESLicense implements Comparable<ESLicense> { public class ESLicense implements Comparable<ESLicense> {

View File

@ -5,17 +5,13 @@
*/ */
package org.elasticsearch.license.core; package org.elasticsearch.license.core;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*; import java.util.*;
public class ESLicenses { public class ESLicenses {
@ -31,7 +27,7 @@ public class ESLicenses {
} }
public static Set<ESLicense> fromSource(String content) throws IOException { public static Set<ESLicense> fromSource(String content) throws IOException {
return fromSource(content.getBytes(Charset.forName("UTF-8"))); return fromSource(content.getBytes(LicensesCharset.UTF_8));
} }
public static Set<ESLicense> fromSource(byte[] bytes) throws IOException { public static Set<ESLicense> fromSource(byte[] bytes) throws IOException {
@ -41,6 +37,7 @@ public class ESLicenses {
private static Set<ESLicense> fromXContent(XContentParser parser) throws IOException { private static Set<ESLicense> fromXContent(XContentParser parser) throws IOException {
Set<ESLicense> esLicenses = new HashSet<>(); Set<ESLicense> esLicenses = new HashSet<>();
final Map<String, Object> licensesMap = parser.mapAndClose(); final Map<String, Object> licensesMap = parser.mapAndClose();
@SuppressWarnings("unchecked")
final List<Map<String, Object>> licenseMaps = (ArrayList<Map<String, Object>>)licensesMap.get("licenses"); final List<Map<String, Object>> licenseMaps = (ArrayList<Map<String, Object>>)licensesMap.get("licenses");
for (Map<String, Object> licenseMap : licenseMaps) { for (Map<String, Object> licenseMap : licenseMaps) {
final ESLicense esLicense = ESLicense.fromXContent(licenseMap); final ESLicense esLicense = ESLicense.fromXContent(licenseMap);

View File

@ -0,0 +1,17 @@
/*
* 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.core;
import java.nio.charset.Charset;
public class LicensesCharset {
/**
* All operations in should use the universal UTF-8 character set.
*/
public static final Charset UTF_8 = Charset.forName("UTF-8");
}

View File

@ -13,13 +13,12 @@ import net.nicholaswilliams.java.licensing.exception.KeyNotFoundException;
import net.nicholaswilliams.java.licensing.licensor.LicenseCreator; import net.nicholaswilliams.java.licensing.licensor.LicenseCreator;
import net.nicholaswilliams.java.licensing.licensor.LicenseCreatorProperties; import net.nicholaswilliams.java.licensing.licensor.LicenseCreatorProperties;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.license.core.*; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.LicensesCharset;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -105,7 +104,7 @@ public class ESLicenseSigner {
final byte[] licenseSignature = licenseCreator.signAndSerializeLicense(license); final byte[] licenseSignature = licenseCreator.signAndSerializeLicense(license);
final byte[] hash = Hasher.hash(Base64.encodeBase64String( final byte[] hash = Hasher.hash(Base64.encodeBase64String(
Files.readAllBytes(publicKeyPath)) Files.readAllBytes(publicKeyPath))
).getBytes(Charset.forName("UTF-8")); ).getBytes(LicensesCharset.UTF_8);
int headerLength = MAGIC_LENGTH + hash.length + 4 + 4; int headerLength = MAGIC_LENGTH + hash.length + 4 + 4;
byte[] bytes = new byte[headerLength + licenseSignature.length]; byte[] bytes = new byte[headerLength + licenseSignature.length];

View File

@ -8,14 +8,14 @@ package org.elasticsearch.license.licensor;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.license.core.DateUtils; import org.elasticsearch.license.core.DateUtils;
import org.elasticsearch.license.core.LicensesCharset;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException; import java.text.ParseException;
import java.util.*; import java.util.*;
import static org.elasticsearch.license.core.ESLicense.Type;
import static org.elasticsearch.license.core.ESLicense.SubscriptionType; import static org.elasticsearch.license.core.ESLicense.SubscriptionType;
import static org.elasticsearch.license.core.ESLicense.Type;
public class LicenseSpec { public class LicenseSpec {
final String uid; final String uid;
@ -43,7 +43,7 @@ public class LicenseSpec {
} }
public static class Builder { private static class Builder {
private String uid; private String uid;
private String issuer; private String issuer;
private String issuedTo; private String issuedTo;
@ -104,6 +104,7 @@ public class LicenseSpec {
if (uid == null) { if (uid == null) {
uid = UUID.randomUUID().toString(); uid = UUID.randomUUID().toString();
} }
//TODO: verify params
return new LicenseSpec(uid, issuer, issuedTo, issueDate, type, subscriptionType, return new LicenseSpec(uid, issuer, issuedTo, issueDate, type, subscriptionType,
feature ,expiryDate, maxNodes); feature ,expiryDate, maxNodes);
} }
@ -128,7 +129,7 @@ public class LicenseSpec {
} }
public static Set<LicenseSpec> fromSource(String content) throws IOException, ParseException { public static Set<LicenseSpec> fromSource(String content) throws IOException, ParseException {
return fromSource(content.getBytes(Charset.forName("UTF-8"))); return fromSource(content.getBytes(LicensesCharset.UTF_8));
} }
public static Set<LicenseSpec> fromSource(byte[] bytes) throws IOException, ParseException { public static Set<LicenseSpec> fromSource(byte[] bytes) throws IOException, ParseException {
@ -138,6 +139,7 @@ public class LicenseSpec {
private static Set<LicenseSpec> fromXContents(XContentParser parser) throws IOException, ParseException { private static Set<LicenseSpec> fromXContents(XContentParser parser) throws IOException, ParseException {
Set<LicenseSpec> licenseSpecs = new HashSet<>(); Set<LicenseSpec> licenseSpecs = new HashSet<>();
final Map<String, Object> licenseSpecMap = parser.mapAndClose(); final Map<String, Object> licenseSpecMap = parser.mapAndClose();
@SuppressWarnings("unchecked")
final List<Map<String, Object>> licenseSpecDefinitions = (ArrayList<Map<String, Object>>)licenseSpecMap.get("licenses"); final List<Map<String, Object>> licenseSpecDefinitions = (ArrayList<Map<String, Object>>)licenseSpecMap.get("licenses");
for (Map<String, Object> licenseSpecDef : licenseSpecDefinitions) { for (Map<String, Object> licenseSpecDef : licenseSpecDefinitions) {
final LicenseSpec licenseSpec = fromXContent(licenseSpecDef); final LicenseSpec licenseSpec = fromXContent(licenseSpecDef);

View File

@ -7,8 +7,6 @@ package org.elasticsearch.license.plugin;
import org.elasticsearch.common.inject.AbstractModule; import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Scopes; import org.elasticsearch.common.inject.Scopes;
import org.elasticsearch.license.manager.ESLicenseManager;
import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.license.plugin.core.LicensesService; import org.elasticsearch.license.plugin.core.LicensesService;
public class LicenseModule extends AbstractModule { public class LicenseModule extends AbstractModule {

View File

@ -6,7 +6,6 @@
package org.elasticsearch.license.plugin; package org.elasticsearch.license.plugin;
import org.elasticsearch.action.ActionModule; import org.elasticsearch.action.ActionModule;
import org.elasticsearch.action.admin.cluster.ClusterAction;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.node.DiscoveryNode; import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.collect.ImmutableSet; import org.elasticsearch.common.collect.ImmutableSet;
@ -22,10 +21,10 @@ import org.elasticsearch.license.plugin.action.get.TransportGetLicenseAction;
import org.elasticsearch.license.plugin.action.put.PutLicenseAction; import org.elasticsearch.license.plugin.action.put.PutLicenseAction;
import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction; import org.elasticsearch.license.plugin.action.put.TransportPutLicenseAction;
import org.elasticsearch.license.plugin.core.LicensesMetaData; import org.elasticsearch.license.plugin.core.LicensesMetaData;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction; import org.elasticsearch.license.plugin.rest.RestDeleteLicenseAction;
import org.elasticsearch.license.plugin.rest.RestGetLicenseAction; import org.elasticsearch.license.plugin.rest.RestGetLicenseAction;
import org.elasticsearch.license.plugin.rest.RestPutLicenseAction; import org.elasticsearch.license.plugin.rest.RestPutLicenseAction;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.plugins.AbstractPlugin; import org.elasticsearch.plugins.AbstractPlugin;
import org.elasticsearch.rest.RestModule; import org.elasticsearch.rest.RestModule;

View File

@ -5,13 +5,10 @@
*/ */
package org.elasticsearch.license.plugin.action.delete; package org.elasticsearch.license.plugin.action.delete;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest; import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.action.support.master.MasterNodeReadOperationRequest;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.license.core.ESLicenses;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;

View File

@ -17,9 +17,6 @@ import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.plugin.core.LicensesManagerService; import org.elasticsearch.license.plugin.core.LicensesManagerService;
import org.elasticsearch.license.plugin.core.LicensesService;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.internal.InternalNode;
import org.elasticsearch.threadpool.ThreadPool; import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;

View File

@ -14,7 +14,9 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
/** /**
* Contains metadata about registered licenses * Contains metadata about registered licenses

View File

@ -6,16 +6,9 @@
package org.elasticsearch.license.plugin.core.trial; package org.elasticsearch.license.plugin.core.trial;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.license.core.LicensesCharset;
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.ByteBuffer;
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder.trialLicensesBuilder; import static org.elasticsearch.license.plugin.core.trial.TrialLicensesBuilder.trialLicensesBuilder;
@ -29,17 +22,17 @@ public class TrialLicenseUtils {
int uidLen = byteBuffer.getInt(); int uidLen = byteBuffer.getInt();
byte[] uidBytes = new byte[uidLen]; byte[] uidBytes = new byte[uidLen];
byteBuffer.get(uidBytes); byteBuffer.get(uidBytes);
String uid = new String(uidBytes, Charset.forName("UTF-8")); String uid = new String(uidBytes, LicensesCharset.UTF_8);
int issuedToLen = byteBuffer.getInt(); int issuedToLen = byteBuffer.getInt();
byte[] issuedToBytes = new byte[issuedToLen]; byte[] issuedToBytes = new byte[issuedToLen];
byteBuffer.get(issuedToBytes); byteBuffer.get(issuedToBytes);
String issuedTo = new String(issuedToBytes, Charset.forName("UTF-8")); String issuedTo = new String(issuedToBytes, LicensesCharset.UTF_8);
int featureLen = byteBuffer.getInt(); int featureLen = byteBuffer.getInt();
byte[] featureBytes = new byte[featureLen]; byte[] featureBytes = new byte[featureLen];
byteBuffer.get(featureBytes); byteBuffer.get(featureBytes);
String feature = new String(featureBytes, Charset.forName("UTF-8")); String feature = new String(featureBytes, LicensesCharset.UTF_8);
int maxNodes = byteBuffer.getInt(); int maxNodes = byteBuffer.getInt();
long issueDate = byteBuffer.getLong(); long issueDate = byteBuffer.getLong();
@ -56,9 +49,9 @@ public class TrialLicenseUtils {
} }
public static String toEncodedTrialLicense(TrialLicenses.TrialLicense trialLicense) { public static String toEncodedTrialLicense(TrialLicenses.TrialLicense trialLicense) {
byte[] uidBytes = trialLicense.uid().getBytes(Charset.forName("UTF-8")); byte[] uidBytes = trialLicense.uid().getBytes(LicensesCharset.UTF_8);
byte[] featureBytes = trialLicense.feature().getBytes(Charset.forName("UTF-8")); byte[] featureBytes = trialLicense.feature().getBytes(LicensesCharset.UTF_8);
byte[] issuedToBytes = trialLicense.issuedTo().getBytes(Charset.forName("UTF-8")); byte[] issuedToBytes = trialLicense.issuedTo().getBytes(LicensesCharset.UTF_8);
// uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate // uid len + uid bytes + issuedTo len + issuedTo bytes + feature bytes length + feature bytes + maxNodes + issueDate + expiryDate
int len = 4 + uidBytes.length + 4 + issuedToBytes.length + 4 + featureBytes.length + 4 + 8 + 8; int len = 4 + uidBytes.length + 4 + issuedToBytes.length + 4 + featureBytes.length + 4 + 8 + 8;

View File

@ -9,7 +9,9 @@ import org.elasticsearch.common.collect.ImmutableMap;
import org.elasticsearch.license.core.DateUtils; import org.elasticsearch.license.core.DateUtils;
import org.elasticsearch.license.core.ESLicense; import org.elasticsearch.license.core.ESLicense;
import java.util.*; import java.util.Collection;
import java.util.Iterator;
import java.util.UUID;
import static org.elasticsearch.license.plugin.core.trial.TrialLicenses.TrialLicense; import static org.elasticsearch.license.plugin.core.trial.TrialLicenses.TrialLicense;

View File

@ -15,7 +15,6 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;

View File

@ -11,7 +11,8 @@ import org.elasticsearch.common.collect.ImmutableSet;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.*; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.manager.Utils; import org.elasticsearch.license.manager.Utils;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequestBuilder; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseRequestBuilder;
import org.elasticsearch.license.plugin.action.delete.DeleteLicenseResponse; import org.elasticsearch.license.plugin.action.delete.DeleteLicenseResponse;

View File

@ -15,7 +15,8 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.TestUtils; import org.elasticsearch.license.TestUtils;
import org.elasticsearch.license.core.*; import org.elasticsearch.license.core.ESLicense;
import org.elasticsearch.license.core.ESLicenses;
import org.elasticsearch.license.manager.ESLicenseManager; import org.elasticsearch.license.manager.ESLicenseManager;
import org.elasticsearch.license.manager.Utils; import org.elasticsearch.license.manager.Utils;
import org.elasticsearch.license.plugin.action.put.PutLicenseRequest; import org.elasticsearch.license.plugin.action.put.PutLicenseRequest;