LOG.trace("[{}] Merging original request {} with new request {}", channelUuid, request, other); request.setRequestId(other.getRequestId()); request.getClientSyncMetaData() .setProfileHash(other.getClientSyncMetaData().getProfileHash()); LOG.debug("[{}] Updated request id and profile hash", channelUuid); ClientSync diff = new ClientSync(); diff.setRequestId(other.getRequestId()); diff.setClientSyncMetaData(other.getClientSyncMetaData()); diff.setUseConfigurationRawSchema(other.isUseConfigurationRawSchema()); if (request.getClientSyncMetaData().getApplicationToken() != null) { LOG.debug("Setting application token, as it was null: {}", request.getClientSyncMetaData().getApplicationToken()); diff.getClientSyncMetaData() .setApplicationToken(request.getClientSyncMetaData().getApplicationToken()); } else { LOG.trace("[{}] Application token is null for request", request); boolean hasProfileSync = other.getProfileSync() != null; if (hasProfileSync) { diff.setProfileSync(other.getProfileSync()); request.setProfileSync(other.getProfileSync()); LOG.debug("[{}] Updated profile request", channelUuid); if (other.getConfigurationSync() != null) { ConfigurationClientSync mergedConfigurationClientSync = hasProfileSync || other.isForceConfigurationSync() ? other.getConfigurationSync() : diff(request.getConfigurationSync(), other.getConfigurationSync());
/** * Returns whether a transport a type is valid. * @param type transport type * @return true if type is valid otherwise false */ public boolean isValid(TransportType type) { switch (type) { case EVENT: return request.getEventSync() != null; case NOTIFICATION: return request.getNotificationSync() != null; case CONFIGURATION: return request.getConfigurationSync() != null; case USER: return request.getUserSync() != null; case PROFILE: return request.getProfileSync() != null; case LOGGING: return request.getLogSync() != null; default: return false; } }
private void syncChannels(ActorContext context, Set<ChannelMetaData> channels, boolean cfUpdate, boolean nfUpdate) { for (ChannelMetaData channel : channels) { ClientSync originalRequest = channel.getRequestMessage().getRequest(); ClientSync newRequest = new ClientSync(); newRequest.setRequestId(originalRequest.getRequestId()); newRequest.setClientSyncMetaData(originalRequest.getClientSyncMetaData()); newRequest.setUseConfigurationRawSchema(originalRequest.isUseConfigurationRawSchema()); if (cfUpdate && originalRequest.getConfigurationSync() != null) { newRequest.setForceConfigurationSync(true); newRequest.setConfigurationSync(originalRequest.getConfigurationSync()); } if (nfUpdate && originalRequest.getNotificationSync() != null) { newRequest.setForceNotificationSync(true); newRequest.setNotificationSync(originalRequest.getNotificationSync()); } LOG.debug("[{}][{}] Processing request {}", endpointKey, actorKey, newRequest); sync(context, new SyncRequestMessage(channel.getRequestMessage().getSession(), newRequest, channel.getRequestMessage().getCommand(), channel.getRequestMessage().getOriginator())); } }
/** * Converts Avro {@link SyncRequest} to {@link ClientSync}. * * @param source the avro structure * @return the client sync */ public static ClientSync convert(SyncRequest source) { ClientSync dest = new ClientSync(); dest.setRequestId(source.getRequestId()); dest.setClientSyncMetaData(convert(source.getSyncRequestMetaData())); dest.setBootstrapSync(convert(source.getBootstrapSyncRequest())); dest.setProfileSync(convert(source.getProfileSyncRequest())); dest.setConfigurationSync(convert(source.getConfigurationSyncRequest())); dest.setNotificationSync(convert(source.getNotificationSyncRequest())); dest.setEventSync(convert(source.getEventSyncRequest())); dest.setUserSync(convert(source.getUserSyncRequest())); dest.setLogSync(convert(source.getLogSyncRequest())); return dest; }
private SyncContext sync(ClientSync request) throws GetDeltaException { if (!request.isValid()) { LOG.warn("[{}] Request is not valid. It does not contain profile information!", endpointKey); return SyncContext.failure(request.getRequestId()); context.setRequestId(request.getRequestId()); context.setStatus(SyncStatus.SUCCESS); context.setEndpointKey(endpointKey); context.setRequestHash(request.hashCode()); context.setMetaData(request.getClientSyncMetaData()); context = operationsService.syncClientProfile(context, request.getProfileSync()); context = operationsService.syncUseConfigurationRawSchema( context, request.isUseConfigurationRawSchema()); context, request.getUserSync()); context = operationsService.processEventListenerRequests(context, request.getEventSync()); context = operationsService.syncConfiguration(context, request.getConfigurationSync()); context = operationsService.syncNotification(context, request.getNotificationSync()); endpointKey, request.hashCode(), context.getResponse());
/** * Updates a request. */ public void updateRequest(ServerSync response) { UUID channelUuid = getChannelUuid(); LOG.debug("[{}] Cleanup profile request", channelUuid); request.setProfileSync(null); if (request.getUserSync() != null) { LOG.debug("[{}] Cleanup user request", channelUuid); request.setUserSync(new UserClientSync()); } if (request.getEventSync() != null) { LOG.debug("[{}] Cleanup event request", channelUuid); request.setEventSync(new EventClientSync()); } if (request.getLogSync() != null) { LOG.debug("[{}] Cleanup log request", channelUuid); request.getLogSync().setLogEntries(null); } if (request.getNotificationSync() != null) { LOG.debug("[{}] Cleanup/update notification request", channelUuid); request.getNotificationSync().setSubscriptionCommands(null); request.getNotificationSync().setAcceptedUnicastNotifications(null); } }
/** * Return true if client sync information is valid or false if not. * * @return true if valid, false if not */ public boolean isValid() { ClientSyncMetaData md = this.getClientSyncMetaData(); // TODO: validate if public key hash matches hash of public key during // profile registration command. if (md.getProfileHash() == null) { ProfileClientSync profileRequest = this.getProfileSync(); if (profileRequest == null || profileRequest.getEndpointPublicKey() == null) { return false; } } return true; }
private ClientSync validate(ClientSync sync) throws PlatformEncDecException { if (sync.getClientSyncMetaData() == null) { throw new PlatformEncDecException( MessageFormat.format("Input data does not have client sync meta data: {0}!", sync)); } return sync; } }
private void parseClientSyncMetaData(ClientSync sync, ByteBuffer buf, int options, int payloadLength) throws PlatformEncDecException { sync.setRequestId(buf.getInt()); ClientSyncMetaData md = new ClientSyncMetaData(); if (hasOption(options, CLIENT_META_SYNC_TIMEOUT_OPTION)) { md.setTimeout((long) buf.getInt()); } if (hasOption(options, CLIENT_META_SYNC_KEY_HASH_OPTION)) { md.setEndpointPublicKeyHash(getNewByteBuffer(buf, PUBLIC_KEY_HASH_SIZE)); } if (hasOption(options, CLIENT_META_SYNC_PROFILE_HASH_OPTION)) { md.setProfileHash(getNewByteBuffer(buf, PROFILE_HASH_SIZE)); } if (hasOption(options, CLIENT_META_SYNC_SDK_TOKEN_OPTION)) { md.setSdkToken(getUtf8String(buf, Constants.SDK_TOKEN_SIZE)); } sync.setClientSyncMetaData(md); }
private PublicKey getPublicKey(ClientSync request) throws GeneralSecurityException { PublicKey endpointKey = null; if (request.getProfileSync() != null && request.getProfileSync().getEndpointPublicKey() != null) { byte[] publicKeySrc = request.getProfileSync().getEndpointPublicKey().array(); endpointKey = KeyUtil.getPublic(publicKeySrc); } if (endpointKey == null) { EndpointObjectHash hash = getEndpointObjectHash(request); endpointKey = cacheService.getEndpointKey(hash); } return endpointKey; }
private void parseLogClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { LogClientSync logSync = new LogClientSync(); logSync.setRequestId(getIntFromUnsignedShort(buf)); int size = getIntFromUnsignedShort(buf); List<LogEntry> logs = new ArrayList<>(size); for (int i = 0; i < size; i++) { logs.add(new LogEntry(getNewByteBuffer(buf, buf.getInt()))); } logSync.setLogEntries(logs); sync.setLogSync(logSync); }
private void parseProfileClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { int payloadLimitPosition = buf.position() + payloadLength; ProfileClientSync profileSync = new ProfileClientSync(); profileSync.setProfileBody(getNewByteBuffer(buf, buf.getInt())); while (buf.position() < payloadLimitPosition) { byte fieldId = buf.get(); // reading unused reserved field buf.get(); switch (fieldId) { case PUBLIC_KEY_FIELD_ID: profileSync.setEndpointPublicKey(getNewByteBuffer(buf, getIntFromUnsignedShort(buf))); break; case ACCESS_TOKEN_FIELD_ID: profileSync.setEndpointAccessToken(getUtf8String(buf)); break; default: break; } } sync.setProfileSync(profileSync); }
private void parseEventClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { EventClientSync eventSync = new EventClientSync(); if (hasOption(options, EVENT_SEQ_NUMBER_REQUEST_OPTION)) { eventSync.setSeqNumberRequest(true); } int payloadLimitPosition = buf.position() + payloadLength; while (buf.position() < payloadLimitPosition) { byte fieldId = buf.get(); // reading unused reserved field buf.get(); switch (fieldId) { case EVENT_LISTENERS_FIELD_ID: eventSync.setEventListenersRequests(parseListenerRequests(buf)); break; case EVENT_LIST_FIELD_ID: eventSync.setEvents(parseEvents(buf)); break; default: break; } } sync.setEventSync(eventSync); }
private void parseNotificationClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { int payloadLimitPosition = buf.position() + payloadLength; NotificationClientSync nfSync = new NotificationClientSync(); nfSync.setTopicListHash(buf.getInt()); while (buf.position() < payloadLimitPosition) { byte fieldId = buf.get(); // reading unused reserved field buf.get(); switch (fieldId) { case NF_TOPIC_STATES_FIELD_ID: nfSync.setTopicStates(parseTopicStates(buf)); break; case NF_UNICAST_LIST_FIELD_ID: nfSync.setAcceptedUnicastNotifications(parseUnicastIds(buf)); break; case NF_SUBSCRIPTION_ADD_FIELD_ID: parseSubscriptionCommands(nfSync, buf, true); break; case NF_SUBSCRIPTION_REMOVE_FIELD_ID: parseSubscriptionCommands(nfSync, buf, false); break; default: break; } } sync.setNotificationSync(nfSync); }
private ClientSync parseExtensions(ByteBuffer buf, int protocolVersion, int extensionsCount) throws PlatformEncDecException { ClientSync sync = new ClientSync(); for (short extPos = 0; extPos < extensionsCount; extPos++) { if (buf.remaining() < MIN_SIZE_OF_EXTENSION_HEADER) {
private void parseConfigurationClientSync(ClientSync sync, ByteBuffer buf, int options, int payloadLength) { ConfigurationClientSync confSync = new ConfigurationClientSync(); if (hasOption(options, CONFIGURATION_HASH_OPTION)) { confSync.setConfigurationHash(getNewByteBuffer(buf, CONFIGURATION_HASH_SIZE)); } if (hasOption(options, CONFIGURATION_RESYNC_OPTION)) { confSync.setResyncOnly(true); } sync.setConfigurationSync(confSync); }
private String getSdkToken(ClientSync request) { return request.getClientSyncMetaData().getSdkToken(); }
protected EndpointObjectHash getEndpointObjectHash(ClientSync request) { return EndpointObjectHash.fromBytes( request.getClientSyncMetaData().getEndpointPublicKeyHash().array()); } }
private long getDelay(SyncRequestMessage requestMessage, long start) { return requestMessage.getRequest() .getClientSyncMetaData() .getTimeout() - (System.currentTimeMillis() - start); }
private ClientSync decodePlatformLevelData(Integer platformId, byte[] requestRaw) throws PlatformEncDecException { PlatformEncDec encDec = platformEncDecMap.get(platformId); if (encDec != null) { ClientSync syncRequest = platformEncDecMap.get(platformId).decode(requestRaw); addAppTokenToClientSyncMetaData(syncRequest.getClientSyncMetaData()); return syncRequest; } else { throw new PlatformEncDecException( MessageFormat.format("Decoder for platform protocol [{0}] is not defined", platformId)); } }