moved an api messsage in common module

This commit is contained in:
Michele Artini 2020-09-15 10:08:06 +02:00
parent 01d1f0fc87
commit 831f55a2af
3 changed files with 75 additions and 78 deletions

View File

@ -12,6 +12,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.search.join.ScoreMode;
@ -35,11 +36,13 @@ import org.springframework.web.bind.annotation.RestController;
import com.google.gson.Gson;
import eu.dnetlib.broker.BrokerConfiguration;
import eu.dnetlib.broker.api.ShortEventMessage;
import eu.dnetlib.broker.common.controllers.AbstractLbsController;
import eu.dnetlib.broker.common.elasticsearch.Notification;
import eu.dnetlib.broker.common.properties.ElasticSearchProperties;
import eu.dnetlib.broker.common.subscriptions.Subscription;
import eu.dnetlib.broker.common.subscriptions.SubscriptionRepository;
import eu.dnetlib.broker.objects.OaBrokerEventPayload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -64,7 +67,7 @@ public class OpenairePublicController extends AbstractLbsController {
@ApiOperation("Returns notifications by subscription using scrolls (first page)")
@GetMapping("/scroll/notifications/bySubscriptionId/{subscrId}")
public ScrollPage<NotificationMessage> prepareScrollNotificationsBySubscrId(@PathVariable final String subscrId) {
public ScrollPage<ShortEventMessage> prepareScrollNotificationsBySubscrId(@PathVariable final String subscrId) {
final Optional<Subscription> optSub = subscriptionRepo.findById(subscrId);
@ -82,7 +85,7 @@ public class OpenairePublicController extends AbstractLbsController {
final SearchScrollHits<Notification> scroll =
esTemplate.searchScrollStart(SCROLL_TIMEOUT_IN_MILLIS, searchQuery, Notification.class, IndexCoordinates.of(props.getNotificationsIndexName()));
if (scroll.hasSearchHits()) {
final List<NotificationMessage> values = calculateNotificationMessages(scroll);
final List<ShortEventMessage> values = calculateNotificationMessages(scroll);
return new ScrollPage<>(scroll.getScrollId(), values.isEmpty() || scroll.getScrollId() == null, values);
} else {
esTemplate.searchScrollClear(Arrays.asList(scroll.getScrollId()));
@ -97,7 +100,7 @@ public class OpenairePublicController extends AbstractLbsController {
@ApiOperation("Returns notifications by opendorar Id (for example: 301) using scrolls (first page)")
@GetMapping("/scroll/notifications/byOpenDoarId/{opendoarId}")
public ScrollPage<NotificationMessage> prepareScrollNotificationsByOpendoarId(@PathVariable final String opendoarId) {
public ScrollPage<ShortEventMessage> prepareScrollNotificationsByOpendoarId(@PathVariable final String opendoarId) {
final ElasticsearchRestTemplate esTemplate = (ElasticsearchRestTemplate) esOperations;
@ -112,7 +115,7 @@ public class OpenairePublicController extends AbstractLbsController {
final SearchScrollHits<Notification> scroll =
esTemplate.searchScrollStart(SCROLL_TIMEOUT_IN_MILLIS, searchQuery, Notification.class, IndexCoordinates.of(props.getNotificationsIndexName()));
if (scroll.hasSearchHits()) {
final List<NotificationMessage> values = calculateNotificationMessages(scroll);
final List<ShortEventMessage> values = calculateNotificationMessages(scroll);
return new ScrollPage<>(scroll.getScrollId(), values.isEmpty() || scroll.getScrollId() == null, values);
} else {
esTemplate.searchScrollClear(Arrays.asList(scroll.getScrollId()));
@ -127,14 +130,14 @@ public class OpenairePublicController extends AbstractLbsController {
@ApiOperation("Returns notifications using scrolls (other pages)")
@GetMapping("/scroll/notifications/{scrollId}")
public ScrollPage<NotificationMessage> scrollNotifications(@PathVariable final String scrollId) {
public ScrollPage<ShortEventMessage> scrollNotifications(@PathVariable final String scrollId) {
final ElasticsearchRestTemplate esTemplate = (ElasticsearchRestTemplate) esOperations;
final SearchScrollHits<Notification> scroll =
esTemplate.searchScrollContinue(scrollId, SCROLL_TIMEOUT_IN_MILLIS, Notification.class, IndexCoordinates.of(props.getNotificationsIndexName()));
if (scroll.hasSearchHits()) {
final List<NotificationMessage> values = calculateNotificationMessages(scroll);
final List<ShortEventMessage> values = calculateNotificationMessages(scroll);
return new ScrollPage<>(scroll.getScrollId(), values.isEmpty() || scroll.getScrollId() == null, values);
} else {
esTemplate.searchScrollClear(Arrays.asList(scroll.getScrollId()));
@ -156,12 +159,12 @@ public class OpenairePublicController extends AbstractLbsController {
IOUtils.write("[\n", gzOut);
ScrollPage<NotificationMessage> page = null;
ScrollPage<ShortEventMessage> page = null;
do {
page = page == null ? prepareScrollNotificationsBySubscrId(subscrId) : scrollNotifications(page.getId());
for (final NotificationMessage msg : page.getValues()) {
for (final ShortEventMessage msg : page.getValues()) {
if (first) {
first = false;
} else {
@ -179,15 +182,31 @@ public class OpenairePublicController extends AbstractLbsController {
}
private List<NotificationMessage> calculateNotificationMessages(final SearchScrollHits<Notification> scroll) {
private List<ShortEventMessage> calculateNotificationMessages(final SearchScrollHits<Notification> scroll) {
if (scroll.getSearchHits().size() > 0) {
return scroll.stream()
.map(SearchHit::getContent)
.map(NotificationMessage::fromNotification)
.map(this::messageFromNotification)
.collect(Collectors.toList());
} else {
return new ArrayList<>();
}
}
private ShortEventMessage messageFromNotification(final Notification n) {
final Gson gson = new Gson();
final OaBrokerEventPayload payload = gson.fromJson(n.getPayload(), OaBrokerEventPayload.class);
final ShortEventMessage res = new ShortEventMessage();
res.setOriginalId(payload.getResult().getOriginalId());
res.setTitle(payload.getResult().getTitles().stream().filter(StringUtils::isNotBlank).findFirst().orElse(null));
res.setTopic(n.getTopic());
res.setTrust(payload.getTrust());
res.generateMessageFromObject(payload.getHighlight());
return res;
}
}

View File

@ -4,7 +4,7 @@ import org.junit.Ignore;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
import eu.dnetlib.broker.oa.controllers.NotificationMessage;
import eu.dnetlib.broker.api.ShortEventMessage;
import eu.dnetlib.broker.oa.controllers.ScrollPage;
public class ScrollTest {
@ -15,7 +15,7 @@ public class ScrollTest {
private static final String subscriptionId = "sub-c9767c84-3597-462b-803b-2d3e09de44c4";
public class TestScrollPage extends ScrollPage<NotificationMessage> {}
public class TestScrollPage extends ScrollPage<ShortEventMessage> {}
@Test
@Ignore
@ -29,7 +29,7 @@ public class ScrollTest {
while (!page.isCompleted()) {
page = getPage(baseUrl + "/api/openaireBroker/scroll/notifications/ " + page.getId());
total += page.getValues().size();
for (final NotificationMessage p : page.getValues()) {
for (final ShortEventMessage p : page.getValues()) {
// DO SOMETHING
}
}

View File

@ -1,4 +1,4 @@
package eu.dnetlib.broker.oa.controllers;
package eu.dnetlib.broker.api;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
@ -9,15 +9,8 @@ import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.gson.Gson;
import eu.dnetlib.broker.common.elasticsearch.Notification;
import eu.dnetlib.broker.objects.OaBrokerEventPayload;
public class NotificationMessage implements Serializable {
public class ShortEventMessage implements Serializable {
/**
*
@ -34,62 +27,6 @@ public class NotificationMessage implements Serializable {
private Map<String, String> message = new LinkedHashMap<>();
private static final Log log = LogFactory.getLog(NotificationMessage.class);
public static NotificationMessage fromNotification(final Notification n) {
final Gson gson = new Gson();
final OaBrokerEventPayload payload = gson.fromJson(n.getPayload(), OaBrokerEventPayload.class);
final NotificationMessage res = new NotificationMessage();
res.setOriginalId(payload.getResult().getOriginalId());
res.setTitle(payload.getResult().getTitles().stream().filter(StringUtils::isNotBlank).findFirst().orElse(null));
res.setTopic(n.getTopic());
res.setTrust(payload.getTrust());
res.setMessage(highlightAsMap("", payload.getHighlight()));
return res;
}
private static Map<String, String> highlightAsMap(final String prefix, final Object bean) {
final Map<String, String> res = new LinkedHashMap<>();
try {
for (final PropertyDescriptor pd : Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors()) {
if (pd.getReadMethod() != null) {
final Object v = pd.getReadMethod().invoke(bean);
if (v != null) {
if (v instanceof List && !((List<?>) v).isEmpty()) {
final List<?> list = (List<?>) v;
for (int i = 0; i < list.size(); i++) {
final Object x = list.get(i);
if (x instanceof String && StringUtils.isNotBlank(x.toString())) {
res.put(prefix + pd.getName() + "[" + i + "]", x.toString());
} else {
res.putAll(highlightAsMap(prefix + pd.getName() + "[" + i + "].", x));
}
}
} else if (v instanceof String) {
res.put(prefix + pd.getName(), v.toString());
} else {
res.putAll(highlightAsMap(pd.getName() + ".", v));
}
}
}
}
return res;
} catch (final Exception e) {
log.warn(e);
return Collections.emptyMap();
}
}
public String getOriginalId() {
return originalId;
}
@ -130,4 +67,45 @@ public class NotificationMessage implements Serializable {
this.message = message;
}
public void generateMessageFromObject(final Object bean) {
this.message = generateMessageFromObject("", bean);
}
private static Map<String, String> generateMessageFromObject(final String prefix, final Object bean) {
final Map<String, String> res = new LinkedHashMap<>();
try {
for (final PropertyDescriptor pd : Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors()) {
if (pd.getReadMethod() != null) {
final Object v = pd.getReadMethod().invoke(bean);
if (v != null) {
if (v instanceof List && !((List<?>) v).isEmpty()) {
final List<?> list = (List<?>) v;
for (int i = 0; i < list.size(); i++) {
final Object x = list.get(i);
if (x instanceof String && StringUtils.isNotBlank(x.toString())) {
res.put(prefix + pd.getName() + "[" + i + "]", x.toString());
} else {
res.putAll(generateMessageFromObject(prefix + pd.getName() + "[" + i + "].", x));
}
}
} else if (v instanceof String) {
res.put(prefix + pd.getName(), v.toString());
} else {
res.putAll(generateMessageFromObject(pd.getName() + ".", v));
}
}
}
}
return res;
} catch (final Exception e) {
return Collections.emptyMap();
}
}
}