Skip to content

Commit

Permalink
Merge pull request #184 from swisspost/feature/some_cleanup
Browse files Browse the repository at this point in the history
Some code cleanup
  • Loading branch information
mcweba authored May 2, 2024
2 parents c8e1083 + ff5b511 commit 412952b
Show file tree
Hide file tree
Showing 17 changed files with 19 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.vertx.core.AsyncResult;
import io.vertx.core.Vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.slf4j.LoggerFactory.getLogger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.swisspush.reststorage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -14,7 +13,7 @@
public class MimeTypeResolver {

private static final Logger log = getLogger(MimeTypeResolver.class);
private Map<String, String> mimeTypes = new HashMap<>();
private final Map<String, String> mimeTypes = new HashMap<>();

private final String defaultMimeType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.vertx.ext.auth.authentication.Credentials;
import io.vertx.ext.auth.authentication.UsernamePasswordCredentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.util.ModuleConfiguration;

import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.util.ModuleConfiguration;

import static org.slf4j.LoggerFactory.getLogger;
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/org/swisspush/reststorage/RestStorageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.slf4j.Logger;
import org.swisspush.reststorage.util.*;

import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.*;

Expand Down Expand Up @@ -194,7 +193,7 @@ public void handle(Resource resource) {

StringBuilder body = new StringBuilder(1024);
String editor = null;
if (editors.size() > 0) {
if (!editors.isEmpty()) {
editor = editors.values().iterator().next();
}
body.append("<!DOCTYPE html>\n");
Expand Down Expand Up @@ -279,12 +278,8 @@ public void handle(Resource resource) {
documentResource.closeHandler.handle(null);
rsp.end();
});
documentResource.addErrorHandler(ex -> {
log.error("TODO error handling", new Exception(ex));
});
documentResource.readStream.exceptionHandler((Handler<Throwable>) ex -> {
log.error("TODO error handling", new Exception(ex));
});
documentResource.addErrorHandler(ex -> log.error("TODO error handling", new Exception(ex)));
documentResource.readStream.exceptionHandler(ex -> log.error("TODO error handling", new Exception(ex)));
pump.start();
}
}
Expand Down Expand Up @@ -758,7 +753,7 @@ private void respondWith(HttpServerResponse response, StatusCode statusCode, Str
}

private String collectionName(String path) {
if (path.equals("/") || path.equals("")) {
if (path.equals("/") || path.isEmpty()) {
return "root";
} else {
return path.substring(path.lastIndexOf("/") + 1);
Expand Down
46 changes: 3 additions & 43 deletions src/main/java/org/swisspush/reststorage/redis/RedisStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import java.util.UUID;
import java.util.stream.Stream;

import static org.swisspush.reststorage.redis.RedisUtils.toPayload;

public class RedisStorage implements Storage {

private final Logger log = LoggerFactory.getLogger(RedisStorage.class);
Expand Down Expand Up @@ -815,7 +817,7 @@ private void handleJsonArrayValues(Response values, Handler<Resource> handler, b
}
}
}
if (allowEmptyReturn && items.size() == 0) {
if (allowEmptyReturn && items.isEmpty()) {
notFound(handler);
} else {
r.items = new ArrayList<>(items);
Expand Down Expand Up @@ -1293,46 +1295,4 @@ public void cleanup(Handler<DocumentResource> handler, String cleanupResourcesAm
private boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}

/**
* from https://github.com/vert-x3/vertx-redis-client/blob/3.9/src/main/java/io/vertx/redis/impl/RedisClientImpl.java#L94
*
* @param parameters
* @return
*/
private static List<String> toPayload(Object... parameters) {
List<String> result = new ArrayList<>(parameters.length);

for (Object param : parameters) {
// unwrap
if (param instanceof JsonArray) {
param = ((JsonArray) param).getList();
}
// unwrap
if (param instanceof JsonObject) {
param = ((JsonObject) param).getMap();
}

if (param instanceof Collection) {
((Collection) param).stream().filter(Objects::nonNull).forEach(o -> result.add(o.toString()));
} else if (param instanceof Map) {
for (Map.Entry<?, ?> pair : ((Map<?, ?>) param).entrySet()) {
result.add(pair.getKey().toString());
result.add(pair.getValue().toString());
}
} else if (param instanceof Stream) {
((Stream) param).forEach(e -> {
if (e instanceof Object[]) {
Collections.addAll(result, (String[]) e);
} else {
result.add(e.toString());
}
});
} else if (param != null) {
result.add(param.toString());
}
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class ResourcesUtils {

private static Logger log = LoggerFactory.getLogger(ResourcesUtils.class);
private static final Logger log = LoggerFactory.getLogger(ResourcesUtils.class);

private ResourcesUtils() {
// prevent instantiation
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mime-types.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ aso=application/vnd.accpac.simply.aso
atc=application/vnd.acucorp
atomcat=application/atomcat+xml
atomsvc=application/atomsvc+xml
atom, xml=application/atom+xml
atomxml=application/atom+xml
atx=application/vnd.antix.game-component
au=audio/basic
avi=video/x-msvideo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testEtag(TestContext context) {

String etag = get("resources/res1").getHeader(ETAG_HEADER);
context.assertNotNull(etag, "Etag header should be available in response headers");
context.assertTrue(etag.length() > 0, "Etag header should not be empty");
context.assertFalse(etag.isEmpty(), "Etag header should not be empty");

// get requests with no if-none-match header should result in statuscode 200
when().get("resources/res1").then().assertThat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,6 @@ private void doAsserts() {
* check if such a directory exists or someone has access to it.
*/
private static String createPseudoFileStorageRoot() {
return new File( "target/fileStorage-"+ UUID.randomUUID().toString() ).getAbsolutePath().replaceAll("\\\\","/"); // <-- Fix windows
return new File( "target/fileStorage-"+ UUID.randomUUID()).getAbsolutePath().replaceAll("\\\\","/"); // <-- Fix windows
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void notifiesResourceAboutExceptionsOnRequest(TestContext testContext) th
@Override
public void put(String path, String etag, boolean merge, long expire, String lockOwner, LockMode lockMode, long lockExpire, boolean storeCompressed, Handler<Resource> handler) {
final DocumentResource resource = new DocumentResource();
resource.writeStream = new FailFastVertxWriteStream<Buffer>() {
resource.writeStream = new FailFastVertxWriteStream<>() {
@Override
public Future<Void> write(Buffer t) {
log.debug("Somewhat irrelevant got written to the resource.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.UUID;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class RedisCleanupLuaScriptTests extends AbstractLuaScriptTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;


public class RedisDelLuaScriptTests extends AbstractLuaScriptTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class RedisGetLuaScriptTests extends AbstractLuaScriptTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class RedisPutLuaScriptTests extends AbstractLuaScriptTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;

public class RedisStorageExpandLuaScriptTests extends AbstractLuaScriptTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class FailFastVertxWebRoutingContext implements RoutingContext {
Expand Down

0 comments on commit 412952b

Please sign in to comment.