Skip to content

Commit 77d6978

Browse files
authored
Merge pull request #176 from in-fke/features/fix-profile-initialization
Features/fix profile initialization (nulls and cross-over in FirmwareManagementProfiles)
2 parents c4540ef + fb53e2d commit 77d6978

File tree

14 files changed

+122
-114
lines changed

14 files changed

+122
-114
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ 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(JSONConfiguration.WEBSOCKET_WORKER_COUNT, DEFAULT_WEBSOCKET_WORKER_COUNT),
87-
drafts) {
85+
new InetSocketAddress(hostname, port),
86+
configuration.getParameter(
87+
JSONConfiguration.WEBSOCKET_WORKER_COUNT, DEFAULT_WEBSOCKET_WORKER_COUNT),
88+
drafts) {
8889
@Override
8990
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
9091
logger.debug(
@@ -115,9 +116,9 @@ public void relay(String message) {
115116
String proxiedAddress = clientHandshake.getFieldValue(HTTP_HEADER_PROXIED_ADDRESS);
116117

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

122123
SessionInformation information =
123124
new SessionInformation.Builder()
@@ -131,13 +132,14 @@ public void relay(String message) {
131132
}
132133

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

142144
String username = null;
143145
byte[] password = null;
@@ -150,25 +152,26 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket web
150152
// split credentials on username and password
151153
for (int i = 0; i < credDecoded.length; i++) {
152154
if (credDecoded[i] == ':') {
153-
username = new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
155+
username =
156+
new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
154157
if (i + 1 < credDecoded.length) {
155158
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
156159
}
157160
break;
158161
}
159162
}
160163
}
161-
if (password == null || password.length < OCPPJ_CP_MIN_PASSWORD_LENGTH || password.length > OCPPJ_CP_MAX_PASSWORD_LENGTH)
164+
if (password == null
165+
|| password.length < OCPPJ_CP_MIN_PASSWORD_LENGTH
166+
|| password.length > OCPPJ_CP_MAX_PASSWORD_LENGTH)
162167
throw new InvalidDataException(401, "Invalid password length");
163168
}
164169

165170
try {
166171
handler.authenticateSession(information, username, password);
167-
}
168-
catch (AuthenticationException e) {
172+
} catch (AuthenticationException e) {
169173
throw new InvalidDataException(e.getErrorCode(), e.getMessage());
170-
}
171-
catch (Exception e) {
174+
} catch (Exception e) {
172175
throw new InvalidDataException(401, e.getMessage());
173176
}
174177
return super.onWebsocketHandshakeReceivedAsServer(webSocket, draft, clientHandshake);

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

Lines changed: 3 additions & 1 deletion
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.model.SessionInformation;
2929

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

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

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ 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;
3132
import java.util.Map;
3233
import java.util.Optional;
3334
import java.util.UUID;
3435
import java.util.concurrent.CompletableFuture;
3536
import java.util.concurrent.ConcurrentHashMap;
36-
37-
import eu.chargetime.ocpp.model.SessionInformation;
3837
import org.slf4j.Logger;
3938
import org.slf4j.LoggerFactory;
4039

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

8584
@Override
86-
public void authenticateSession(SessionInformation information, String username, byte[] password) throws AuthenticationException {
85+
public void authenticateSession(
86+
SessionInformation information, String username, byte[] password)
87+
throws AuthenticationException {
8788
serverEvents.authenticateSession(information, username, password);
8889
}
8990

9091
@Override
9192
public void newSession(ISession session, SessionInformation information) {
9293
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-
}
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");
124123
}
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-
});
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+
});
156159

157160
sessions.put(session.getSessionId(), session);
158161

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ 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) throws AuthenticationException {}
32+
default void authenticateSession(SessionInformation information, String username, byte[] password)
33+
throws AuthenticationException {}
3334

3435
void newSession(UUID sessionIndex, SessionInformation information);
3536

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ 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+
}
4346
profile = ownerProfile;
4447
}
4548

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/feature/profile/ClientCoreProfile.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ public ClientCoreProfile(ClientCoreEventHandler handler) {
5555
features = new ArrayList<>();
5656
eventHandler = handler;
5757

58-
features.add(new AuthorizeFeature(null));
59-
features.add(new BootNotificationFeature(null));
58+
features.add(new AuthorizeFeature(this));
59+
features.add(new BootNotificationFeature(this));
6060
features.add(new ChangeAvailabilityFeature(this));
6161
features.add(new ChangeConfigurationFeature(this));
6262
features.add(new ClearCacheFeature(this));
6363
features.add(new DataTransferFeature(this));
6464
features.add(new GetConfigurationFeature(this));
65-
features.add(new HeartbeatFeature(null));
66-
features.add(new MeterValuesFeature(null));
65+
features.add(new HeartbeatFeature(this));
66+
features.add(new MeterValuesFeature(this));
6767
features.add(new RemoteStartTransactionFeature(this));
6868
features.add(new RemoteStopTransactionFeature(this));
6969
features.add(new ResetFeature(this));
70-
features.add(new StartTransactionFeature(null));
71-
features.add(new StatusNotificationFeature(null));
72-
features.add(new StopTransactionFeature(null));
70+
features.add(new StartTransactionFeature(this));
71+
features.add(new StatusNotificationFeature(this));
72+
features.add(new StopTransactionFeature(this));
7373
features.add(new UnlockConnectorFeature(this));
7474
}
7575

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/feature/profile/ClientFirmwareManagementProfile.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public class ClientFirmwareManagementProfile implements Profile {
4141
public ClientFirmwareManagementProfile(ClientFirmwareManagementEventHandler eventHandler) {
4242
this.eventHandler = eventHandler;
4343
features = new HashSet<>();
44-
features.add(new DiagnosticsStatusNotificationFeature(null));
45-
features.add(new FirmwareStatusNotificationFeature(null));
4644
features.add(new GetDiagnosticsFeature(this));
4745
features.add(new UpdateFirmwareFeature(this));
4846
}

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/feature/profile/ServerCoreProfile.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ public ServerCoreProfile(ServerCoreEventHandler handler) {
4444
features = new HashSet<>();
4545
features.add(new AuthorizeFeature(this));
4646
features.add(new BootNotificationFeature(this));
47-
features.add(new ChangeAvailabilityFeature(null));
48-
features.add(new ChangeConfigurationFeature(null));
49-
features.add(new ClearCacheFeature(null));
47+
features.add(new ChangeAvailabilityFeature(this));
48+
features.add(new ChangeConfigurationFeature(this));
49+
features.add(new ClearCacheFeature(this));
5050
features.add(new DataTransferFeature(this));
51-
features.add(new GetConfigurationFeature(null));
51+
features.add(new GetConfigurationFeature(this));
5252
features.add(new HeartbeatFeature(this));
5353
features.add(new MeterValuesFeature(this));
54-
features.add(new RemoteStartTransactionFeature(null));
55-
features.add(new RemoteStopTransactionFeature(null));
56-
features.add(new ResetFeature(null));
54+
features.add(new RemoteStartTransactionFeature(this));
55+
features.add(new RemoteStopTransactionFeature(this));
56+
features.add(new ResetFeature(this));
5757
features.add(new StartTransactionFeature(this));
5858
features.add(new StatusNotificationFeature(this));
5959
features.add(new StopTransactionFeature(this));
60-
features.add(new UnlockConnectorFeature(null));
60+
features.add(new UnlockConnectorFeature(this));
6161
}
6262

6363
@Override

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/feature/profile/ServerFirmwareManagementProfile.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ public class ServerFirmwareManagementProfile implements Profile {
4545
public ServerFirmwareManagementProfile(ServerFirmwareManagementEventHandler eventHandler) {
4646
this.eventHandler = eventHandler;
4747
features = new HashSet<>();
48-
features.add(new GetDiagnosticsFeature(null));
4948
features.add(new DiagnosticsStatusNotificationFeature(this));
5049
features.add(new FirmwareStatusNotificationFeature(this));
51-
features.add(new UpdateFirmwareFeature(null));
5250
}
5351

5452
@Override

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/feature/profile/ServerLocalAuthListProfile.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public class ServerLocalAuthListProfile implements Profile {
4242

4343
public ServerLocalAuthListProfile() {
4444
featureList = new HashSet<>();
45-
featureList.add(new GetLocalListVersionFeature(null));
46-
featureList.add(new SendLocalListFeature(null));
45+
featureList.add(new GetLocalListVersionFeature(this));
46+
featureList.add(new SendLocalListFeature(this));
4747
}
4848

4949
@Override

0 commit comments

Comments
 (0)