@@ -27,6 +27,23 @@ public abstract class AbstractOcppResponseHandler<S extends RequestType, T exten
2727 // The default initial capacity is 10. We probably won't need that much.
2828 private ArrayList <OcppCallback <T >> callbackList = new ArrayList <>(2 );
2929
30+ // -------------------------------------------------------------------------
31+ // AsyncHandler
32+ // -------------------------------------------------------------------------
33+
34+ @ Override
35+ public void handleResponse (Response <T > res ) {
36+ try {
37+ processResponse (res .get ());
38+ } catch (Exception e ) {
39+ processException (e );
40+ }
41+ }
42+
43+ // -------------------------------------------------------------------------
44+ // OcppResponseHandler
45+ // -------------------------------------------------------------------------
46+
3047 @ Override
3148 public void addCallback (OcppCallback <T > cb ) {
3249 callbackList .add (cb );
@@ -37,32 +54,21 @@ public S getRequest() {
3754 return requestTask .getRequest ();
3855 }
3956
40- // -------------------------------------------------------------------------
41- // AsyncHandler
42- // -------------------------------------------------------------------------
43-
4457 @ Override
45- public void handleResponse (Response <T > res ) {
46- try {
47- handleResult (res .get ());
48- success (res .get ());
58+ public abstract void handleResult (T response );
4959
50- } catch (Exception e ) {
51- handleException (e );
52- failed (e .getMessage ());
53- }
60+ @ Override
61+ public void handleException (Exception e ) {
62+ processException (e );
5463 }
5564
5665 // -------------------------------------------------------------------------
57- // OcppResponseHandler
58- //
59- // Skip the method handleResult(T response), since it should be
60- // implemented by subclasses depending on the actual response
66+ // WsOcppResponseHandler
6167 // -------------------------------------------------------------------------
6268
6369 @ Override
64- public void handleException ( Exception e ) {
65- requestTask . addNewError ( chargeBoxId , e );
70+ public void handleResponse ( T response ) {
71+ processResponse ( response );
6672 }
6773
6874 /**
@@ -73,10 +79,12 @@ public void handleException(Exception e) {
7379 */
7480 @ Override
7581 public void handleError (OcppJsonError error ) {
76- requestTask .addNewResponse (chargeBoxId , error .toString ());
77-
78- // But, as far as the callbacks are concerned, this is still a failure.
79- failed (error .getErrorDescription ());
82+ try {
83+ requestTask .addNewResponse (chargeBoxId , error .toString ());
84+ } finally {
85+ // But, as far as the callbacks are concerned, this is still a failure.
86+ failed (error .getErrorDescription ());
87+ }
8088 }
8189
8290 // -------------------------------------------------------------------------
@@ -88,6 +96,22 @@ public void handleError(OcppJsonError error) {
8896 // callback in line.
8997 // -------------------------------------------------------------------------
9098
99+ private void processResponse (T response ) {
100+ try {
101+ handleResult (response );
102+ } finally {
103+ success (response );
104+ }
105+ }
106+
107+ private void processException (Exception e ) {
108+ try {
109+ requestTask .addNewError (chargeBoxId , e );
110+ } finally {
111+ failed (e .getMessage ());
112+ }
113+ }
114+
91115 private void success (T response ) {
92116 for (OcppCallback <T > c : callbackList ) {
93117 try {
0 commit comments