Skip to content

Commit

Permalink
update navigation api,
Browse files Browse the repository at this point in the history
add links to content and frontend
  • Loading branch information
Thorsten Marx committed Nov 21, 2024
1 parent f37c955 commit b3f6087
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public class ContentResolver {
private final DB db;

public Optional<ContentResponse> getStaticContent (String uri) {
if (uri.endsWith(".md")) {
return Optional.empty();
}
if (uri.startsWith("/")) {
uri = uri.substring(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@
* #L%
*/

import com.condation.cms.api.configuration.configs.SiteConfiguration;
import com.condation.cms.api.db.ContentNode;
import com.condation.cms.api.db.DB;
import com.condation.cms.api.db.cms.ReadOnlyFile;
import com.condation.cms.api.extensions.http.HttpHandler;
import com.condation.cms.api.feature.features.ConfigurationFeature;
import com.condation.cms.api.request.RequestContext;
import com.condation.cms.api.utils.PathUtil;
import com.condation.cms.api.utils.RequestUtil;
import com.condation.cms.modules.system.helpers.NodeHelper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.io.Content;
Expand Down Expand Up @@ -66,8 +72,12 @@ public boolean handle(Request request, Response response, Callback callback) thr
return true;
}

final ContentNode node = resolved.get();
final Map<String, Object> data = new HashMap<>(node.data());
data.put("_links", NodeHelper.getLinks(node, request));

response.getHeaders().add(HttpHeader.CONTENT_TYPE, "application/json; charset=utf-8");
Content.Sink.write(response, true, GSON.toJson(resolved.get().data()), callback);
Content.Sink.write(response, true, GSON.toJson(data), callback);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@
import com.condation.cms.api.utils.PathUtil;
import com.condation.cms.api.utils.RequestUtil;
import com.condation.cms.filesystem.metadata.AbstractMetaData;
import com.condation.cms.modules.system.helpers.NodeHelper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.server.AbstractMetaDataConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.Callback;
Expand Down Expand Up @@ -74,31 +75,60 @@ public boolean handle(Request request, Response response, Callback callback) thr
}

var filePath = PathUtil.toRelativeFile(file, contentBase);
if (!db.getContent().isVisible(filePath)) {

/*if (!db.getContent().isVisible(filePath)) {
Response.writeError(request, response, callback, 403);
return true;
}
}*/

final ContentNode contentNode = db.getContent().byUri(filePath).get();
final Optional<ContentNode> contentNode = db.getContent().byUri(filePath);

List<NavNode> children = new ArrayList<>();
db.getContent().listDirectories(file, "").stream()
.filter(child -> AbstractMetaData.isVisible(child))
.map(child -> new NavNode(child.uri()))
.forEach(children::add);
.map(child -> new NavNode(
NodeHelper.getPath(child),
NodeHelper.getLinks(child, request))
).forEach(children::add);

final String nodeUri = NodeHelper.getPath(uri);
db.getContent().listContent(file, "").stream()
.filter(child -> AbstractMetaData.isVisible(child))
.filter(child ->
!NodeHelper.getPath(child).equals(nodeUri)
)
.map(child -> new NavNode(
NodeHelper.getPath(child),
NodeHelper.getLinks(child, request))
).forEach(children::add);

children.sort((node1, node2) -> node1.path.compareTo(node2.path));

NavNode node;
if (contentNode.isPresent()) {
node = new NavNode(
NodeHelper.getPath(contentNode.get()),
NodeHelper.getLinks(contentNode.get(), request),
children
);
} else {
node = new NavNode(
"/" + uri,
Collections.emptyMap(),
children
);
}

NavNode node = new NavNode(contentNode.uri(), children);

response.getHeaders().add(HttpHeader.CONTENT_TYPE, "application/json; charset=utf-8");
Content.Sink.write(response, true, GSON.toJson(node), callback);

return true;
}

private static record NavNode (String path, List<NavNode> children) {
public NavNode (String path) {
this(path, Collections.emptyList());
private static record NavNode (String path, Map<String, String> _links, List<NavNode> children) {
public NavNode (String path, Map<String, String> _links) {
this(path, _links, Collections.emptyList());
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.condation.cms.modules.system.helpers;

/*-
* #%L
* cms-system-modules
* %%
* Copyright (C) 2023 - 2024 CondationCMS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.condation.cms.api.configuration.configs.SiteConfiguration;
import com.condation.cms.api.db.ContentNode;
import com.condation.cms.api.feature.features.ConfigurationFeature;
import com.condation.cms.api.request.RequestContext;
import java.util.Map;
import org.eclipse.jetty.server.Request;

/**
*
* @author t.marx
*/
public final class NodeHelper {

private NodeHelper() {}

public static Map<String, String> getLinks (ContentNode node, Request request) {
return getLinks(node.uri(), request);
}

public static Map<String, String> getLinks (String nodeUri, Request request) {

var requestContext = (RequestContext) request.getAttribute("_requestContext");
var siteProperties = requestContext.get(ConfigurationFeature.class).configuration().get(SiteConfiguration.class).siteProperties();

var contextPath = siteProperties.contextPath();
if (!contextPath.endsWith("/")) {
contextPath += "/";
}

if (nodeUri.endsWith("index.md")) {
nodeUri = nodeUri.replaceFirst("index.md", "");
}

if (nodeUri.endsWith(".md")) {
nodeUri = nodeUri.substring(0, nodeUri.length() - 3);
}

return Map.of(
"_self", "%s%s".formatted(contextPath, nodeUri),
"_content", "%sapi/v1/content/%s".formatted(contextPath, nodeUri)
);
}

public static String getPath (ContentNode node) {
return getPath(node.uri());
}

public static String getPath (String uri) {
if (uri.endsWith("index.md")) {
uri = uri.replaceFirst("index.md", "");
}
if (!uri.startsWith("/")) {
uri = "/" + uri;
}
if (uri.endsWith(".md")) {
uri = uri.substring(0, uri.length() - 3);
}
return uri;
}
}

0 comments on commit b3f6087

Please sign in to comment.