-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSw360LicenseService.java
More file actions
399 lines (355 loc) · 18.4 KB
/
Sw360LicenseService.java
File metadata and controls
399 lines (355 loc) · 18.4 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright Siemens AG, 2017-2018. Part of the SW360 Portal Project.
* Copyright Ritankar Saha <ritankar.saha786@gmail.com>, 2025.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.rest.resourceserver.license;
import lombok.RequiredArgsConstructor;
import com.google.common.collect.ImmutableSet;
import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.common.SW360Utils;
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
import org.eclipse.sw360.exporter.LicsExporter;
import org.eclipse.sw360.exporter.utils.ZipTools;
import org.eclipse.sw360.importer.LicsImporter;
import org.eclipse.sw360.rest.resourceserver.core.BadRequestClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.zip.ZipOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import static org.eclipse.sw360.datahandler.common.CommonUtils.isNullEmptyOrWhitespace;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Sw360LicenseService {
@Value("${sw360.thrift-server-url:http://localhost:8080}")
private String thriftServerUrl;
private static final String CONTENT_TYPE = "application/zip";
LicenseType lType = new LicenseType();
public List<License> getLicenses() throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
return sw360LicenseClient.getLicenseSummary();
}
public License getLicenseById(String licenseId) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
// TODO Kai Tödter 2017-01-26
// What is the semantics of the second parameter (organization)?
License license = null;
try {
license = sw360LicenseClient.getByID(licenseId, "?");
} catch (SW360Exception exp) {
if (exp.getErrorCode() == 404) {
throw new ResourceNotFoundException(exp.getWhy());
} else {
throw new RuntimeException(exp.getWhy());
}
}
return license;
}
public void deleteLicenseById(String licenseId, User user) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
RequestStatus deleteLicenseStatus = sw360LicenseClient.deleteLicense(licenseId, user);
if (deleteLicenseStatus == RequestStatus.IN_USE) {
throw new BadRequestClientException("Unable to delete license. License is in Use");
} else if (deleteLicenseStatus == RequestStatus.FAILURE) {
throw new RuntimeException("Unable to delete License");
}
}
public void deleteAllLicenseInfo(User user) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, user)) {
RequestSummary deleteLicenseStatus = sw360LicenseClient.deleteAllLicenseInformation(user);
} else {
throw new BadRequestClientException("Unable to delete license. User is not admin");
}
}
public License createLicense(License license, User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
license.setId(license.getShortname());
List<License> licenses = sw360LicenseClient.addLicenses(Collections.singletonList(license), sw360User);
for (License newLicense : licenses) {
if (license.getFullname().equals(newLicense.getFullname())) {
return newLicense;
}
}
return null;
}
public RequestStatus updateLicenseToDB(License license, Set<String> obligationIds, User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (obligationIds.isEmpty()){
license.unsetObligations();
} else {
List<String> obligationIdList = new ArrayList<>(obligationIds);
List<Obligation> obligations = sw360LicenseClient.getObligationsByIds(obligationIdList);
checkObligationLevel(obligations, sw360User);
license.setObligationDatabaseIds(obligationIds);
license.setObligations(obligations);
}
return sw360LicenseClient.updateLicense(license, sw360User, sw360User);
}
public RequestStatus updateLicense(License license, User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (null != license){
Set<String> obligationIds = license.getObligationDatabaseIds();
if (CommonUtils.isNullOrEmptyCollection(obligationIds)) {
license.unsetObligations();
} else {
List<String> obligationIdList = new ArrayList<>(obligationIds);
List<Obligation> obligations = sw360LicenseClient.getObligationsByIds(obligationIdList);
checkObligationLevel(obligations, sw360User);
license.setObligationDatabaseIds(obligationIds);
license.setObligations(obligations);
}
}
return sw360LicenseClient.updateLicense(license, sw360User, sw360User);
}
public Set<String> getIdObligationsContainWhitelist(User sw360User, String licenseId, Set<String> diffIds) throws TException {
Set<String> obligationIdTrue = new HashSet<>();
String organisation = sw360User.getDepartment();
String businessUnit = SW360Utils.getBUFromOrganisation(organisation);
List<Obligation> obligations = getObligationsByLicenseId(licenseId);
for (Obligation obligation : obligations) {
String obligationId = obligation.getId();
Set<String> currentWhitelist = obligation.whitelist != null ? obligation.whitelist : new HashSet<>();
if (diffIds.contains(obligationId) && currentWhitelist.contains(businessUnit)) {
obligationIdTrue.add(obligationId);
}
}
return obligationIdTrue;
}
public RequestStatus updateWhitelist(Set<String> obligationIds, String licenseId, User user) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
return sw360LicenseClient.updateWhitelist(licenseId, ImmutableSet.copyOf(obligationIds), user);
}
public List<Obligation> getObligationsByLicenseId(String id) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
List<Obligation> obligations = sw360LicenseClient.getObligationsByLicenseId(id);
if (CommonUtils.isNullOrEmptyCollection(obligations)) {
return Collections.emptyList();
}
return obligations;
}
public List<LicenseType> getLicenseTypes() throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
List<LicenseType> licenseTypes = sw360LicenseClient.getLicenseTypes();
if (CommonUtils.isNullOrEmptyCollection(licenseTypes)) {
return Collections.emptyList();
}
return licenseTypes;
}
public void checkObligationIds(Set<String> obligationIds) throws TException {
if (obligationIds.isEmpty()) {
throw new BadRequestClientException("Cannot update because no obligation id input");
}
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
ArrayList<String> obligationIdsIncorrect = new ArrayList<>();
for (String obligationId : obligationIds) {
if (isNullEmptyOrWhitespace(obligationId)) {
throw new BadRequestClientException("Obligation id cannot be empty");
}
try {
sw360LicenseClient.getObligationsById(obligationId);
} catch (Exception e) {
obligationIdsIncorrect.add(obligationId);
}
}
if (!obligationIdsIncorrect.isEmpty()) {
throw new BadRequestClientException("Obligation ids " + obligationIdsIncorrect + " incorrect");
}
}
public void checkObligationLevel(List<Obligation> obligations, User sw360User) {
List<String> obligationsLevelIncorrect = new ArrayList<>();
for (Obligation oblig : obligations) {
if (oblig.getObligationLevel().toString().equals("LICENSE_OBLIGATION")) {
oblig.addToWhitelist(sw360User.getDepartment());
} else {
obligationsLevelIncorrect.add(oblig.getId());
}
}
if (!obligationsLevelIncorrect.isEmpty()) {
throw new BadRequestClientException("Obligation with ids: " + obligationsLevelIncorrect + " level is not License Obligation");
}
}
private LicenseService.Iface getThriftLicenseClient() throws TTransportException {
THttpClient thriftClient = new THttpClient(thriftServerUrl + "/licenses/thrift");
TProtocol protocol = new TCompactProtocol(thriftClient);
return new LicenseService.Client(protocol);
}
public RequestSummary importSpdxInformation(User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestSummary allSPDXLicenseStatus = sw360LicenseClient.importAllSpdxLicenses(sw360User);
return allSPDXLicenseStatus;
} else {
throw new BadRequestClientException("Unable to import All Spdx license. User is not admin");
}
}
public void getDownloadLicenseArchive(User sw360User ,HttpServletRequest request,HttpServletResponse response) throws TException,IOException{
if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
throw new BadRequestClientException("Unable to download archive license. User is not admin");
}
try {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
String fileConstant="LicensesBackup.lics";
Map<String, InputStream> fileNameToStreams = (new LicsExporter(sw360LicenseClient)).getFilenameToCSVStreams();
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
for (Map.Entry<String, InputStream> entry : fileNameToStreams.entrySet()) {
ZipTools.addToZip(zipOutputStream, entry.getKey(), entry.getValue());
}
zipOutputStream.flush();
zipOutputStream.close();
final ByteArrayInputStream zipFile = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
String filename = String.format(fileConstant, SW360Utils.getCreatedOn());
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-Disposition", String.format("license; filename=\"%s\"", filename));
copyDataStreamToResponse(response, zipFile);
} catch (SW360Exception exp) {
if (exp.getErrorCode() == 404) {
throw new ResourceNotFoundException(exp.getWhy());
} else {
throw new RuntimeException(exp.getWhy());
}
}
}
private void copyDataStreamToResponse(HttpServletResponse response, ByteArrayInputStream buffer) throws IOException {
FileCopyUtils.copy(buffer, response.getOutputStream());
}
public void uploadLicense(User sw360User, MultipartFile file, boolean overwriteIfExternalIdMatches, boolean overwriteIfIdMatchesEvenWithoutExternalIdMatch) throws IOException, TException {
final HashMap<String, InputStream> inputMap = new HashMap<>();
if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
throw new BadRequestClientException("Unable to upload license file. User is not admin");
}
try {
InputStream inputStream = file.getInputStream();
ZipTools.extractZipToInputStreamMap(inputStream, inputMap);
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
final LicsImporter licsImporter = new LicsImporter(sw360LicenseClient, overwriteIfExternalIdMatches, overwriteIfIdMatchesEvenWithoutExternalIdMatch);
licsImporter.importLics(sw360User, inputMap);
}finally {
for (InputStream inputStream : inputMap.values()) {
inputStream.close();
}
}
}
public RequestSummary importOsadlInformation(User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
return sw360LicenseClient.importAllOSADLLicenses(sw360User);
} else {
throw new BadRequestClientException("Unable to import All OSADL license obligations. User is not admin");
}
}
public RequestStatus addLicenseType(User sw360User, String licenseType, HttpServletRequest request) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (StringUtils.isNotEmpty(licenseType)) {
lType.setLicenseType(licenseType);
}
else {
throw new BadRequestClientException("license type is empty");
}
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus status = sw360LicenseClient.addLicenseType(lType, sw360User);
} else {
throw new BadRequestClientException("Unable to create License Type. User is not admin");
}
} catch ( Exception e) {
throw new TException(e.getMessage());
}
return RequestStatus.SUCCESS;
}
public List<LicenseType> quickSearchLicenseType(String searchElem) {
try {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
List<LicenseType> rawResults = sw360LicenseClient.searchByLicenseType(searchElem);
Map<String, LicenseType> uniqueResults = new LinkedHashMap<>();
for (LicenseType license : rawResults) {
String id = license.getId();
if (id != null && !id.isEmpty()) {
uniqueResults.put(id, license);
}
}
List<LicenseType> sortedResults = new ArrayList<>(uniqueResults.values());
sortedResults.sort(Comparator.comparing(LicenseType::getLicenseType, String.CASE_INSENSITIVE_ORDER));
return sortedResults;
} catch (TException e) {
throw new RuntimeException("Error performing licenseType search", e);
}
}
public RequestStatus deleteLicenseType(String id, User sw360User) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
throw new AccessDeniedException("Unable to delete license type. User is not admin");
}
try {
sw360LicenseClient.getLicenseTypeById(id);
} catch (TException e) {
throw new ResourceNotFoundException("License type not found with ID: " + id);
}
RequestStatus status = sw360LicenseClient.deleteLicenseType(id, sw360User);
if (status == RequestStatus.FAILURE) {
throw new SW360Exception("License type could not be deleted.");
}
return status;
}
public int getLicenseTypeUsageCount(String licenseTypeId, User sw360User) throws TException{
if (!PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
throw new AccessDeniedException("User is not authorized to retrieve license type usage details.");
}
try {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
List<License> allLicenses = sw360LicenseClient.getLicenseSummary();
return (int) allLicenses.stream()
.filter(license -> license.getLicenseType() != null && license.getLicenseType().getId().equals(licenseTypeId))
.count();
} catch (SW360Exception sw360Exp) {
if (sw360Exp.getErrorCode() == 404) {
throw new ResourceNotFoundException("License type not found with ID: " + licenseTypeId);
} else {
throw sw360Exp;
}
}
}
/**
* Search licenses for given text in fullname or shortname.
* @param searchText String to search.
* @return Sorted list of licenses.
* @throws TException If Thrift had issues.
*/
public List<License> searchLicenses(String searchText) throws TException {
LicenseService.Iface sw360LicenseClient = getThriftLicenseClient();
List<License> licenses = sw360LicenseClient.searchLicense(searchText);
licenses.sort(Comparator.comparing(License::getFullname, String.CASE_INSENSITIVE_ORDER));
return licenses;
}
}