Skip to content

Commit

Permalink
Add initial implementation of ID Match client
Browse files Browse the repository at this point in the history
This is work in progress (only requests are sent,
responses are not parsed).
  • Loading branch information
Tadeas Chrapovic committed Jan 14, 2022
1 parent 062ca96 commit 0291211
Show file tree
Hide file tree
Showing 23 changed files with 814 additions and 16 deletions.
5 changes: 5 additions & 0 deletions model/model-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@
<artifactId>jakarta.servlet-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

<!-- Test -->
<dependency>
<groupId>com.evolveum.midpoint</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.evolveum.midpoint.model.impl.correlator.idmatch;

import com.evolveum.midpoint.model.impl.correlator.idmatch.data.structure.JsonListStructure;
import com.evolveum.midpoint.model.impl.correlator.idmatch.operations.Client;
import com.evolveum.midpoint.prism.Item;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
Expand All @@ -18,17 +20,17 @@
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.IdMatchCorrelatorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAttributesType;

import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;
import com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.google.common.annotations.VisibleForTesting;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Set;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;

public class IdMatchServiceImpl implements IdMatchService {

Expand All @@ -54,19 +56,73 @@ private IdMatchServiceImpl(@NotNull String url, @Nullable String username, @Null

LOGGER.trace("Executing match for:\n{}", attributes.debugDumpLazily(1));

// just a demo
//noinspection unchecked
Client client = new Client(url, username, password.getClearValue());

PrismContainerValue<ShadowAttributesType> pcv = attributes.asPrismContainerValue();
for (Item<?, ?> item : pcv.getItems()) {
LOGGER.info("Got attribute {} with value(s): {}",
item.getElementName().getLocalPart(), item.getRealValues());
}

// TODO implement
List<JsonListStructure> jsonList;

jsonList = generateJson(pcv);

String sorLabel;
String sorId;
String objectToSend;

for (JsonListStructure jsonListStructure : jsonList) {
sorLabel = jsonListStructure.getSorLabel();
sorId = jsonListStructure.getSorId();
objectToSend = jsonListStructure.getObjectToSend();

client.peoplePut(sorLabel, sorId, objectToSend);
}

return MatchingResult.forUncertain(null, Set.of());
}

public List<JsonListStructure> generateJson(PrismContainerValue<ShadowAttributesType> attributes) {

List<JsonListStructure> jsonList = new ArrayList<>();

JsonFactory factory = new JsonFactory();
StringWriter jsonString = new StringWriter();
JsonGenerator generator;

try {
generator = factory.createGenerator(jsonString);
generator.useDefaultPrettyPrinter();
generator.writeStartObject();
generator.writeFieldName("sorAttributes");
generator.writeStartObject();

String sorLabel = "sor";
String uid = null;
String elementName;
String elementRealValue;

for (Item<?, ?> item : attributes.getItems()) {
elementName = item.getElementName().getLocalPart();
elementRealValue = item.getRealValue(String.class);

if (elementName.equals("uid")) {
uid = elementRealValue;
} else {
generator.writeFieldName(elementName.toLowerCase());
generator.writeString(elementRealValue);
}
}

generator.writeEndObject();
generator.writeEndObject();
generator.close();
jsonList.add(new JsonListStructure(sorLabel, uid, String.valueOf(jsonString)));
} catch (IOException e) {
//TODO throw exception
e.printStackTrace();
}

return jsonList;
}

@Override
public void resolve(
@NotNull ShadowAttributesType attributes,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.constants;

public enum Channel {


URL_PREFIX_MAIN_OPERATIONS("/match/api/1/v1/people/"),
URL_PREFIX_GET_MATCH_REQUEST_MATCH_ID("/match/api/1/v1/matchRequests/"),
URL_PREFIX_GET_MATCH_REQUEST_REFERENCE_ID("/match/api/1/v1/matchRequests?referenceId=");


private final String url;

Channel(String envUrl) {
this.url = envUrl;
}

public String getUrl() {
return url;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.constants;

public enum MatchStatus {


PENDING("/match/api/1/v1/matchRequests?status=pending"),
RESOLVED("/match/api/1/v1/matchRequests?status=resolved");


private final String url;

MatchStatus(String envUrl) {
this.url = envUrl;
}

public String getUrl() {
return url;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.constants;

public enum Operations {
PUT,
POST,
GET,
DELETE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.data;

public class ListResponse {


String message;
String entity;
String responseCode;


public ListResponse(String message, String entity, String responseCode) {
this.message = message;
this.entity = entity;
this.responseCode = responseCode;
}


public String getMessage() {
return message;
}


public String getEntity() {
return entity;
}


public String getResponseCode() {
return responseCode;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.data.structure;

public class JsonListStructure {


public JsonListStructure(String sorLabel, String sorId, String objectToSend) {
this.sorLabel = sorLabel;
this.sorId = sorId;
this.objectToSend = objectToSend;
}

String sorLabel;
String sorId;
String objectToSend;


public String getSorLabel() {
return sorLabel;
}


public String getSorId() {
return sorId;
}


public String getObjectToSend() {
return objectToSend;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.data.structure;

import java.util.List;

public class UserDataStructure {


String sorLabel;
String sorId;
List<UserSpecParameter> object;


public UserDataStructure(String sorLabel, String sorId, List<UserSpecParameter> object) {
this.sorLabel = sorLabel;
this.sorId = sorId;
this.object = object;
}


public String getSorLabel() {
return sorLabel;
}


public String getSorId() {
return sorId;
}

public List<UserSpecParameter> getObject() {
return object;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.data.structure;

public class UserSpecParameter {


String groupName;
String objectName;
String objectType;
String object;


public UserSpecParameter(String groupName, String objectName, String objectType, String object) {
this.groupName = groupName;
this.objectName = objectName;
this.objectType = objectType;
this.object = object;
}


public String getGroupName() {
return groupName;
}


public String getObjectName() {
return objectName;
}


public String getObjectType() {
return objectType;
}


public String getObject() {
return object;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.operations;

import com.evolveum.midpoint.model.impl.correlator.idmatch.data.ListResponse;

import java.io.IOException;
import java.util.List;

public interface ApacheApiRequest {

void doRequest(String urlPrefix, String urlSuffix, String jsonString) throws IOException;

List<ListResponse> listResponse();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.evolveum.midpoint.model.impl.correlator.idmatch.operations;


import com.evolveum.midpoint.model.impl.correlator.idmatch.operations.auth.AuthenticationProvider;

import java.io.IOException;


public class ApacheContext {

public ApacheApiRequest apacheApiRequest;

public ApacheContext(ApacheApiRequest apacheApiRequest) {
this.apacheApiRequest = apacheApiRequest;
}

public void executeRequest(AuthenticationProvider authenticationProvider, String urlPrefix, String jsonString, String urlSuffix){
try {
apacheApiRequest.doRequest(urlPrefix,jsonString,urlSuffix);
} catch (IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit 0291211

Please sign in to comment.