Search in sources :

Example 11 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo in project cdap by caskdata.

the class RemoteNotificationFeedManager method getFeed.

@Override
public NotificationFeedInfo getFeed(NotificationFeedId feed) throws NotificationFeedNotFoundException, NotificationFeedException {
    String path = String.format("namespaces/%s/feeds/categories/%s/names/%s", feed.getNamespace(), feed.getCategory(), feed.getFeed());
    HttpResponse response = execute(remoteClient.requestBuilder(HttpMethod.GET, path).build());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotificationFeedNotFoundException(feed);
    } else if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new NotificationFeedException("Cannot get notification feed. Reason: " + response);
    }
    return ObjectResponse.fromJsonBody(response, NotificationFeedInfo.class).getResponseObject();
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) HttpResponse(co.cask.common.http.HttpResponse) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo)

Example 12 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo in project cdap by caskdata.

the class NotificationFeedHttpHandler method createFeed.

@PUT
@Path("/feeds/categories/{feed-category}/names/{feed-name}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void createFeed(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("feed-category") String category, @PathParam("feed-name") String name) {
    try {
        NotificationFeedInfo feedInfo;
        try {
            Map<String, String> body = parseBody(request, MAP_TYPE);
            String description = body == null ? null : body.get("description");
            feedInfo = new NotificationFeedInfo(namespaceId, category, name, description);
        } catch (IllegalArgumentException e) {
            responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("Could not create Notification Feed. %s", e.getMessage()));
            return;
        }
        if (feedManager.createFeed(feedInfo)) {
            responder.sendString(HttpResponseStatus.OK, "Notification Feed created successfully");
        } else {
            LOG.trace("Notification Feed already exists.");
            responder.sendString(HttpResponseStatus.OK, "Notification Feed already exists.");
        }
    } catch (NotificationFeedException e) {
        LOG.error("Could not create notification feed.", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (JsonSyntaxException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Invalid json object provided in request body.");
    } catch (IOException e) {
        LOG.error("Failed to read Notification feed request body.", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (Throwable t) {
        LOG.debug("Error in creating notification feed.", t);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) JsonSyntaxException(com.google.gson.JsonSyntaxException) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) IOException(java.io.IOException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 13 with NotificationFeedInfo

use of co.cask.cdap.proto.notification.NotificationFeedInfo in project cdap by caskdata.

the class NotificationFeedHttpHandler method listFeeds.

@GET
@Path("/feeds")
public void listFeeds(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) {
    try {
        List<NotificationFeedInfo> feeds = feedManager.listFeeds(new NamespaceId(namespaceId));
        responder.sendJson(HttpResponseStatus.OK, feeds);
    } catch (NotificationFeedException e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, String.format("Could not check subscribe permission for Notification Feed. %s", e.getMessage()));
    } catch (Throwable t) {
        LOG.debug("Error in listing notification feeds.", t);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getMessage());
    }
}
Also used : NotificationFeedException(co.cask.cdap.notifications.feeds.NotificationFeedException) NotificationFeedInfo(co.cask.cdap.proto.notification.NotificationFeedInfo) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

NotificationFeedInfo (co.cask.cdap.proto.notification.NotificationFeedInfo)13 NotificationFeedException (co.cask.cdap.notifications.feeds.NotificationFeedException)6 Test (org.junit.Test)4 NotificationFeedNotFoundException (co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException)3 HttpResponse (org.apache.http.HttpResponse)3 NotificationFeedId (co.cask.cdap.proto.id.NotificationFeedId)2 HttpResponse (co.cask.common.http.HttpResponse)2 Path (javax.ws.rs.Path)2 RetryableException (co.cask.cdap.api.retry.RetryableException)1 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)1 NotificationContext (co.cask.cdap.notifications.service.NotificationContext)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 TypeToken (com.google.common.reflect.TypeToken)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1