Prevent NPE in NetworkTabJSONResolver

Affects issues:
- Fixed #2079
This commit is contained in:
Risto Lahtela 2021-10-30 09:23:06 +03:00
parent d30d87b346
commit 88b30b9287
2 changed files with 18 additions and 6 deletions

View File

@ -20,7 +20,6 @@ import com.djrapitops.plan.identification.ServerUUID;
import com.djrapitops.plan.processing.Processing;
import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.WebserverSettings;
import com.djrapitops.plan.utilities.UnitSemaphoreAccessLock;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -29,6 +28,7 @@ import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.function.Supplier;
@ -45,7 +45,7 @@ public class AsyncJSONResolverService {
private final JSONStorage jsonStorage;
private final Map<String, Future<JSONStorage.StoredJSON>> currentlyProcessing;
private final Map<String, Long> previousUpdates;
private final UnitSemaphoreAccessLock accessLock; // Access lock prevents double processing same resource
private final ReentrantLock accessLock; // Access lock prevents double processing same resource
@Inject
public AsyncJSONResolverService(
@ -59,7 +59,7 @@ public class AsyncJSONResolverService {
currentlyProcessing = new ConcurrentHashMap<>();
previousUpdates = new ConcurrentHashMap<>();
accessLock = new UnitSemaphoreAccessLock();
accessLock = new ReentrantLock();
}
public <T> JSONStorage.StoredJSON resolve(
@ -131,7 +131,7 @@ public class AsyncJSONResolverService {
long updateThreshold = config.get(WebserverSettings.REDUCED_REFRESH_BARRIER);
Future<JSONStorage.StoredJSON> updatedJSON;
accessLock.enter();
accessLock.lock();
try {
// Check if the json is already being created
updatedJSON = currentlyProcessing.get(identifier);
@ -141,7 +141,7 @@ public class AsyncJSONResolverService {
currentlyProcessing.put(identifier, updatedJSON);
}
} finally {
accessLock.exit();
accessLock.unlock();
}
return updatedJSON;
}

View File

@ -24,7 +24,9 @@ import com.djrapitops.plan.delivery.web.resolver.request.Request;
import com.djrapitops.plan.delivery.web.resolver.request.WebUser;
import com.djrapitops.plan.delivery.webserver.cache.AsyncJSONResolverService;
import com.djrapitops.plan.delivery.webserver.cache.DataID;
import com.djrapitops.plan.delivery.webserver.cache.JSONStorage;
import com.djrapitops.plan.identification.Identifiers;
import com.djrapitops.plan.utilities.java.Maps;
import java.util.Optional;
import java.util.function.Supplier;
@ -60,9 +62,19 @@ public class NetworkTabJSONResolver<T> implements Resolver {
}
private Response getResponse(Request request) {
JSONStorage.StoredJSON json = asyncJSONResolverService.resolve(Identifiers.getTimestamp(request), dataID, jsonCreator);
if (json == null) {
return Response.builder()
.setMimeType(MimeType.JSON)
.setJSONContent(asyncJSONResolverService.resolve(Identifiers.getTimestamp(request), dataID, jsonCreator).json)
.setJSONContent(Maps.builder(String.class, String.class)
.put("error", "Json failed to generate for some reason, see /Plan/logs for errors")
.build())
.build();
}
return Response.builder()
.setMimeType(MimeType.JSON)
.setJSONContent(json.json)
.build();
}
}