Skip to content

Package: UserCredentialFiltered

UserCredentialFiltered

nameinstructionbranchcomplexitylinemethod
UserCredentialFiltered()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
count(ScopeId, EntityId, CredentialQuery)
M: 21 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
create(ScopeId, EntityId, CredentialCreator)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getAll(ScopeId, EntityId, int, int)
M: 32 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2023, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.resources.v1.resources;
14:
15: import org.eclipse.kapua.KapuaException;
16: import org.eclipse.kapua.app.api.core.model.CountResult;
17: import org.eclipse.kapua.app.api.core.model.EntityId;
18: import org.eclipse.kapua.app.api.core.model.ScopeId;
19: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
20: import org.eclipse.kapua.locator.KapuaLocator;
21: import org.eclipse.kapua.model.query.predicate.AndPredicate;
22: import org.eclipse.kapua.service.KapuaService;
23: import org.eclipse.kapua.service.authentication.credential.Credential;
24: import org.eclipse.kapua.service.authentication.credential.CredentialAttributes;
25: import org.eclipse.kapua.service.authentication.credential.CredentialCreator;
26: import org.eclipse.kapua.service.authentication.credential.CredentialFactory;
27: import org.eclipse.kapua.service.authentication.credential.CredentialListResult;
28: import org.eclipse.kapua.service.authentication.credential.CredentialQuery;
29: import org.eclipse.kapua.service.authentication.credential.CredentialService;
30:
31: import javax.ws.rs.Consumes;
32: import javax.ws.rs.DefaultValue;
33: import javax.ws.rs.GET;
34: import javax.ws.rs.POST;
35: import javax.ws.rs.Path;
36: import javax.ws.rs.PathParam;
37: import javax.ws.rs.Produces;
38: import javax.ws.rs.QueryParam;
39: import javax.ws.rs.core.MediaType;
40: import javax.ws.rs.core.Response;
41:
42: @Path("/{scopeId}/user/{userId}/credentials")
43: public class UserCredentialFiltered extends AbstractKapuaResource {
44: private final KapuaLocator locator = KapuaLocator.getInstance();
45: private final CredentialService credentialService = locator.getService(CredentialService.class);
46: private final CredentialFactory credentialFactory = locator.getFactory(CredentialFactory.class);
47:
48:
49: /**
50: * Gets the {@link Credential} list in the scope.
51: *
52: * @param scopeId The {@link ScopeId} in which to search results.
53: * @param userId The {@link EntityId} for which search results.
54: * @param offset The result set offset.
55: * @param limit The result set limit.
56: * @return The {@link CredentialListResult} of all the credentials associated to the current selected scope.
57: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
58: * @since 1.0.0
59: */
60: @GET
61: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
62: public CredentialListResult getAll(
63: @PathParam("scopeId") ScopeId scopeId,
64: @PathParam("userId") EntityId userId,
65: @QueryParam("offset") @DefaultValue("0") int offset,
66: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
67: CredentialQuery query = credentialFactory.newQuery(scopeId);
68:
69: AndPredicate andPredicate = query.andPredicate();
70: andPredicate.and(query.attributePredicate(CredentialAttributes.USER_ID, userId));
71: query.setPredicate(andPredicate);
72:
73: query.setOffset(offset);
74: query.setLimit(limit);
75:
76: return credentialService.query(query);
77: }
78:
79:
80: /**
81: * Counts the results with the given {@link CredentialQuery} parameter.
82: *
83: * @param scopeId The {@link ScopeId} in which to count results.
84: * @param userId The {@link EntityId} for which count results.
85: * @param query The {@link CredentialQuery} to use to filter results.
86: * @return The count of all the result matching the given {@link CredentialQuery} parameter.
87: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
88: * @since 1.0.0
89: */
90: @POST
91: @Path("_count")
92: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
93: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
94: public CountResult count(
95: @PathParam("scopeId") ScopeId scopeId,
96: @PathParam("userId") EntityId userId,
97: CredentialQuery query) throws KapuaException {
98: AndPredicate andPredicate = query.andPredicate();
99: andPredicate.and(query.attributePredicate(CredentialAttributes.USER_ID, userId));
100: query.setPredicate(andPredicate);
101:
102: return new CountResult(credentialService.count(query));
103: }
104:
105:
106: /**
107: * Creates a new Credential based on the information provided in CredentialCreator
108: * parameter.
109: *
110: * @param scopeId The {@link ScopeId} in which to create the {@link Credential}.
111: * @param userId The {@link EntityId} for which create the {@link Credential}.
112: * @param credentialCreator Provides the information for the new Credential to be created.
113: * @return The newly created Credential object.
114: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
115: * @since 1.0.0
116: */
117: @POST
118: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
119: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
120: public Response create(
121: @PathParam("scopeId") ScopeId scopeId,
122: @PathParam("userId") EntityId userId,
123: CredentialCreator credentialCreator) throws KapuaException {
124: credentialCreator.setScopeId(scopeId);
125: credentialCreator.setUserId(userId);
126:
127: return returnCreated(credentialService.create(credentialCreator));
128: }
129: }