use of org.gluu.oxtrust.model.scim2.ListResponse in project oxTrust by GluuFederation.
the class ListResponseJsonSerializer method serialize.
@Override
public void serialize(ListResponse listResponse, JsonGenerator jGen, SerializerProvider provider) throws IOException {
try {
jGen.writeStartObject();
jGen.writeArrayFieldStart("schemas");
for (String schema : listResponse.getSchemas()) jGen.writeString(schema);
jGen.writeEndArray();
jGen.writeNumberField("totalResults", listResponse.getTotalResults());
if (!skipResults) {
if (listResponse.getItemsPerPage() > 0) {
// these two bits are "REQUIRED when partial results are returned due to pagination." (section 3.4.2 RFC 7644)
jGen.writeNumberField("startIndex", listResponse.getStartIndex());
jGen.writeNumberField("itemsPerPage", listResponse.getItemsPerPage());
}
// Section 3.4.2 RFC 7644: Resources [...] REQUIRED if "totalResults" is non-zero
if (listResponse.getTotalResults() > 0) {
jGen.writeArrayFieldStart("Resources");
if (listResponse.getResources().size() > 0)
for (BaseScimResource resource : listResponse.getResources()) {
JsonNode jsonResource = mapper.readTree(resourceSerializer.serialize(resource, attributes, excludeAttributes));
jGen.writeTree(jsonResource);
}
else if (jsonResources != null)
for (JsonNode node : jsonResources) jGen.writeTree(node);
jGen.writeEndArray();
}
}
jGen.writeEndObject();
} catch (Exception e) {
throw new IOException(e);
}
}
use of org.gluu.oxtrust.model.scim2.ListResponse in project oxTrust by GluuFederation.
the class FidoDeviceWebService method searchDevices.
@GET
@Produces({ Constants.MEDIA_TYPE_SCIM_JSON + "; charset=utf-8", MediaType.APPLICATION_JSON + "; charset=utf-8" })
@HeaderParam("Accept")
@DefaultValue(Constants.MEDIA_TYPE_SCIM_JSON)
@ApiOperation(value = "Search devices", notes = "Returns a list of devices (https://tools.ietf.org/html/rfc7644#section-3.4.2.2)", response = ListResponse.class)
public Response searchDevices(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @QueryParam("userId") final String userId, @QueryParam(OxTrustConstants.QUERY_PARAMETER_FILTER) final String filterString, @QueryParam(OxTrustConstants.QUERY_PARAMETER_START_INDEX) final int startIndex, @QueryParam(OxTrustConstants.QUERY_PARAMETER_COUNT) final int count, @QueryParam(OxTrustConstants.QUERY_PARAMETER_SORT_BY) final String sortBy, @QueryParam(OxTrustConstants.QUERY_PARAMETER_SORT_ORDER) final String sortOrder, @QueryParam(OxTrustConstants.QUERY_PARAMETER_ATTRIBUTES) final String attributesArray) throws Exception {
Response authorizationResponse;
if (jsonConfigurationService.getOxTrustappConfiguration().isScimTestMode()) {
log.info(" ##### SCIM Test Mode is ACTIVE");
authorizationResponse = processTestModeAuthorization(token);
} else {
authorizationResponse = processAuthorization(authorization);
}
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
if (count > getMaxCount()) {
String detail = "Too many results (=" + count + ") would be returned; max is " + getMaxCount() + " only.";
return getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.TOO_MANY, detail);
} else {
log.info(" Searching devices from LDAP ");
String baseDn = fidoDeviceService.getDnForFidoDevice(userId, null);
log.info("##### baseDn = " + baseDn);
VirtualListViewResponse vlvResponse = new VirtualListViewResponse();
List<GluuCustomFidoDevice> gluuCustomFidoDevices = search(baseDn, GluuCustomFidoDevice.class, filterString, startIndex, count, sortBy, sortOrder, vlvResponse, attributesArray);
ListResponse devicesListResponse = new ListResponse();
List<String> schema = new ArrayList<String>();
schema.add(Constants.LIST_RESPONSE_SCHEMA_ID);
log.info(" setting schema");
devicesListResponse.setSchemas(schema);
// Set total
devicesListResponse.setTotalResults(vlvResponse.getTotalResults());
if (count > 0 && gluuCustomFidoDevices != null && !gluuCustomFidoDevices.isEmpty()) {
for (GluuCustomFidoDevice gluuCustomFidoDevice : gluuCustomFidoDevices) {
FidoDevice fidoDevice = copyUtils2.copy(gluuCustomFidoDevice, new FidoDevice());
devicesListResponse.getResources().add(fidoDevice);
}
// Set the rest of results info
devicesListResponse.setItemsPerPage(vlvResponse.getItemsPerPage());
devicesListResponse.setStartIndex(vlvResponse.getStartIndex());
}
// Serialize to JSON
String json = serializeToJson(devicesListResponse, attributesArray);
URI location = new URI(appConfiguration.getBaseEndpoint() + "/scim/v2/FidoDevices");
return Response.ok(json).location(location).build();
}
} catch (Exception e) {
log.error("Error in searchDevices", e);
e.printStackTrace();
return getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_FILTER, INTERNAL_SERVER_ERROR_MESSAGE);
}
}
use of org.gluu.oxtrust.model.scim2.ListResponse in project oxTrust by GluuFederation.
the class GroupWebService method searchGroups.
@GET
@Produces({ Constants.MEDIA_TYPE_SCIM_JSON + "; charset=utf-8", MediaType.APPLICATION_JSON + "; charset=utf-8" })
@HeaderParam("Accept")
@DefaultValue(Constants.MEDIA_TYPE_SCIM_JSON)
@ApiOperation(value = "Search groups", notes = "Returns a list of groups (https://tools.ietf.org/html/rfc7644#section-3.4.2.2)", response = ListResponse.class)
public Response searchGroups(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @QueryParam(OxTrustConstants.QUERY_PARAMETER_FILTER) final String filterString, @QueryParam(OxTrustConstants.QUERY_PARAMETER_START_INDEX) final int startIndex, @QueryParam(OxTrustConstants.QUERY_PARAMETER_COUNT) final int count, @QueryParam(OxTrustConstants.QUERY_PARAMETER_SORT_BY) final String sortBy, @QueryParam(OxTrustConstants.QUERY_PARAMETER_SORT_ORDER) final String sortOrder, @QueryParam(OxTrustConstants.QUERY_PARAMETER_ATTRIBUTES) final String attributesArray) throws Exception {
Response authorizationResponse;
if (jsonConfigurationService.getOxTrustappConfiguration().isScimTestMode()) {
log.info(" ##### SCIM Test Mode is ACTIVE");
authorizationResponse = processTestModeAuthorization(token);
} else {
authorizationResponse = processAuthorization(authorization);
}
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
if (count > getMaxCount()) {
String detail = "Too many results (=" + count + ") would be returned; max is " + getMaxCount() + " only.";
return getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.TOO_MANY, detail);
} else {
log.info(" Searching groups from LDAP ");
VirtualListViewResponse vlvResponse = new VirtualListViewResponse();
List<GluuGroup> groupList = search(groupService.getDnForGroup(null), GluuGroup.class, filterString, startIndex, count, sortBy, sortOrder, vlvResponse, attributesArray);
// List<GluuGroup> groupList = groupService.getAllGroupsList();
ListResponse groupsListResponse = new ListResponse();
List<String> schema = new ArrayList<String>();
schema.add(Constants.LIST_RESPONSE_SCHEMA_ID);
log.info(" setting schema");
groupsListResponse.setSchemas(schema);
// Set total
groupsListResponse.setTotalResults(vlvResponse.getTotalResults());
if (count > 0 && groupList != null && !groupList.isEmpty()) {
for (GluuGroup gluuGroup : groupList) {
Group group = copyUtils2.copy(gluuGroup, null);
log.info(" group to be added displayName : " + group.getDisplayName());
groupsListResponse.getResources().add(group);
log.info(" group added? : " + groupsListResponse.getResources().contains(group));
}
// Set the rest of results info
groupsListResponse.setItemsPerPage(vlvResponse.getItemsPerPage());
groupsListResponse.setStartIndex(vlvResponse.getStartIndex());
}
// Serialize to JSON
String json = serializeToJson(groupsListResponse, attributesArray);
URI location = new URI(appConfiguration.getBaseEndpoint() + "/scim/v2/Groups");
return Response.ok(json).location(location).build();
}
} catch (Exception ex) {
log.error("Error in searchGroups", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_FILTER, INTERNAL_SERVER_ERROR_MESSAGE);
}
}
use of org.gluu.oxtrust.model.scim2.ListResponse in project oxTrust by GluuFederation.
the class SchemaWebService method serve.
@GET
@Produces(MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT)
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@RejectFilterParam
public Response serve() {
Response response;
try {
int total = resourceSchemas.size();
ListResponse listResponse = new ListResponse(1, total, total);
for (String urn : resourceSchemas.keySet()) {
listResponse.addResource(getSchemaInstance(resourceSchemas.get(urn), urn));
}
String json = resourceSerializer.getListResponseMapper().writeValueAsString(listResponse);
response = Response.ok(json).location(new URI(endpointUrl)).build();
} catch (Exception e) {
log.error("Failure at serve method", e);
response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
}
return response;
}
use of org.gluu.oxtrust.model.scim2.ListResponse in project oxTrust by GluuFederation.
the class SearchResourcesWebService method search.
@POST
@Consumes({ MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT, MediaType.APPLICATION_JSON + UTF8_CHARSET_FRAGMENT })
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@ProtectedApi
@RefAdjusted
@ApiOperation(value = "General search POST /.search", notes = "Returns a list of resources (https://tools.ietf.org/html/rfc7644#section-3.4.3)", response = ListResponse.class)
public Response search(@ApiParam(value = "SearchRequest", required = true) SearchRequest searchRequest) {
SearchRequest searchReq = new SearchRequest();
Response response = prepareSearchRequest(searchRequest.getSchemas(), searchRequest.getFilter(), searchRequest.getSortBy(), searchRequest.getSortOrder(), searchRequest.getStartIndex(), searchRequest.getCount(), searchRequest.getAttributesStr(), searchRequest.getExcludedAttributesStr(), searchReq);
if (response == null) {
try {
List<JsonNode> resources = new ArrayList<JsonNode>();
Pair<Integer, Integer> totals = computeResults(searchReq, resources);
ListResponseJsonSerializer custSerializer = new ListResponseJsonSerializer(resourceSerializer, searchReq.getAttributesStr(), searchReq.getExcludedAttributesStr(), searchReq.getCount() == 0);
if (resources.size() > 0)
custSerializer.setJsonResources(resources);
ObjectMapper objmapper = new ObjectMapper();
SimpleModule module = new SimpleModule("ListResponseModule", Version.unknownVersion());
module.addSerializer(ListResponse.class, custSerializer);
objmapper.registerModule(module);
// Provide to constructor original start index, and totals calculated in computeResults call
ListResponse listResponse = new ListResponse(searchReq.getStartIndex(), totals.getFirst(), totals.getSecond());
String json = objmapper.writeValueAsString(listResponse);
response = Response.ok(json).location(new URI(endpointUrl)).build();
} catch (Exception e) {
log.error("Failure at search method", e);
response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
}
}
return response;
}
Aggregations