Search in sources :

Example 1 with ListResponseUserSerializer

use of org.gluu.oxtrust.service.antlr.scimFilter.util.ListResponseUserSerializer in project oxTrust by GluuFederation.

the class BulkWebService method processBulkOperations.

@POST
@Consumes({ Constants.MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@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 = "Bulk Operations", notes = "Bulk Operations (https://tools.ietf.org/html/rfc7644#section-3.7)", response = BulkResponse.class)
public Response processBulkOperations(// @Context HttpServletResponse response,
@HeaderParam("Authorization") String authorization, @HeaderParam("Content-Length") int contentLength, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @ApiParam(value = "BulkRequest", required = true) BulkRequest bulkRequest) 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 {
        /*
			 * J2EContext context = new J2EContext(request, response); int
			 * removePathLength = "/Bulk".length(); String domain =
			 * context.getFullRequestURL(); if (domain.endsWith("/")) {
			 * removePathLength++; } domain = domain.substring(0,
			 * domain.length() - removePathLength);
			 */
        log.info("##### Operation count = " + bulkRequest.getOperations().size());
        log.info("##### Content-Length = " + contentLength);
        if (bulkRequest.getOperations().size() > MAX_BULK_OPERATIONS || contentLength > MAX_BULK_PAYLOAD_SIZE) {
            StringBuilder message = new StringBuilder("The size of the bulk operation exceeds the ");
            if (bulkRequest.getOperations().size() > MAX_BULK_OPERATIONS && contentLength <= MAX_BULK_PAYLOAD_SIZE) {
                message.append("maxOperations (").append(MAX_BULK_OPERATIONS).append(")");
            } else if (bulkRequest.getOperations().size() <= MAX_BULK_OPERATIONS && contentLength > MAX_BULK_PAYLOAD_SIZE) {
                message.append("maxPayloadSize (").append(MAX_BULK_PAYLOAD_SIZE).append(")");
            } else if (bulkRequest.getOperations().size() > MAX_BULK_OPERATIONS && contentLength > MAX_BULK_PAYLOAD_SIZE) {
                message.append("maxOperations (").append(MAX_BULK_OPERATIONS).append(") ");
                message.append("and ");
                message.append("maxPayloadSize (").append(MAX_BULK_PAYLOAD_SIZE).append(")");
            }
            log.info("Payload Too Large: " + message.toString());
            return getErrorResponse(413, message.toString());
        }
        int failOnErrorsLimit = (bulkRequest.getFailOnErrors() != null) ? bulkRequest.getFailOnErrors() : 0;
        int failOnErrorsCount = 0;
        List<BulkOperation> bulkOperations = bulkRequest.getOperations();
        BulkResponse bulkResponse = new BulkResponse();
        Map<String, String> processedBulkIds = new LinkedHashMap<String, String>();
        operationsLoop: for (BulkOperation operation : bulkOperations) {
            log.info(" Checking operations... ");
            if (operation.getPath().startsWith("/Users")) {
                // operation = processUserOperation(operation, domain);
                operation = processUserOperation(operation, processedBulkIds);
            } else if (operation.getPath().startsWith("/Groups")) {
                // operation = processGroupOperation(operation, domain);
                operation = processGroupOperation(operation, processedBulkIds);
            }
            bulkResponse.getOperations().add(operation);
            // Error handling
            String okCode = String.valueOf(Response.Status.OK.getStatusCode());
            String createdCode = String.valueOf(Response.Status.CREATED.getStatusCode());
            if (!operation.getStatus().equalsIgnoreCase(okCode) && !operation.getStatus().equalsIgnoreCase(createdCode)) {
                failOnErrorsCount++;
                if ((failOnErrorsLimit > 0) && (failOnErrorsCount >= failOnErrorsLimit)) {
                    break operationsLoop;
                }
            }
        }
        URI location = new URI(appConfiguration.getBaseEndpoint() + "/scim/v2/Bulk");
        // Serialize to JSON
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
        SimpleModule customBulkOperationsModule = new SimpleModule("CustomBulkOperationsModule", new Version(1, 0, 0, ""));
        // Custom serializers for both User and Group
        ListResponseUserSerializer userSerializer = new ListResponseUserSerializer();
        ListResponseGroupSerializer groupSerializer = new ListResponseGroupSerializer();
        customBulkOperationsModule.addSerializer(User.class, userSerializer);
        customBulkOperationsModule.addSerializer(Group.class, groupSerializer);
        mapper.registerModule(customBulkOperationsModule);
        String json = mapper.writeValueAsString(bulkResponse);
        return Response.ok(json).location(location).build();
    } catch (Exception ex) {
        log.error("Error in processBulkOperations", ex);
        ex.printStackTrace();
        return getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE);
    }
}
Also used : ListResponseUserSerializer(org.gluu.oxtrust.service.antlr.scimFilter.util.ListResponseUserSerializer) BulkOperation(org.gluu.oxtrust.model.scim2.BulkOperation) BulkResponse(org.gluu.oxtrust.model.scim2.BulkResponse) URI(java.net.URI) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) LinkedHashMap(java.util.LinkedHashMap) Response(javax.ws.rs.core.Response) BulkResponse(org.gluu.oxtrust.model.scim2.BulkResponse) ErrorResponse(org.gluu.oxtrust.model.scim2.ErrorResponse) Version(org.codehaus.jackson.Version) ListResponseGroupSerializer(org.gluu.oxtrust.service.antlr.scimFilter.util.ListResponseGroupSerializer) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) SimpleModule(org.codehaus.jackson.map.module.SimpleModule) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(com.wordnik.swagger.annotations.ApiOperation)

Example 2 with ListResponseUserSerializer

use of org.gluu.oxtrust.service.antlr.scimFilter.util.ListResponseUserSerializer in project oxTrust by GluuFederation.

the class UserWebService method serializeToJson.

private String serializeToJson(Object object, String attributesArray) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
    SimpleModule customScimFilterModule = new SimpleModule("CustomScim2UserFilterModule", new Version(1, 0, 0, ""));
    ListResponseUserSerializer serializer = new ListResponseUserSerializer();
    serializer.setAttributesArray(attributesArray);
    customScimFilterModule.addSerializer(User.class, serializer);
    mapper.registerModule(customScimFilterModule);
    return mapper.writeValueAsString(object);
}
Also used : ListResponseUserSerializer(org.gluu.oxtrust.service.antlr.scimFilter.util.ListResponseUserSerializer) Version(org.codehaus.jackson.Version) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) SimpleModule(org.codehaus.jackson.map.module.SimpleModule)

Aggregations

Version (org.codehaus.jackson.Version)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 SimpleModule (org.codehaus.jackson.map.module.SimpleModule)2 ListResponseUserSerializer (org.gluu.oxtrust.service.antlr.scimFilter.util.ListResponseUserSerializer)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 URI (java.net.URI)1 LinkedHashMap (java.util.LinkedHashMap)1 Consumes (javax.ws.rs.Consumes)1 DefaultValue (javax.ws.rs.DefaultValue)1 HeaderParam (javax.ws.rs.HeaderParam)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Response (javax.ws.rs.core.Response)1 PersonRequiredFieldsException (org.gluu.oxtrust.exception.PersonRequiredFieldsException)1 BulkOperation (org.gluu.oxtrust.model.scim2.BulkOperation)1 BulkResponse (org.gluu.oxtrust.model.scim2.BulkResponse)1 ErrorResponse (org.gluu.oxtrust.model.scim2.ErrorResponse)1 ListResponseGroupSerializer (org.gluu.oxtrust.service.antlr.scimFilter.util.ListResponseGroupSerializer)1 DuplicateEntryException (org.gluu.site.ldap.exception.DuplicateEntryException)1 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)1