Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
midpoint-connectors/connector-scim2/src/main/java/edu/unc/polygon/connector/scim2/Scim2Connector.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
681 lines (645 sloc)
35.2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.unc.polygon.connector.scim2; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpRequest; | |
import org.apache.http.entity.ByteArrayEntity; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.CloseableHttpResponse; | |
import org.apache.http.client.methods.HttpHead; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.client.methods.HttpPatch; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
import org.apache.http.util.EntityUtils; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.auth.AuthenticationException; | |
import org.apache.http.client.utils.URIBuilder; | |
import org.apache.http.client.methods.*; | |
import org.apache.http.annotation.NotThreadSafe; | |
import org.json.JSONArray; | |
import org.json.JSONObject; | |
import org.json.JSONException; | |
//import org.identityconnectors.common.Base64; | |
import org.identityconnectors.common.StringUtil; | |
import org.identityconnectors.framework.common.objects.ObjectClassInfoBuilder; | |
import org.identityconnectors.framework.common.objects.Schema; | |
import org.identityconnectors.framework.common.objects.SchemaBuilder; | |
import org.identityconnectors.framework.spi.Configuration; | |
import org.identityconnectors.framework.spi.Connector; | |
import org.identityconnectors.framework.spi.PoolableConnector; | |
import org.identityconnectors.framework.spi.ConnectorClass; | |
import org.identityconnectors.framework.spi.operations.*; | |
import org.identityconnectors.framework.common.exceptions.*; | |
import org.identityconnectors.framework.common.exceptions.ConfigurationException; | |
import org.identityconnectors.framework.common.exceptions.ConnectorException; | |
import org.identityconnectors.framework.common.objects.*; | |
import org.identityconnectors.common.logging.Log; | |
import org.identityconnectors.common.security.GuardedString; | |
import org.identityconnectors.framework.common.objects.filter.FilterTranslator; | |
import com.evolveum.polygon.common.GuardedStringAccessor; | |
import java.io.IOException; | |
import java.util.Iterator; | |
import java.util.HashMap; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.ArrayList; | |
import java.io.*; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.lang.Object; | |
import java.util.Base64; | |
import java.time.LocalDateTime; | |
import com.evolveum.polygon.rest.AbstractRestConnector; | |
/** | |
* @author ethan@unc.edu | |
* | |
*/ | |
@ConnectorClass(displayNameKey = "connector.federation.manager.display", configurationClass = Scim2Configuration.class) | |
public class Scim2Connector extends AbstractRestConnector<Scim2Configuration> implements TestOp, SchemaOp, PoolableConnector, SearchOp<Scim2Filter>, SyncOp { | |
private static final Log LOG = Log.getLog(Scim2Connector.class); | |
private static final String ATTR_ID = "id"; //__UID__ | |
private static final String ATTR_USER_NAME = "userName"; // __NAME__ | |
private static final String ATTR_ACTIVE = "active"; | |
public static final String ATTR_EXTERNAL_ID = "externalId"; | |
//public static final String ATTR_ONYEN = "onyen"; | |
//public static final String ATTR_MAIL = "email"; | |
//public static final String ATTR_NAME = "id"; //hope to change this to username later __NAME__ | |
//public static final String ATTR_FAX_NUMBER = "faxnumber"; | |
//public static final String ATTR_MOBILE_NUMBER = "mobilenumber"; | |
//public static final String ATTR_PHONE_NUMBER = "phonenumber"; | |
//public static final String ATTR_ROLES = "roles"; | |
//public static final String ATTR_FIRST_NAME = "firstname"; | |
//public static final String ATTR_LAST_NAME = "lastname"; | |
//public static final String ATTR_MIDDLE_NAME = "middlename"; | |
//public static final String ATTR_INFORMAL_NAME = "informalname"; | |
//public static final String ATTR_WEBSITE = "website"; | |
public static final String CONTENT_TYPE = "application/json"; | |
private Scim2Configuration configuration; | |
@Override | |
public void init(Configuration configuration) { | |
LOG.info(">>> Initializing connector"); | |
if (!(configuration instanceof Scim2Configuration)) { | |
throw new ConfigurationException("Configuration is not instance of " + Scim2Configuration.class.getName()); | |
} | |
Scim2Configuration scim2Config = (Scim2Configuration) configuration; | |
scim2Config.validate(); | |
this.configuration = scim2Config; | |
//super.init(configuration); | |
getConfiguration(); | |
LOG.info(">>> Connector initialization finished"); | |
} | |
@Override | |
public void dispose() { | |
configuration = null; | |
//handler = null; | |
} | |
@Override | |
public void test() { | |
LOG.info(">>> TestOp"); | |
try { | |
CloseableHttpClient client = HttpClients.createDefault(); | |
HttpRequestBase restHead; | |
restHead = new HttpHead(configuration.getServiceAddress() + "/scim/v2/onyen/Users"); | |
authHeader(restHead); | |
CloseableHttpResponse headResponse = client.execute(restHead); | |
processResponseErrors(headResponse); | |
} catch (IOException IOE) { | |
throw new ConnectorIOException(IOE.getMessage(), IOE); | |
} | |
} | |
@Override | |
public void checkAlive() { | |
test(); | |
} | |
private void authHeader(HttpRequest request) { | |
//request.setHeader(configuration.getTokenName(), configuration.getTokenValue()); | |
GuardedString guardedPassword = configuration.getPassword(); | |
GuardedStringAccessor accessor = new GuardedStringAccessor(); | |
guardedPassword.access(accessor); | |
String credentials = configuration.getUsername() + ":" + accessor.getClearString(); | |
//LOG.info("credentials--" + credentials); | |
//byte[] credentialBytes = credentials.getBytes(); | |
//String encodedCredentials = Base64.encode(credentials.getBytes()); | |
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); | |
String credentialHeader = "Basic " + encodedCredentials; | |
LOG.info("credentialHeader--" + credentialHeader); | |
request.setHeader("Authorization", credentialHeader); | |
} | |
@Override | |
public Schema schema() { | |
SchemaBuilder schemaBuilder = new SchemaBuilder(Scim2Connector.class); | |
//Begin Account Objectclass | |
Set<AttributeInfo> accountAttributes = new HashSet<AttributeInfo>(); | |
AttributeInfoBuilder id = new AttributeInfoBuilder(); | |
id.setName(ATTR_ID); | |
id.setCreateable(true); | |
id.setUpdateable(true); | |
id.setReadable(true); | |
id.setRequired(true); | |
id.setMultiValued(false); | |
accountAttributes.add(id.build()); | |
AttributeInfoBuilder externalId = new AttributeInfoBuilder(); | |
externalId.setName(ATTR_EXTERNAL_ID); | |
externalId.setCreateable(true); | |
externalId.setUpdateable(true); | |
externalId.setReadable(true); | |
externalId.setRequired(false); | |
externalId.setMultiValued(false); | |
accountAttributes.add(externalId.build()); | |
AttributeInfoBuilder userName = new AttributeInfoBuilder(); | |
userName.setName(ATTR_USER_NAME); | |
userName.setCreateable(true); | |
userName.setUpdateable(true); | |
userName.setReadable(true); | |
userName.setRequired(false); | |
userName.setMultiValued(true); | |
accountAttributes.add(userName.build()); | |
//AttributeInfoBuilder onyen = new AttributeInfoBuilder(); | |
/*onyen.setName(ATTR_ONYEN); | |
onyen.setCreateable(true); | |
onyen.setUpdateable(true); | |
onyen.setReadable(true); | |
onyen.setRequired(false); | |
onyen.setMultiValued(true); | |
accountAttributes.add(onyen.build()); */ | |
AttributeInfoBuilder active = new AttributeInfoBuilder(); | |
active.setType(boolean.class); | |
active.setName(ATTR_ACTIVE); | |
active.setCreateable(true); | |
active.setUpdateable(true); | |
active.setReadable(true); | |
active.setRequired(false); | |
active.setMultiValued(false); | |
accountAttributes.add(active.build()); | |
accountAttributes.add(OperationalAttributeInfos.ENABLE); | |
schemaBuilder.defineObjectClass(ObjectClass.ACCOUNT_NAME,accountAttributes); | |
//Finish Account Objectclass, add others before return if needed | |
LOG.info(">>> schema finished"); | |
return schemaBuilder.build(); | |
} | |
public FilterTranslator<Scim2Filter> createFilterTranslator(ObjectClass oc, OperationOptions oo) { | |
LOG.info("inside createFilterTranslator"); | |
return new Scim2FilterTranslator(); | |
} | |
@Override | |
public SyncToken getLatestSyncToken(ObjectClass objectClass) { | |
return new SyncToken(LocalDateTime.now().toString() + "Z"); | |
} | |
@Override | |
public void sync(ObjectClass oc, SyncToken fromToken, SyncResultsHandler handler, OperationOptions oo) { | |
if (fromToken == null) { | |
fromToken = getLatestSyncToken(oc); | |
} | |
SyncToken afterToken = getLatestSyncToken(oc); | |
LOG.info("starting sync"); | |
LOG.info("ObjectClass.ACCOUNT_NAME is " + ObjectClass.ACCOUNT_NAME); | |
LOG.info("sync ObjectClass is " + oc.getObjectClassValue() + "--"); | |
LOG.info("fromToken value is " + fromToken); | |
String Scim2Attributes = ""; | |
String Scim2ExcludedAttributes = ""; | |
String Scim2Filter = ""; | |
String Scim2SortBy = ""; | |
String Scim2SortOrder = ""; | |
String Scim2StartIndex = "0"; | |
String Scim2Count = "2000"; | |
/* if (configuration.getScim2Attributes() != null && !StringUtil.isEmpty(configuration.getScim2Attributes())) { | |
Scim2Attributes = "attributes=" + configuration.getScim2Attributes(); | |
} | |
if (configuration.getScim2ExcludedAttributes() != null && !StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
Scim2ExcludedAttributes = "excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
if (configuration.getScim2Filter() != null && !StringUtil.isEmpty(configuration.getScim2Filter())) { | |
Scim2Filter = "filter=" + configuration.getScim2Filter(); | |
} | |
if (configuration.getScim2SortBy() != null && !StringUtil.isEmpty(configuration.getScim2SortBy())) { | |
Scim2SortBy = "sortBy=" + configuration.getScim2SortBy(); | |
} | |
if (configuration.getScim2SortOrder() != null && !StringUtil.isEmpty(configuration.getScim2SortOrder())) { | |
Scim2SortOrder = "sortOrder=" + configuration.getScim2SortOrder(); | |
}*/ | |
if (configuration.getScim2StartIndex() != null && !StringUtil.isEmpty(configuration.getScim2StartIndex())) { | |
Scim2StartIndex = "startIndex=" + configuration.getScim2StartIndex(); | |
} | |
if (configuration.getScim2Count() != null && !StringUtil.isEmpty(configuration.getScim2Count())) { | |
Scim2Count = "count=" + configuration.getScim2Count(); | |
} | |
//Integer pagedResultsOffset = oo.getPagedResultsOffset(); | |
Integer startIndex = new Integer(Scim2StartIndex); | |
String parameters = ""; | |
if (!StringUtil.isEmpty(configuration.getScim2Attributes())) { | |
parameters = "&attributes=" + configuration.getScim2Attributes(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
parameters = parameters + "&excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2Filter())) { | |
parameters = parameters + "&filter=" + configuration.getScim2Filter() + "+and+(meta.created+gt+%22" + (String)fromToken.getValue() + "%22+or+onyen.expirationDate+gt+%22" + (String)fromToken.getValue() + "%22)"; | |
} | |
else { | |
parameters = parameters + "&filter=" + "meta.created+gt+%22" + (String)fromToken.getValue() + "%22+or+onyen.expirationDate+gt+%22" + (String)fromToken.getValue()+ "%22"; | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2SortBy())) { | |
parameters = parameters + "&sortBy=" + configuration.getScim2SortBy(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2SortOrder())) { | |
parameters = parameters + "&sortOrder=" + configuration.getScim2SortOrder(); | |
} | |
LOG.info("Parameters for sync searcch are " + parameters); | |
boolean morePages = true; | |
while ( morePages ) { | |
try { | |
CloseableHttpClient client = HttpClients.createDefault(); | |
HttpRequestBase restGet; | |
restGet = new HttpGet(configuration.getServiceAddress() + "/scim/v2/onyen/Users/?startIndex=" + Scim2StartIndex + "&count=" + Scim2Count + parameters ); | |
authHeader(restGet); | |
//CloseableHttpResponse getResponse = client.execute(restGet); | |
//processResponseErrors(getResponse); | |
JSONObject searchResponseJson = new JSONObject(); | |
searchResponseJson = callRequest(restGet); | |
FileWriter file9 = new FileWriter("/tmp/file9.txt"); | |
file9.write(searchResponseJson.toString()); | |
System.out.println("Successfully Copied JSON Object to File..."); | |
System.out.println("\nJSON Object: " + searchResponseJson); | |
file9.flush(); | |
file9.close(); | |
JSONArray peopleArray = new JSONArray(); | |
if ( searchResponseJson.has("Resources") && searchResponseJson.get("Resources") != null ) { | |
peopleArray = searchResponseJson.getJSONArray("Resources"); | |
Integer Scim2CountInteger = new Integer(Scim2Count); | |
if ( peopleArray.length() < Scim2CountInteger ) { | |
morePages = false; | |
} | |
else { | |
startIndex = startIndex + Scim2CountInteger; | |
Scim2StartIndex = startIndex.toString(); | |
} | |
for ( int i = 0; i < peopleArray.length(); i++ ) { | |
JSONObject personJson = peopleArray.getJSONObject(i); | |
LOG.info(">>> processing ID " + i); | |
////handleUserId( handler, personJson ); | |
SyncDeltaBuilder builder = new SyncDeltaBuilder(); | |
builder.setDeltaType(SyncDeltaType.CREATE_OR_UPDATE); | |
builder.setObjectClass(ObjectClass.ACCOUNT); | |
builder.setToken(afterToken); | |
LOG.info("Building sync connector object for " + String.valueOf(personJson.getString(ATTR_ID))); | |
ConnectorObjectBuilder objectBuild = new ConnectorObjectBuilder(); | |
objectBuild.setUid(new Uid(String.valueOf(personJson.getString(ATTR_ID)))); | |
String [] potentialAttributes = { ATTR_ID, ATTR_EXTERNAL_ID, ATTR_USER_NAME, ATTR_ACTIVE}; | |
for ( String potentialAttribute: potentialAttributes ) { | |
LOG.info(">>> checking for attribute " + potentialAttribute ); | |
if ( personJson.has(potentialAttribute) && personJson.get(potentialAttribute) != null && !JSONObject.NULL.equals(personJson.get(potentialAttribute)) ) { | |
LOG.info(">>> found attribute " + potentialAttribute ); | |
if ( potentialAttribute.equals(ATTR_EXTERNAL_ID) ) { | |
//builder.setName(attributes.getString(potentialAttribute)); | |
objectBuild.setName(personJson.getString(ATTR_EXTERNAL_ID)); | |
addAttr(objectBuild, potentialAttribute, personJson.getString(potentialAttribute)); | |
} | |
if ( potentialAttribute.equals(ATTR_USER_NAME) ) { | |
//builder.setName(attributes.getString(potentialAttribute)); | |
//objectBuild.setName(personJson.getString(ATTR_USER_NAME)); | |
LOG.info(">>> Processing userName " + potentialAttribute ); | |
JSONObject onyenInfo = new JSONObject(); | |
onyenInfo = (JSONObject)personJson.get("urn:ietf:params:scim:schema:unc:core:2.0:Onyen"); | |
onyenInfo.put(ATTR_USER_NAME,personJson.getString(ATTR_USER_NAME)); | |
addAttr(objectBuild, ATTR_USER_NAME, onyenInfo.toString()); | |
String logSafeString = onyenInfo.toString().replace('{','$').replace('}','#'); | |
LOG.info(">>> Just added onyen information " + logSafeString ); | |
if ((String)onyenInfo.get("type") == "PRIMARY") { | |
objectBuild.setUid(personJson.getString(ATTR_ID)); | |
addAttr(objectBuild, ATTR_ID, personJson.getString(ATTR_ID)); | |
} | |
} | |
/*if ( potentialAttribute.equals(ATTR_ID) ) { | |
objectBuild.setUid(personJson.getString(ATTR_ID)); | |
addAttr(objectBuild, potentialAttribute, personJson.getString(potentialAttribute)); | |
}*/ | |
if ( potentialAttribute.equals(ATTR_ACTIVE) ) { | |
addAttr(objectBuild, potentialAttribute, personJson.getBoolean(potentialAttribute)); | |
} | |
/*else { | |
addAttr(objectBuild, potentialAttribute, personJson.getString(potentialAttribute)); | |
}*/ | |
} | |
} | |
ConnectorObject connectorObject = objectBuild.build(); | |
builder.setObject(connectorObject); | |
handler.handle(builder.build()); | |
} | |
} | |
else { | |
morePages = false; | |
LOG.info("No data object found in response"); | |
} | |
} catch (IOException IOE) { | |
throw new ConnectorIOException(IOE.getMessage(), IOE); | |
} | |
} | |
} | |
@Override | |
public void executeQuery(ObjectClass oc, Scim2Filter filter, ResultsHandler handler, OperationOptions oo) { | |
LOG.info("starting executeQuery"); | |
LOG.info("ObjectClass.ACCOUNT_NAME is " + ObjectClass.ACCOUNT_NAME); | |
LOG.info("executeQuery ObjectClass is " + oc.getObjectClassValue() + "--"); | |
String Scim2Attributes = ""; | |
String Scim2ExcludedAttributes = ""; | |
String Scim2Filter = ""; | |
String Scim2SortBy = ""; | |
String Scim2SortOrder = ""; | |
String Scim2StartIndex = "0"; | |
String Scim2Count = "2000"; | |
if (configuration.getScim2Attributes() != null && !StringUtil.isEmpty(configuration.getScim2Attributes())) { | |
Scim2Attributes = "attributes=" + configuration.getScim2Attributes(); | |
} | |
if (configuration.getScim2ExcludedAttributes() != null && !StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
Scim2ExcludedAttributes = "excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
if (configuration.getScim2Filter() != null && !StringUtil.isEmpty(configuration.getScim2Filter())) { | |
Scim2Filter = "filter=" + configuration.getScim2Filter(); | |
} | |
if (configuration.getScim2SortBy() != null && !StringUtil.isEmpty(configuration.getScim2SortBy())) { | |
Scim2SortBy = "sortBy=" + configuration.getScim2SortBy(); | |
} | |
if (configuration.getScim2SortOrder() != null && !StringUtil.isEmpty(configuration.getScim2SortOrder())) { | |
Scim2SortOrder = "sortOrder=" + configuration.getScim2SortOrder(); | |
} | |
if (configuration.getScim2StartIndex() != null && !StringUtil.isEmpty(configuration.getScim2StartIndex())) { | |
Scim2StartIndex = "startIndex=" + configuration.getScim2StartIndex(); | |
} | |
if (configuration.getScim2Count() != null && !StringUtil.isEmpty(configuration.getScim2Count())) { | |
Scim2Count = "count=" + configuration.getScim2Count(); | |
} | |
if ( oc.is(ObjectClass.ACCOUNT_NAME)) { | |
LOG.info("executeQuery ObjectClass.ACCOUNT_NAME"); | |
if (filter != null && filter.byUid != null) { | |
String searchId = filter.byUid; | |
String parameters = ""; | |
JSONObject uidSearchResponseJson = new JSONObject(); | |
if (!StringUtil.isEmpty(configuration.getScim2Attributes()) && !StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
parameters = "?attributes=" + configuration.getScim2Attributes() + "&excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
else if (!StringUtil.isEmpty(configuration.getScim2Attributes())) { | |
parameters = "?attributes=" + configuration.getScim2Attributes(); | |
} | |
else if (!StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
parameters = "?excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
HttpGet request = new HttpGet(configuration.getServiceAddress() + "/scim/v2/onyen/Users/" + searchId + parameters); | |
try { | |
uidSearchResponseJson = callRequest(request); | |
FileWriter file8 = new FileWriter("/tmp/file8.txt"); | |
file8.write(uidSearchResponseJson.toString()); | |
System.out.println("Successfully Copied JSON Object to File..."); | |
System.out.println("\nJSON Object: " + uidSearchResponseJson); | |
file8.flush(); | |
file8.close(); | |
//builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(ATTR_ID)))); | |
handleUserId( handler, uidSearchResponseJson ); | |
} catch (IOException IOE) { | |
throw new ConnectorIOException(IOE.getMessage(), IOE); | |
} | |
} | |
else if (filter != null && filter.byName != null) { | |
String searchName = filter.byName; | |
String parameters = ""; | |
JSONObject nameSearchResponseJson = new JSONObject(); | |
if (!StringUtil.isEmpty(configuration.getScim2Attributes()) && !StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
parameters = "?attributes=" + configuration.getScim2Attributes() + "&excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
else if (!StringUtil.isEmpty(configuration.getScim2Attributes())) { | |
parameters = "?attributes=" + configuration.getScim2Attributes(); | |
} | |
else if (!StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
parameters = "?excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
HttpGet request = new HttpGet(configuration.getServiceAddress() + "/scim/v2/onyen/Users/" + searchName + parameters); | |
try { | |
nameSearchResponseJson = callRequest(request); | |
FileWriter file8 = new FileWriter("/tmp/file8.txt"); | |
file8.write(nameSearchResponseJson.toString()); | |
System.out.println("Successfully Copied JSON Object to File..."); | |
System.out.println("\nJSON Object: " + nameSearchResponseJson); | |
file8.flush(); | |
file8.close(); | |
//builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(ATTR_ID)))); | |
handleUserId( handler, nameSearchResponseJson ); | |
} catch (IOException IOE) { | |
throw new ConnectorIOException(IOE.getMessage(), IOE); | |
} | |
} | |
else if (filter != null && filter.byExternalId != null) { | |
String searchExternalId = filter.byExternalId; | |
String parameters = ""; | |
Scim2Filter = "?filter=externalId+eq+%22" + searchExternalId + "%22"; | |
JSONObject externalIdSearchResponseJson = new JSONObject(); | |
if (!StringUtil.isEmpty(configuration.getScim2Attributes())) { | |
parameters = "&attributes=" + configuration.getScim2Attributes(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
parameters = parameters + "&excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
HttpGet request = new HttpGet(configuration.getServiceAddress() + "/scim/v2/onyen/Users/" + Scim2Filter + parameters); | |
try { | |
externalIdSearchResponseJson = callRequest(request); | |
FileWriter file8 = new FileWriter("/tmp/file8.txt"); | |
file8.write(externalIdSearchResponseJson.toString()); | |
System.out.println("Successfully Copied JSON Object to File..."); | |
System.out.println("\nJSON Object: " + externalIdSearchResponseJson); | |
file8.flush(); | |
file8.close(); | |
//builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(ATTR_ID)))); | |
handleUserId( handler, externalIdSearchResponseJson ); | |
} catch (IOException IOE) { | |
throw new ConnectorIOException(IOE.getMessage(), IOE); | |
} | |
} | |
else { | |
//wide open search | |
Integer pagedResultsOffset = oo.getPagedResultsOffset(); | |
Integer startIndex = new Integer(Scim2StartIndex); | |
String parameters = ""; | |
if (!StringUtil.isEmpty(configuration.getScim2Attributes())) { | |
parameters = "&attributes=" + configuration.getScim2Attributes(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2ExcludedAttributes())) { | |
parameters = parameters + "&excludedAttributes=" + configuration.getScim2ExcludedAttributes(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2Filter())) { | |
parameters = parameters + "&filter=" + configuration.getScim2Filter(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2SortBy())) { | |
parameters = parameters + "&sortBy=" + configuration.getScim2SortBy(); | |
} | |
if (!StringUtil.isEmpty(configuration.getScim2SortOrder())) { | |
parameters = parameters + "&sortOrder=" + configuration.getScim2SortOrder(); | |
} | |
boolean morePages = true; | |
while ( morePages ) { | |
try { | |
CloseableHttpClient client = HttpClients.createDefault(); | |
HttpRequestBase restGet; | |
restGet = new HttpGet(configuration.getServiceAddress() + "/scim/v2/onyen/Users/?startIndex=" + Scim2StartIndex + "&count=" + Scim2Count + parameters ); | |
authHeader(restGet); | |
//CloseableHttpResponse getResponse = client.execute(restGet); | |
//processResponseErrors(getResponse); | |
JSONObject searchResponseJson = new JSONObject(); | |
searchResponseJson = callRequest(restGet); | |
FileWriter file9 = new FileWriter("/tmp/file9.txt"); | |
file9.write(searchResponseJson.toString()); | |
System.out.println("Successfully Copied JSON Object to File..."); | |
System.out.println("\nJSON Object: " + searchResponseJson); | |
file9.flush(); | |
file9.close(); | |
JSONArray peopleArray = new JSONArray(); | |
if ( searchResponseJson.has("Resources") && searchResponseJson.get("Resources") != null ) { | |
peopleArray = searchResponseJson.getJSONArray("Resources"); | |
Integer Scim2CountInteger = new Integer(Scim2Count); | |
if ( peopleArray.length() < Scim2CountInteger ) { | |
morePages = false; | |
} | |
else { | |
startIndex = startIndex + Scim2CountInteger; | |
Scim2StartIndex = startIndex.toString(); | |
} | |
for ( int i = 0; i < peopleArray.length(); i++ ) { | |
JSONObject personJson = peopleArray.getJSONObject(i); | |
LOG.info(">>> processing ID " + i); | |
handleUserId( handler, personJson ); | |
} | |
} | |
else { | |
morePages = false; | |
LOG.info("No data object found in response"); | |
} | |
} catch (IOException IOE) { | |
throw new ConnectorIOException(IOE.getMessage(), IOE); | |
} | |
} | |
} | |
} | |
} | |
protected JSONObject callRequest(HttpRequestBase request) throws IOException { | |
// don't log request here - password field !!! | |
LOG.info("request URI: {0}", request.getURI()); | |
request.setHeader("Content-Type", CONTENT_TYPE); | |
CloseableHttpClient client = HttpClients.createDefault(); | |
authHeader(request); | |
CloseableHttpResponse response = client.execute(request); | |
LOG.info("response: "); | |
String responseText = EntityUtils.toString(response.getEntity()); | |
FileWriter file = new FileWriter("/tmp/file1.txt"); | |
file.write(responseText); | |
System.out.println("Successfully Copied JSON Object to File..."); | |
System.out.println("\nJSON Object: " + responseText); | |
file.flush(); | |
file.close(); | |
int statusCode = response.getStatusLine().getStatusCode(); | |
/* No status responses to handle yet | |
if (statusCode == 500) { | |
JSONObject errorResult = new JSONObject(responseText); | |
//JSONObject errorData = errorResult.getJSONObject("data"); | |
if (errorResult.getString("code").equals("existing_user_login")) { | |
closeResponse(response); | |
throw new AlreadyExistsException(jo.getString(ATTR_NAME)); | |
} | |
if (errorResult.getString("code").equals("existing_user_email")) { | |
closeResponse(response); | |
throw new AlreadyExistsException(jo.getString(ATTR_MAIL)); | |
} | |
} | |
if (statusCode == 404) { | |
JSONObject errorResult = new JSONObject(responseText); | |
//JSONObject errorData = errorResult.getJSONObject("data"); | |
if (errorResult.getString("code").equals("rest_user_invalid_id")) { | |
closeResponse(response); | |
throw new UnknownUidException(errorResult.toString()); | |
} | |
} | |
*/ | |
processResponseErrors(response); | |
JSONObject result = new JSONObject (responseText); | |
LOG.info("response body: "); | |
closeResponse(response); | |
return result; | |
} | |
protected JSONArray callRequestArray(HttpRequestBase request) throws IOException { | |
// don't log request here - password field !!! | |
LOG.info("request URI: {0}", request.getURI()); | |
request.setHeader("Content-Type", CONTENT_TYPE); | |
CloseableHttpClient client = HttpClients.createDefault(); | |
authHeader(request); | |
CloseableHttpResponse response = client.execute(request); | |
LOG.info("response: "); | |
String responseText = EntityUtils.toString(response.getEntity()); | |
FileWriter file = new FileWriter("/tmp/file2.txt"); | |
file.write(responseText); | |
System.out.println("Successfully Copied JSON Object to File..."); | |
System.out.println("\nJSON Object: " + responseText); | |
file.flush(); | |
file.close(); | |
processResponseErrors(response); | |
JSONArray result = new JSONArray (responseText); | |
LOG.info("response body: "); | |
closeResponse(response); | |
return result; | |
} | |
private void handleUserId ( ResultsHandler handler, JSONObject uidSearchResponseJson ) { | |
LOG.info("inside handleUserId " + String.valueOf(uidSearchResponseJson.getString(ATTR_ID))); | |
ConnectorObjectBuilder builder = new ConnectorObjectBuilder(); | |
builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getString(ATTR_ID)))); | |
//JSONObject attributes = new JSONObject(); | |
//if ( uidSearchResponseJson.has("attributes") && uidSearchResponseJson.get("attributes") != null ) { | |
// attributes = (JSONObject) uidSearchResponseJson.get("attributes"); | |
//} | |
//builder.setUid(new Uid(String.valueOf(id))); | |
String [] potentialAttributes = { ATTR_ID, ATTR_EXTERNAL_ID, ATTR_USER_NAME, ATTR_ACTIVE}; | |
for ( String potentialAttribute: potentialAttributes ) { | |
LOG.info(">>> checking for attribute " + potentialAttribute ); | |
if ( uidSearchResponseJson.has(potentialAttribute) && uidSearchResponseJson.get(potentialAttribute) != null && !JSONObject.NULL.equals(uidSearchResponseJson.get(potentialAttribute)) ) { | |
LOG.info(">>> found attribute " + potentialAttribute ); | |
/*if ( potentialAttribute.equals(ATTR_USER_NAME) ) { | |
//builder.setName(attributes.getString(potentialAttribute)); | |
builder.setName(uidSearchResponseJson.getString(ATTR_USER_NAME)); | |
}*/ | |
if ( potentialAttribute.equals(ATTR_EXTERNAL_ID) ) { | |
//builder.setName(attributes.getString(potentialAttribute)); | |
builder.setName(uidSearchResponseJson.getString(ATTR_EXTERNAL_ID)); | |
addAttr(builder, potentialAttribute, uidSearchResponseJson.getString(potentialAttribute)); | |
} | |
if ( potentialAttribute.equals(ATTR_USER_NAME) ) { | |
//builder.setName(attributes.getString(potentialAttribute)); | |
//objectBuild.setName(personJson.getString(ATTR_USER_NAME)); | |
LOG.info(">>> Inside processing userName " + potentialAttribute ); | |
JSONObject onyenInfo = new JSONObject(); | |
onyenInfo = (JSONObject)uidSearchResponseJson.get("urn:ietf:params:scim:schema:unc:core:2.0:Onyen"); | |
onyenInfo.put(ATTR_USER_NAME,uidSearchResponseJson.getString(ATTR_USER_NAME)); | |
addAttr(builder, ATTR_USER_NAME, onyenInfo.toString()); | |
String logSafeString = onyenInfo.toString().replace('{','$').replace('}','#'); | |
LOG.info(">>> Just added onyen information " + logSafeString ); | |
if ((String)onyenInfo.get("type") == "PRIMARY") { | |
builder.setUid(uidSearchResponseJson.getString(ATTR_ID)); | |
addAttr(builder, ATTR_ID, uidSearchResponseJson.getString(ATTR_ID)); | |
} | |
} | |
/*if ( potentialAttribute.equals(ATTR_ID) ) { | |
builder.setUid(uidSearchResponseJson.getString(ATTR_ID)); | |
addAttr(builder, potentialAttribute, uidSearchResponseJson.getString(potentialAttribute)); | |
}*/ | |
if ( potentialAttribute.equals(ATTR_ACTIVE) ) { | |
addAttr(builder, potentialAttribute, uidSearchResponseJson.getBoolean(potentialAttribute)); | |
} | |
/*else { | |
addAttr(builder, potentialAttribute, uidSearchResponseJson.getString(potentialAttribute)); | |
}*/ | |
} | |
} | |
ConnectorObject connectorObject = builder.build(); | |
handler.handle(connectorObject); | |
} | |
} |