com.synopsys.integration.blackduck.service.dataservice.NotificationService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blackduck-common Show documentation
Show all versions of blackduck-common Show documentation
A library for using various capabilities of Black Duck, notably the REST API and signature scanning.
/**
* blackduck-common
*
* Copyright (c) 2021 Synopsys, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.synopsys.integration.blackduck.service.dataservice;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery;
import com.synopsys.integration.blackduck.api.generated.view.UserView;
import com.synopsys.integration.blackduck.api.manual.contract.NotificationViewData;
import com.synopsys.integration.blackduck.api.manual.temporary.enumeration.NotificationType;
import com.synopsys.integration.blackduck.api.manual.view.NotificationUserView;
import com.synopsys.integration.blackduck.api.manual.view.NotificationView;
import com.synopsys.integration.blackduck.http.BlackDuckPageDefinition;
import com.synopsys.integration.blackduck.http.BlackDuckPageResponse;
import com.synopsys.integration.blackduck.http.BlackDuckRequestBuilder;
import com.synopsys.integration.blackduck.http.BlackDuckRequestFactory;
import com.synopsys.integration.blackduck.http.BlackDuckRequestFilter;
import com.synopsys.integration.blackduck.service.BlackDuckApiClient;
import com.synopsys.integration.blackduck.service.DataService;
import com.synopsys.integration.exception.IntegrationException;
import com.synopsys.integration.log.IntLogger;
import com.synopsys.integration.rest.HttpUrl;
import com.synopsys.integration.rest.RestConstants;
public class NotificationService extends DataService {
// ejk - to get all notifications:
// /api/notifications?startDate=2019-07-01T00:00:00.000Z&endDate=2019-07-15T00:00:00.000Z&filter=notificationType:BOM_EDIT&filter=notificationType:LICENSE_LIMIT&filter=notificationType:POLICY_OVERRIDE&filter=notificationType:RULE_VIOLATION&filter=notificationType:RULE_VIOLATION_CLEARED&filter=notificationType:VERSION_BOM_CODE_LOCATION_BOM_COMPUTED&filter=notificationType:VULNERABILITY&filter=notificationType:PROJECT&filter=notificationType:PROJECT_VERSION
public NotificationService(BlackDuckApiClient blackDuckApiClient, BlackDuckRequestFactory blackDuckRequestFactory, IntLogger logger) {
super(blackDuckApiClient, blackDuckRequestFactory, logger);
}
public List getAllNotifications(Date startDate, Date endDate) throws IntegrationException {
List allKnownNotificationTypes = getAllKnownNotificationTypes();
BlackDuckRequestBuilder requestBuilder = createNotificationRequestBuilder(startDate, endDate, allKnownNotificationTypes);
return blackDuckApiClient.getAllResponses(ApiDiscovery.NOTIFICATIONS_LINK_RESPONSE, requestBuilder);
}
public BlackDuckPageResponse getPageOfNotifications(Date startDate, Date endDate, BlackDuckPageDefinition blackDuckPageDefinition) throws IntegrationException {
List allKnownNotificationTypes = getAllKnownNotificationTypes();
BlackDuckRequestBuilder requestBuilder = createNotificationRequestBuilder(startDate, endDate, allKnownNotificationTypes);
return blackDuckApiClient.getPageResponse(requestBuilder, NotificationView.class, blackDuckPageDefinition);
}
public List getAllUserNotifications(UserView user, Date startDate, Date endDate) throws IntegrationException {
List allKnownNotificationTypes = getAllKnownNotificationTypes();
BlackDuckRequestBuilder requestBuilder = prepareUserNotificationsRequest(user, startDate, endDate, allKnownNotificationTypes);
return blackDuckApiClient.getAllResponses(requestBuilder, NotificationUserView.class);
}
public BlackDuckPageResponse getPageOfUserNotifications(UserView user, Date startDate, Date endDate, BlackDuckPageDefinition blackDuckPageDefinition) throws IntegrationException {
List allKnownNotificationTypes = getAllKnownNotificationTypes();
BlackDuckRequestBuilder requestBuilder = prepareUserNotificationsRequest(user, startDate, endDate, allKnownNotificationTypes);
return blackDuckApiClient.getPageResponse(requestBuilder, NotificationUserView.class, blackDuckPageDefinition);
}
public List getFilteredNotifications(Date startDate, Date endDate, List notificationTypesToInclude) throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = createNotificationRequestBuilder(startDate, endDate, notificationTypesToInclude);
return blackDuckApiClient.getAllResponses(ApiDiscovery.NOTIFICATIONS_LINK_RESPONSE, requestBuilder);
}
public List getFilteredUserNotifications(UserView user, Date startDate, Date endDate, List notificationTypesToInclude) throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = prepareUserNotificationsRequest(user, startDate, endDate, notificationTypesToInclude);
return blackDuckApiClient.getAllResponses(requestBuilder, NotificationUserView.class);
}
/**
* @return The java.util.Date of the most recent notification. If there are no notifications, the current date will be returned. This can set an initial start time window for all future notifications.
* @throws IntegrationException
*/
public Date getLatestNotificationDate() throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = createLatestDateRequestBuilder();
List notifications = blackDuckApiClient.getSomeResponses(ApiDiscovery.NOTIFICATIONS_LINK_RESPONSE, requestBuilder, 1);
return getFirstCreatedAtDate(notifications);
}
/**
* @return The java.util.Date of the most recent notification in the user's stream. If there are no notifications, the current date will be returned. This can set an initial start time window for all future notifications.
* @throws IntegrationException
*/
public Date getLatestUserNotificationDate(UserView userView) throws IntegrationException {
BlackDuckRequestBuilder requestBuilder = createLatestDateRequestBuilder();
List userNotifications = blackDuckApiClient.getSomeResponses(userView, UserView.NOTIFICATIONS_LINK_RESPONSE, requestBuilder, 1);
return getFirstCreatedAtDate(userNotifications);
}
private Date getFirstCreatedAtDate(List extends NotificationViewData> notifications) {
if (notifications.size() == 1) {
return notifications.get(0).getCreatedAt();
} else {
return new Date();
}
}
private BlackDuckRequestBuilder prepareUserNotificationsRequest(UserView user, Date startDate, Date endDate, List notificationTypesToInclude) throws IntegrationException {
HttpUrl url = user.getFirstLink(UserView.NOTIFICATIONS_LINK);
return createNotificationRequestBuilder(startDate, endDate, notificationTypesToInclude)
.url(url);
}
private BlackDuckRequestBuilder createLatestDateRequestBuilder() {
return blackDuckRequestFactory
.createCommonGetRequestBuilder()
.addBlackDuckFilter(createFilterForAllKnownTypes());
}
private List getAllKnownNotificationTypes() {
List allKnownTypes = Stream.of(NotificationType.values()).map(Enum::name).collect(Collectors.toList());
return allKnownTypes;
}
private BlackDuckRequestFilter createFilterForAllKnownTypes() {
return createFilterForSpecificTypes(getAllKnownNotificationTypes());
}
private BlackDuckRequestFilter createFilterForSpecificTypes(List notificationTypesToInclude) {
return BlackDuckRequestFilter.createFilterWithMultipleValues("notificationType", notificationTypesToInclude);
}
private BlackDuckRequestBuilder createNotificationRequestBuilder(Date startDate, Date endDate, List notificationTypesToInclude) {
SimpleDateFormat sdf = new SimpleDateFormat(RestConstants.JSON_DATE_FORMAT);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String startDateString = sdf.format(startDate);
String endDateString = sdf.format(endDate);
BlackDuckRequestFilter notificationTypeFilter = createFilterForSpecificTypes(notificationTypesToInclude);
return blackDuckRequestFactory
.createCommonGetRequestBuilder()
.addQueryParameter("startDate", startDateString)
.addQueryParameter("endDate", endDateString)
.addBlackDuckFilter(notificationTypeFilter);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy