*/
package org.sonar.server.edition;
-import com.google.common.collect.ImmutableList;
-import java.util.List;
+import com.google.common.collect.ImmutableSet;
+import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
import javax.annotation.concurrent.Immutable;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
@Immutable
public class License {
+ private static final Logger LOG = Loggers.get(License.class);
+ private static final String EDITION_KEY = "Edition";
+ private static final String PLUGINS_KEY = "Plugins";
+
private final String editionKey;
- private final List<String> pluginKeys;
+ private final Set<String> pluginKeys;
private final String content;
- public License(String editionKey, List<String> pluginKeys, String content) {
+ public License(String editionKey, Collection<String> pluginKeys, String content) {
this.editionKey = enforceNotNullNorEmpty(editionKey, "editionKey");
- this.pluginKeys = ImmutableList.copyOf(pluginKeys);
+ this.pluginKeys = ImmutableSet.copyOf(pluginKeys);
this.content = enforceNotNullNorEmpty(content, "content");
}
return editionKey;
}
- public List<String> getPluginKeys() {
+ public Set<String> getPluginKeys() {
return pluginKeys;
}
public String getContent() {
return content;
}
+
+ public static Optional<License> parse(String base64) {
+ try {
+ String data = new String(Base64.decodeBase64(base64.trim().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
+
+ Properties props = new Properties();
+ props.load(new StringReader(data));
+
+ Collection<String> plugins = Arrays.asList(StringUtils.split(props.getProperty(PLUGINS_KEY), ','));
+ String editionKey = props.getProperty(EDITION_KEY);
+
+ if (editionKey != null && !plugins.isEmpty()) {
+ return Optional.of(new License(editionKey, plugins, base64));
+ } else {
+ LOG.debug("Failed to parse license: no edition key and/or no plugin found");
+ }
+ } catch (Exception e) {
+ LOG.debug("Failed to parse license", e);
+ }
+ return Optional.empty();
+
+ }
}
*/
package org.sonar.server.edition;
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.Base64;
import java.util.Collections;
+import java.util.Optional;
+import java.util.Properties;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
new License("edition-key", Collections.emptyList(), "");
}
+ @Test
+ public void parse_returns_empty_if_license_is_invalid_string() {
+ assertThat(License.parse("trash")).isEmpty();
+ }
+
+ @Test
+ public void parse_succeeds() throws IOException {
+ Properties props = new Properties();
+ props.setProperty("Plugins", "plugin1,plugin2");
+ props.setProperty("Edition", "dev");
+ StringWriter writer = new StringWriter();
+ props.store(writer, "");
+
+ byte[] encoded = Base64.getEncoder().encode(writer.toString().getBytes());
+
+ Optional<License> license = License.parse(new String(encoded));
+ assertThat(license).isPresent();
+ assertThat(license.get().getEditionKey()).isEqualTo("dev");
+ assertThat(license.get().getPluginKeys()).containsOnly("plugin1", "plugin2");
+ }
+
+ @Test
+ public void parse_is_empty_if_no_plugin() throws IOException {
+ Properties props = new Properties();
+ props.setProperty("Plugins", "");
+ props.setProperty("Edition", "dev");
+ StringWriter writer = new StringWriter();
+ props.store(writer, "");
+
+ byte[] encoded = Base64.getEncoder().encode(writer.toString().getBytes());
+
+ Optional<License> license = License.parse(new String(encoded));
+ assertThat(license).isEmpty();
+ }
+
@Test
public void verify_getters() {
- ImmutableList<String> pluginKeys = ImmutableList.of("a", "b", "c");
+ ImmutableSet<String> pluginKeys = ImmutableSet.of("a", "b", "c");
License underTest = new License("edition-key", pluginKeys, "content");