Search in sources :

Example 81 with DefaultValue

use of javax.ws.rs.DefaultValue in project graylog2-server by Graylog2.

the class StreamResource method getPage.

@GET
@Timed
@Path("/paginated")
@ApiOperation(value = "Get a paginated list of streams")
@Produces(MediaType.APPLICATION_JSON)
public StreamPageListResponse getPage(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description") @DefaultValue(StreamImpl.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
    SearchQuery searchQuery;
    try {
        searchQuery = searchQueryParser.parse(query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final Predicate<StreamDTO> permissionFilter = streamDTO -> isPermitted(RestPermissions.STREAMS_READ, streamDTO.id());
    final PaginatedList<StreamDTO> result = paginatedStreamService.findPaginated(searchQuery, permissionFilter, page, perPage, sort, order);
    final List<String> streamIds = result.stream().map(streamDTO -> streamDTO.id()).collect(Collectors.toList());
    final Map<String, List<StreamRule>> streamRuleMap = streamRuleService.loadForStreamIds(streamIds);
    final List<StreamDTO> streams = result.stream().map(streamDTO -> {
        List<StreamRule> rules = streamRuleMap.getOrDefault(streamDTO.id(), Collections.emptyList());
        return streamDTO.toBuilder().rules(rules).build();
    }).collect(Collectors.toList());
    final long total = paginatedStreamService.count();
    final PaginatedList<StreamDTO> streamDTOS = new PaginatedList<>(streams, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
    return StreamPageListResponse.create(query, streamDTOS.pagination(), total, sort, order, streams);
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) DateTimeZone(org.joda.time.DateTimeZone) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) Tools(org.graylog2.plugin.Tools) UserContext(org.graylog.security.UserContext) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlertService(org.graylog2.alerts.AlertService) StreamRule(org.graylog2.plugin.streams.StreamRule) NotEmpty(javax.validation.constraints.NotEmpty) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) SearchQueryField(org.graylog2.search.SearchQueryField) AlertConditionSummary(org.graylog2.rest.models.streams.alerts.AlertConditionSummary) StreamImpl(org.graylog2.streams.StreamImpl) StreamRuleService(org.graylog2.streams.StreamRuleService) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) BadRequestException(javax.ws.rs.BadRequestException) IndexSet(org.graylog2.indexer.IndexSet) URI(java.net.URI) DELETE(javax.ws.rs.DELETE) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) StreamRouterEngine(org.graylog2.streams.StreamRouterEngine) ISODateTimeFormat(org.joda.time.format.ISODateTimeFormat) ImmutableSet(com.google.common.collect.ImmutableSet) Context(javax.ws.rs.core.Context) ImmutableMap(com.google.common.collect.ImmutableMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Timed(com.codahale.metrics.annotation.Timed) CreateStreamRequest(org.graylog2.rest.resources.streams.requests.CreateStreamRequest) CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) List(java.util.List) Response(javax.ws.rs.core.Response) Stream(org.graylog2.plugin.streams.Stream) AuditEventTypes(org.graylog2.audit.AuditEventTypes) StreamService(org.graylog2.streams.StreamService) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) AlertReceivers(org.graylog2.rest.models.alarmcallbacks.requests.AlertReceivers) StreamDTO(org.graylog2.streams.StreamDTO) CreateConditionRequest(org.graylog2.rest.models.streams.alerts.requests.CreateConditionRequest) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PathParam(javax.ws.rs.PathParam) PaginatedStreamService(org.graylog2.streams.PaginatedStreamService) CloneStreamRequest(org.graylog2.rest.resources.streams.requests.CloneStreamRequest) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) TestMatchResponse(org.graylog2.rest.resources.streams.responses.TestMatchResponse) StreamPageListResponse(org.graylog2.rest.resources.streams.responses.StreamPageListResponse) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses) StreamListResponse(org.graylog2.rest.resources.streams.responses.StreamListResponse) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) UpdateStreamRequest(org.graylog2.rest.models.streams.requests.UpdateStreamRequest) Lists(com.google.common.collect.Lists) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SearchQuery(org.graylog2.search.SearchQuery) NotFoundException(org.graylog2.database.NotFoundException) IndexSetRegistry(org.graylog2.indexer.IndexSetRegistry) ExecutorService(java.util.concurrent.ExecutorService) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) DateTime(org.joda.time.DateTime) RestResource(org.graylog2.shared.rest.resources.RestResource) OutputSummary(org.graylog2.rest.models.system.outputs.responses.OutputSummary) Maps(com.google.common.collect.Maps) AlarmCallbackConfigurationService(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationService) Output(org.graylog2.plugin.streams.Output) ApiResponse(io.swagger.annotations.ApiResponse) ValidationException(org.graylog2.plugin.database.ValidationException) RestPermissions(org.graylog2.shared.security.RestPermissions) StreamResponse(org.graylog2.rest.resources.streams.responses.StreamResponse) ObjectId(org.bson.types.ObjectId) PUT(javax.ws.rs.PUT) StreamRuleImpl(org.graylog2.streams.StreamRuleImpl) Message(org.graylog2.plugin.Message) Collections(java.util.Collections) BadRequestException(javax.ws.rs.BadRequestException) PaginatedList(org.graylog2.database.PaginatedList) List(java.util.List) ArrayList(java.util.ArrayList) PaginatedList(org.graylog2.database.PaginatedList) StreamDTO(org.graylog2.streams.StreamDTO) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 82 with DefaultValue

use of javax.ws.rs.DefaultValue in project graylog2-server by Graylog2.

the class UsersResource method getPage.

@GET
@Timed
@Path("/paginated")
@ApiOperation(value = "Get paginated list of users")
@RequiresPermissions(RestPermissions.USERS_LIST)
@Produces(MediaType.APPLICATION_JSON)
public PaginatedResponse<UserOverviewDTO> getPage(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description") @DefaultValue(UserOverviewDTO.FIELD_FULL_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
    SearchQuery searchQuery;
    final AllUserSessions sessions = AllUserSessions.create(sessionService);
    try {
        searchQuery = searchQueryParser.parse(query);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
    }
    final PaginatedList<UserOverviewDTO> result = paginatedUserService.findPaginated(searchQuery, page, perPage, sort, order);
    final Set<String> allRoleIds = result.stream().flatMap(userDTO -> {
        if (userDTO.roles() != null) {
            return userDTO.roles().stream();
        }
        return Stream.empty();
    }).collect(Collectors.toSet());
    Map<String, String> roleNameMap;
    try {
        roleNameMap = getRoleNameMap(allRoleIds);
    } catch (org.graylog2.database.NotFoundException e) {
        throw new NotFoundException("Couldn't find roles: " + e.getMessage());
    }
    final UserOverviewDTO adminUser = getAdminUserDTO(sessions);
    List<UserOverviewDTO> users = result.stream().map(userDTO -> {
        UserOverviewDTO.Builder builder = userDTO.toBuilder().fillSession(sessions.forUser(userDTO));
        if (userDTO.roles() != null) {
            builder.roles(userDTO.roles().stream().map(roleNameMap::get).collect(Collectors.toSet()));
        }
        return builder.build();
    }).collect(Collectors.toList());
    final PaginatedList<UserOverviewDTO> userOverviewDTOS = new PaginatedList<>(users, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
    return PaginatedResponse.create("users", userOverviewDTOS, query, Collections.singletonMap("admin_user", adminUser));
}
Also used : SearchQuery(org.graylog2.search.SearchQuery) USERS_TOKENCREATE(org.graylog2.shared.security.RestPermissions.USERS_TOKENCREATE) StringUtils(org.apache.commons.lang.StringUtils) DateTimeZone(org.joda.time.DateTimeZone) UserList(org.graylog2.rest.models.users.responses.UserList) USERS_PERMISSIONSEDIT(org.graylog2.shared.security.RestPermissions.USERS_PERMISSIONSEDIT) Produces(javax.ws.rs.Produces) UserManagementService(org.graylog2.shared.users.UserManagementService) USERS_TOKENLIST(org.graylog2.shared.security.RestPermissions.USERS_TOKENLIST) Date(java.util.Date) UserContext(org.graylog.security.UserContext) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) WildcardPermission(org.apache.shiro.authz.permission.WildcardPermission) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) PaginatedList(org.graylog2.database.PaginatedList) MongoDBSessionService(org.graylog2.security.MongoDBSessionService) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) SearchQueryField(org.graylog2.search.SearchQueryField) AccessTokenService(org.graylog2.security.AccessTokenService) USERS_ROLESEDIT(org.graylog2.shared.security.RestPermissions.USERS_ROLESEDIT) CreateUserRequest(org.graylog2.rest.models.users.requests.CreateUserRequest) Locale(java.util.Locale) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) ChangePasswordRequest(org.graylog2.rest.models.users.requests.ChangePasswordRequest) AccessToken(org.graylog2.security.AccessToken) DELETE(javax.ws.rs.DELETE) Token(org.graylog2.rest.models.users.responses.Token) Context(javax.ws.rs.core.Context) ImmutableMap(com.google.common.collect.ImmutableMap) TokenSummary(org.graylog2.rest.models.users.responses.TokenSummary) Collection(java.util.Collection) Set(java.util.Set) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) TokenList(org.graylog2.rest.models.users.responses.TokenList) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) PaginatedUserService(org.graylog2.users.PaginatedUserService) NotFoundException(javax.ws.rs.NotFoundException) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) AuditEventTypes(org.graylog2.audit.AuditEventTypes) UserSummary(org.graylog2.rest.models.users.responses.UserSummary) Startpage(org.graylog2.rest.models.users.requests.Startpage) Optional(java.util.Optional) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) GRNPermission(org.graylog.security.permissions.GRNPermission) USERS_TOKENREMOVE(org.graylog2.shared.security.RestPermissions.USERS_TOKENREMOVE) UserOverviewDTO(org.graylog2.users.UserOverviewDTO) Iterables(com.google.common.collect.Iterables) PathParam(javax.ws.rs.PathParam) NotBlank(javax.validation.constraints.NotBlank) SearchQueryParser(org.graylog2.search.SearchQueryParser) GET(javax.ws.rs.GET) RoleService(org.graylog2.users.RoleService) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses) Roles(org.graylog2.shared.users.Roles) ArrayList(java.util.ArrayList) MongoDbSession(org.graylog2.security.MongoDbSession) Inject(javax.inject.Inject) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) AuditEvent(org.graylog2.audit.jersey.AuditEvent) Api(io.swagger.annotations.Api) SearchQuery(org.graylog2.search.SearchQuery) Nullable(javax.annotation.Nullable) PermissionEditRequest(org.graylog2.rest.models.users.requests.PermissionEditRequest) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ChangeUserRequest(org.graylog2.shared.users.ChangeUserRequest) ForbiddenException(javax.ws.rs.ForbiddenException) RestResource(org.graylog2.shared.rest.resources.RestResource) Collectors.maxBy(java.util.stream.Collectors.maxBy) UpdateUserPreferences(org.graylog2.rest.models.users.requests.UpdateUserPreferences) ApiResponse(io.swagger.annotations.ApiResponse) ValidationException(org.graylog2.plugin.database.ValidationException) RestPermissions(org.graylog2.shared.security.RestPermissions) USERS_EDIT(org.graylog2.shared.security.RestPermissions.USERS_EDIT) PUT(javax.ws.rs.PUT) PaginatedResponse(org.graylog2.rest.models.PaginatedResponse) User(org.graylog2.plugin.database.users.User) Role(org.graylog2.shared.users.Role) Comparator(java.util.Comparator) Collections(java.util.Collections) NotFoundException(javax.ws.rs.NotFoundException) UserOverviewDTO(org.graylog2.users.UserOverviewDTO) BadRequestException(javax.ws.rs.BadRequestException) PaginatedList(org.graylog2.database.PaginatedList) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 83 with DefaultValue

use of javax.ws.rs.DefaultValue in project raml-module-builder by folio-org.

the class AnnotationGrabber method getParameterNames.

public static JsonObject getParameterNames(Method method) throws Exception {
    // need to handle default values
    JsonObject retObject = new JsonObject();
    Parameter[] nonAnnotationParams = method.getParameters();
    Annotation[][] annotations = method.getParameterAnnotations();
    Class<?>[] parameterTypes = method.getParameterTypes();
    int k = 0;
    for (Annotation[] annotation : annotations) {
        Class<?> parameterType = parameterTypes[k++];
        if (annotation.length == 0) {
            // we are here because - there is a param but it is not annotated - this
            // will occur for post / put
            // requests - the entity to save/update will not be annotated
            JsonObject obj = null;
            obj = new JsonObject();
            // this will be
            obj.put("value", nonAnnotationParams[k - 1].getName());
            // a generic
            // name - unless
            // debug info
            // turned on for
            // javac (store
            // information
            // about method
            // parameters)
            obj.put("type", parameterType.getCanonicalName());
            obj.put("order", k - 1);
            obj.put("param_type", NON_ANNOTATED_PARAM);
            retObject.put("" + (k - 1), obj);
        }
        JsonObject prevObjForDefaultVal = null;
        for (Annotation a : annotation) {
            JsonObject obj = null;
            if (a instanceof HeaderParam) {
                obj = new JsonObject();
                obj.put("value", ((HeaderParam) a).value());
                obj.put("type", parameterType.getCanonicalName());
                obj.put("order", k - 1);
                obj.put("param_type", HEADER_PARAM);
            } else if (a instanceof PathParam) {
                obj = new JsonObject();
                obj.put("value", ((PathParam) a).value());
                obj.put("type", parameterType.getCanonicalName());
                obj.put("order", k - 1);
                obj.put("param_type", PATH_PARAM);
            } else if (a instanceof QueryParam) {
                obj = new JsonObject();
                obj.put("value", ((QueryParam) a).value());
                obj.put("type", parameterType.getCanonicalName());
                obj.put("order", k - 1);
                obj.put("param_type", QUERY_PARAM);
            } else if (a instanceof DefaultValue && prevObjForDefaultVal != null) {
                // default values originate in the raml and appear after the parameter
                // they are to be applied to
                String defaultValue = ((DefaultValue) a).value();
                // push it into the previously scanned parameter
                prevObjForDefaultVal.put("default_value", defaultValue);
                prevObjForDefaultVal = null;
            }
            if (obj != null) {
                prevObjForDefaultVal = obj;
                // obj may be null in case of @DefaultValue annotation which i am
                // currently ignoring
                retObject.put("" + (k - 1), obj);
            }
        }
    }
    return retObject;
}
Also used : HeaderParam(javax.ws.rs.HeaderParam) JsonObject(io.vertx.core.json.JsonObject) Annotation(java.lang.annotation.Annotation) DefaultValue(javax.ws.rs.DefaultValue) QueryParam(javax.ws.rs.QueryParam) Parameter(java.lang.reflect.Parameter) PathParam(javax.ws.rs.PathParam)

Aggregations

DefaultValue (javax.ws.rs.DefaultValue)83 Produces (javax.ws.rs.Produces)67 Response (javax.ws.rs.core.Response)63 Path (javax.ws.rs.Path)56 HeaderParam (javax.ws.rs.HeaderParam)49 GET (javax.ws.rs.GET)46 URI (java.net.URI)42 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)36 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)35 QueryParam (javax.ws.rs.QueryParam)34 Consumes (javax.ws.rs.Consumes)32 POST (javax.ws.rs.POST)30 PathParam (javax.ws.rs.PathParam)29 List (java.util.List)24 ArrayList (java.util.ArrayList)23 Inject (javax.inject.Inject)20 PUT (javax.ws.rs.PUT)20 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)20 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)19 MediaType (javax.ws.rs.core.MediaType)19