All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bdware.doip.audit.client.DelegateDoipMessageCallback Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.audit.client;

import org.bdware.doip.audit.AuditDoaClient;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.DoipResponseCode;
import org.bdware.doip.endpoint.client.DoipMessageCallback;

import java.net.URI;

public class DelegateDoipMessageCallback implements DoipMessageCallback {


    private final AuditDoaClient auditDoaClient;
    private final DoipMessageCallback originalCallback;
    DoipMessage originalMessage;
    AuditDoipClient previousClient;

    public DelegateDoipMessageCallback(AuditDoaClient auditDoaClient, AuditDoipClient previousClient, DoipMessageCallback doipMessageCallback, DoipMessage originalMessage) {
        this.auditDoaClient = auditDoaClient;
        this.originalCallback = doipMessageCallback;
        this.originalMessage = originalMessage;
        this.previousClient = previousClient;
    }

    @Override
    public void onResult(DoipMessage msg) {
        if (msg.header.parameters.response == DoipResponseCode.DelegateRequired) {
            try {
                if (msg.header.parameters.attributes.has("port")) {
                    int port = msg.header.parameters.attributes.get("port").getAsInt();
                    URI uri = new URI(previousClient.serverURL);
                    String host = uri.getHost();
                    if (msg.header.parameters.attributes.has("host"))
                        host = msg.header.parameters.attributes.get("host").getAsString();
                    String newAddress = String.format("%s://%s:%d", uri.getScheme(), host, port);
                    String version = "2.1";
                    if (msg.header.parameters.attributes.has("version"))
                        version = msg.header.parameters.attributes.get("version").getAsString();
                    String repoID = previousClient.getServerRepoId();
                    if (msg.header.parameters.attributes.has("repoID"))
                        repoID = msg.header.parameters.attributes.get("repoID").getAsString();
                    AuditDoipClient client = auditDoaClient.getClientByUrl(repoID, newAddress, version);
                    this.previousClient = client;
                    client.sendRawMessage(originalMessage, this);
                }
            } catch (Exception e) {
                e.printStackTrace();
                originalCallback.onResult(msg);
            }
        } else originalCallback.onResult(msg);
        return;

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy