Skip to content
Open
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
171 changes: 171 additions & 0 deletions elemental-api/elemental-expath-pkg-mgmt-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (C) 2014, Evolved Binary Ltd

This file was originally ported from FusionDB to Elemental by
Evolved Binary, for the benefit of the Elemental Open Source community.
Only the ported code as it appears in this file, at the time that
it was contributed to Elemental, was re-licensed under The GNU
Lesser General Public License v2.1 only for use in Elemental.

This license grant applies only to a snapshot of the code as it
appeared when ported, it does not offer or infer any rights to either
updates of this source code or access to the original source code.

The GNU Lesser General Public License v2.1 only license follows.

=====================================================================

Elemental
Copyright (C) 2024, Evolved Binary Ltd

admin@evolvedbinary.com
https://www.evolvedbinary.com | https://www.elemental.xyz

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; version 2.1.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>xyz.elemental</groupId>
<artifactId>elemental-api</artifactId>
<version>7.7.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

<artifactId>elemental-expath-pkg-mgmt-api</artifactId>

<name>Elemental EXPath Package Management API</name>
<description>HTTP API for managing EXPath Packages in Elemental</description>

<scm>
<connection>scm:git:https://github.com/evolvedbinary/elemental.git</connection>
<developerConnection>scm:git:https://github.com/evolvedbinary/elemental.git</developerConnection>
<url>scm:git:https://github.com/evolvedbinary/elemental.git</url>
<tag>HEAD</tag>
</scm>

<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>

<dependency>
<groupId>xyz.elemental.fork.org.exist-db</groupId>
<artifactId>exist-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>xyz.elemental</groupId>
<artifactId>elemental-media-type-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.expath.packaging</groupId>
<artifactId>pkg-java</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>com.evolvedbinary.thirdparty.xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<scope>test</scope>
</dependency>

<!-- needed for starting up a jetty server -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-deploy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jmx</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
</testResource>
<testResource>
<directory>src/test/resources-filtered</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<configuration>
<licenseSets>
<licenseSet>
<header>${project.parent.relativePath}/../elemental-parent/FDB-backport-to-EDB-LGPL-21-ONLY-license.template.txt</header>
</licenseSet>
</licenseSets>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<jetty.home>${project.basedir}/../../exist-jetty-config/target/classes/org/exist/jetty</jetty.home>
<elemental.configurationFile>${project.build.testOutputDirectory}/conf.xml</elemental.configurationFile>
<elemental.jetty.standalone.webapp.dir>${project.build.testOutputDirectory}/standalone-webapp</elemental.jetty.standalone.webapp.dir>
<log4j.configurationFile>${project.build.testOutputDirectory}/log4j2.xml</log4j.configurationFile>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package xyz.elemental.api.expath.pkg.mgmt;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.EXistException;
import org.exist.http.servlets.AbstractExistHttpServlet;
import org.exist.repo.ExistRepository;
import org.exist.security.Subject;
import org.exist.storage.DBBroker;
import org.exist.storage.txn.Txn;
import org.exist.util.serializer.SAXSerializer;
import org.exist.util.serializer.SerializerPool;
import org.expath.pkg.repo.Packages;
import org.expath.pkg.repo.Package;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import xyz.elemental.mediatype.MediaType;

import javax.xml.transform.OutputKeys;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Optional;
import java.util.Properties;


public class ExpathPkgMgmtServlet extends AbstractExistHttpServlet {

private final static Logger LOG = LogManager.getLogger(ExpathPkgMgmtServlet.class);

protected final static Properties defaultProperties = new Properties();

static {
defaultProperties.setProperty(OutputKeys.INDENT, "yes");
defaultProperties.setProperty(OutputKeys.MEDIA_TYPE, MediaType.APPLICATION_XML);
}

private static final String PKG_API_NS = "http://expath.org/ns/pkg-api";
private static final String PKG_NS = "http://expath.org/ns/pkg";
private static final String PKG_PREFIX = "pkg";

@Override
public Logger getLog() {
return LOG;
}

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

// authenticate the user
final Subject user = authenticate(request, response);
if (user == null) {
// You now get a HTTP Authentication challenge if there is no user
return;
}
// process the request and a connection for this user
try (final DBBroker broker = getPool().get(Optional.of(user));
final Txn transaction = getPool().getTransactionManager().beginTransaction()) {

try {
final Optional<ExistRepository> repo = broker.getBrokerPool().getExpathRepo();

if (repo.isPresent()) {
writePackageList(response, getContainerEncoding(), repo.get());
} else {
throw new ServletException("expath repository not available");
}

transaction.commit();

} catch (final Throwable t) {
transaction.abort();
throw t;
}

} catch (final EXistException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} catch (final Throwable e) {
getLog().error(e.getMessage(), e);
throw new ServletException("An error occurred: " + e.getMessage(), e);
}
}

protected void writePackageList(final HttpServletResponse response, final String encoding,
final ExistRepository repo) throws IOException {

response.setContentType(MediaType.APPLICATION_XML + "; charset=" + encoding);

SAXSerializer serializer = null;

Writer writerToClose = null;
try {
final Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);

serializer.setOutput(writer, defaultProperties);

serializer.startDocument();
serializer.startPrefixMapping("", PKG_API_NS);
serializer.startPrefixMapping(PKG_PREFIX, PKG_NS);

final AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "spec", "spec", "CDATA", "1.0.0");

serializer.startElement(PKG_API_NS, "package-list", "package-list", attrs);

for (final Packages pkgs : repo.getParentRepo().listPackages()) {
final Package pkg = pkgs.latest();
attrs.clear();
attrs.addAttribute("", "name", "name", "CDATA", pkgs.name());
attrs.addAttribute("", "abbrev", "abbrev", "CDATA", pkg.getAbbrev());
attrs.addAttribute("", "version", "version", "CDATA", pkg.getVersion());
serializer.startElement(PKG_NS, "package", PKG_PREFIX + ":package", attrs);
serializer.endElement(PKG_NS, "package", PKG_PREFIX + ":package");
}

serializer.endElement(PKG_API_NS, "package-list", "package-list");

serializer.endDocument();

writer.flush();
writerToClose = writer;

} catch (final SAXException e) {
// should never happen
LOG.warn("Error while serializing package list: {}", e.getMessage(), e);
} finally {
if (serializer != null) {
SerializerPool.getInstance().returnObject(serializer);
}
if (writerToClose != null) {
writerToClose.close();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package xyz.elemental.api.expath.pkg.mgmt;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.fluent.Request;
import org.exist.test.ExistWebServer;
import org.junit.ClassRule;
import org.junit.Test;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;

import javax.xml.transform.Source;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class ExpathPkgMgmtServletTest {

@ClassRule
public static final ExistWebServer existWebServer = new ExistWebServer(true, false, true, true);

private static String getPackageUri() {
return "http://localhost:" + existWebServer.getPort() + "/expath/package/";
}

@Test
public void packageQuery() throws IOException {
final HttpResponse response = Request.Get(getPackageUri())
.execute()
.returnResponse();
final int resultStatusCode = response.getStatusLine().getStatusCode();
assertEquals("Server returned response code: " + resultStatusCode, HttpStatus.SC_OK, resultStatusCode);
final String responseBody = readResponse(response.getEntity());

final Source expectedSource = Input.from("<package-list xmlns=\"http://expath.org/ns/pkg-api\" spec=\"1.0.0\"/>").build();
final Source actualSource = Input.from(responseBody).build();
final Diff diff = DiffBuilder.compare(expectedSource)
.withTest(actualSource)
.checkForSimilar()
.build();

assertFalse(diff.toString(), diff.hasDifferences());
}

private String readResponse(final HttpEntity response) throws IOException {
try (final InputStream is = response.getContent();
final BufferedReader reader = new BufferedReader(new InputStreamReader(is, UTF_8))) {
String line;
final StringBuilder out = new StringBuilder();
while ((line = reader.readLine()) != null) {
out.append(line);
out.append("\r\n");
}
return out.toString();
}
}
}
Loading
Loading