wkd-java/wkd-test-suite/src/main/java/pgp/wkd/test_suite/WkdDirectoryStructure.java

132 lines
3.8 KiB
Java

// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package pgp.wkd.test_suite;
import pgp.wkd.WKDAddress;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
public abstract class WkdDirectoryStructure {
protected final String domain;
protected final File rootDir;
protected final File wellKnown;
protected final File openpgpkey;
public WkdDirectoryStructure(File rootDirectory, String domain) {
this.domain = domain;
this.rootDir = rootDirectory;
wellKnown = new File(rootDirectory, ".well-known");
openpgpkey = new File(wellKnown, "openpgpkey");
}
public abstract File getHu();
public abstract Path getRelativeCertificatePath(String mailAddress);
public abstract void mkdirs() throws IOException;
protected void mkdir(File dir) throws IOException {
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("Cannot create directory '" + dir.getAbsolutePath() + "'.");
}
if (dir.isFile()) {
throw new IOException("Cannot create directory '" + dir.getAbsolutePath() + "': Is a file.");
}
}
public abstract URI getAddress(String mail);
public abstract File resolve(Path path);
public static class DirectMethod extends WkdDirectoryStructure {
private final File hu;
public DirectMethod(File rootDirectory, String domain) {
super(rootDirectory, domain);
this.hu = new File(openpgpkey, "hu");
}
@Override
public File getHu() {
return hu;
}
@Override
public Path getRelativeCertificatePath(String mailAddress) {
WKDAddress address = WKDAddress.fromEmail(mailAddress);
String path = address.getDirectMethodURI().getPath();
String fileName = path.substring(path.lastIndexOf('/') + 1);
return rootDir.toPath().relativize(new File(getHu(), fileName).toPath());
}
@Override
public void mkdirs() throws IOException {
mkdir(rootDir);
mkdir(wellKnown);
mkdir(openpgpkey);
mkdir(hu);
}
@Override
public URI getAddress(String mail) {
return WKDAddress.fromEmail(mail).getDirectMethodURI();
}
@Override
public File resolve(Path path) {
return rootDir.toPath().resolve(path).toFile();
}
}
public static class AdvancedMethod extends WkdDirectoryStructure {
private final File domainFile;
private final File hu;
public AdvancedMethod(File rootDir, String domain) {
super(rootDir, domain);
this.domainFile = new File(openpgpkey, domain);
this.hu = new File(domainFile, "hu");
}
@Override
public File getHu() {
return hu;
}
@Override
public Path getRelativeCertificatePath(String mailAddress) {
WKDAddress address = WKDAddress.fromEmail(mailAddress);
String path = address.getAdvancedMethodURI().getPath();
String fileName = path.substring(path.lastIndexOf('/') + 1);
return rootDir.toPath().relativize(new File(getHu(), fileName).toPath());
}
@Override
public void mkdirs() throws IOException {
mkdir(rootDir);
mkdir(wellKnown);
mkdir(openpgpkey);
mkdir(domainFile);
mkdir(hu);
}
@Override
public URI getAddress(String mail) {
return WKDAddress.fromEmail(mail).getAdvancedMethodURI();
}
@Override
public File resolve(Path path) {
return rootDir.toPath().resolve(path).toFile();
}
}
}