001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * SonarQube is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020package org.sonar.api.config; 021 022import com.google.common.annotations.VisibleForTesting; 023import com.google.common.collect.Maps; 024import org.apache.commons.codec.binary.Base64; 025import org.apache.commons.io.Charsets; 026import org.apache.commons.io.IOUtils; 027import org.apache.commons.lang.StringUtils; 028import org.sonar.api.utils.DateUtils; 029 030import javax.annotation.Nullable; 031 032import java.io.IOException; 033import java.io.StringReader; 034import java.util.Calendar; 035import java.util.Date; 036import java.util.List; 037import java.util.Map; 038 039/** 040 * SonarSource license. This class aims to extract metadata but not to validate or - of course - 041 * to generate license 042 * 043 * @since 3.0 044 */ 045public final class License { 046 private String product; 047 private String organization; 048 private String expirationDate; 049 private String type; 050 private String server; 051 private Map<String, String> additionalProperties; 052 053 private License(Map<String, String> properties) { 054 this.additionalProperties = Maps.newHashMap(properties); 055 product = StringUtils.defaultString(get("Product", properties), get("Plugin", properties)); 056 organization = StringUtils.defaultString(get("Organisation", properties), get("Name", properties)); 057 expirationDate = StringUtils.defaultString(get("Expiration", properties), get("Expires", properties)); 058 type = get("Type", properties); 059 server = get("Server", properties); 060 // SONAR-4340 Don't expose Digest and Obeo properties 061 additionalProperties.remove("Digest"); 062 additionalProperties.remove("Obeo"); 063 } 064 065 private String get(String key, Map<String, String> properties) { 066 additionalProperties.remove(key); 067 return properties.get(key); 068 } 069 070 /** 071 * Get additional properties available on this license (like threshold conditions) 072 * @since 3.6 073 */ 074 public Map<String, String> additionalProperties() { 075 return additionalProperties; 076 } 077 078 @Nullable 079 public String getProduct() { 080 return product; 081 } 082 083 @Nullable 084 public String getOrganization() { 085 return organization; 086 } 087 088 @Nullable 089 public String getExpirationDateAsString() { 090 return expirationDate; 091 } 092 093 @Nullable 094 public Date getExpirationDate() { 095 return DateUtils.parseDateQuietly(expirationDate); 096 } 097 098 public boolean isExpired() { 099 return isExpired(new Date()); 100 } 101 102 @VisibleForTesting 103 boolean isExpired(Date now) { 104 Date date = getExpirationDate(); 105 if (date == null) { 106 return false; 107 } 108 // SONAR-6079 include last day 109 Calendar cal = Calendar.getInstance(); 110 cal.setTime(date); 111 cal.add(Calendar.DAY_OF_MONTH, 1); 112 cal.add(Calendar.SECOND, -1); 113 return now.after(cal.getTime()); 114 } 115 116 @Nullable 117 public String getType() { 118 return type; 119 } 120 121 @Nullable 122 public String getServer() { 123 return server; 124 } 125 126 public static License readBase64(String base64) { 127 return readPlainText(new String(Base64.decodeBase64(base64.getBytes(Charsets.UTF_8)), Charsets.UTF_8)); 128 } 129 130 @VisibleForTesting 131 static License readPlainText(String data) { 132 Map<String, String> props = Maps.newHashMap(); 133 StringReader reader = new StringReader(data); 134 try { 135 List<String> lines = IOUtils.readLines(reader); 136 for (String line : lines) { 137 if (StringUtils.isNotBlank(line) && line.indexOf(':') > 0) { 138 String key = StringUtils.substringBefore(line, ":"); 139 String value = StringUtils.substringAfter(line, ":"); 140 props.put(StringUtils.trimToEmpty(key), StringUtils.trimToEmpty(value)); 141 } 142 } 143 144 } catch (IOException e) { 145 // silently ignore 146 147 } finally { 148 IOUtils.closeQuietly(reader); 149 } 150 return new License(props); 151 } 152}