Skip to content

Commit

Permalink
🐛 Fix MongoDB URL mapping for trailing slashes in path template matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed Nov 6, 2024
1 parent 4f2a1b1 commit 686a170
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public BadRequestException(String message) {
/**
*
* @param message
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
*/
public BadRequestException(String message, boolean jsonMessage) {
super(message);
Expand All @@ -80,7 +80,7 @@ public BadRequestException(String message, boolean jsonMessage) {
/**
*
* @param message
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
* @param contentType error response content type
*/
public BadRequestException(String message, boolean jsonMessage, String contentType) {
Expand All @@ -105,7 +105,7 @@ public BadRequestException(String message, int statusCode) {
*
* @param message
* @param statusCode
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
*/
public BadRequestException(String message, int statusCode, boolean jsonMessage) {
super(message);
Expand All @@ -118,7 +118,7 @@ public BadRequestException(String message, int statusCode, boolean jsonMessage)
*
* @param message
* @param statusCode
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
* @param contentType error response content type
*/
public BadRequestException(String message, int statusCode, boolean jsonMessage, String contentType) {
Expand All @@ -136,14 +136,14 @@ public BadRequestException(String message, int statusCode, boolean jsonMessage,
public BadRequestException(String message, Throwable cause) {
super(message, cause);
this.jsonMessage = false;
this.contentType = Exchange.JSON_MEDIA_TYPE;
this.contentType = Exchange.TEXT_PLAIN_CONTENT_TYPE;
}

/**
*
* @param message
* @param cause
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
*/
public BadRequestException(String message, boolean jsonMessage, Throwable cause) {
super(message, cause);
Expand All @@ -155,7 +155,7 @@ public BadRequestException(String message, boolean jsonMessage, Throwable cause)
*
* @param message
* @param cause
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
* @param contentType error response content type
*/
public BadRequestException(String message, boolean jsonMessage, String contentType, Throwable cause) {
Expand All @@ -181,7 +181,7 @@ public BadRequestException(String message, int statusCode, Throwable cause) {
*
* @param message
* @param statusCode
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
* @param cause
*/
public BadRequestException(String message, int statusCode, boolean jsonMessage, Throwable cause) {
Expand All @@ -195,7 +195,7 @@ public BadRequestException(String message, int statusCode, boolean jsonMessage,
*
* @param message
* @param statusCode
* @param jsonMessage mark message as json
* @param jsonMessage mark message as json, i.e. the string is valid json
* @param contentType error response content type
* @param cause
*/
Expand Down
5 changes: 5 additions & 0 deletions commons/src/main/java/org/restheart/exchange/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public abstract class Exchange<T> {
*/
public static final String APPLICATION_PDF_TYPE = "application/pdf";

/**
*
*/
public static final String TEXT_PLAIN_CONTENT_TYPE = "text/plain";

/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ private String unmapPathUri(String mappedUri) {
}

private String unmapPathTemplateUri(String mappedUri) {
var ret = URLUtils.removeTrailingSlashes(mappedUri);
var ret = mappedUri;
var rewriteUri = replaceParamsWithActualValues();

var replacedWhatUri = replaceParamsWithinWhatUri();
Expand All @@ -526,7 +526,7 @@ private String unmapPathTemplateUri(String mappedUri) {
ret = URLUtils.removeTrailingSlashes(URLUtils.removeTrailingSlashes(replacedWhatUri) + ret);
}

return ret.isEmpty() ? SLASH : ret;
return ret.isEmpty() ? SLASH : URLUtils.removeTrailingSlashes(ret);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@

package org.restheart.mongodb;

import com.mongodb.MongoCommandException;
import com.mongodb.MongoTimeoutException;
import com.mongodb.client.MongoClient;

import org.restheart.cache.Cache.EXPIRE_POLICY;
import org.restheart.cache.CacheFactory;
import org.restheart.cache.LoadingCache;
import org.restheart.cache.Cache.EXPIRE_POLICY;

import static org.restheart.utils.BsonUtils.document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.restheart.utils.BsonUtils.document;
import com.mongodb.MongoCommandException;
import com.mongodb.MongoTimeoutException;
import com.mongodb.client.MongoClient;

public class ConnectionChecker {
private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionChecker.class);
Expand All @@ -45,7 +43,7 @@ public class ConnectionChecker {
mclient.getDatabase("admin").runCommand(document().put("ping", 1).get());
return true;
} catch(Throwable t) {
LOGGER.error("Error checking connection to MongoDb", t);
LOGGER.error("Error checking connection to MongoDB", t);
return false;
}
});
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/restheart/Bootstrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ private static class Args {
@Option(names = { "-v", "--version" }, versionHelp = true, description = "Print product version to the output stream and exit")
boolean versionRequested;

@Option(names = { "-s", "--standalone" }, description = "Use an alternate configuration that disables all plugins depending from MongoDb")
@Option(names = { "-s", "--standalone" }, description = "Use an alternate configuration that disables all plugins depending from MongoDB")
boolean standalone;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void init() {

MongoClientSingleton.init(mongoConnetion);

// force first connection to MongoDb
// force first connection to MongoDB
MongoClientSingleton.getInstance().client();
}

Expand Down
6 changes: 3 additions & 3 deletions mongodb/src/main/java/org/restheart/mongodb/MongoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void handle(MongoRequest request, MongoResponse response) throws Exceptio
if (mclient != null) {
this.pipeline.handleRequest(request.getExchange());
} else {
final var error = "MongoDb is not availabe";
final var error = "MongoDB is not availabe";

response.setInError(500, error);
LOGGER.error(error);
Expand Down Expand Up @@ -201,10 +201,10 @@ public Consumer<HttpServerExchange> requestInitializer() {
if (mm != null) {
MongoRequest.init(e, mm.uri, mm.resource);
} else {
LOGGER.warn("No MongoDb resource bound for {}. "
LOGGER.warn("No MongoDB resource bound for {}. "
+ "Check mongo service configuration: "
+ "'mongo-mounts' and plugin arg 'uri'", path);
throw new BadRequestException(HttpStatus.SC_BAD_GATEWAY);
throw new BadRequestException("No MongoDB resource bound for " + path, HttpStatus.SC_BAD_GATEWAY);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
*/
package org.restheart.mongodb.db.sessions;

import com.mongodb.ClientSessionOptions;
import com.mongodb.client.MongoClient;
import com.mongodb.MongoQueryException;

import static com.mongodb.client.model.Filters.eq;

import java.util.Optional;
import java.util.UUID;

Expand All @@ -34,6 +28,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.ClientSessionOptions;
import com.mongodb.MongoQueryException;
import com.mongodb.client.MongoClient;
import static com.mongodb.client.model.Filters.eq;

/**
*
* @author Andrea Di Cesare {@literal <[email protected]>}
Expand Down Expand Up @@ -175,7 +174,7 @@ private static long getTxnNumFromExc(MongoQueryException mqe) {
private static final String TXN = "txnNumber";
/**
* errorMsg can be the transaction number or:
* - from MongoDb 6: with txnNumberAndRetryCounter { txnNumber: 10, txnRetryCounter: 0 }
* - from MongoDB 6: with txnNumberAndRetryCounter { txnNumber: 10, txnRetryCounter: 0 }
* - from MongoDB 5: with { txnNumber: 10 }
* - from MongoDB < 5: with txnNumber 10
* @param errorMsg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void changeStreamEventsLoop() {
} catch(MongoInterruptedException mie) {
close();
} catch(MongoException mqe) {
LOGGER.error("MongoDb error on ChangeStreamWorker {}, restarting a new worker", key, mqe);
LOGGER.error("MongoDB error on ChangeStreamWorker {}, restarting a new worker", key, mqe);

try {
Thread.sleep(1_000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
*/
package org.restheart.mongodb.services;

import com.mongodb.client.MongoClient;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import io.undertow.server.HttpServerExchange;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.bson.BsonDocument;
import org.bson.BsonString;
import org.bson.BsonValue;
Expand All @@ -38,11 +36,16 @@
import org.restheart.plugins.Inject;
import org.restheart.plugins.RegisterPlugin;
import org.restheart.plugins.Service;
import org.restheart.utils.HttpStatus;
import org.restheart.utils.BsonUtils;
import org.restheart.utils.HttpStatus;

import com.mongodb.client.MongoClient;
import com.mongodb.client.model.FindOneAndUpdateOptions;

import io.undertow.server.HttpServerExchange;

/**
* service to upload a csv file in a MongoDb collection
* service to upload a csv file in a MongoDB collection
*
* query parameters:<br>
* - db=&lt;db_name&gt; *required<br>
Expand All @@ -60,7 +63,7 @@
* @author Andrea Di Cesare {@literal <[email protected]>}
*/
@SuppressWarnings("unchecked")
@RegisterPlugin(name = "csvLoader", description = "Uploads a csv file in a MongoDb collection", secure = true, defaultURI = "/csv")
@RegisterPlugin(name = "csvLoader", description = "Uploads a csv file in a MongoDB collection", secure = true, defaultURI = "/csv")
public class CsvLoader implements Service<BsonFromCsvRequest, BsonResponse> {
/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class MongoAclPermission extends BaseAclPermission {
}

/**
* build acl permission from MongoDb document
* build acl permission from MongoDB document
*
* @param doc
*/
Expand Down

0 comments on commit 686a170

Please sign in to comment.