1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.config;
import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.DateUtils;
/**
* SonarSource license. This class aims to extract metadata but not to validate or - of course -
* to generate license
*
* @since 3.0
*/
public final class License {
private String product;
private String organization;
private String expirationDate;
private String type;
private String server;
private Map<String, String> additionalProperties;
private License(Map<String, String> properties) {
this.additionalProperties = new HashMap<>(properties);
product = StringUtils.defaultString(get("Product", properties), get("Plugin", properties));
organization = StringUtils.defaultString(get("Organisation", properties), get("Name", properties));
expirationDate = StringUtils.defaultString(get("Expiration", properties), get("Expires", properties));
type = get("Type", properties);
server = get("Server", properties);
// SONAR-4340 Don't expose Digest and Obeo properties
additionalProperties.remove("Digest");
additionalProperties.remove("Obeo");
}
private String get(String key, Map<String, String> properties) {
additionalProperties.remove(key);
return properties.get(key);
}
/**
* Get additional properties available on this license (like threshold conditions)
* @since 3.6
*/
public Map<String, String> additionalProperties() {
return additionalProperties;
}
@Nullable
public String getProduct() {
return product;
}
@Nullable
public String getOrganization() {
return organization;
}
@Nullable
public String getExpirationDateAsString() {
return expirationDate;
}
@Nullable
public Date getExpirationDate() {
return DateUtils.parseDateQuietly(expirationDate);
}
public boolean isExpired() {
return isExpired(new Date());
}
@VisibleForTesting
boolean isExpired(Date now) {
Date date = getExpirationDate();
if (date == null) {
return false;
}
// SONAR-6079 include last day
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.SECOND, -1);
return now.after(cal.getTime());
}
@Nullable
public String getType() {
return type;
}
@Nullable
public String getServer() {
return server;
}
public static License readBase64(String base64) {
return readPlainText(new String(Base64.decodeBase64(base64.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
}
@VisibleForTesting
static License readPlainText(String data) {
Map<String, String> props = new HashMap<>();
try {
List<String> lines = IOUtils.readLines(new StringReader(data));
for (String line : lines) {
if (StringUtils.isNotBlank(line) && line.indexOf(':') > 0) {
String key = StringUtils.substringBefore(line, ":");
String value = StringUtils.substringAfter(line, ":");
props.put(StringUtils.trimToEmpty(key), StringUtils.trimToEmpty(value));
}
}
} catch (IOException e) {
// silently ignore
}
return new License(props);
}
}
|