Home > database >  Method body is duplicating, only signature is changing
Method body is duplicating, only signature is changing

Time:09-29

I have generated objects with wsimport from WSDL definition.

In my main class, I just declare manually the endpoints I need, and pass it the PK to make the call.

I have to set up a HTTPS connection with a client PK. Thing is for each endpoint, I must set the authenticateClient. Bad thing is content is equal, just the signature is changing to another interface.

Isn't there a way not to duplicate the same method code again and again ?

private void authenticateClient(AdamConsultationMesuresServiceReadPortType port) {
        Client client = ClientProxy.getClient(port);
        HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
        SpringSoapClientConfig soapClientConfig = new SpringSoapClientConfig();
        KeyManagerFactory keyManagerFactory = soapClientConfig.getKeyManagerFactory(company);
        TLSClientParameters tslClientParameters = httpConduit.getTlsClientParameters();
        if (tslClientParameters == null) {
            tslClientParameters = new TLSClientParameters();
        }
        tslClientParameters.setKeyManagers(keyManagerFactory.getKeyManagers());
        tslClientParameters.setDisableCNCheck(true);
        httpConduit.setTlsClientParameters(tslClientParameters);
    }

    private void authenticateClientCommandeCollectePublicationMesures(CommandeCollectePublicationMesuresPortType port) {
        Client client = ClientProxy.getClient(port);
        HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
        SpringSoapClientConfig soapClientConfig = new SpringSoapClientConfig();
        KeyManagerFactory keyManagerFactory = soapClientConfig.getKeyManagerFactory(company);
        TLSClientParameters tslClientParameters = httpConduit.getTlsClientParameters();
        if (tslClientParameters == null) {
            tslClientParameters = new TLSClientParameters();
        }
        tslClientParameters.setKeyManagers(keyManagerFactory.getKeyManagers());
        tslClientParameters.setDisableCNCheck(true);
        httpConduit.setTlsClientParameters(tslClientParameters);
    }

Should I have define the input parameter as a Generic interface type or something like that ?

CodePudding user response:

Very basic and simplicit approach.

private void authenticateClient(AdamConsultationMesuresServiceReadPortType port) {
        doYourStuff(ClientProxy.getClient(port));
    }

private void authenticateClientCommandeCollectePublicationMesures(CommandeCollectePublicationMesuresPortType port) {
        doYourStuff(ClientProxy.getClient(port));
    }


private void doYourStuff(Client client){

    HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
    SpringSoapClientConfig soapClientConfig = new SpringSoapClientConfig();
    KeyManagerFactory keyManagerFactory = soapClientConfig.getKeyManagerFactory(company);
    TLSClientParameters tslClientParameters = httpConduit.getTlsClientParameters();
    if (tslClientParameters == null) {
        tslClientParameters = new TLSClientParameters();
    }
    tslClientParameters.setKeyManagers(keyManagerFactory.getKeyManagers());
    tslClientParameters.setDisableCNCheck(true);
    httpConduit.setTlsClientParameters(tslClientParameters);

}
  • Related