Skip to content

Commit f73078a

Browse files
authored
Merge branch 'ChargeTimeEU:master' into master
2 parents c4ea934 + 1874cb2 commit f73078a

File tree

22 files changed

+173
-121
lines changed

22 files changed

+173
-121
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public JSONCommunicator(Radio radio) {
7777
super(radio);
7878
}
7979

80-
private static class ZonedDateTimeSerializer implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
80+
private static class ZonedDateTimeSerializer
81+
implements JsonSerializer<ZonedDateTime>, JsonDeserializer<ZonedDateTime> {
8182

8283
@Override
8384
public JsonElement serialize(
@@ -87,8 +88,8 @@ public JsonElement serialize(
8788

8889
@Override
8990
public ZonedDateTime deserialize(
90-
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
91-
throws JsonParseException {
91+
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
92+
throws JsonParseException {
9293
return ZonedDateTime.parse(jsonElement.getAsJsonPrimitive().getAsString());
9394
}
9495
}

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ of this software and associated documentation files (the "Software"), to deal
2727

2828
import eu.chargetime.ocpp.model.SessionInformation;
2929
import eu.chargetime.ocpp.wss.WssFactoryBuilder;
30-
import java.io.IOException;
3130
import java.net.ConnectException;
3231
import java.net.InetSocketAddress;
3332
import java.nio.charset.StandardCharsets;
@@ -82,9 +81,10 @@ public WebSocketListener(ISessionFactory sessionFactory, Draft... drafts) {
8281
public void open(String hostname, int port, ListenerEvents handler) {
8382
server =
8483
new WebSocketServer(
85-
new InetSocketAddress(hostname, port),
86-
configuration.getParameter(JSONConfiguration.WEBSOCKET_WORKER_COUNT, DEFAULT_WEBSOCKET_WORKER_COUNT),
87-
drafts) {
84+
new InetSocketAddress(hostname, port),
85+
configuration.getParameter(
86+
JSONConfiguration.WEBSOCKET_WORKER_COUNT, DEFAULT_WEBSOCKET_WORKER_COUNT),
87+
drafts) {
8888
@Override
8989
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
9090
if(Draft_HttpHealthCheck.isHttp(clientHandshake)){
@@ -120,9 +120,9 @@ public void relay(String message) {
120120
String proxiedAddress = clientHandshake.getFieldValue(HTTP_HEADER_PROXIED_ADDRESS);
121121

122122
logger.debug(
123-
"New web-socket connection opened from address: {} proxied for: {}",
124-
webSocket.getRemoteSocketAddress(),
125-
proxiedAddress);
123+
"New web-socket connection opened from address: {} proxied for: {}",
124+
webSocket.getRemoteSocketAddress(),
125+
proxiedAddress);
126126

127127
SessionInformation information =
128128
new SessionInformation.Builder()
@@ -136,13 +136,14 @@ public void relay(String message) {
136136
}
137137

138138
@Override
139-
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket webSocket, Draft draft,
140-
ClientHandshake clientHandshake) throws InvalidDataException {
139+
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(
140+
WebSocket webSocket, Draft draft, ClientHandshake clientHandshake)
141+
throws InvalidDataException {
141142
SessionInformation information =
142-
new SessionInformation.Builder()
143-
.Identifier(clientHandshake.getResourceDescriptor())
144-
.InternetAddress(webSocket.getRemoteSocketAddress())
145-
.build();
143+
new SessionInformation.Builder()
144+
.Identifier(clientHandshake.getResourceDescriptor())
145+
.InternetAddress(webSocket.getRemoteSocketAddress())
146+
.build();
146147

147148
String username = null;
148149
byte[] password = null;
@@ -155,25 +156,26 @@ public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket web
155156
// split credentials on username and password
156157
for (int i = 0; i < credDecoded.length; i++) {
157158
if (credDecoded[i] == ':') {
158-
username = new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
159+
username =
160+
new String(Arrays.copyOfRange(credDecoded, 0, i), StandardCharsets.UTF_8);
159161
if (i + 1 < credDecoded.length) {
160162
password = Arrays.copyOfRange(credDecoded, i + 1, credDecoded.length);
161163
}
162164
break;
163165
}
164166
}
165167
}
166-
if (password == null || password.length < OCPPJ_CP_MIN_PASSWORD_LENGTH || password.length > OCPPJ_CP_MAX_PASSWORD_LENGTH)
168+
if (password == null
169+
|| password.length < OCPPJ_CP_MIN_PASSWORD_LENGTH
170+
|| password.length > OCPPJ_CP_MAX_PASSWORD_LENGTH)
167171
throw new InvalidDataException(401, "Invalid password length");
168172
}
169173

170174
try {
171175
handler.authenticateSession(information, username, password);
172-
}
173-
catch (AuthenticationException e) {
176+
} catch (AuthenticationException e) {
174177
throw new InvalidDataException(e.getErrorCode(), e.getMessage());
175-
}
176-
catch (Exception e) {
178+
} catch (Exception e) {
177179
throw new InvalidDataException(401, e.getMessage());
178180
}
179181
return super.onWebsocketHandshakeReceivedAsServer(webSocket, draft, clientHandshake);

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ buildscript {
1515

1616
allprojects {
1717
group = 'eu.chargetime.ocpp'
18-
version = '0.5-SNAPSHOT'
18+
version = '1.0.2'
1919
}
2020

2121
subprojects {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public static void setExecutor(ExecutorService newExecutor) {
4343

4444
@Override
4545
public void fulfill(
46-
CompletableFuture<Confirmation> promise, SessionEvents eventHandler, Request request) {
47-
executor.submit(() -> promiseFulfiller.fulfill(promise, eventHandler, request));
46+
CompletableFuture<Confirmation> promise, SessionEvents eventHandler, Request request) {
47+
executor.submit(() -> promiseFulfiller.fulfill(promise, eventHandler, request));
4848
}
4949

5050
public AsyncPromiseFulfillerDecorator(PromiseFulfiller promiseFulfiller) {

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,45 @@
1-
package eu.chargetime.ocpp; /*
2-
ChargeTime.eu - Java-OCA-OCPP
1+
package eu.chargetime.ocpp;
2+
/*
3+
ChargeTime.eu - Java-OCA-OCPP
34
4-
MIT License
5+
MIT License
56
6-
Copyright (C) 2016-2018 Thomas Volden <[email protected]>
7+
Copyright (C) 2016-2018 Thomas Volden <[email protected]>
78
8-
Permission is hereby granted, free of charge, to any person obtaining a copy
9-
of this software and associated documentation files (the "Software"), to deal
10-
in the Software without restriction, including without limitation the rights
11-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
copies of the Software, and to permit persons to whom the Software is
13-
furnished to do so, subject to the following conditions:
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
1415
15-
The above copyright notice and this permission notice shall be included in all
16-
copies or substantial portions of the Software.
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
1718
18-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
SOFTWARE.
25-
*/
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.
26+
*/
2627

27-
/** Exception returned to an outgoing request if an error is reported from the other end. */
28+
/**
29+
* Exception returned to an outgoing request if an error is reported from the other end.
30+
*/
2831
public class CallErrorException extends Exception {
32+
2933
private String errorCode;
3034
private String errorDescription;
3135
private Object payload;
3236

3337
/**
3438
* Constructor.
3539
*
36-
* @param errorCode send from the other end.
40+
* @param errorCode send from the other end.
3741
* @param errorDescription describing the error.
38-
* @param payload raw payload send from the other end.
42+
* @param payload raw payload send from the other end.
3943
*/
4044
public CallErrorException(String errorCode, String errorDescription, Object payload) {
4145
super(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ public CompletableFuture<Confirmation> send(Request request)
163163
}
164164

165165
public UUID getSessionId() {
166-
return this.session.getSessionId();
166+
return this.session.getSessionId();
167167
}
168168
}

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: 72 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-
}
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-
}
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");
151123
}
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

@@ -216,6 +219,10 @@ public CompletableFuture<Confirmation> send(UUID sessionIndex, Request request)
216219
return promise;
217220
}
218221

222+
public boolean isSessionOpen(UUID sessionIndex) {
223+
return sessions.containsKey(sessionIndex);
224+
}
225+
219226
/**
220227
* Close connection to a client
221228
*

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

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

3434
void newSession(UUID sessionIndex, SessionInformation information);
3535

0 commit comments

Comments
 (0)