diff --git a/commons/src/main/java/org/restheart/configuration/Configuration.java b/commons/src/main/java/org/restheart/configuration/Configuration.java index 3cfb02108..d246dc7d8 100644 --- a/commons/src/main/java/org/restheart/configuration/Configuration.java +++ b/commons/src/main/java/org/restheart/configuration/Configuration.java @@ -33,6 +33,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.jxpath.JXPathContext; @@ -427,7 +428,7 @@ private static Map overrideConfiguration(Map con var _pwd = cs.getPassword(); if (_pwd != null) { var pwd = new String(_pwd); - maskedValue.put(k, svalue.replaceFirst(pwd, MASK)); + maskedValue.put(k, svalue.replaceFirst(Pattern.quote(pwd), MASK)); } } catch (Throwable t) { maskedValue.put(k, mapValue); @@ -444,7 +445,7 @@ private static Map overrideConfiguration(Map con var _pwd = cs.getPassword(); if (_pwd != null) { var pwd = new String(_pwd); - LOGGER.info(LOG_PATTERN, o.path(), svalue.replaceFirst(pwd, MASK)); + LOGGER.info(LOG_PATTERN, o.path(), svalue.replaceFirst(Pattern.quote(pwd), MASK)); } } catch (Throwable t) { LOGGER.info(LOG_PATTERN, o.path(), o.value()); diff --git a/commons/src/main/java/org/restheart/exchange/MongoRequest.java b/commons/src/main/java/org/restheart/exchange/MongoRequest.java index ab5b70b1f..a6381a6da 100644 --- a/commons/src/main/java/org/restheart/exchange/MongoRequest.java +++ b/commons/src/main/java/org/restheart/exchange/MongoRequest.java @@ -27,7 +27,7 @@ import java.util.Deque; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; +import java.util.regex.Pattern; import java.util.stream.IntStream; import org.bson.BsonArray; @@ -77,7 +77,7 @@ import org.restheart.mongodb.db.sessions.ClientSessionImpl; import static org.restheart.utils.BsonUtils.document; import org.restheart.utils.MongoServiceAttachments; -import org.restheart.utils.URLUtils; +import static org.restheart.utils.URLUtils.removeTrailingSlashes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -124,8 +124,7 @@ public class MongoRequest extends BsonRequest { private BsonValue documentId; - private String mappedUri = null; - private String unmappedUri = null; + private String mongoResourceUri = null; private final String etag; @@ -161,19 +160,17 @@ public class MongoRequest extends BsonRequest { private final Optional rsOps; - protected MongoRequest(HttpServerExchange exchange, String requestUri, String resourceUri) { + protected MongoRequest(HttpServerExchange exchange, String whereUri, String whatUri) { super(exchange); - this.whereUri = URLUtils.removeTrailingSlashes(requestUri == null + this.whereUri = removeTrailingSlashes(whereUri == null ? null - : requestUri.startsWith("/") ? requestUri - : "/" + requestUri); + : whereUri.startsWith("/") ? whereUri + : "/" + whereUri); - this.whatUri = URLUtils.removeTrailingSlashes(resourceUri == null ? null - : resourceUri.startsWith("/") || "*".equals(resourceUri) ? resourceUri - : "/" + resourceUri); - - this.mappedUri = exchange.getRequestPath(); + this.whatUri = removeTrailingSlashes(whatUri == null ? null + : whatUri.startsWith("/") || "*".equals(whatUri) ? whatUri + : "/" + whatUri); if (exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY) != null) { this.pathTemplateMatch = exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY); @@ -182,20 +179,18 @@ protected MongoRequest(HttpServerExchange exchange, String requestUri, String re var params = this.pathTemplateMatch.getParameters(); var hostname = this.wrapped.getHostName(); if (hostname != null) { - params.put("host", hostname ); - - var parts = hostname.split("\\."); - + params.put("host", hostname); + final var parts = hostname.split("\\."); IntStream.range(0, parts.length).forEach(idx -> params.put("host[".concat(String.valueOf(idx)).concat("]"), parts[idx] )); } } else { this.pathTemplateMatch = null; } - this.unmappedUri = unmapUri(exchange.getRequestPath()); + this.mongoResourceUri = mongoUriFromRequestPath(exchange.getRequestPath()); // "/db/collection/document" --> { "", "mappedDbName", "collection", "document" } - this.pathTokens = this.unmappedUri.split(SLASH); + this.pathTokens = this.mongoResourceUri.split(SLASH); this.type = selectRequestType(pathTokens); @@ -294,30 +289,28 @@ private String defaultWriteMode() { * * @param exchange * - * the exchange request path (mapped uri) is rewritten replacing the - * resourceUri with the requestUri - * - * the special resourceUri value * means any resource: the requestUri is - * mapped to the root resource / + * the exchange request path (mapped resolvedTemplate) is rewritten replacing the + * whereUri with the whatUri + * the special whatUri value * means any resource * * example 1 * - * resourceUri = /db/mycollection requestUri = / + * whatUri=/db/mycollection whereUri=/ * - * then the requestPath / is rewritten to /db/mycollection + * then the requestPath / is rewritten as /db/mycollection * * example 2 * - * resourceUri = * requestUri = /data + * whatUri=*, whereUri=/data * - * then the requestPath /data is rewritten to / + * then the requestPath /data is rewritten as / * - * @param requestUri the request URI to map to the resource URI - * @param resourceUri the resource URI identifying a resource in the DB + * @param whereUri the where URI + * @param whatUri the what URI idenitifying a MongoDB resource, as /db/collection * @return the MongoRequest */ - public static MongoRequest init(HttpServerExchange exchange, String requestUri, String resourceUri) { - return new MongoRequest(exchange, requestUri, resourceUri); + public static MongoRequest init(HttpServerExchange exchange, String whereUri, String whatUri) { + return new MongoRequest(exchange, whereUri, whatUri); } public static MongoRequest of(HttpServerExchange exchange) { @@ -482,63 +475,63 @@ static TYPE selectRequestType(String[] pathTokens) { } /** - * given a mapped uri (/some/mapping/coll) returns the canonical uri - * (/db/coll) URLs are mapped to mongodb resources by using the mongo-mounts + * given a request path (eg. /some/mapped/coll) returns the mongo resource uri + * (eg. /db/coll); request paths are mapped to mongodb resources by mongo-mounts * configuration properties. note that the mapped uri can make use of path - * templates (/some/{path}/template/*) + * templates (/some/{path}/template/{*}) * - * @param mappedUri - * @return + * @param path the request path + * @return the mongo resource uri */ - private String unmapUri(String mappedUri) { + private String mongoUriFromRequestPath(String path) { // don't unmap URIs statring with /_sessions - if (mappedUri.startsWith("/".concat(_SESSIONS))) { - return mappedUri; + if (path.startsWith("/".concat(_SESSIONS))) { + return path; } if (this.pathTemplateMatch == null) { - return unmapPathUri(mappedUri); + return mongoUriFromPathMatch(path); } else { - return unmapPathTemplateUri(mappedUri); + return mongoUruFromPathTemplateMatch(path); } } - private String unmapPathUri(String mappedUri) { - var ret = URLUtils.removeTrailingSlashes(mappedUri); + private String mongoUriFromPathMatch(String requestPath) { + var mongoUri = removeTrailingSlashes(requestPath); - if (whatUri.equals("*")) { + if (this.whatUri.equals("*")) { if (!this.whereUri.equals(SLASH)) { - ret = ret.replaceFirst("^" + this.whereUri, ""); + mongoUri = mongoUri.replaceFirst("^" + Pattern.quote(this.whereUri), ""); } } else if (!this.whereUri.equals(SLASH)) { - ret = URLUtils.removeTrailingSlashes(ret.replaceFirst("^" + this.whereUri, this.whatUri)); + // problem, whereUri can contain regex special chars such as * + mongoUri = removeTrailingSlashes(mongoUri.replaceFirst("^" + Pattern.quote(this.whereUri), this.whatUri)); } else { - ret = URLUtils.removeTrailingSlashes(URLUtils.removeTrailingSlashes(this.whatUri) + ret); + mongoUri = removeTrailingSlashes(removeTrailingSlashes(this.whatUri) + mongoUri); } - return ret.isEmpty() ? SLASH : ret; + return mongoUri.isEmpty() ? SLASH : mongoUri; } - private String unmapPathTemplateUri(String mappedUri) { - var ret = mappedUri; - var rewriteUri = replaceParamsWithActualValues(); + private String mongoUruFromPathTemplateMatch(String requestPath) { + // requestPath=/api/a/b + // what=* + // where=/api/{*} -> /api/a/b + // -> /a/b + var mongoUri = removeTrailingSlashes(requestPath); - var replacedWhatUri = replaceParamsWithinWhatUri(); - // replace params with in whatUri - // eg what: /{account}, where: /{account}/* + final var _whatUri = resolveTemplate(this.whatUri); + final var _whereUri = resolveTemplate(this.whereUri); - // now replace mappedUri with resolved path template - if (replacedWhatUri.equals("*")) { - if (!this.whereUri.equals(SLASH)) { - ret = ret.replaceFirst("^" + rewriteUri, ""); - } - } else if (!this.whereUri.equals(SLASH)) { - ret = URLUtils.removeTrailingSlashes(ret.replaceFirst("^" + rewriteUri, replacedWhatUri)); + if (_whatUri.equals("*")) { + mongoUri = mongoUri.replaceFirst("^" + Pattern.quote(_whereUri), ""); + } else if (!_whereUri.equals(SLASH)) { + mongoUri = removeTrailingSlashes(mongoUri.replaceFirst("^" + Pattern.quote(_whereUri), _whatUri)); } else { - ret = URLUtils.removeTrailingSlashes(URLUtils.removeTrailingSlashes(replacedWhatUri) + ret); + mongoUri = removeTrailingSlashes(removeTrailingSlashes(_whatUri) + mongoUri); } - return ret.isEmpty() ? SLASH : URLUtils.removeTrailingSlashes(ret); + return mongoUri.isEmpty() ? SLASH : removeTrailingSlashes(mongoUri); } /** @@ -546,83 +539,65 @@ private String unmapPathTemplateUri(String mappedUri) { * (/some/mapping/uri) relative to this context. URLs are mapped to mongodb * resources via the mongo-mounts configuration properties * - * @param unmappedUri + * @param mongoResourceUri * @return */ - public String mapUri(String unmappedUri) { + public String requestPathFromMongoUri(String mongoResourceUri) { if (this.pathTemplateMatch == null) { - return mapPathUri(unmappedUri); + return requestPathFromPathMatch(mongoResourceUri); } else { - return mapPathTemplateUri(unmappedUri); + return requestPathFromPathTemplateMatch(mongoResourceUri); } } - private String mapPathUri(String unmappedUri) { - var ret = URLUtils.removeTrailingSlashes(unmappedUri); + private String requestPathFromPathMatch(String mongoUri) { + var requestPath = removeTrailingSlashes(mongoUri); if (whatUri.equals("*")) { if (!this.whereUri.equals(SLASH)) { - return this.whereUri + unmappedUri; + return this.whereUri + mongoUri; } } else { - ret = URLUtils.removeTrailingSlashes(ret.replaceFirst("^" + this.whatUri, this.whereUri)); + requestPath = removeTrailingSlashes(requestPath.replaceFirst("^" + Pattern.quote(this.whatUri), this.whereUri)); } - if (ret.isEmpty()) { - ret = SLASH; + if (requestPath.isEmpty()) { + requestPath = SLASH; } else { - ret = ret.replaceAll("//", "/"); + requestPath = requestPath.replaceAll("//", "/"); } - return ret; + return requestPath; } - private String mapPathTemplateUri(String unmappedUri) { - var ret = URLUtils.removeTrailingSlashes(unmappedUri); - var rewriteUri = replaceParamsWithActualValues(); - var replacedWhatUri = replaceParamsWithinWhatUri(); + private String requestPathFromPathTemplateMatch(String mongoUri) { + var requestPath = removeTrailingSlashes(mongoUri); + var resolvedWhere = resolveTemplate(this.whereUri); + var resolvedWhat = resolveTemplate(this.whatUri); // now replace mappedUri with resolved path template - if (replacedWhatUri.equals("*")) { + if (resolvedWhat.equals("*")) { if (!this.whereUri.equals(SLASH)) { - return rewriteUri + unmappedUri; + return resolvedWhere + mongoUri; } } else { - ret = URLUtils.removeTrailingSlashes(ret.replaceFirst("^" + replacedWhatUri, rewriteUri)); + requestPath = removeTrailingSlashes(requestPath.replaceFirst("^" + Pattern.quote(resolvedWhat), resolvedWhere)); } - return ret.isEmpty() ? SLASH : ret; + return requestPath.isEmpty() ? SLASH : requestPath; } - private String replaceParamsWithinWhatUri() { - String uri = this.whatUri; + /** + * @return the resolved template with actual paramenters + */ + private String resolveTemplate(String template) { + String resolvedTemplate = template; // replace params within whatUri - // eg what: /{prefix}_db, where: /{prefix}/* + // eg _whatUri: /{prefix}_db, _whereUri: /{prefix}/* for (var key : this.pathTemplateMatch.getParameters().keySet()) { - uri = uri.replace("{".concat(key).concat("}"), this.pathTemplateMatch.getParameters().get(key)); + resolvedTemplate = resolvedTemplate.replace("{".concat(key).concat("}"), this.pathTemplateMatch.getParameters().get(key)); } - return uri; - } - - private String replaceParamsWithActualValues() { - // path template with variables resolved to actual values - var rewriteUri = this.pathTemplateMatch.getMatchedTemplate(); - // remove trailing wildcard from template - if (rewriteUri.endsWith("/*")) { - rewriteUri = rewriteUri.substring(0, rewriteUri.length() - 2); - } - // collect params - this.pathTemplateMatch - .getParameters() - .keySet() - .stream() - .filter(key -> !key.equals("*")) - .collect(Collectors.toMap(key -> key, key -> this.pathTemplateMatch.getParameters().get(key))); - // replace params with actual values - for (var key : this.pathTemplateMatch.getParameters().keySet()) { - rewriteUri = rewriteUri.replace("{".concat(key).concat("}"), this.pathTemplateMatch.getParameters().get(key)); - } - return rewriteUri; + return removeTrailingSlashes(resolvedTemplate); } /** @@ -638,8 +613,8 @@ private String replaceParamsWithActualValues() { */ public boolean isParentAccessible() { return getType() == TYPE.DB - ? mappedUri.split(SLASH).length > 1 - : mappedUri.split(SLASH).length > 2; + ? getExchange().getRequestPath().split(SLASH).length > 1 + : getExchange().getRequestPath().split(SLASH).length > 2; } /** @@ -732,14 +707,14 @@ public boolean isReservedResource() { /** * @return the whereUri */ - public String getUriPrefix() { + public String getWhereUri() { return whereUri; } /** * @return the whatUri */ - public String getMappingUri() { + public String getWhatUri() { return whatUri; } @@ -1000,23 +975,12 @@ public void setDbProps(BsonDocument dbProps) { /** * - * The unmapped uri is the cononical uri of a mongodb resource (e.g. - * /db/coll). - * - * @return the unmappedUri - */ - public String getUnmappedRequestUri() { - return unmappedUri; - } - - /** - * The mapped uri is the exchange request uri. This is "mapped" by the - * mongo-mounts mapping paramenters. + * Return the uri of the mongodb resource ass /db/coll or /db/coll/docid. * - * @return the mappedUri + * @return the mongoResourceUri */ - public String getMappedRequestUri() { - return mappedUri; + public String getMongoResourceUri() { + return mongoResourceUri; } /** diff --git a/commons/src/test/java/io/undertow/server/HttpServerExchange.java b/commons/src/test/java/io/undertow/server/HttpServerExchange.java index 7d8df4cc3..3d6dc038b 100644 --- a/commons/src/test/java/io/undertow/server/HttpServerExchange.java +++ b/commons/src/test/java/io/undertow/server/HttpServerExchange.java @@ -19,10 +19,6 @@ */ package io.undertow.server; -import io.undertow.security.api.SecurityContext; -import io.undertow.util.AbstractAttachable; -import io.undertow.util.HeaderMap; -import io.undertow.util.HttpString; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.ArrayDeque; @@ -33,6 +29,11 @@ import org.xnio.channels.StreamSourceChannel; +import io.undertow.security.api.SecurityContext; +import io.undertow.util.AbstractAttachable; +import io.undertow.util.HeaderMap; +import io.undertow.util.HttpString; + /** * A mock for io.undertow.server.HttpServerExchange The original class is final * so it can't be mocked directly. Then use Mockito: @@ -185,4 +186,8 @@ public StreamSourceChannel getRequestChannel() { public long getRequestContentLength() { return 0; } + + public String getHostName() { + return "localhost"; + } } diff --git a/commons/src/test/java/org/restheart/exchange/BsonRequestTest.java b/commons/src/test/java/org/restheart/exchange/BsonRequestTest.java index 2ebd5c9ba..4124a9831 100644 --- a/commons/src/test/java/org/restheart/exchange/BsonRequestTest.java +++ b/commons/src/test/java/org/restheart/exchange/BsonRequestTest.java @@ -20,29 +20,26 @@ package org.restheart.exchange; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; +import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import io.undertow.server.HttpServerExchange; import io.undertow.util.HttpString; +import io.undertow.util.PathTemplate; +import io.undertow.util.PathTemplateMatch; +import io.undertow.util.PathTemplateMatcher; /** * * @author Maurizio Turatti {@literal } */ public class BsonRequestTest { - - private static final Logger LOG = LoggerFactory.getLogger(BsonRequestTest.class); - /** * */ @@ -153,49 +150,49 @@ public void testGetMappedRequestUri() { String whereUri = "/"; MongoRequest request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/mycollection"); + assertEquals("/db/mycollection", request.getMongoResourceUri()); whatUri = "*"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/"); + assertEquals("/", request.getMongoResourceUri()); whatUri = "*"; whereUri = "/data"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/"); + assertEquals("/", request.getMongoResourceUri()); whatUri = "/data"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/data"); + assertEquals("/data", request.getMongoResourceUri()); whatUri = "/db/coll"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/coll"); + assertEquals("/db/coll", request.getMongoResourceUri()); whatUri = "/db/coll/doc"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/coll/doc"); + assertEquals("/db/coll/doc", request.getMongoResourceUri()); whatUri = "/db/coll/"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/coll"); + assertEquals("/db/coll", request.getMongoResourceUri()); whatUri = "/db/coll////"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/coll"); + assertEquals("/db/coll", request.getMongoResourceUri()); } /** @@ -211,24 +208,68 @@ public void testGetMappedRequestUri2() { String whereUri = "/"; MongoRequest request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/mycollection/x"); + assertEquals("/db/mycollection/x", request.getMongoResourceUri()); whatUri = "*"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/x"); + assertEquals("/x", request.getMongoResourceUri()); whatUri = "db"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/x"); + assertEquals("/db/x", request.getMongoResourceUri()); whatUri = "db/coll"; whereUri = "/"; request = MongoRequest.init(ex, whereUri, whatUri); - assertEquals( request.getUnmappedRequestUri(), "/db/coll/x"); + assertEquals("/db/coll/x", request.getMongoResourceUri()); + } + + /** + * + */ + @Test + public void testGetMappedRequestUriWithPathTemplate() { + // where=/{*} + assertEquals("/", request("/", "/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a", request("/a", "/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a/b", request("/a/b", "/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a/b/c", request("/a/b/c", "/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a/b/*", request("/a/b/*", "/{*}", "{*}").getMongoResourceUri()); + + assertEquals("/", request("/api/", "/api/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a", request("/api/a", "/api/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a/b", request("/api/a/b", "/api/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a/b/c", request("/api/a/b/c", "/api/{*}", "{*}").getMongoResourceUri()); + assertEquals("/a/b/*", request("/api/a/b/*", "/api/{*}", "{*}").getMongoResourceUri()); + + assertEquals("/x/y/z", request("/x/y/z", "/{a}/{b}/{c}", "/{a}/{b}/{c}").getMongoResourceUri()); + assertEquals("/a/b/c", request("/a/b/c", "/{a}/{b}/{*}", "/{a}/{b}/{*}").getMongoResourceUri()); + assertEquals("/a/b/*", request("/a/b/*", "/{a}/{b}/{*}", "/{a}/{b}/{*}").getMongoResourceUri()); + + assertEquals("/1-2-x", request("/api/1/2", "/api/{a}/{b}", "/{a}-{b}-x").getMongoResourceUri()); + + assertEquals("/1-2", request("/1/2", "/{b}/{*}", "/{b}-{*}").getMongoResourceUri()); + + // *=don't work well with path templates, matching paths always map to root resource + assertEquals("/", request("/api/1/2", "/api/{*}", "*").getMongoResourceUri()); + } + + private MongoRequest request(String path, String where, String what) { + var ex = mock(HttpServerExchange.class); + when(ex.getRequestPath()).thenReturn(path); + when(ex.getRequestMethod()).thenReturn(HttpString.EMPTY); + + var ptm = new PathTemplateMatcher(); + ptm.add(PathTemplate.create(where), ""); + + var match = ptm.match(path); + when(ex.getAttachment(PathTemplateMatch.ATTACHMENT_KEY)).thenReturn(match); + + return MongoRequest.init(ex, where, what); } } diff --git a/mongodb/src/main/java/org/restheart/mongodb/hal/IndexesRepresentationFactory.java b/mongodb/src/main/java/org/restheart/mongodb/hal/IndexesRepresentationFactory.java index d2067865a..300a2208c 100644 --- a/mongodb/src/main/java/org/restheart/mongodb/hal/IndexesRepresentationFactory.java +++ b/mongodb/src/main/java/org/restheart/mongodb/hal/IndexesRepresentationFactory.java @@ -20,8 +20,8 @@ */ package org.restheart.mongodb.hal; -import io.undertow.server.HttpServerExchange; import static java.lang.Math.toIntExact; + import org.bson.BsonArray; import org.bson.BsonInt32; import org.bson.BsonString; @@ -34,6 +34,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import io.undertow.server.HttpServerExchange; + /** * * @author Andrea Di Cesare {@literal } @@ -62,8 +64,7 @@ public Resource getRepresentation( throws IllegalQueryParamenterException { var request = MongoRequest.of(exchange); - String requestPath = MongoURLUtils.removeTrailingSlashes( - request.getUnmappedRequestUri()); + String requestPath = MongoURLUtils.removeTrailingSlashes(request.getMongoResourceUri()); String queryString = exchange.getQueryString() == null || exchange.getQueryString().isEmpty() @@ -94,11 +95,7 @@ public Resource getRepresentation( rep.addProperty("_returned", new BsonInt32(toIntExact(count))); if (!embeddedData.isEmpty()) { - embeddedDocuments( - embeddedData, - requestPath, - rep, - request.isFullHalMode()); + embeddedDocuments(embeddedData, rep, request.isFullHalMode()); } } @@ -128,7 +125,6 @@ public Resource getRepresentation( private static void embeddedDocuments( BsonArray embeddedData, - String requestPath, Resource rep, boolean isHal) { embeddedData.stream() diff --git a/mongodb/src/main/java/org/restheart/mongodb/handlers/changestreams/GetChangeStreamHandler.java b/mongodb/src/main/java/org/restheart/mongodb/handlers/changestreams/GetChangeStreamHandler.java index ca1b33b72..3d31b12be 100644 --- a/mongodb/src/main/java/org/restheart/mongodb/handlers/changestreams/GetChangeStreamHandler.java +++ b/mongodb/src/main/java/org/restheart/mongodb/handlers/changestreams/GetChangeStreamHandler.java @@ -114,7 +114,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { } catch (QueryNotFoundException ex) { response.setInError(HttpStatus.SC_NOT_FOUND, "Change Stream does not exist"); - LOGGER.debug("Requested Change Stream {} does not exist", request.getUnmappedRequestUri()); + LOGGER.debug("Requested Change Stream {} does not exist", request.getMongoResourceUri()); next(exchange); } catch (QueryVariableNotBoundException ex) { @@ -161,7 +161,7 @@ private List getResolvedStagesAsList(MongoRequest request) throws .findFirst(); if (!_query.isPresent()) { - throw new QueryNotFoundException("Stream " + request.getUnmappedRequestUri() + " does not exist"); + throw new QueryNotFoundException("Stream " + request.getMongoResourceUri() + " does not exist"); } var pipeline = _query.get(); diff --git a/mongodb/src/main/java/org/restheart/mongodb/handlers/sessions/PostTxnsHandler.java b/mongodb/src/main/java/org/restheart/mongodb/handlers/sessions/PostTxnsHandler.java index 2bb408554..d76aa5db0 100644 --- a/mongodb/src/main/java/org/restheart/mongodb/handlers/sessions/PostTxnsHandler.java +++ b/mongodb/src/main/java/org/restheart/mongodb/handlers/sessions/PostTxnsHandler.java @@ -20,9 +20,8 @@ */ package org.restheart.mongodb.handlers.sessions; -import io.undertow.server.HttpServerExchange; -import io.undertow.util.HttpString; import java.util.UUID; + import org.bson.BsonString; import org.restheart.exchange.MongoRequest; import org.restheart.exchange.MongoResponse; @@ -36,6 +35,9 @@ import org.restheart.utils.HttpStatus; import static org.restheart.utils.RepresentationUtils.getReferenceLink; +import io.undertow.server.HttpServerExchange; +import io.undertow.util.HttpString; + /** * * creates a session with a started transaction @@ -86,7 +88,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { // propagate the transaction TxnsUtils.propagateSession(cs); - response.getHeaders().add(HttpString.tryFromString("Location"), getReferenceLink(request.getMappedRequestUri(), new BsonString("" + nextTxnId))); + response.getHeaders().add(HttpString.tryFromString("Location"), getReferenceLink(request.getMongoResourceUri(), new BsonString("" + nextTxnId))); response.setStatusCode(HttpStatus.SC_CREATED); } else { diff --git a/mongodb/src/main/java/org/restheart/mongodb/utils/MongoURLUtils.java b/mongodb/src/main/java/org/restheart/mongodb/utils/MongoURLUtils.java index 68c4c8a75..cdc914977 100644 --- a/mongodb/src/main/java/org/restheart/mongodb/utils/MongoURLUtils.java +++ b/mongodb/src/main/java/org/restheart/mongodb/utils/MongoURLUtils.java @@ -20,9 +20,9 @@ */ package org.restheart.mongodb.utils; -import io.undertow.server.HttpServerExchange; import java.util.Arrays; import java.util.Objects; + import org.bson.BsonBoolean; import org.bson.BsonDateTime; import org.bson.BsonMaxKey; @@ -45,6 +45,8 @@ import org.restheart.utils.BsonUtils; import org.restheart.utils.URLUtils; +import io.undertow.server.HttpServerExchange; + /** * * @author Andrea Di Cesare {@literal } @@ -189,7 +191,7 @@ static public String getUriWithDocId( sb.append("?id_type=").append(docIdType.name()); } - return request.mapUri(sb.toString()); + return request.requestPathFromMongoUri(sb.toString()); } /** @@ -215,7 +217,7 @@ static public String getUriWithFilterMany( .append("{'$in'").append(":") .append(getIdsString(ids)).append("}}"); - return request.mapUri(sb.toString()); + return request.requestPathFromMongoUri(sb.toString()); } /** @@ -244,7 +246,7 @@ static public String getUriWithFilterOne( .append(getIdString(id)) .append("}"); - return request.mapUri(sb.toString()); + return request.requestPathFromMongoUri(sb.toString()); } /** @@ -271,7 +273,7 @@ static public String getUriWithFilterManyInverse( .append("':{").append("'$elemMatch':{'$eq':") .append(getIdString(id)).append("}}}"); - return BsonUtils.minify(request.mapUri(sb.toString())).toString(); + return BsonUtils.minify(request.requestPathFromMongoUri(sb.toString())).toString(); } private static BsonNumber getIdAsNumber(String id) throws IllegalArgumentException {