/
DefaultSystemMessageCreator.java
54 lines (44 loc) · 2 KB
/
DefaultSystemMessageCreator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*******************************************************************************
* Copyright (c) 2017, 2018 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.broker.core.message.system;
import java.util.Map;
import org.eclipse.kapua.broker.core.plugin.KapuaConnectionContext;
import com.google.common.base.Splitter;
/**
* Default system message creator
*
* @since 1.0
*/
public class DefaultSystemMessageCreator implements SystemMessageCreator {
private static final String FIELD_SEPARATOR = ",,";
private static final String PAIR_SEPARATOR = ",";
private static final String DEVICE_ID_KEY = "DeviceId";
private static final String EVENT_KEY = "Event";
private static final String USERNAME_KEY = "Username";
@Override
public String createMessage(SystemMessageType systemMessageType, KapuaConnectionContext kbc) {
StringBuilder builder = new StringBuilder();
builder.append(EVENT_KEY).append(PAIR_SEPARATOR).append(systemMessageType.name());
builder.append(FIELD_SEPARATOR).append(DEVICE_ID_KEY).append(PAIR_SEPARATOR).append(kbc.getClientId());
builder.append(FIELD_SEPARATOR).append(USERNAME_KEY).append(PAIR_SEPARATOR).append(kbc.getUserName());
return builder.toString();
}
public Map<String, String> convertFrom(String message) {
return Splitter.on(FIELD_SEPARATOR).withKeyValueSeparator(PAIR_SEPARATOR).split(message);
}
public String getDeviceId(Map<String, String> map) {
return map.get(DEVICE_ID_KEY);
}
public String getUsername(Map<String, String> map) {
return map.get(USERNAME_KEY);
}
}