Skip to content

Commit 9a50c75

Browse files
authored
Merge branch 'ChargeTimeEU:master' into master
2 parents d5485cc + 63ff70c commit 9a50c75

File tree

22 files changed

+158
-123
lines changed

22 files changed

+158
-123
lines changed

OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ public WebSocketListener(ISessionFactory sessionFactory, Draft... drafts) {
8282
public void open(String hostname, int port, ListenerEvents handler) {
8383
server =
8484
new WebSocketServer(
85-
new InetSocketAddress(hostname, port),
86-
configuration.getParameter(
87-
JSONConfiguration.WEBSOCKET_WORKER_COUNT, DEFAULT_WEBSOCKET_WORKER_COUNT),
88-
drafts) {
85+
new InetSocketAddress(hostname, port),
86+
configuration.getParameter(JSONConfiguration.WEBSOCKET_WORKER_COUNT, DEFAULT_WEBSOCKET_WORKER_COUNT),
87+
drafts) {
8988
@Override
9089
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
9190
logger.debug(
@@ -116,9 +115,9 @@ public void relay(String message) {
116115
String proxiedAddress = clientHandshake.getFieldValue(HTTP_HEADER_PROXIED_ADDRESS);
117116

118117
logger.debug(
119-
"New web-socket connection opened from address: {} proxied for: {}",
120-
webSocket.getRemoteSocketAddress(),
121-
proxiedAddress);
118+
"New web-socket connection opened from address: {} proxied for: {}",
119+
webSocket.getRemoteSocketAddress(),
120+
proxiedAddress);
122121

123122
SessionInformation information =
124123
new SessionInformation.Builder()
@@ -132,14 +131,13 @@ public void relay(String message) {
132131
}
133132

134133
@Override
135-
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
136-
WebSocket webSocket, Draft draft, ClientHandshake clientHandshake)
137-
throws InvalidDataException {
134+
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket webSocket, Draft draft,
135+
ClientHandshake clientHandshake) throws InvalidDataException {
138136
SessionInformation information =
139-
new SessionInformation.Builder()
140-
.Identifier(clientHandshake.getResourceDescriptor())
141-
.InternetAddress(webSocket.getRemoteSocketAddress())
142-
.build();
137+
new SessionInformation.Builder()
138+
.Identifier(clientHandshake.getResourceDescriptor())
139+
.InternetAddress(webSocket.getRemoteSocketAddress())
140+
.build();
143141

144142
String username = null;
145143
byte[] password = null;
@@ -152,26 +150,25 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
152150
// split credentials on username and password
153151
for (int i = 0; i < credDecoded.length; i++) {
154152
if (credDecoded[i] == ':') {
155-
username =
156-
new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
153+
username = new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
157154
if (i + 1 < credDecoded.length) {
158155
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
159156
}
160157
break;
161158
}
162159
}
163160
}
164-
if (password == null
165-
|| password.length < OCPPJ_CP_MIN_PASSWORD_LENGTH
166-
|| password.length > OCPPJ_CP_MAX_PASSWORD_LENGTH)
161+
if (password == null || password.length < OCPPJ_CP_MIN_PASSWORD_LENGTH || password.length > OCPPJ_CP_MAX_PASSWORD_LENGTH)
167162
throw new InvalidDataException(401, "Invalid password length");
168163
}
169164

170165
try {
171166
handler.authenticateSession(information, username, password);
172-
} catch (AuthenticationException e) {
167+
}
168+
catch (AuthenticationException e) {
173169
throw new InvalidDataException(e.getErrorCode(), e.getMessage());
174-
} catch (Exception e) {
170+
}
171+
catch (Exception e) {
175172
throw new InvalidDataException(401, e.getMessage());
176173
}
177174
return super.onWebsocketHandshakeReceivedAsServer(webSocket, draft, clientHandshake);

ocpp-common/src/main/java/eu/chargetime/ocpp/Client.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
3030
import eu.chargetime.ocpp.model.Confirmation;
3131
import eu.chargetime.ocpp.model.Request;
3232
import java.util.Optional;
33+
import java.util.UUID;
3334
import java.util.concurrent.CompletableFuture;
3435
import org.slf4j.Logger;
3536
import org.slf4j.LoggerFactory;
@@ -89,7 +90,7 @@ public void handleConfirmation(String uniqueId, Confirmation confirmation) {
8990
public Confirmation handleRequest(Request request) throws UnsupportedFeatureException {
9091
Optional<Feature> featureOptional = featureRepository.findFeature(request);
9192
if (featureOptional.isPresent()) {
92-
return featureOptional.get().handleRequest(null, request);
93+
return featureOptional.get().handleRequest(getSessionId(), request);
9394
} else {
9495
throw new UnsupportedFeatureException();
9596
}
@@ -160,4 +161,8 @@ public CompletableFuture<Confirmation> send(Request request)
160161
session.sendRequest(featureOptional.get().getAction(), request, id);
161162
return promise;
162163
}
164+
165+
public UUID getSessionId() {
166+
return this.session.getSessionId();
167+
}
163168
}

ocpp-common/src/main/java/eu/chargetime/ocpp/ListenerEvents.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ of this software and associated documentation files (the "Software"), to deal
2828
import eu.chargetime.ocpp.model.SessionInformation;
2929

3030
public interface ListenerEvents {
31-
void authenticateSession(SessionInformation information, String username, byte[] password)
32-
throws AuthenticationException;
33-
31+
void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException;
3432
void newSession(ISession session, SessionInformation information);
3533
}

ocpp-common/src/main/java/eu/chargetime/ocpp/Server.java

Lines changed: 65 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ of this software and associated documentation files (the "Software"), to deal
2828
import eu.chargetime.ocpp.feature.Feature;
2929
import eu.chargetime.ocpp.model.Confirmation;
3030
import eu.chargetime.ocpp.model.Request;
31-
import eu.chargetime.ocpp.model.SessionInformation;
3231
import java.util.Map;
3332
import java.util.Optional;
3433
import java.util.UUID;
3534
import java.util.concurrent.CompletableFuture;
3635
import java.util.concurrent.ConcurrentHashMap;
36+
37+
import eu.chargetime.ocpp.model.SessionInformation;
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

@@ -82,80 +83,76 @@ public void open(String hostname, int port, ServerEvents serverEvents) {
8283
new ListenerEvents() {
8384

8485
@Override
85-
public void authenticateSession(
86-
SessionInformation information, String username, byte[] password)
87-
throws AuthenticationException {
86+
public void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException {
8887
serverEvents.authenticateSession(information, username, password);
8988
}
9089

9190
@Override
9291
public void newSession(ISession session, SessionInformation information) {
9392
session.accept(
94-
new SessionEvents() {
95-
@Override
96-
public void handleConfirmation(String uniqueId, Confirmation confirmation) {
97-
98-
Optional<CompletableFuture<Confirmation>> promiseOptional =
99-
promiseRepository.getPromise(uniqueId);
100-
if (promiseOptional.isPresent()) {
101-
promiseOptional.get().complete(confirmation);
102-
promiseRepository.removePromise(uniqueId);
103-
} else {
104-
logger.debug("Promise not found for confirmation {}", confirmation);
105-
}
106-
}
107-
108-
@Override
109-
public Confirmation handleRequest(Request request)
110-
throws UnsupportedFeatureException {
111-
Optional<Feature> featureOptional = featureRepository.findFeature(request);
112-
if (featureOptional.isPresent()) {
113-
Optional<UUID> sessionIdOptional = getSessionID(session);
114-
if (sessionIdOptional.isPresent()) {
115-
return featureOptional
116-
.get()
117-
.handleRequest(sessionIdOptional.get(), request);
118-
} else {
119-
logger.error(
120-
"Unable to handle request ({}), the active session was not found.",
121-
request);
122-
throw new IllegalStateException("Active session not found");
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+
}
123124
}
124-
} else {
125-
throw new UnsupportedFeatureException();
126-
}
127-
}
128-
129-
@Override
130-
public void handleError(
131-
String uniqueId, String errorCode, String errorDescription, Object payload) {
132-
Optional<CompletableFuture<Confirmation>> promiseOptional =
133-
promiseRepository.getPromise(uniqueId);
134-
if (promiseOptional.isPresent()) {
135-
promiseOptional
136-
.get()
137-
.completeExceptionally(
138-
new CallErrorException(errorCode, errorDescription, payload));
139-
promiseRepository.removePromise(uniqueId);
140-
} else {
141-
logger.debug("Promise not found for error {}", errorDescription);
142-
}
143-
}
144-
145-
@Override
146-
public void handleConnectionClosed() {
147-
Optional<UUID> sessionIdOptional = getSessionID(session);
148-
if (sessionIdOptional.isPresent()) {
149-
serverEvents.lostSession(sessionIdOptional.get());
150-
sessions.remove(sessionIdOptional.get());
151-
} else {
152-
logger.warn("Active session not found");
153-
}
154-
}
155-
156-
@Override
157-
public void handleConnectionOpened() {}
158-
});
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+
});
159156

160157
sessions.put(session.getSessionId(), session);
161158

ocpp-common/src/main/java/eu/chargetime/ocpp/ServerEvents.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
2929
import java.util.UUID;
3030

3131
public interface ServerEvents {
32-
default void authenticateSession(SessionInformation information, String username, byte[] password)
33-
throws AuthenticationException {}
32+
default void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException {}
3433

3534
void newSession(UUID sessionIndex, SessionInformation information);
3635

ocpp-common/src/main/java/eu/chargetime/ocpp/feature/ProfileFeature.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public abstract class ProfileFeature implements Feature {
4040
* @param ownerProfile the {@link Profile} that owns the function.
4141
*/
4242
public ProfileFeature(Profile ownerProfile) {
43-
if (ownerProfile == null) {
44-
throw new IllegalArgumentException("need non-null profile");
45-
}
4643
profile = ownerProfile;
4744
}
4845

ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/JSONTestClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import eu.chargetime.ocpp.model.Confirmation;
3232
import eu.chargetime.ocpp.model.Request;
3333
import java.util.Collections;
34+
import java.util.UUID;
3435
import java.util.concurrent.CompletionStage;
3536
import org.java_websocket.drafts.Draft;
3637
import org.java_websocket.drafts.Draft_6455;
@@ -88,4 +89,9 @@ public void disconnect() {
8889
public boolean isClosed() {
8990
return transmitter.isClosed();
9091
}
92+
93+
@Override
94+
public UUID getSessionId() {
95+
return client.getSessionId();
96+
}
9197
}

ocpp-v1_6-test/src/main/java/eu/chargetime/ocpp/test/SOAPTestClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ of this software and associated documentation files (the "Software"), to deal
3535
import java.io.IOException;
3636
import java.net.InetSocketAddress;
3737
import java.net.URL;
38+
import java.util.UUID;
3839
import java.util.concurrent.CompletionStage;
3940
import java.util.concurrent.ExecutionException;
4041
import java.util.concurrent.ExecutorService;
@@ -116,6 +117,11 @@ public boolean isClosed() {
116117
return transmitter.isClosed();
117118
}
118119

120+
@Override
121+
public UUID getSessionId() {
122+
return client.getSessionId();
123+
}
124+
119125
private int getPort() {
120126
return callback.getPort() == -1 ? 8000 : callback.getPort();
121127
}

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/IClientAPI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ of this software and associated documentation files (the "Software"), to deal
2828
import eu.chargetime.ocpp.feature.profile.Profile;
2929
import eu.chargetime.ocpp.model.Confirmation;
3030
import eu.chargetime.ocpp.model.Request;
31+
32+
import java.util.UUID;
3133
import java.util.concurrent.CompletionStage;
3234

3335
public interface IClientAPI {
@@ -41,4 +43,6 @@ CompletionStage<Confirmation> send(Request request)
4143
void disconnect();
4244

4345
boolean isClosed();
46+
47+
UUID getSessionId();
4448
}

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/JSONClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import eu.chargetime.ocpp.wss.WssSocketBuilder;
99
import java.io.IOException;
1010
import java.util.Collections;
11+
import java.util.UUID;
1112
import java.util.concurrent.CompletionStage;
1213
import javax.net.ssl.SSLContext;
1314
import org.java_websocket.drafts.Draft;
@@ -171,4 +172,9 @@ public void disconnect() {
171172
public boolean isClosed() {
172173
return transmitter.isClosed();
173174
}
175+
176+
@Override
177+
public UUID getSessionId() {
178+
return client.getSessionId();
179+
}
174180
}

0 commit comments

Comments
 (0)