diff --git a/connector-federation-manager/src/main/java/edu/unc/polygon/connector/federationManager/FederationManagerConnector.java b/connector-federation-manager/src/main/java/edu/unc/polygon/connector/federationManager/FederationManagerConnector.java index 083a02d..fc99e70 100644 --- a/connector-federation-manager/src/main/java/edu/unc/polygon/connector/federationManager/FederationManagerConnector.java +++ b/connector-federation-manager/src/main/java/edu/unc/polygon/connector/federationManager/FederationManagerConnector.java @@ -10,6 +10,7 @@ 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; @@ -68,6 +69,7 @@ public class FederationManagerConnector extends AbstractRestConnector>> TestOp"); + try { + CloseableHttpClient client = HttpClients.createDefault(); + HttpRequestBase restHead; + restHead = new HttpHead(configuration.getServiceAddress() + "/siteadmin/api/people"); + authHeader(restHead); + CloseableHttpResponse headResponse = client.execute(restHead); + processResponseErrors(headResponse); + } catch (IOException IOE) { + throw new ConnectorIOException(IOE.getMessage(), IOE); + } } @@ -112,6 +125,11 @@ public void checkAlive() { test(); } + private void authHeader(HttpRequest request) { + request.setHeader(configuration.getTokenName(), configuration.getTokenValue()); + + } + @Override public Schema schema() { SchemaBuilder schemaBuilder = new SchemaBuilder(FederationManagerConnector.class); @@ -218,31 +236,120 @@ public Schema schema() { public FilterTranslator createFilterTranslator(ObjectClass oc, OperationOptions oo) { LOG.info("inside createFilterTranslator"); return new FederationManagerFilterTranslator(); - } + } - @Override - public void executeQuery(ObjectClass oc, FederationManagerFilter filter, ResultsHandler handler, OperationOptions oo) { - if ( oc.is(ObjectClass.ACCOUNT_NAME)) { - if (filter != null && filter.byUid != null) { - //not sure if this use case exists yet - } + @Override + public void executeQuery(ObjectClass oc, FederationManagerFilter filter, ResultsHandler handler, OperationOptions oo) { + if ( oc.is(ObjectClass.ACCOUNT_NAME)) { + /* Filtered searches aren't supported by this API yet, so we'll skip over these for now + if (filter != null && filter.byUid != null) { + //not sure if this use case exists yet + } - else if (filter != null && filter.byName != null) { - //not sure if this use case exists yet - } + else if (filter != null && filter.byName != null) { + //not sure if this use case exists yet + } - else if (filter != null && filter.byEmailAddress != null) { - //not sure if this use case exists yet - } + else if (filter != null && filter.byEmailAddress != null) { + //not sure if this use case exists yet + } + else { + //wide open search + } + */ + try { + CloseableHttpClient client = HttpClients.createDefault(); + HttpRequestBase restGet; + restGet = new HttpGet(configuration.getServiceAddress() + "/siteadmin/api/people"); + authHeader(restGet); + //CloseableHttpResponse getResponse = client.execute(restGet); + //processResponseErrors(getResponse); + JSONArray searchResponseJson = new JSONArray(); + searchResponseJson = callRequestArray(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(); + for ( int i = 0; i < searchResponseJson.length(); i++ ) { + JSONObject uidSearchResponseJson = searchResponseJson.getJSONObject(i); + LOG.info(">>> processing ID " + i); + handleUserId( handler, uidSearchResponseJson ); + } - else { - //wide open search - } + } catch (IOException IOE) { + throw new ConnectorIOException(IOE.getMessage(), IOE); + } + } + + } + + 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.getInt(ATTR_ID))); + ConnectorObjectBuilder builder = new ConnectorObjectBuilder(); + builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(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_NAME, ATTR_MAIL, ATTR_FIRST_NAME, ATTR_LAST_NAME, ATTR_MIDDLE_NAME, ATTR_ORGANIZATION_ID, ATTR_FAX_NUMBER, ATTR_MOBILE_NUMBER, ATTR_PHONE_NUMBER, ATTR_INFORMAL_NAME, ATTR_WEBSITE}; + /* + private static final String ATTR_ID = "id"; //__UID__ + private static final String ATTR_ORGANIZATION_ID = "organization_id"; + public static final String ATTR_NAME = "email"; //hope to change this to username later __NAME__ + public static final String ATTR_MAIL = "email"; + 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"; + */ + for ( String potentialAttribute: potentialAttributes ) { + LOG.info(">>> checking for attribute " + potentialAttribute ); + if ( attributes.has(potentialAttribute) && attributes.get(potentialAttribute) != null && !JSONObject.NULL.equals(attributes.get(potentialAttribute)) ) { + LOG.info(">>> found attribute " + potentialAttribute ); + if ( potentialAttribute.equals(ATTR_NAME) ) { + builder.setName(attributes.getString(potentialAttribute)); + } + else { + addAttr(builder, potentialAttribute, attributes.getString(potentialAttribute)); + } + } + } + ConnectorObject connectorObject = builder.build(); + handler.handle(connectorObject); + } - } - } }