Commit 61cd1883 authored by Paromita Mitra's avatar Paromita Mitra Committed by Bhushan Rade
Browse files

Pubsub design2 ibm

parent 23697bf2
/*
* Copyright 2017-2020, Schlumberger
*
* Licensed 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 org.opengroup.osdu.register.provider.ibm.api;
import java.util.ArrayList;
import java.util.Collections;
import org.opengroup.osdu.core.common.model.notification.Topic;
import org.opengroup.osdu.register.api.SubscriberApi;
import org.opengroup.osdu.register.api.dto.SubscriptionInfo;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.register.logging.AuditLogger;
import org.opengroup.osdu.register.subscriber.model.Secret;
import org.opengroup.osdu.register.subscriber.model.Subscription;
import org.opengroup.osdu.register.provider.ibm.subscriber.SubscriptionRepository;
import org.opengroup.osdu.register.provider.interfaces.subscriber.ISubscriptionRepository;
import org.opengroup.osdu.register.subscriber.services.AvailableTopics;
import org.opengroup.osdu.register.subscriber.services.CreateSubscription;
import org.opengroup.osdu.register.subscriber.services.DeleteSubscription;
import org.opengroup.osdu.register.subscriber.services.UpdateSubscriptionSecret;
import org.opengroup.osdu.register.utils.ServiceRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
@RestController
@RequestMapping("/")
@RequestScope
@Validated
public class SubscriptionApi{
@Inject
private Provider<ISubscriptionRepository> subscriptionRepository;
@GetMapping("subscription/topic/{topic}")
@PreAuthorize("@authorizationFilter.hasAnyPermission('" + ServiceRole.OPS + "', '" + ServiceRole.ADMIN + "', '" + ServiceRole.EDITOR + "')")
public ResponseEntity<List<SubscriptionInfo>> getSubscriptionsByTopic(@PathVariable("topic") @NotBlank String topic) {
//removed getByTopic from ISubscriptionRepository, to remove dependency on register-core for now. Need to change afterwards
SubscriptionRepository subsRepo = (SubscriptionRepository) subscriptionRepository.get();
List<Subscription> subscriptions = subsRepo.getByTopic(topic);
List<SubscriptionInfo> subsInfoList = new ArrayList<SubscriptionInfo>();
for(Subscription subs: subscriptions) {
subsInfoList.add(new SubscriptionInfo(subs));
}
return new ResponseEntity<>(subsInfoList, HttpStatus.OK);
}
}
......@@ -25,6 +25,7 @@ import org.springframework.stereotype.Repository;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.query.QueryBuilder;
import com.cloudant.client.api.query.QueryResult;
import com.cloudant.client.api.query.Sort;
import com.cloudant.client.org.lightcouch.DocumentConflictException;
import com.cloudant.client.org.lightcouch.NoDocumentException;
......@@ -196,4 +197,20 @@ public class DatastoreAccess implements IDatastoreAccess {
doc.getNotificationId(), secret);
}
@Override
public List<Subscription> getByTopic(String topic) {
Database db = dataStoreTenants.get(getDbNameWithTenant());
List<Subscription> subscriptions = new ArrayList<Subscription>();
QueryResult<SubscriptionDoc> results = db.query(new QueryBuilder(
eq("topic", topic)).
build(), SubscriptionDoc.class);
if (results == null || results.getDocs().isEmpty()) {
throw new AppException(404, "Not Found",
format("Sunscription not found for topic %s ", topic));
}
subscriptions = results.getDocs().stream().map(i -> convertToSubscription(i)).collect(Collectors.toList());
return subscriptions;
}
}
......@@ -13,6 +13,8 @@ public interface IDatastoreAccess {
Subscription create(Subscription s);
Subscription get(String id);
List<Subscription> getByTopic(String topic);
List<Subscription> query(String notificationId);
......
......@@ -45,6 +45,11 @@ public class SubscriptionRepository implements ISubscriptionRepository {
public boolean delete(String id) {
return datastoreAccess.delete(id);
}
public List<Subscription> getByTopic(String topic) {
return datastoreAccess.getByTopic(topic);
}
@Override
public boolean patch(Subscription subscription, Secret secret) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment