Search in sources :

Example 6 with BulkResponse

use of io.jans.scim.model.scim2.bulk.BulkResponse in project jans by JanssenProject.

the class GroupsBulkTest method bulkObject.

@Test(dependsOnMethods = "bulkJson")
public void bulkObject() {
    logger.info("Sending a bulk with a patch to insert admin user into group");
    // Creates a patch request consisting of adding the admin user to the group created
    PatchOperation po = new PatchOperation();
    po.setOperation("add");
    po.setPath("members.value");
    po.setValue(getAdminId());
    PatchRequest pr = new PatchRequest();
    pr.setOperations(Collections.singletonList(po));
    // Creates the bulk operation associated to the patch request
    BulkOperation bop = new BulkOperation();
    bop.setMethod("PATCH");
    bop.setPath("/Groups/" + groupId);
    bop.setData(mapper.convertValue(pr, new TypeReference<Map<String, Object>>() {
    }));
    BulkRequest breq = new BulkRequest();
    breq.setOperations(Collections.singletonList(bop));
    // Send bulk and check success of processing
    Response response = client.processBulkOperations(breq);
    assertEquals(response.getStatus(), Status.OK.getStatusCode());
    BulkResponse bres = response.readEntity(BulkResponse.class);
    assertSuccessfulOps(bres.getOperations());
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) BulkOperation(io.jans.scim.model.scim2.bulk.BulkOperation) BulkRequest(io.jans.scim.model.scim2.bulk.BulkRequest) PatchOperation(io.jans.scim.model.scim2.patch.PatchOperation) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 7 with BulkResponse

use of io.jans.scim.model.scim2.bulk.BulkResponse in project jans by JanssenProject.

the class GroupsBulkTest method bulkJson.

@Parameters("groups_bulk")
@Test
public void bulkJson(String json) {
    logger.info("Creating one user and one group using bulk json string");
    Response response = client.processBulkOperations(json);
    assertEquals(response.getStatus(), Status.OK.getStatusCode());
    BulkResponse br = response.readEntity(BulkResponse.class);
    List<BulkOperation> ops = br.getOperations();
    assertSuccessfulOps(ops);
    String location = ops.get(0).getLocation();
    assertNotNull(location);
    userId = location.substring(location.lastIndexOf("/") + 1);
    logger.info("User id is {}", userId);
    location = ops.get(1).getLocation();
    assertNotNull(location);
    groupId = location.substring(location.lastIndexOf("/") + 1);
    logger.info("Group id is {}", groupId);
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) BulkOperation(io.jans.scim.model.scim2.bulk.BulkOperation) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 8 with BulkResponse

use of io.jans.scim.model.scim2.bulk.BulkResponse in project jans by JanssenProject.

the class BulkWebService method processBulkOperations.

@javax.ws.rs.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(scopes = { "https://jans.io/scim/bulk" })
public Response processBulkOperations(BulkRequest request) {
    Response response = prepareRequest(request, getValueFromHeaders(httpHeaders, "Content-Length"));
    if (response == null) {
        log.debug("Executing web service method. processBulkOperations");
        int i, errors = 0;
        List<BulkOperation> operations = request.getOperations();
        List<BulkOperation> responseOperations = new ArrayList<>();
        Map<String, String> processedBulkIds = new HashMap<>();
        for (i = 0; i < operations.size() && errors < request.getFailOnErrors(); i++) {
            BulkOperation operation = operations.get(i);
            BulkOperation operationResponse = new BulkOperation();
            Response subResponse;
            String method = operation.getMethod();
            String bulkId = operation.getBulkId();
            try {
                String path = operation.getPath();
                BaseScimWebService service = getWSForPath(path);
                String fragment = getFragment(path, service, processedBulkIds);
                Verb verb = Verb.valueOf(method);
                String data = operation.getDataStr();
                if (!verb.equals(DELETE))
                    data = replaceBulkIds(data, processedBulkIds);
                Pair<Response, String> pair = execute(verb, service, data, fragment);
                String idCreated = pair.getSecond();
                subResponse = pair.getFirst();
                int status = subResponse.getStatus();
                if (familyOf(status).equals(SUCCESSFUL)) {
                    if (!verb.equals(DELETE)) {
                        if (verb.equals(POST)) {
                            // Update bulkIds
                            processedBulkIds.put(bulkId, idCreated);
                            fragment = idCreated;
                        }
                        String loc = service.getEndpointUrl() + "/" + fragment;
                        operationResponse.setLocation(loc);
                    }
                } else {
                    operationResponse.setResponse(subResponse.getEntity());
                    errors += familyOf(status).equals(CLIENT_ERROR) || familyOf(status).equals(SERVER_ERROR) ? 1 : 0;
                }
                subResponse.close();
                operationResponse.setStatus(Integer.toString(status));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                subResponse = getErrorResponse(BAD_REQUEST, ErrorScimType.INVALID_SYNTAX, e.getMessage());
                operationResponse.setStatus(Integer.toString(BAD_REQUEST.getStatusCode()));
                operationResponse.setResponse(subResponse.getEntity());
                errors++;
            }
            operationResponse.setBulkId(bulkId);
            operationResponse.setMethod(method);
            responseOperations.add(operationResponse);
            log.debug("Operation {} processed with status {}. Method {}, Accumulated errors {}", i + 1, operationResponse.getStatus(), method, errors);
        }
        try {
            BulkResponse bulkResponse = new BulkResponse();
            bulkResponse.setOperations(responseOperations);
            String json = mapper.writeValueAsString(bulkResponse);
            response = Response.ok(json).build();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response = getErrorResponse(INTERNAL_SERVER_ERROR, e.getMessage());
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) BulkOperation(io.jans.scim.model.scim2.bulk.BulkOperation) ArrayList(java.util.ArrayList) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) Response(javax.ws.rs.core.Response) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ProtectedApi(io.jans.scim.service.filter.ProtectedApi)

Aggregations

Response (javax.ws.rs.core.Response)8 BulkOperation (io.jans.scim.model.scim2.bulk.BulkOperation)7 BulkResponse (io.jans.scim.model.scim2.bulk.BulkResponse)7 Test (org.testng.annotations.Test)6 Parameters (org.testng.annotations.Parameters)4 ListResponse (io.jans.scim.model.scim2.ListResponse)3 BulkRequest (io.jans.scim.model.scim2.bulk.BulkRequest)3 BaseTest (io.jans.scim2.client.BaseTest)3 UserBaseTest (io.jans.scim2.client.UserBaseTest)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 PatchOperation (io.jans.scim.model.scim2.patch.PatchOperation)2 PatchRequest (io.jans.scim.model.scim2.patch.PatchRequest)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Consumes (javax.ws.rs.Consumes)2 DefaultValue (javax.ws.rs.DefaultValue)2 HeaderParam (javax.ws.rs.HeaderParam)2 Produces (javax.ws.rs.Produces)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 SearchRequest (io.jans.scim.model.scim2.SearchRequest)1