use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class CopyUtils2 method copy.
/**
* Copy data from ScimPerson object to GluuCustomPerson object "Reda"
*
* @param source
* @param destination
* @return
* @throws Exception
*/
public GluuCustomPerson copy(User source, GluuCustomPerson destination, boolean isUpdate) throws Exception {
if (source == null || !isValidData(source, isUpdate)) {
return null;
}
if (destination == null) {
log.trace(" creating a new GluuCustomPerson instant ");
destination = new GluuCustomPerson();
}
log.trace(" setting schemas ");
destination.setSchemas(source.getSchemas());
if (isUpdate) {
personService.addCustomObjectClass(destination);
log.trace(" setting userName ");
if (source.getUserName() != null && source.getUserName().length() > 0) {
destination.setUid(source.getUserName());
}
if (source.getName() != null) {
log.trace(" setting givenname ");
if (source.getName().getGivenName() != null && source.getName().getGivenName().length() > 0) {
destination.setGivenName(source.getName().getGivenName());
}
log.trace(" setting famillyname ");
if (source.getName().getFamilyName() != null && source.getName().getFamilyName().length() > 0) {
destination.setSurname(source.getName().getFamilyName());
}
log.trace(" setting middlename ");
if (source.getName().getMiddleName() != null && source.getName().getMiddleName().length() > 0) {
// destination.setAttribute("oxTrustMiddleName", source.getName().getMiddleName());
destination.setAttribute("middleName", source.getName().getMiddleName());
}
log.trace(" setting honor");
if (source.getName().getHonorificPrefix() != null && source.getName().getHonorificPrefix().length() > 0) {
destination.setAttribute("oxTrusthonorificPrefix", source.getName().getHonorificPrefix());
}
if (source.getName().getHonorificSuffix() != null && source.getName().getHonorificSuffix().length() > 0) {
destination.setAttribute("oxTrusthonorificSuffix", source.getName().getHonorificSuffix());
}
}
log.trace(" setting displayname ");
if (source.getDisplayName() != null && source.getDisplayName().length() > 0) {
destination.setDisplayName(source.getDisplayName());
}
log.trace(" setting externalID ");
if (source.getExternalId() != null && source.getExternalId().length() > 0) {
destination.setAttribute("oxTrustExternalId", source.getExternalId());
}
log.trace(" setting nickname ");
if (source.getNickName() != null && source.getNickName().length() > 0) {
// destination.setAttribute("oxTrustNickName", source.getNickName());
destination.setAttribute("nickname", source.getNickName());
}
log.trace(" setting profileURL ");
if (source.getProfileUrl() != null && source.getProfileUrl().length() > 0) {
destination.setAttribute("oxTrustProfileURL", source.getProfileUrl());
}
// getting emails
log.trace(" setting emails ");
if (source.getEmails() != null && source.getEmails().size() > 0) {
/*
List<Email> emails = source.getEmails();
// StringWriter listOfEmails = new StringWriter();
// mapper.writeValue(listOfEmails, emails);
List<String> emailList = new ArrayList<String>();
for (Email email : emails) {
emailList.add(mapper.writeValueAsString(email));
}
// destination.setAttribute("oxTrustEmail", listOfEmails.toString());
destination.setAttribute("oxTrustEmail", emailList.toArray(new String[]{}));
*/
setAttributeListValue(destination, source.getEmails(), "oxTrustEmail");
}
// getting addresses
log.trace(" setting addresses ");
if (source.getAddresses() != null && source.getAddresses().size() > 0) {
setAttributeListValue(destination, source.getAddresses(), "oxTrustAddresses");
}
// getting phone numbers;
log.trace(" setting phoneNumbers ");
if (source.getPhoneNumbers() != null && source.getPhoneNumbers().size() > 0) {
setAttributeListValue(destination, source.getPhoneNumbers(), "oxTrustPhoneValue");
}
// getting ims
log.trace(" setting ims ");
if (source.getIms() != null && source.getIms().size() > 0) {
setAttributeListValue(destination, source.getIms(), "oxTrustImsValue");
}
// getting Photos
log.trace(" setting photos ");
if (source.getPhotos() != null && source.getPhotos().size() > 0) {
setAttributeListValue(destination, source.getPhotos(), "oxTrustPhotos");
}
if (source.getUserType() != null && source.getUserType().length() > 0) {
destination.setAttribute("oxTrustUserType", source.getUserType());
}
if (source.getTitle() != null && source.getTitle().length() > 0) {
destination.setAttribute("oxTrustTitle", source.getTitle());
}
if (source.getPreferredLanguage() != null && source.getPreferredLanguage().length() > 0) {
destination.setPreferredLanguage(source.getPreferredLanguage());
}
if (source.getLocale() != null && source.getLocale().length() > 0) {
// destination.setAttribute("oxTrustLocale", source.getLocale());
destination.setAttribute("locale", source.getLocale());
}
if (source.getTimezone() != null && source.getTimezone().length() > 0) {
destination.setTimezone(source.getTimezone());
}
if (source.isActive() != null) {
destination.setAttribute("oxTrustActive", source.isActive().toString());
}
if (source.getPassword() != null && source.getPassword().length() > 0) {
destination.setUserPassword(source.getPassword());
}
// getting user groups
log.trace(" setting groups ");
if (source.getGroups() != null && source.getGroups().size() > 0) {
List<GroupRef> listGroups = source.getGroups();
List<String> members = new ArrayList<String>();
for (GroupRef group : listGroups) {
members.add(groupService.getDnForGroup(group.getValue()));
}
destination.setMemberOf(members);
}
// getting roles
log.trace(" setting roles ");
if (source.getRoles() != null && source.getRoles().size() > 0) {
setAttributeListValue(destination, source.getRoles(), "oxTrustRole");
}
// getting entitlements
log.trace(" setting entitlements ");
if (source.getEntitlements() != null && source.getEntitlements().size() > 0) {
setAttributeListValue(destination, source.getEntitlements(), "oxTrustEntitlements");
}
// getting x509Certificates
log.trace(" setting certs ");
if (source.getX509Certificates() != null && source.getX509Certificates().size() > 0) {
setAttributeListValue(destination, source.getX509Certificates(), "oxTrustx509Certificate");
}
log.trace(" setting extensions ");
if (source.getExtensions() != null && (source.getExtensions().size() > 0)) {
destination.setExtensions(source.getExtensions());
}
/*
// getting customAttributes
log.trace("getting custom attributes");
if (source.getCustomAttributes() != null) {
log.trace("source.getCustomAttributes() != null");
log.trace("getting a list of ScimCustomAttributes");
List<CustomAttributes> customAttr = source.getCustomAttributes();
log.trace("checling every attribute in the request");
for (CustomAttributes oneAttr : customAttr) {
if (oneAttr == null) {
continue;
}
int countValues = oneAttr.getValues().size();
if (countValues == 0) {
log.trace("setting a empty attribute");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), oneAttr.getValues().toArray(new String[0]));
} else if (countValues == 1) {
log.trace("setting a single attribute");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), oneAttr.getValues().get(0));
} else if (countValues > 1) {
log.trace("setting a multivalued attribute");
List<String> listOfAttr = oneAttr.getValues();
String[] AttrArray = new String[listOfAttr.size()];
int i = 0;
for (String oneValue : listOfAttr) {
if (oneValue != null && oneValue.length() > 0) {
log.trace("setting a value");
AttrArray[i] = oneValue;
i++;
}
}
log.trace("setting the list of multivalued attributes");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), AttrArray);
}
}
}
*/
// log.trace("getting meta attributes");
/*
if (source.getMeta()!=null && source.getMeta().getAttributes() != null) {
log.trace("source.getCustomAttributes() != null");
log.trace("getting a list of ScimCustomAttributes");
Set<String> customAttr = source.getMeta().getAttributes();
log.trace("checling every attribute in the request");
for (String oneAttr : customAttr) {
if (oneAttr == null) {
continue;
}
destination.setAttribute(oneAttr.replaceAll(" ", ""), "");
*/
/* NOTE : WRITE CODE FOR THIS
int countValues = oneAttr.getValues().size();
if (countValues == 0) {
log.trace("setting a empty attribute");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), oneAttr.getValues().toArray(new String[0]));
} else if (countValues == 1) {
log.trace("setting a single attribute");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), oneAttr.getValues().get(0));
} else if (countValues > 1) {
log.trace("setting a multivalued attribute");
List<String> listOfAttr = oneAttr.getValues();
String[] AttrArray = new String[listOfAttr.size()];
int i = 0;
for (String oneValue : listOfAttr) {
if (oneValue != null && oneValue.length() > 0) {
log.trace("setting a value");
AttrArray[i] = oneValue;
i++;
}
}
log.trace("setting the list of multivalued attributes");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), AttrArray);
}
*/
/*
}
}
*/
} else {
try {
if (personService.getPersonByUid(source.getUserName()) != null) {
throw new DuplicateEntryException("Duplicate UID value: " + source.getUserName());
}
personService.addCustomObjectClass(destination);
log.trace(" setting userName ");
if (source.getUserName() != null && source.getUserName().length() > 0) {
destination.setUid(source.getUserName());
}
if (source.getName() != null) {
log.trace(" setting givenname ");
if (source.getName().getGivenName() != null && source.getName().getGivenName().length() > 0) {
destination.setGivenName(source.getName().getGivenName());
}
log.trace(" setting famillyname ");
if (source.getName().getFamilyName() != null && source.getName().getFamilyName().length() > 0) {
destination.setSurname(source.getName().getFamilyName());
}
log.trace(" setting middlename ");
if (source.getName().getMiddleName() != null && source.getName().getMiddleName().length() > 0) {
destination.setAttribute("middleName", source.getName().getMiddleName());
}
log.trace(" setting honor");
if (source.getName().getHonorificPrefix() != null && source.getName().getHonorificPrefix().length() > 0) {
destination.setAttribute("oxTrusthonorificPrefix", source.getName().getHonorificPrefix());
}
if (source.getName().getHonorificSuffix() != null && source.getName().getHonorificSuffix().length() > 0) {
destination.setAttribute("oxTrusthonorificSuffix", source.getName().getHonorificSuffix());
}
}
log.trace(" setting displayname ");
if (source.getDisplayName() != null && source.getDisplayName().length() > 0) {
destination.setDisplayName(source.getDisplayName());
}
log.trace(" setting externalID ");
if (source.getExternalId() != null && source.getExternalId().length() > 0) {
destination.setAttribute("oxTrustExternalId", source.getExternalId());
}
log.trace(" setting nickname ");
if (source.getNickName() != null && source.getNickName().length() > 0) {
destination.setAttribute("nickname", source.getNickName());
}
log.trace(" setting profileURL ");
if (source.getProfileUrl() != null && source.getProfileUrl().length() > 0) {
destination.setAttribute("oxTrustProfileURL", source.getProfileUrl());
}
// getting emails
log.trace(" setting emails ");
if (source.getEmails() != null && source.getEmails().size() > 0) {
/*
List<Email> emails = source.getEmails();
// StringWriter listOfEmails = new StringWriter();
// mapper.writeValue(listOfEmails, emails);
List<String> emailList = new ArrayList<String>();
for (Email email : emails) {
emailList.add(mapper.writeValueAsString(email));
}
// destination.setAttribute("oxTrustEmail", listOfEmails.toString());
destination.setAttribute("oxTrustEmail", emailList.toArray(new String[]{}));
*/
setAttributeListValue(destination, source.getEmails(), "oxTrustEmail");
}
// getting addresses
log.trace(" setting addresses ");
if (source.getAddresses() != null && source.getAddresses().size() > 0) {
setAttributeListValue(destination, source.getAddresses(), "oxTrustAddresses");
}
// getting phone numbers;
log.trace(" setting phoneNumbers ");
if (source.getPhoneNumbers() != null && source.getPhoneNumbers().size() > 0) {
setAttributeListValue(destination, source.getPhoneNumbers(), "oxTrustPhoneValue");
}
// getting ims
log.trace(" setting ims ");
if (source.getIms() != null && source.getIms().size() > 0) {
setAttributeListValue(destination, source.getIms(), "oxTrustImsValue");
}
// getting Photos
log.trace(" setting photos ");
if (source.getPhotos() != null && source.getPhotos().size() > 0) {
setAttributeListValue(destination, source.getPhotos(), "oxTrustPhotos");
}
if (source.getUserType() != null && source.getUserType().length() > 0) {
destination.setAttribute("oxTrustUserType", source.getUserType());
}
if (source.getTitle() != null && source.getTitle().length() > 0) {
destination.setAttribute("oxTrustTitle", source.getTitle());
}
if (source.getPreferredLanguage() != null && source.getPreferredLanguage().length() > 0) {
destination.setPreferredLanguage(source.getPreferredLanguage());
}
if (source.getLocale() != null && source.getLocale().length() > 0) {
// destination.setAttribute("oxTrustLocale", source.getLocale());
destination.setAttribute("locale", source.getLocale());
}
if (source.getTimezone() != null && source.getTimezone().length() > 0) {
destination.setTimezone(source.getTimezone());
}
if (source.isActive() != null) {
destination.setAttribute("oxTrustActive", source.isActive().toString());
}
if (source.getPassword() != null && source.getPassword().length() > 0) {
destination.setUserPassword(source.getPassword());
}
// getting user groups
log.trace(" setting groups ");
if (source.getGroups() != null && source.getGroups().size() > 0) {
List<GroupRef> listGroups = source.getGroups();
List<String> members = new ArrayList<String>();
for (GroupRef group : listGroups) {
members.add(groupService.getDnForGroup(group.getValue()));
}
destination.setMemberOf(members);
}
// getting roles
log.trace(" setting roles ");
if (source.getRoles() != null && source.getRoles().size() > 0) {
setAttributeListValue(destination, source.getRoles(), "oxTrustRole");
}
// getting entitlements
log.trace(" setting entitlements ");
if (source.getEntitlements() != null && source.getEntitlements().size() > 0) {
setAttributeListValue(destination, source.getEntitlements(), "oxTrustEntitlements");
}
// getting x509Certificates
log.trace(" setting certs ");
if (source.getX509Certificates() != null && source.getX509Certificates().size() > 0) {
setAttributeListValue(destination, source.getX509Certificates(), "oxTrustx509Certificate");
}
log.trace(" setting extensions ");
if (source.getExtensions() != null && (source.getExtensions().size() > 0)) {
destination.setExtensions(source.getExtensions());
}
/*
// getting customAttributes
log.trace("getting custom attributes");
if (source.getCustomAttributes() != null && source.getCustomAttributes().size() > 0) {
log.trace("source.getCustomAttributes() != null");
log.trace("getting a list of CustomAttributes");
List<CustomAttributes> customAttr = source.getCustomAttributes();
log.trace("checling every attribute in the request");
for (CustomAttributes oneAttr : customAttr) {
if (oneAttr != null && oneAttr.getValues().size() == 1) {
log.trace("setting a single attribute");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), oneAttr.getValues().get(0));
} else if (oneAttr != null && oneAttr.getValues().size() > 1) {
log.trace("setting a multivalued attribute");
List<String> listOfAttr = oneAttr.getValues();
String[] AttrArray = new String[listOfAttr.size()];
int i = 0;
for (String oneValue : listOfAttr) {
if (oneValue != null && oneValue.length() > 0) {
log.trace("setting a value");
AttrArray[i] = oneValue;
i++;
}
}
log.trace("setting the list of multivalued attributes");
destination.setAttribute(oneAttr.getName().replaceAll(" ", ""), AttrArray);
}
}
}
*/
} catch (DuplicateEntryException e) {
throw e;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
setGluuStatus(source, destination);
return destination;
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class UserWebService method updateUser.
@Path("{id}")
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateUser(@HeaderParam("Authorization") String authorization, @PathParam("id") String id, ScimPerson person) throws Exception {
Response authorizationResponse = processAuthorization(authorization);
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
GluuCustomPerson gluuPerson = personService.getPersonByInum(id);
if (gluuPerson == null) {
return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
} else {
// Validate if attempting to update userName of a different id
if (person.getUserName() != null) {
GluuCustomPerson personToFind = new GluuCustomPerson();
personToFind.setUid(person.getUserName());
List<GluuCustomPerson> foundPersons = personService.findPersons(personToFind, 2);
if (foundPersons != null && foundPersons.size() > 0) {
for (GluuCustomPerson foundPerson : foundPersons) {
if (foundPerson != null && !foundPerson.getInum().equalsIgnoreCase(gluuPerson.getInum())) {
throw new DuplicateEntryException("Cannot update userName of a different id: " + person.getUserName());
}
}
}
}
}
GluuCustomPerson newGluuPerson = copyUtils.copy(person, gluuPerson, true);
if (person.getGroups().size() > 0) {
serviceUtil.groupMembersAdder(newGluuPerson, personService.getDnForPerson(id));
}
// Sync email, forward ("oxTrustEmail" -> "mail")
newGluuPerson = ServiceUtil.syncEmailForward(newGluuPerson, false);
// For custom script: update user
if (externalScimService.isEnabled()) {
externalScimService.executeScimUpdateUserMethods(newGluuPerson);
}
personService.updatePerson(newGluuPerson);
log.debug(" person updated ");
ScimPerson newPerson = copyUtils.copy(newGluuPerson, null);
// person_update = copyUtils.copy(gluuPerson, null, attributes);
URI location = new URI("/Users/" + id);
return Response.ok(newPerson).location(location).build();
} catch (EntryPersistenceException ex) {
ex.printStackTrace();
return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
} catch (DuplicateEntryException ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (Exception ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class UserWebService method createUser.
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createUser(@HeaderParam("Authorization") String authorization, ScimPerson person) throws Exception {
Response authorizationResponse = processAuthorization(authorization);
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
log.debug(" copying gluuperson ");
GluuCustomPerson gluuPerson = copyUtils.copy(person, null, false);
if (gluuPerson == null) {
return getErrorResponse("Failed to create user", Response.Status.BAD_REQUEST.getStatusCode());
}
log.debug(" generating inum ");
// inumService.generateInums(Configuration.INUM_TYPE_PEOPLE_SLUG);
String inum = personService.generateInumForNewPerson();
// //personService.generateInumForNewPerson();
log.debug(" getting DN ");
String dn = personService.getDnForPerson(inum);
log.debug(" getting iname ");
String iname = personService.generateInameForNewPerson(person.getUserName());
log.debug(" setting dn ");
gluuPerson.setDn(dn);
log.debug(" setting inum ");
gluuPerson.setInum(inum);
log.debug(" setting iname ");
gluuPerson.setIname(iname);
log.debug(" setting commonName ");
gluuPerson.setCommonName(gluuPerson.getGivenName() + " " + gluuPerson.getSurname());
log.info("gluuPerson.getMemberOf().size() : " + gluuPerson.getMemberOf().size());
if (person.getGroups().size() > 0) {
log.info(" jumping to groupMembersAdder ");
log.info("gluuPerson.getDn() : " + gluuPerson.getDn());
serviceUtil.groupMembersAdder(gluuPerson, gluuPerson.getDn());
}
// Sync email, forward ("oxTrustEmail" -> "mail")
gluuPerson = ServiceUtil.syncEmailForward(gluuPerson, false);
// For custom script: create user
if (externalScimService.isEnabled()) {
externalScimService.executeScimCreateUserMethods(gluuPerson);
}
log.debug("adding new GluuPerson");
personService.addPerson(gluuPerson);
ScimPerson newPerson = copyUtils.copy(gluuPerson, null);
String uri = "/Users/" + newPerson.getId();
return Response.created(URI.create(uri)).entity(newPerson).build();
} catch (DuplicateEntryException ex) {
log.error("Failed to create user", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (PersonRequiredFieldsException ex) {
log.error("PersonRequiredFieldsException: ", ex);
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (Exception ex) {
log.error("Failed to create user", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class UserWebService method createUser.
@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 = "Create user", notes = "Create user (https://tools.ietf.org/html/rfc7644#section-3.3)", response = User.class)
public Response createUser(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @ApiParam(value = "User", required = true) User user, @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 {
User createdUser = scim2UserService.createUser(user);
// Serialize to JSON
String json = serializeToJson(createdUser, attributesArray);
URI location = new URI(createdUser.getMeta().getLocation());
// Return HTTP response with status code 201 Created
return Response.created(location).entity(json).build();
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage());
} catch (PersonRequiredFieldsException ex) {
log.error("PersonRequiredFieldsException: ", ex);
return getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_VALUE, ex.getMessage());
} catch (Exception ex) {
log.error("Failed to create user", ex.getMessage());
return getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE);
}
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class FidoDeviceWebService method updateDevice.
@Path("{id}")
@PUT
@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 = "Update device", notes = "Update device (https://tools.ietf.org/html/rfc7644#section-3.5.1)", response = FidoDevice.class)
public Response updateDevice(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @PathParam("id") String id, @ApiParam(value = "FidoDevice", required = true) FidoDevice fidoDevice, @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 (!id.equalsIgnoreCase(fidoDevice.getId())) {
String detail = "Path param id does not match with device id";
return getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_VALUE, detail);
} else {
FidoDevice updatedFidoDevice = scim2FidoDeviceService.updateFidoDevice(id, fidoDevice);
// Serialize to JSON
String json = serializeToJson(updatedFidoDevice, attributesArray);
URI location = new URI(updatedFidoDevice.getMeta().getLocation());
return Response.ok(json).location(location).build();
}
} catch (EntryPersistenceException epe) {
log.error("Failed to update device", epe);
epe.printStackTrace();
return getErrorResponse(Response.Status.NOT_FOUND, ErrorScimType.INVALID_VALUE, "Resource " + id + " not found");
} catch (DuplicateEntryException dee) {
log.error("DuplicateEntryException", dee);
dee.printStackTrace();
return getErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, dee.getMessage());
} catch (Exception e) {
log.error("Failed to update device", e);
e.printStackTrace();
return getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE);
}
}
Aggregations