use of io.jans.scim.model.scim2.bulk.BulkResponse in project oxTrust by GluuFederation.
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
@ApiOperation(value = "Bulk Operations", notes = "Bulk Operations (https://tools.ietf.org/html/rfc7644#section-3.7)", response = BulkResponse.class)
public Response processBulkOperations(@ApiParam(value = "BulkRequest", required = true) 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<BulkOperation>();
Map<String, String> processedBulkIds = new HashMap<String, String>();
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;
}
use of io.jans.scim.model.scim2.bulk.BulkResponse in project jans by JanssenProject.
the class GroupsBulkTest method delete.
@Test(dependsOnMethods = "bulkObject", alwaysRun = true)
public void delete() {
logger.info("Cleaning...");
// Prepare a bulk with 2 deletes
List<BulkOperation> ops = new ArrayList<>();
BulkOperation op = new BulkOperation();
op.setMethod("DELETE");
op.setPath("/Groups/" + groupId);
ops.add(op);
op = new BulkOperation();
op.setMethod("DELETE");
op.setPath("/Users/" + userId);
ops.add(op);
BulkRequest breq = new BulkRequest();
breq.setOperations(ops);
// Execute and check success
Response response = client.processBulkOperations(breq);
assertEquals(response.getStatus(), Status.OK.getStatusCode());
BulkResponse bres = response.readEntity(BulkResponse.class);
ops = bres.getOperations();
assertTrue(ops.stream().allMatch(oper -> Integer.parseInt(oper.getStatus()) == Status.NO_CONTENT.getStatusCode()));
}
use of io.jans.scim.model.scim2.bulk.BulkResponse in project jans by JanssenProject.
the class UsersBulkTest method bulkJson1.
@Parameters("users_bulk1")
@Test
public void bulkJson1(String json) {
logger.info("Sending a bulk with POST, PUT, and PATCH operations...");
Response response = client.processBulkOperations(json);
assertEquals(response.getStatus(), Status.OK.getStatusCode());
BulkResponse br = response.readEntity(BulkResponse.class);
List<BulkOperation> ops = br.getOperations();
assertTrue(ops.size() > 2);
assertSuccessfulOps(ops);
String location = ops.get(0).getLocation();
assertNotNull(location);
id = location.substring(location.lastIndexOf("/") + 1);
logger.info("User id is {}", id);
}
use of io.jans.scim.model.scim2.bulk.BulkResponse in project jans by JanssenProject.
the class UsersBulkTest method bulkWithObject.
@Test(dependsOnMethods = "bulkJson1")
public void bulkWithObject() {
logger.info("Sending a bulk with one DELETE...");
BulkOperation op = new BulkOperation();
op.setMethod("DELETE");
op.setPath("/Users/" + id);
BulkRequest breq = new BulkRequest();
breq.setOperations(Collections.singletonList(op));
Response response = client.processBulkOperations(breq);
assertEquals(response.getStatus(), Status.OK.getStatusCode());
BulkResponse bres = response.readEntity(BulkResponse.class);
List<BulkOperation> ops = bres.getOperations();
assertEquals(ops.size(), 1);
// Verify resource was deleted
assertEquals(Status.NO_CONTENT.getStatusCode(), Integer.parseInt(ops.get(0).getStatus()));
}
use of io.jans.scim.model.scim2.bulk.BulkResponse in project jans by JanssenProject.
the class UsersBulkTest method bulkJson2.
@Parameters("users_bulk2")
@Test(dependsOnMethods = "bulkWithObject")
public void bulkJson2(String json) throws Exception {
logger.info("Sending a bulk with POSTs and DELETEs operations...");
Response response = client.processBulkOperations(json);
assertEquals(response.getStatus(), Status.OK.getStatusCode());
BulkResponse br = response.readEntity(BulkResponse.class);
List<BulkOperation> ops = br.getOperations();
// Check that the attempt to update non-existing user returned 404
BulkOperation failed = ops.remove(ops.size() - 1);
assertEquals(Integer.parseInt(failed.getStatus()), Status.NOT_FOUND.getStatusCode());
assertSuccessfulOps(ops);
}
Aggregations