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
The newest version!
package org.bdware.doip.audit;
import com.google.gson.JsonObject;
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;
}
public void sendRawMessageToRepo(String repoId, DoipMessage msg, DoipMessageCallback doipMessageCallback) {
AuditDoipClient client = convertDoidToRepo(repoId);
DelegateDoipMessageCallback delegateDoipMessageCallback = new DelegateDoipMessageCallback(
this, client,
doipMessageCallback, msg);
client.sendMessage(msg, delegateDoipMessageCallback);
}
@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);
}
public AuditDoipClient convertDoidToRepoInternal(String doid) throws Exception {
StateInfoBase content = irsClient.resolve(doid);
JsonObject jo = content.handleValues;
if (jo.has("bdwType") && jo.get("bdwType").getAsString().equals("DDO")) {
//TODO @Liuyihao AuditDoipClusterClient?
} else if (jo.has("address") && jo.has("version")) {
//just ignore
} else if (jo.has("repoId") && jo.has("appendixes")) {
//use repoId as address
String repoId = jo.get("repoId").getAsString();
JsonObject appendixes = jo.get("appendixes").getAsJsonObject();
jo = appendixes.get(repoId).getAsJsonObject();
} else if (jo.has("repoId")) {
StateInfoBase stateInfoBase = irsClient.resolve(jo.get("repoId").getAsString());
jo = stateInfoBase.handleValues;
}
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;
}
}
public AuditDoipClient convertDoidToRepo(String doid) {
AuditDoipClient ret = null;
int i = 0;
int lastPos;
for (; ret == null; ) {
try {
ret = convertDoidToRepoInternal(doid);
if (ret == null) {
lastPos = doid.lastIndexOf("/");
if (lastPos == -1) break;
doid = doid.substring(0, lastPos - 1);
}
} catch (Exception e) {
lastPos = doid.lastIndexOf("/");
if (lastPos == -1) break;
doid = doid.substring(0, lastPos - 1);
}
if (ret != null) return ret;
if (i++ > 10) break;
}
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,"[AuditDoaClient.sendMessageSync]");
}
@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;
}
}
}