Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* #L%
*/
import java.nio.file.Path;
import java.util.Map;
import java.util.List;

/**
*
Expand All @@ -45,4 +45,10 @@ public interface ServerProperties {
public IPCProperties ipc();

public PerformanceProperties performance();

public List<String> moduleRepositories ();

public List<String> themeRepositories ();

public List<String> extensionRepositories ();
}
7 changes: 6 additions & 1 deletion cms-core/config/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ pool_enabled = false
test = ["eins", "zwei"]

[map]
test = {"key1"="value2", "key2"="value2"}
test = {"key1"="value2", "key2"="value2"}

[urls]
themes = ["https://mythemes.com"]
extensions = ["https://myextensions.com"]
modules = ["https://mymodules.com"]
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import com.condation.cms.api.configuration.Configuration;
import com.condation.cms.api.configuration.configs.ServerConfiguration;
import com.condation.cms.api.configuration.configs.SiteConfiguration;
import com.condation.cms.core.configuration.configs.MediaConfiguration;
import com.condation.cms.core.configuration.configs.SimpleConfiguration;
import com.condation.cms.core.configuration.configs.TaxonomyConfiguration;
import com.condation.cms.core.configuration.properties.ExtendedServerProperties;
import com.condation.cms.core.configuration.properties.ExtendedSiteProperties;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.condation.cms.api.utils.ServerUtil;
import com.condation.cms.core.configuration.configs.SimpleConfiguration;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

/**
*
Expand Down Expand Up @@ -92,6 +94,21 @@ public String env() {
public boolean dev() {
return !Constants.Environments.PROD.equals(env());
}

@Override
public List<String> moduleRepositories() {
return (List<String>) configuration.getOrDefault("urls.modules", Collections.emptyList());
}

@Override
public List<String> themeRepositories() {
return (List<String>) configuration.getOrDefault("urls.themes", Collections.emptyList());
}

@Override
public List<String> extensionRepositories() {
return (List<String>) configuration.getOrDefault("urls.extensions", Collections.emptyList());
}

public static record Server (int port, String ip) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ public void test_properties () {
Assertions.assertThat(serverProperties.serverPort()).isEqualTo(1010);
}

@Test
public void test_urls () {
var serverProperties = new ExtendedServerProperties(configuration);

Assertions.assertThat(serverProperties.extensionRepositories())
.containsExactly("https://myextensions.com");

Assertions.assertThat(serverProperties.moduleRepositories())
.containsExactly("https://mymodules.com");

Assertions.assertThat(serverProperties.themeRepositories())
.containsExactly("https://mythemes.com");
}

@Data
@NoArgsConstructor
public static class Server {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.yaml.snakeyaml.Yaml;

/**
*
* @author t.marx
* @param <T>
*/
@Slf4j
@RequiredArgsConstructor
public class RemoteModuleRepository<T> {
Expand All @@ -51,70 +47,66 @@ public class RemoteModuleRepository<T> {
.build();

private final Class<T> type;
private final String baseUrl;
private final List<String> baseUrls;

public boolean exists(String id) {
try {
var moduleInfoUrl = baseUrl + "/main/%s/%s.yaml"
.formatted(id, id);

URI uri = URI.create(moduleInfoUrl);
HttpRequest request = HttpRequest.newBuilder(uri).build();
return client.send(request, HttpResponse.BodyHandlers.ofString()).statusCode() == 200;
} catch (IOException | InterruptedException ex) {
log.error("", ex);
for (String baseUrl : baseUrls) {
try {
String moduleInfoUrl = baseUrl + "/%s/%s.yaml".formatted(id, id);
URI uri = URI.create(moduleInfoUrl);
HttpRequest request = HttpRequest.newBuilder(uri).build();
int status = client.send(request, HttpResponse.BodyHandlers.ofString()).statusCode();
if (status == 200) {
return true;
}
} catch (IOException | InterruptedException ex) {
log.warn("Failed checking existence at {}: {}", baseUrl, ex.getMessage());
}
}

return false;
}

public Optional<T> getInfo(String extension) {
try {
var moduleInfoUrl = baseUrl + "/main/%s/%s.yaml"
.formatted(extension, extension);

URI uri = URI.create(moduleInfoUrl);
HttpRequest request = HttpRequest.newBuilder(uri)
.build();
final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
return Optional.empty();
for (String baseUrl : baseUrls) {
try {
String moduleInfoUrl = baseUrl + "/%s/%s.yaml".formatted(extension, extension);
URI uri = URI.create(moduleInfoUrl);
HttpRequest request = HttpRequest.newBuilder(uri).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
String content = response.body();
return Optional.of(new Yaml().loadAs(content, type));
}
} catch (IOException | InterruptedException ex) {
log.warn("Failed loading info from {}: {}", baseUrl, ex.getMessage());
}
String content = response.body();

return Optional.of(new Yaml().loadAs(content, type));
} catch (IOException | InterruptedException ex) {
log.error("", ex);
}

return Optional.empty();
}

public void download(String url, Path target) {

try {
Path tempDirectory = Files.createTempDirectory("modules");
if (SystemUtils.IS_OS_UNIX) {
Files.setPosixFilePermissions(tempDirectory, PosixFilePermissions.fromString("rwx------"));
} else {
var f = tempDirectory.toFile();
File f = tempDirectory.toFile();
f.setReadable(true, true);
f.setWritable(true, true);
f.setExecutable(true, true);
}

var request = HttpRequest.newBuilder(URI.create(url)).GET().build();
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).GET().build();
HttpResponse<Path> response = client.send(
request,
HttpResponse.BodyHandlers.ofFile(tempDirectory.resolve(System.currentTimeMillis() + ".zip")));

var downloaded = response.body();

Path downloaded = response.body();
File moduleTempDir = InstallationHelper.unpackArchive(downloaded.toFile(), tempDirectory.toFile());

InstallationHelper.moveDirectoy(moduleTempDir, target.resolve(moduleTempDir.getName()).toFile());

} catch (Exception ex) {
log.error("", ex);
log.error("Error downloading module: {}", ex.getMessage(), ex);
throw new RuntimeException("error downloading module");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,70 +22,73 @@
* #L%
*/


import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;

/**
*
* @author t.marx
*/
@Slf4j
@RequiredArgsConstructor
public class RemoteRepository {

HttpClient client = HttpClient.newHttpClient();

public boolean exists (String extension) {
try {
var moduleInfoUrl = "https://raw.githubusercontent.com/CondationCMS/extension-registry/main/%s/%s.yaml"
.formatted(extension, extension);

URI uri = URI.create(moduleInfoUrl);
HttpRequest request = HttpRequest.newBuilder(uri).build();
return client.send(request, BodyHandlers.ofString()).statusCode() == 200;
} catch (IOException | InterruptedException ex) {
log.error("", ex);
}

return false;
private final HttpClient client = HttpClient.newHttpClient();
private final List<String> baseUrls;

public boolean exists(String extension) {
return baseUrls.stream().anyMatch(baseUrl -> {
String url = baseUrl + "%s%s/%s.yaml".formatted(
baseUrl.endsWith("/") ? "" : "/",
extension,
extension);
try {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
int statusCode = client.send(request, BodyHandlers.ofString()).statusCode();
return statusCode == 200;
} catch (IOException | InterruptedException ex) {
log.warn("Failed checking existence at {}: {}", url, ex.getMessage());
return false;
}
});
}

public Optional<String> getContent (String extension) {
try {
var moduleInfoUrl = "https://raw.githubusercontent.com/CondationCMS/extension-registry/main/%s/%s.js"
.formatted(extension, extension);

URI uri = URI.create(moduleInfoUrl);
HttpRequest request = HttpRequest.newBuilder(uri).build();
return Optional.of(client.send(request, BodyHandlers.ofString()).body());
} catch (IOException | InterruptedException ex) {
log.error("", ex);

public Optional<String> getContent(String extension) {
for (String baseUrl : baseUrls) {
String url = baseUrl + "%s%s/%s.js".formatted(
baseUrl.endsWith("/") ? "" : "/",
extension,
extension);
try {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
String body = client.send(request, BodyHandlers.ofString()).body();
return Optional.ofNullable(body);
} catch (IOException | InterruptedException ex) {
log.warn("Failed loading content from {}: {}", url, ex.getMessage());
}
}

return Optional.empty();
}

public Optional<ExtensionInfo> getInfo (String extension) {
try {
var moduleInfoUrl = "https://raw.githubusercontent.com/CondationCMS/extension-registry/main/%s/%s.yaml"
.formatted(extension, extension);

URI uri = URI.create(moduleInfoUrl);
HttpRequest request = HttpRequest.newBuilder(uri).build();
String content = client.send(request, BodyHandlers.ofString()).body();


return Optional.of(new Yaml().loadAs(content, ExtensionInfo.class));
} catch (IOException | InterruptedException ex) {
log.error("", ex);

public Optional<ExtensionInfo> getInfo(String extension) {
for (String baseUrl : baseUrls) {
String url = baseUrl + "%s%s/%s.yaml".formatted(
baseUrl.endsWith("/") ? "" : "/",
extension,
extension);
try {
HttpRequest request = HttpRequest.newBuilder(URI.create(url)).build();
String content = client.send(request, BodyHandlers.ofString()).body();
ExtensionInfo info = new Yaml().loadAs(content, ExtensionInfo.class);
return Optional.of(info);
} catch (IOException | InterruptedException ex) {
log.warn("Failed loading info from {}: {}", url, ex.getMessage());
}
}

return Optional.empty();
}
}
Loading