@Override public void persist() { if (hasUpdate) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(baos, null); SpecificDatumWriter<Topic> datumWriter = new SpecificDatumWriter<>(Topic.class); try { for (Topic topic : topicMap.values()) { datumWriter.write(topic, encoder); LOG.info("Persisted {}", topic); } encoder.flush(); String base64Str = new String(base64.encodeBase64(baos.toByteArray()), Charset.forName("UTF-8")); state.setProperty(TOPIC_LIST, base64Str); } catch (IOException e) { LOG.error("Can't persist topic list info", e); } baos = new ByteArrayOutputStream(); try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(nfSubscriptions); String base64Str = new String(base64.encodeBase64(baos.toByteArray()), Charset.forName("UTF-8")); state.setProperty(NF_SUBSCRIPTIONS, base64Str); } catch (IOException e) { LOG.error("Can't persist notification subscription info", e); } StringBuilder attachedEndpointsString = new StringBuilder(); for (Map.Entry<EndpointAccessToken, EndpointKeyHash> attached : attachedEndpoints.entrySet()) { attachedEndpointsString .append(attached.getKey().getToken()) .append(":") .append(attached.getValue().getKeyHash()) .append(','); } state.setProperty(ATTACHED_ENDPOINTS, attachedEndpointsString.toString()); state.setProperty(EVENT_SEQ_NUM, "" + eventSequence.get()); if (topicListHash != null) { state.setProperty(TOPIC_LIST_HASH, "" + topicListHash); } OutputStream os = null; try { storage.renameTo(stateFileLocation, stateFileLocation + "_bckp"); os = storage.openForWrite(stateFileLocation); state.store(os, null); hasUpdate = false; } catch (IOException e) { LOG.error("Can't persist state file", e); } finally { IOUtils.closeQuietly(os); } } }
@Override public EndpointObjectHash getProfileHash() { return EndpointObjectHash.fromBytes( base64.decodeBase64( state .getProperty( PROFILE_HASH, new String(base64.encodeBase64(new byte[0]), Charsets.UTF_8)) .getBytes(Charsets.UTF_8))); }
@Override public EndpointKeyHash getEndpointKeyHash() { if (keyHash == null) { EndpointObjectHash publicKeyHash = EndpointObjectHash.fromSHA1(getOrInitKeyPair().getPublic().getEncoded()); keyHash = new EndpointKeyHash(new String(base64.encodeBase64(publicKeyHash.getData()))); } return keyHash; }
private boolean isSDKPropertiesUpdated(KaaClientProperties sdkProperties) { byte[] hashFromSDK = sdkProperties.getPropertiesHash(); byte[] hashFromStateFile = base64.decodeBase64( state .getProperty( PROPERTIES_HASH, new String(base64.encodeBase64(new byte[0]), Charsets.UTF_8)) .getBytes(Charsets.UTF_8)); return !Arrays.equals(hashFromSDK, hashFromStateFile); }
@Override public void setProfileHash(EndpointObjectHash hash) { setStateStringValue( PROFILE_HASH, new String(base64.encodeBase64(hash.getData()), Charsets.UTF_8)); }
private void setPropertiesHash(byte[] hash) { setStateStringValue(PROPERTIES_HASH, new String(base64.encodeBase64(hash), Charsets.UTF_8)); }