-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SavedSearchesResource.java
184 lines (166 loc) · 7.33 KB
/
SavedSearchesResource.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.rest.resources.search;
import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.graylog2.audit.AuditEventTypes;
import org.graylog2.audit.jersey.AuditEvent;
import org.graylog2.database.NotFoundException;
import org.graylog2.decorators.DecoratorProcessor;
import org.graylog2.indexer.searches.Searches;
import org.graylog2.plugin.Tools;
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.plugin.database.ValidationException;
import org.graylog2.rest.resources.search.requests.CreateSavedSearchRequest;
import org.graylog2.savedsearches.SavedSearch;
import org.graylog2.savedsearches.SavedSearchService;
import org.graylog2.shared.security.RestPermissions;
import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.BadRequestException;
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.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
import java.util.List;
import java.util.Map;
@RequiresAuthentication
@Api(value = "Search/Saved", description = "Saved searches")
@Path("/search/saved")
public class SavedSearchesResource extends SearchResource {
private final SavedSearchService savedSearchService;
@Inject
public SavedSearchesResource(Searches searches,
SavedSearchService savedSearchService,
ClusterConfigService clusterConfigService,
DecoratorProcessor decoratorProcessor) {
super(searches, clusterConfigService, decoratorProcessor);
this.savedSearchService = savedSearchService;
}
@POST
@Timed
@ApiOperation(value = "Create a new saved search")
@RequiresPermissions(RestPermissions.SAVEDSEARCHES_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponse(code = 400, message = "Validation error")
@AuditEvent(type = AuditEventTypes.SAVED_SEARCH_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true)
@Valid CreateSavedSearchRequest cr) throws ValidationException {
if (!isTitleTaken("", cr.title())) {
final String msg = "Cannot save search " + cr.title() + ". Title is already taken.";
throw new BadRequestException(msg);
}
final SavedSearch search = savedSearchService.create(cr.title(), cr.query(), getCurrentUser().getName(), Tools.nowUTC());
final String id = savedSearchService.save(search);
final URI searchUri = getUriBuilderToSelf().path(SavedSearchesResource.class)
.path("{searchId}")
.build(id);
return Response.created(searchUri).entity(ImmutableMap.of("search_id", id)).build();
}
@GET
@Timed
@ApiOperation(value = "Get a list of all saved searches")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Object> list() {
final List<Map<String, Object>> searches = Lists.newArrayList();
for (SavedSearch search : savedSearchService.all()) {
if (isPermitted(RestPermissions.SAVEDSEARCHES_READ, search.getId())) {
searches.add(search.asMap());
}
}
return ImmutableMap.of(
"total", searches.size(),
"searches", searches);
}
@PUT
@Path("/{searchId}")
@Timed
@RequiresPermissions(RestPermissions.SAVEDSEARCHES_EDIT)
@ApiOperation(value = "Update a saved search")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = {
@ApiResponse(code = 404, message = "Saved search not found."),
@ApiResponse(code = 400, message = "Invalid ObjectId."),
@ApiResponse(code = 400, message = "Validation error")
})
@AuditEvent(type = AuditEventTypes.SAVED_SEARCH_UPDATE)
public Map<String, Object> update(@ApiParam(name = "searchId", required = true)
@PathParam("searchId") String searchId,
@ApiParam(name = "JSON body", required = true)
@Valid CreateSavedSearchRequest cr) throws NotFoundException, ValidationException {
final SavedSearch search = savedSearchService.load(searchId);
if (!isTitleTaken(searchId, cr.title())) {
final String msg = "Cannot save search " + cr.title() + ". Title is already taken.";
throw new BadRequestException(msg);
}
savedSearchService.update(search, cr.title(), cr.query());
return search.asMap();
}
@GET
@Path("/{searchId}")
@Timed
@ApiOperation(value = "Get a single saved search")
@ApiResponses(value = {
@ApiResponse(code = 404, message = "Saved search not found."),
@ApiResponse(code = 400, message = "Invalid ObjectId.")
})
public Map<String, Object> get(@ApiParam(name = "searchId", required = true)
@PathParam("searchId") String searchId) throws NotFoundException {
checkPermission(RestPermissions.SAVEDSEARCHES_READ, searchId);
return savedSearchService.load(searchId).asMap();
}
@DELETE
@Path("/{searchId}")
@Timed
@ApiOperation(value = "Delete a saved search")
@ApiResponses(value = {
@ApiResponse(code = 404, message = "Saved search not found."),
@ApiResponse(code = 400, message = "Invalid ObjectId.")
})
@AuditEvent(type = AuditEventTypes.SAVED_SEARCH_DELETE)
public void delete(@ApiParam(name = "searchId", required = true)
@PathParam("searchId") String searchId) throws NotFoundException {
checkPermission(RestPermissions.SAVEDSEARCHES_EDIT, searchId);
final SavedSearch search = savedSearchService.load(searchId);
savedSearchService.destroy(search);
}
private boolean isTitleTaken(String searchId, String title) {
for (SavedSearch savedSearch : savedSearchService.all()) {
if (!savedSearch.getId().equals(searchId) && savedSearch.getTitle().equals(title)) {
return false;
}
}
return true;
}
}