Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: cleanup control chars from jsonb #1034

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -53,14 +54,17 @@ public class DefaultEventReportingService implements EventReportingService, Exec
private final int maxBatchSize;
private final Object batchLock = new Object();
private final ScheduledExecutorService flushScheduler;
private final PersistenceService persistenceService;

@Inject
public DefaultEventReportingService(InstanceId instanceId,
ProcessConfiguration processConfiguration,
ApiClient apiClient) {
ApiClient apiClient,
PersistenceService persistenceService) {
this.instanceId = instanceId;
this.processEventsApi = new ProcessEventsApi(apiClient);
this.maxBatchSize = processConfiguration.events().batchSize();
this.persistenceService = persistenceService;
this.eventQueue = initializeQueue(maxBatchSize);
this.flushScheduler = Executors.newSingleThreadScheduledExecutor();

Expand Down Expand Up @@ -132,6 +136,9 @@ private void sendBatch(List<ProcessEventRequest> eventBatch) {
try {
getProcessEventsApi().batchEvent(instanceId.getValue(), eventBatch);
} catch (ApiException e) {
for (var event: eventBatch) {
saveEvent(event);
}
log.warn("Error while sending batch of {} event{} to the server: {}",
eventBatch.size(), eventBatch.isEmpty() ? "" : "s", e.getMessage());
}
Expand All @@ -141,6 +148,7 @@ private void sendSingle(ProcessEventRequest req) {
try {
getProcessEventsApi().event(instanceId.getValue(), req);
} catch (ApiException e) {
saveEvent(req);
log.warn("error while sending an event to the server: {}", e.getMessage());
}
}
Expand Down Expand Up @@ -171,4 +179,11 @@ public void run() {
}
}

private void saveEvent(ProcessEventRequest event) {
try {
persistenceService.persistFile("invalid_event_" + UUID.randomUUID() + ".json", out -> processEventsApi.getApiClient().getObjectMapper().writeValue(out, event));
} catch (Exception e) {
log.warn("can't save event", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* 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.
Expand Down Expand Up @@ -81,7 +81,7 @@ private static class MockedEventReportingService extends DefaultEventReportingSe
private final AtomicInteger flushCounter;

public MockedEventReportingService(ProcessConfiguration processConfiguration) {
super(new InstanceId(UUID.randomUUID()), processConfiguration, mock(ApiClient.class));
super(new InstanceId(UUID.randomUUID()), processConfiguration, mock(ApiClient.class), mock(PersistenceService.class));
this.mockProcessEventsApi = mock(ProcessEventsApi.class);
this.flushCounter = new AtomicInteger();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* 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.
Expand All @@ -29,6 +29,7 @@
import javax.inject.Singleton;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Pattern;

@Named
@Singleton
Expand All @@ -37,6 +38,8 @@ public class ConcordObjectMapper {
public static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {
};

private static final Pattern CONTROL_CHARS = Pattern.compile("[\\x00-\\x1F]");

private final ObjectMapper delegate;

@Inject
Expand Down Expand Up @@ -113,7 +116,7 @@ public <T> T fromString(String s, Class<T> valueType) {
}

private static String removeUnsupportedEscape(String str) {
return str.replace("\\u0000", "");
return CONTROL_CHARS.matcher(str).replaceAll("");
}

private <T> T deserialize(String o, TypeReference<T> valueTypeRef) {
Expand Down
Loading