@@ -33,6 +33,8 @@ of this software and associated documentation files (the "Software"), to deal
3333import java .util .UUID ;
3434import java .util .concurrent .CompletableFuture ;
3535import java .util .concurrent .ConcurrentHashMap ;
36+
37+ import eu .chargetime .ocpp .model .SessionInformation ;
3638import org .slf4j .Logger ;
3739import org .slf4j .LoggerFactory ;
3840
@@ -78,80 +80,89 @@ public void open(String hostname, int port, ServerEvents serverEvents) {
7880 listener .open (
7981 hostname ,
8082 port ,
81- (session , information ) -> {
82- session .accept (
83- new SessionEvents () {
84- @ Override
85- public void handleConfirmation (String uniqueId , Confirmation confirmation ) {
86-
87- Optional <CompletableFuture <Confirmation >> promiseOptional =
88- promiseRepository .getPromise (uniqueId );
89- if (promiseOptional .isPresent ()) {
90- promiseOptional .get ().complete (confirmation );
91- promiseRepository .removePromise (uniqueId );
92- } else {
93- logger .debug ("Promise not found for confirmation {}" , confirmation );
94- }
95- }
96-
97- @ Override
98- public Confirmation handleRequest (Request request )
99- throws UnsupportedFeatureException {
100- Optional <Feature > featureOptional = featureRepository .findFeature (request );
101- if (featureOptional .isPresent ()) {
102- Optional <UUID > sessionIdOptional = getSessionID (session );
103- if (sessionIdOptional .isPresent ()) {
104- return featureOptional .get ().handleRequest (sessionIdOptional .get (), request );
105- } else {
106- logger .error (
107- "Unable to handle request ({}), the active session was not found." ,
108- request );
109- throw new IllegalStateException ("Active session not found" );
110- }
111- } else {
112- throw new UnsupportedFeatureException ();
113- }
114- }
115-
116- @ Override
117- public void handleError (
118- String uniqueId , String errorCode , String errorDescription , Object payload ) {
119- Optional <CompletableFuture <Confirmation >> promiseOptional =
120- promiseRepository .getPromise (uniqueId );
121- if (promiseOptional .isPresent ()) {
122- promiseOptional
123- .get ()
124- .completeExceptionally (
125- new CallErrorException (errorCode , errorDescription , payload ));
126- promiseRepository .removePromise (uniqueId );
127- } else {
128- logger .debug ("Promise not found for error {}" , errorDescription );
129- }
130- }
131-
132- @ Override
133- public void handleConnectionClosed () {
134- Optional <UUID > sessionIdOptional = getSessionID (session );
135- if (sessionIdOptional .isPresent ()) {
136- serverEvents .lostSession (sessionIdOptional .get ());
137- sessions .remove (sessionIdOptional .get ());
138- } else {
139- logger .warn ("Active session not found" );
140- }
141- }
142-
143- @ Override
144- public void handleConnectionOpened () {}
145- });
146-
147- sessions .put (session .getSessionId (), session );
148-
149- Optional <UUID > sessionIdOptional = getSessionID (session );
150- if (sessionIdOptional .isPresent ()) {
151- serverEvents .newSession (sessionIdOptional .get (), information );
152- logger .debug ("Session created: {}" , session .getSessionId ());
153- } else {
154- throw new IllegalStateException ("Failed to create a session" );
83+ new ListenerEvents () {
84+
85+ @ Override
86+ public void authenticateSession (SessionInformation information , String username , byte [] password ) throws AuthenticationException {
87+ serverEvents .authenticateSession (information , username , password );
88+ }
89+
90+ @ Override
91+ public void newSession (ISession session , SessionInformation information ) {
92+ session .accept (
93+ new SessionEvents () {
94+ @ Override
95+ public void handleConfirmation (String uniqueId , Confirmation confirmation ) {
96+
97+ Optional <CompletableFuture <Confirmation >> promiseOptional =
98+ promiseRepository .getPromise (uniqueId );
99+ if (promiseOptional .isPresent ()) {
100+ promiseOptional .get ().complete (confirmation );
101+ promiseRepository .removePromise (uniqueId );
102+ } else {
103+ logger .debug ("Promise not found for confirmation {}" , confirmation );
104+ }
105+ }
106+
107+ @ Override
108+ public Confirmation handleRequest (Request request )
109+ throws UnsupportedFeatureException {
110+ Optional <Feature > featureOptional = featureRepository .findFeature (request );
111+ if (featureOptional .isPresent ()) {
112+ Optional <UUID > sessionIdOptional = getSessionID (session );
113+ if (sessionIdOptional .isPresent ()) {
114+ return featureOptional .get ().handleRequest (sessionIdOptional .get (), request );
115+ } else {
116+ logger .error (
117+ "Unable to handle request ({}), the active session was not found." ,
118+ request );
119+ throw new IllegalStateException ("Active session not found" );
120+ }
121+ } else {
122+ throw new UnsupportedFeatureException ();
123+ }
124+ }
125+
126+ @ Override
127+ public void handleError (
128+ String uniqueId , String errorCode , String errorDescription , Object payload ) {
129+ Optional <CompletableFuture <Confirmation >> promiseOptional =
130+ promiseRepository .getPromise (uniqueId );
131+ if (promiseOptional .isPresent ()) {
132+ promiseOptional
133+ .get ()
134+ .completeExceptionally (
135+ new CallErrorException (errorCode , errorDescription , payload ));
136+ promiseRepository .removePromise (uniqueId );
137+ } else {
138+ logger .debug ("Promise not found for error {}" , errorDescription );
139+ }
140+ }
141+
142+ @ Override
143+ public void handleConnectionClosed () {
144+ Optional <UUID > sessionIdOptional = getSessionID (session );
145+ if (sessionIdOptional .isPresent ()) {
146+ serverEvents .lostSession (sessionIdOptional .get ());
147+ sessions .remove (sessionIdOptional .get ());
148+ } else {
149+ logger .warn ("Active session not found" );
150+ }
151+ }
152+
153+ @ Override
154+ public void handleConnectionOpened () {}
155+ });
156+
157+ sessions .put (session .getSessionId (), session );
158+
159+ Optional <UUID > sessionIdOptional = getSessionID (session );
160+ if (sessionIdOptional .isPresent ()) {
161+ serverEvents .newSession (sessionIdOptional .get (), information );
162+ logger .debug ("Session created: {}" , session .getSessionId ());
163+ } else {
164+ throw new IllegalStateException ("Failed to create a session" );
165+ }
155166 }
156167 });
157168 }
0 commit comments