This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
RestFeeds.java
163 lines (136 loc) · 6.35 KB
/
RestFeeds.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.hawkular.inventory.rest.deprecated;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.Response.Status.FORBIDDEN;
import static org.hawkular.inventory.rest.RequestUtil.extractPaging;
import java.util.Set;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.hawkular.inventory.api.Tenants;
import org.hawkular.inventory.api.model.Feed;
import org.hawkular.inventory.api.paging.Page;
import org.hawkular.inventory.paths.CanonicalPath;
import org.hawkular.inventory.rest.ResponseUtil;
import org.hawkular.inventory.rest.RestBase;
import org.hawkular.inventory.rest.json.ApiError;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
/**
* @author Lukas Krejci
* @since 0.0.1
*/
@Path("/deprecated")
@Produces(value = APPLICATION_JSON)
@Consumes(value = APPLICATION_JSON)
@Api(value = "/deprecated", description = "CRUD of feeds", tags = {"Deprecated"})
public class RestFeeds extends RestBase {
@POST
@Path("/feeds")
@ApiOperation("Registers a feed with the inventory, giving it a unique ID.")
@ApiResponses({
@ApiResponse(code = 201, message = "OK", response = Feed.class),
@ApiResponse(code = 400, message = "Invalid inputs", response = ApiError.class),
@ApiResponse(code = 401, message = "Unauthorized access"),
@ApiResponse(code = 500, message = "Server error", response = ApiError.class)
})
public Response register(Feed.Blueprint blueprint, @Context UriInfo uriInfo) {
String tenantId = getTenantId();
CanonicalPath tenant = CanonicalPath.of().tenant(tenantId).get();
if (!security.canCreate(Feed.class).under(tenant)) {
return Response.status(FORBIDDEN).build();
}
Feed feed = inventory.inspect(tenant, Tenants.Single.class).feeds().create(blueprint).entity();
return ResponseUtil.created(feed, uriInfo, feed.getId()).entity(feed).build();
}
@GET
@Path("/feeds")
@ApiOperation("Return all the feeds registered with the inventory")
@ApiResponses({
@ApiResponse(code = 200, message = "OK", response = Set.class),
@ApiResponse(code = 400, message = "Invalid inputs", response = ApiError.class),
@ApiResponse(code = 401, message = "Unauthorized access"),
@ApiResponse(code = 404, message = "Environment doesn't exist", response = ApiError.class),
@ApiResponse(code = 500, message = "Server error", response = ApiError.class)
})
public Response getAll(@Context UriInfo uriInfo) {
String tenantId = getTenantId();
Page<Feed> ret = inventory.tenants().get(tenantId).feeds().getAll().entities(extractPaging(uriInfo));
return pagedResponse(Response.ok(), uriInfo, ret).build();
}
@GET
@Path("/feeds/{feedId}")
@ApiOperation("Return a single feed by its ID.")
@ApiResponses({
@ApiResponse(code = 200, message = "OK", response = Set.class),
@ApiResponse(code = 400, message = "Invalid inputs", response = ApiError.class),
@ApiResponse(code = 401, message = "Unauthorized access"),
@ApiResponse(code = 404, message = "Environment or feed doesn't exist", response = ApiError.class),
@ApiResponse(code = 500, message = "Server error", response = ApiError.class)
})
public Response get(@PathParam("feedId") String feedId) {
String tenantId = getTenantId();
return Response.ok(inventory.tenants().get(tenantId).feeds().get(feedId).entity()).build();
}
@PUT
@Path("/feeds/{feedId}")
@ApiOperation("Updates a feed")
@ApiResponses({
@ApiResponse(code = 204, message = "OK"),
@ApiResponse(code = 401, message = "Unauthorized access"),
@ApiResponse(code = 404, message = "Environment or the feed doesn't exist", response = ApiError.class),
@ApiResponse(code = 400, message = "The update failed because of invalid data"),
@ApiResponse(code = 500, message = "Server error", response = ApiError.class)
})
public Response update(@PathParam("feedId") String feedId, Feed.Update update) {
String tenantId = getTenantId();
if (!security.canUpdate(CanonicalPath.of().tenant(tenantId).feed(feedId).get())) {
return Response.status(FORBIDDEN).build();
}
inventory.tenants().get(tenantId).feeds().update(feedId, update);
return Response.noContent().build();
}
@DELETE
@Path("/feeds/{feedId}")
@ApiOperation("Deletes a feed")
@ApiResponses({
@ApiResponse(code = 204, message = "OK"),
@ApiResponse(code = 401, message = "Unauthorized access"),
@ApiResponse(code = 404, message = "Environment or the feed doesn't exist", response = ApiError.class),
@ApiResponse(code = 400, message = "The delete failed because it would make inventory invalid"),
@ApiResponse(code = 500, message = "Server error", response = ApiError.class)
})
public Response delete(@PathParam("feedId") String feedId) {
String tenantId = getTenantId();
if (!security.canDelete(CanonicalPath.of().tenant(tenantId).feed(feedId).get())) {
return Response.status(FORBIDDEN).build();
}
inventory.tenants().get(tenantId).feeds().delete(feedId);
return Response.noContent().build();
}
}