org.bdware.doip.audit.AuditDoaClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
package org.bdware.doip.audit;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bdware.doip.audit.client.AuditDoipClient;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.audit.client.DelegateDoipMessageCallback;
import org.bdware.doip.audit.writer.AuditConfig;
import org.bdware.doip.codec.digitalObject.DigitalObject;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipMessageFactory;
import org.bdware.doip.codec.doipMessage.DoipMessageSigner;
import org.bdware.doip.codec.exception.DoipConnectException;
import org.bdware.doip.codec.metadata.SearchParameter;
import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.doip.endpoint.client.ClientConfig;
import org.bdware.doip.endpoint.client.DoipClient;
import org.bdware.doip.endpoint.client.DoipMessageCallback;
import org.bdware.irp.stateinfo.StateInfoBase;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class AuditDoaClient implements DoipClient {
private final String clientDoid;
private final AuditConfig auditConfig;
private final DoipMessageSigner signer;
AuditIrpClient irsClient;
Map doipConnections;
Map addressToClient;
Map repoIDToUrl;
public AuditDoaClient(String clientDoid, EndpointConfig config, DoipMessageSigner signer) {
irsClient = new AuditIrpClient(config);
doipConnections = new ConcurrentHashMap<>();
addressToClient = new ConcurrentHashMap<>();
repoIDToUrl = new ConcurrentHashMap<>();
this.clientDoid = clientDoid;
auditConfig = irsClient.getAuditConfig();
this.signer = signer;
}
@Override
public void hello(String doid, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(doid);
DoipMessage msg =
new DoipMessageFactory.DoipMessageBuilder()
.createRequest(doid, BasicOperations.Hello.getName())
.create();
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
private AuditDoipClient convertDoidToRepo(String doid) {
try {
StateInfoBase content = irsClient.resolve(doid);
JsonObject jo = content.handleValues;
if (jo.has("repoInfo"))
jo = JsonParser.parseString(jo.get("repoInfo").getAsString()).getAsJsonObject();
String url = jo.get("address").getAsString();
String version = jo.get("version").getAsString();
String repoID = jo.get("doId").getAsString();
if (doipConnections.containsKey(repoID)) {
AuditDoipClient ret = doipConnections.get(repoID);
if (!ret.isConnected())
ret.reconnect();
return ret;
} else {
AuditDoipClient doipClient = new AuditDoipClient(auditConfig, clientDoid, signer);
doipClient.connect(repoID, ClientConfig.fromUrl(url, version));
doipConnections.put(repoID, doipClient);
addressToClient.put(url, doipClient);
return doipClient;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void listOperations(String doid, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(doid);
DoipMessage msg =
new DoipMessageFactory.DoipMessageBuilder()
.createRequest(doid, BasicOperations.ListOps.getName())
.create();
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
@Override
public void retrieve(String doid, String element, boolean includeElementData, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(doid);
DoipMessage msg =
new DoipMessageFactory.DoipMessageBuilder()
.createRequest(doid, BasicOperations.Retrieve.getName())
.create();
if (element != null) msg.header.parameters.addAttribute("element", element);
if (includeElementData) msg.header.parameters.addAttribute("includeElementData", "true");
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
@Override
public void create(String doipService, DigitalObject digitalObject, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(doipService);
DoipMessage msg =
new DoipMessageFactory.DoipMessageBuilder()
.createRequest(doipService, BasicOperations.Create.getName())
.setBody(digitalObject)
.create();
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
@Override
public void update(DigitalObject digitalObject, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(digitalObject.id);
DoipMessage msg =
new DoipMessageFactory.DoipMessageBuilder()
.createRequest(digitalObject.id, BasicOperations.Update.getName())
.setBody(digitalObject)
.create();
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
@Override
public void delete(String doid, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(doid);
DoipMessage msg =
new DoipMessageFactory.DoipMessageBuilder()
.createRequest(doid, BasicOperations.Delete.getName())
.create();
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
@Override
public void search(String doid, SearchParameter sp, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(doid);
DoipMessage msg =
new DoipMessageFactory.DoipMessageBuilder()
.createRequest(doid, BasicOperations.Search.getName())
.addAttributes("query", sp.query)
.addAttributes("pageNum", sp.pageNum)
.addAttributes("pageSize", sp.pageSize)
.addAttributes("type", sp.type)
.create();
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
@Override
public void sendRawMessage(DoipMessage doipMessage, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(doipMessage.header.parameters.id);
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, doipMessage);
client.sendRawMessage(doipMessage, delegateDoipMessageCallback);
}
public DoipMessage sendRawMessageSync(DoipMessage doipMessage, long timeout) {
DoipMessage[] result = new DoipMessage[1];
DoipMessageCallback callback = new DoipMessageCallback() {
@Override
public void onResult(DoipMessage msg) {
result[0] = msg;
synchronized (result) {
result.notify();
}
}
};
sendRawMessage(doipMessage, callback);
synchronized (result) {
try {
result.wait(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (result[0] != null) return result[0];
return DoipMessageFactory.createTimeoutResponse(doipMessage.requestID);
}
@Override
public void close() {
}
@Override
public void connect(ClientConfig clientConfig) {
}
@Override
public void reconnect() throws DoipConnectException {
}
@Override
public String getRepoUrl() {
return null;
}
@Override
public String getRecipientID() {
return null;
}
@Override
public void setRecipientID(String s) {
}
@Override
public boolean isConnected() {
return true;
}
public AuditDoipClient getClientByUrl(String repoID, String address, String version) {
if (addressToClient.containsKey(address))
return addressToClient.get(address);
else {
AuditDoipClient doipClient = new AuditDoipClient(auditConfig, clientDoid, signer);
doipClient.connect(repoID, ClientConfig.fromUrl(address, version));
addressToClient.put(address, doipClient);
return doipClient;
}
}
}