Skip to content

Commit

Permalink
Merge pull request #471 from axonivy-market/cxfInterceptors
Browse files Browse the repository at this point in the history
interceptor on client by example
  • Loading branch information
ivy-rew authored Sep 13, 2023
2 parents c1d2a3f + cb2df4c commit 39542d2
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package soap.bpm;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import com.axonivy.connectivity.soap.client.CustomLogFeature;
import ch.ivyteam.ivy.bpm.engine.client.BpmClient;
import ch.ivyteam.ivy.bpm.exec.client.IvyProcessTest;
import ch.ivyteam.ivy.security.ISession;

@IvyProcessTest
public class TestClientedInterceptedPersonService {

@Test
void customLogInterceptor(BpmClient bpmClient, ISession session) {
assertThat((String)session.getAttribute("lastExchange"))
.isNull();

bpmClient.start()
.process("soap/client/interceptedService/clientIntercept.ivp")
.as().session(session)
.execute();

assertThat((String)session.getAttribute("lastExchange"))
.as("session attrs set by "+CustomLogFeature.class)
.isNotBlank();
}
}
3 changes: 2 additions & 1 deletion connectivity/connectivity-demos/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src_wsproc">
<classpathentry kind="src" path="src_wsproc">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
Expand Down Expand Up @@ -48,5 +48,6 @@
<classpathentry exported="true" kind="lib" path="lib/generated/rest/jaxRsClient_ae69ba01-79b7-4dce-9049-900f8f420907.jar"/>
<classpathentry exported="true" kind="lib" path="lib/generated/rest/jaxRsClient_65f8e5a4-768d-4a68-813a-e6d569cda522.jar"/>
<classpathentry exported="true" kind="lib" path="lib/generated/rest/jaxRsClient_4d9a8b09-9968-4476-a8ac-b71a94d25e94.jar"/>
<classpathentry exported="true" kind="lib" path="lib_ws/client/cxfClient_16330E44A158D09C.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
31 changes: 24 additions & 7 deletions connectivity/connectivity-demos/config/webservice-clients.yaml
Original file line number Diff line number Diff line change
@@ -1,40 +1,57 @@
# yaml-language-server: $schema=https://json-schema.axonivy.com/app/0.0.1/webservice-clients.json
WebServiceClients:
personService:
Id: 16150E44A158D09C
ServiceClass: com.axonivy.connectivity.soap.service.client.PersonServiceService
WsdlUrl: http://127.0.0.1:8081/designer/ws/connectivity-demos/16150E1D07E8CA18?WSDL
WsCallLibrary: CXF
ServiceClass: com.axonivy.connectivity.soap.service.client.PersonServiceService
Features:
- ch.ivyteam.ivy.webservice.exec.cxf.feature.HttpBasicAuthenticationFeature
Properties:
username: theBoss
password: ${decrypt:\u007B\u0012\u0012\u0075\u002F\u0017\u00D1\u0083\u00C4\u009C\u0094\u00B0\u009A\u00B2\u00ED\u006B\u00AE\u000D\u005A\u0007\u0032\u009C\u0070\u00D6\u006F\u0027\u0008\u0080\u0085\u00CA\u005F\u00B5\u0094\u006C\u0091\u002D\u001A\u00F9\u0082\u0013\u004E\u00C4\u00C0\u0099\u00DA\u003B\u00C4\u0074}
Endpoints:
PersonServicePort:
- '${ivy.app.baseurl}/ws/connectivity-demos/16150E1D07E8CA18'
- ${ivy.app.baseurl}/ws/connectivity-demos/16150E1D07E8CA18
technicalBackend:
Id: 162B962523BAAB85
ServiceClass: com.axonivy.connectivity.client.TechnicalBackendServiceService
WsdlUrl: http://localhost:8081/designer/ws/connectivity-demos/162B95BB70C3178E?WSDL
WsCallLibrary: CXF
ServiceClass: com.axonivy.connectivity.client.TechnicalBackendServiceService
Endpoints:
TechnicalBackendServicePort:
- '${ivy.app.baseurl}/ws/connectivity-demos/162B95BB70C3178E'
- ${ivy.app.baseurl}/ws/connectivity-demos/162B95BB70C3178E
smartbearTests:
Id: 162B97C859B22CA3
ServiceClass: com.smartbear.sample.test.client.SampleWebService
WsdlUrl: http://secure.smartbearsoftware.com/samples/testcomplete12/webservices/Service.asmx?WSDL
WsCallLibrary: CXF
ServiceClass: com.smartbear.sample.test.client.SampleWebService
Endpoints:
SampleWebServiceSoap:
- http://secure.smartbearsoftware.com/samples/testcomplete12/webservices/Service.asmx
SampleWebServiceSoap12:
- http://secure.smartbearsoftware.com/samples/testcomplete12/webservices/Service.asmx
#the backend defines a custom interceptor
interceptedService:
Id: 16D2A643A3A25C52
ServiceClass: com.axonivy.connectivity.soap.interceptor.client.ServiceWithExceptionService
WsdlUrl: http://localhost:8081/designer/ws/connectivity-demos/16D29AE50A7A6E34?WSDL
WsCallLibrary: CXF
ServiceClass: com.axonivy.connectivity.soap.interceptor.client.ServiceWithExceptionService
Endpoints:
ServiceWithExceptionPort:
- '${ivy.app.baseurl}/ws/connectivity-demos/16D29AE50A7A6E34'
- ${ivy.app.baseurl}/ws/connectivity-demos/16D29AE50A7A6E34
#client with a custom interceptor feature
interceptedPersonService:
Id: 16330E44A158D09C
ServiceClass: soap.intercepted.client.person.ivyteam.ch.client.PersonServiceService
WsdlUrl: http://127.0.0.1:8081/designer/ws/connectivity-demos/16150E1D07E8CA18?WSDL
WsCallLibrary: CXF
Features:
- ch.ivyteam.ivy.webservice.exec.cxf.feature.HttpBasicAuthenticationFeature
- com.axonivy.connectivity.soap.client.CustomLogFeature
Properties:
username: theBoss
password: ${decrypt:\u007B\u0012\u0012\u0075\u002F\u0017\u00D1\u0083\u00C4\u009C\u0094\u00B0\u009A\u00B2\u00ED\u006B\u00AE\u000D\u005A\u0007\u0032\u009C\u0070\u00D6\u006F\u0027\u0008\u0080\u0085\u00CA\u005F\u00B5\u0094\u006C\u0091\u002D\u001A\u00F9\u0082\u0013\u004E\u00C4\u00C0\u0099\u00DA\u003B\u00C4\u0074}
Endpoints:
PersonServicePort:
- ${ivy.app.baseurl}/ws/connectivity-demos/16150E1D07E8CA18
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema" : "https://json-schema.axonivy.com/process/11.2.1/process.json",
"id" : "18A89E15504D8B30",
"config" : {
"data" : "com.axonivy.connectivity.Data"
},
"elements" : [ {
"id" : "f0",
"type" : "RequestStart",
"name" : "start",
"config" : {
"signature" : "start"
},
"visual" : {
"at" : { "x" : 96, "y" : 64 }
},
"connect" : [
{ "id" : "f2", "to" : "f1" }
]
}, {
"id" : "f1",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 352, "y" : 64 }
}
} ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
}, {
"id" : "f2",
"type" : "WebServiceCall",
"name" : [
"intercepted",
"backend"
],
"config" : {
"clientId" : "16D2A643A3A25C52",
"operation" : {
Expand Down Expand Up @@ -70,5 +74,44 @@
"connect" : [
{ "id" : "f6", "to" : "f2" }
]
}, {
"id" : "f7",
"type" : "RequestStart",
"name" : "interceptingClient",
"config" : {
"signature" : "clientIntercept"
},
"visual" : {
"at" : { "x" : 96, "y" : 280 }
},
"connect" : [
{ "id" : "f9", "to" : "f8" }
]
}, {
"id" : "f8",
"type" : "WebServiceCall",
"name" : [
"custom client",
"interceptor"
],
"config" : {
"clientId" : "16330E44A158D09C",
"operation" : {
"name" : "getPersons",
"port" : "PersonServicePort"
}
},
"visual" : {
"at" : { "x" : 224, "y" : 280 }
},
"connect" : [
{ "id" : "f11", "to" : "f10" }
]
}, {
"id" : "f10",
"type" : "TaskEnd",
"visual" : {
"at" : { "x" : 352, "y" : 280 }
}
} ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.axonivy.connectivity.soap.client;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.http.HttpStatus;
import ch.ivyteam.ivy.environment.Ivy;
import ch.ivyteam.ivy.webservice.exec.feature.WebServiceClientFeature;
import ch.ivyteam.ivy.webservice.exec.feature.WebServiceClientFeatureContext;

/**
* Demonstrates the setup of custom interceptors that track
* the messages going out to the remote service and their responses.
*/
public class CustomLogFeature implements WebServiceClientFeature {

private static final String EXCHANGE_ID_KEY = "exchangeId";

@Override
public void initialize(WebServiceClientFeatureContext context) {
Client proxy = ClientProxy.getClient(context.getBindingProvider());
proxy.getOutInterceptors().add(new CxfSendInterceptor(Phase.SEND_ENDING));
proxy.getInInterceptors().add(new CxfReceiveInterceptor(Phase.RECEIVE));
Ivy.log().info("call initiated with custom interceptors provided by "+this);
}

public static class CxfSendInterceptor extends AbstractPhaseInterceptor<Message> {

public CxfSendInterceptor(String phase) {
super(phase);
}

@Override
public void handleMessage(Message message) throws Fault {
String exchangeId = getExchangeKey(message);
Ivy.session().setAttribute("lastExchange", exchangeId);
Ivy.log().debug("started exchange "+exchangeId);
}

}

public class CxfReceiveInterceptor extends AbstractPhaseInterceptor<Message> {

public CxfReceiveInterceptor(String phase) {
super(phase);
}

@Override
public void handleMessage(Message message) throws Fault {
int responseCode = (int) message.get(Message.RESPONSE_CODE);
Ivy.log().debug("ended exchange "+getExchangeKey(message));
if (HttpStatus.SC_OK != responseCode) {
Ivy.log().error("request failed with http error code "+responseCode);
}
}
}

private static String getExchangeKey(Message message) {
return (String) message.getExchange().get(EXCHANGE_ID_KEY);
}
}

0 comments on commit 39542d2

Please sign in to comment.