2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.notification.workers.impl;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.HttpClient;
25 import org.apache.http.client.methods.HttpGet;
26 import org.apache.http.impl.client.HttpClientBuilder;
27 import org.codehaus.jackson.map.ObjectMapper;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.notification.config.ConfigurationManager;
31 import org.openecomp.sdc.notification.types.NotificationsStatusDto;
32 import org.openecomp.sdc.notification.workers.NewNotificationsReader;
34 import javax.ws.rs.core.HttpHeaders;
35 import javax.ws.rs.core.MediaType;
36 import java.io.InputStreamReader;
37 import java.util.UUID;
39 public class NewNotificationsReaderRestImpl implements NewNotificationsReader {
41 private static final String USER_ID_HEADER_PARAM = "USER_ID";
42 private static final String LAST_DELIVERED_QUERY_PARAM = "LAST_DELIVERED_EVENT_ID";
43 private static final String LIMIT_QUERY_PARAM = "NOTIFICATION_ROWS_LIMIT";
44 private static final String BE_HOST = "beHost";
45 private static final String BE_PORT = "beHttpPort";
46 private static final String DEFAULT_BE_HOST = "localhost";
47 private static final int DEFAULT_BE_PORT = 8080;
48 private static final String URL = "http://%s:%d/onboarding-api/v1.0/notifications/worker?";
49 private static final ObjectMapper mapper = new ObjectMapper();
51 private static String beHost;
52 private static int bePort;
54 private static final Logger LOGGER = LoggerFactory.getLogger(NewNotificationsReaderRestImpl.class);
56 public NewNotificationsReaderRestImpl() {
57 ConfigurationManager cm = ConfigurationManager.getInstance();
58 bePort = cm.getConfigValue(BE_PORT, DEFAULT_BE_PORT);
59 beHost = cm.getConfigValue(BE_HOST, DEFAULT_BE_HOST);
62 public NotificationsStatusDto getNewNotifications(String ownerId, UUID eventId, int limit) {
63 HttpClient client = HttpClientBuilder.create().build();
64 String url = String.format(URL, beHost, bePort);
66 url = url + LIMIT_QUERY_PARAM + "=" + limit;
67 if (eventId != null) {
68 url = url + "&" + LAST_DELIVERED_QUERY_PARAM + "=" + eventId;
71 HttpGet request = new HttpGet(url);
72 request.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
73 request.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
74 request.addHeader(USER_ID_HEADER_PARAM, ownerId);
77 HttpResponse response = client.execute(request);
78 return mapper.readValue(new InputStreamReader(response.getEntity().getContent()), NotificationsStatusDto.class);
79 } catch (Exception e) {
80 LOGGER.error("Failed to execute the request {}", url, e);