Browse Source

Added redis session store

pull/4631/head
AndrewVolosytnykhThingsboard 5 years ago
committed by Andrew Shvayka
parent
commit
e3fa441d6b
  1. 4
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbL2M2MDtlsSessionInMemoryStore.java
  2. 66
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbLwM2MDtlsSessionRedisStore.java
  3. 3
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbLwM2MDtlsSessionStore.java
  4. 6
      common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbLwM2mStoreFactory.java

4
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbL2M2MDtlsSessionInMemoryStore.java

@ -15,14 +15,10 @@
*/
package org.thingsboard.server.transport.lwm2m.server.store;
import org.springframework.stereotype.Component;
import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
import org.thingsboard.server.transport.lwm2m.secure.TbX509DtlsSessionInfo;
import java.util.concurrent.ConcurrentHashMap;
@Component
@TbLwM2mTransportComponent
public class TbL2M2MDtlsSessionInMemoryStore implements TbLwM2MDtlsSessionStore {
private final ConcurrentHashMap<String, TbX509DtlsSessionInfo> store = new ConcurrentHashMap<>();

66
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbLwM2MDtlsSessionRedisStore.java

@ -0,0 +1,66 @@
/**
* Copyright © 2016-2021 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.transport.lwm2m.server.store;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.transport.lwm2m.secure.TbX509DtlsSessionInfo;
public class TbLwM2MDtlsSessionRedisStore implements TbLwM2MDtlsSessionStore {
private static final String SESSION_EP = "SESSION#EP#";
RedisConnectionFactory connectionFactory;
public TbLwM2MDtlsSessionRedisStore(RedisConnectionFactory redisConnectionFactory) {
this.connectionFactory = redisConnectionFactory;
}
@Override
public void put(String endpoint, TbX509DtlsSessionInfo msg) {
try (var c = connectionFactory.getConnection()) {
var msgJson = JacksonUtil.convertValue(msg, JsonNode.class);
if (msgJson != null) {
c.set(getKey(endpoint), msgJson.toString().getBytes());
} else {
throw new RuntimeException("Problem with serialization of message: " + msg.toString());
}
}
}
@Override
public TbX509DtlsSessionInfo get(String endpoint) {
try (var c = connectionFactory.getConnection()) {
var data = c.get(getKey(endpoint));
if (data != null) {
return JacksonUtil.fromString(new String(data), TbX509DtlsSessionInfo.class);
} else {
return null;
}
}
}
@Override
public void remove(String endpoint) {
try (var c = connectionFactory.getConnection()) {
c.del(getKey(endpoint));
}
}
private byte[] getKey(String endpoint) {
return (SESSION_EP + endpoint).getBytes();
}
}

3
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbLwM2MDtlsSessionStore.java

@ -24,9 +24,6 @@ public interface TbLwM2MDtlsSessionStore {
TbX509DtlsSessionInfo get(String endpoint);
void remove(String endpoint);
//TODO: add way to delete the session by endpoint.
}

6
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/store/TbLwM2mStoreFactory.java

@ -60,4 +60,10 @@ public class TbLwM2mStoreFactory {
new TbLwM2mRedisSecurityStore(redisConfiguration.get().redisConnectionFactory()) : new InMemorySecurityStore());
}
@Bean
private TbLwM2MDtlsSessionStore sessionStore() {
return redisConfiguration.isPresent() && useRedis ?
new TbLwM2MDtlsSessionRedisStore(redisConfiguration.get().redisConnectionFactory()) : new TbL2M2MDtlsSessionInMemoryStore();
}
}

Loading…
Cancel
Save