Skip to content

Commit

Permalink
#4 add module for form processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Dec 20, 2023
1 parent 0c5f798 commit 2235d16
Show file tree
Hide file tree
Showing 12 changed files with 640 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cms-server/hosts/demo/config/forms.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mail:
smtp:
hostname: 127.0.0.1
port: 3025
username: test@example.test
password: password
forms:
- name: contact
to: thmarx@gmx.net
subject: Ich suche Kontakt!
4 changes: 4 additions & 0 deletions modules/forms-module/module.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id=${module.id}
name=${module.name}
version=${project.version}
priority=${module.priority}
120 changes: 120 additions & 0 deletions modules/forms-module/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.github.thmarx.cms.modules</groupId>
<artifactId>cms-modules</artifactId>
<version>3.2.0-SNAPSHOT</version>
</parent>

<artifactId>forms-module</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<module.id>forms-module</module.id>
<module.name>forms module</module.name>
<module.priority>NORMAL</module.priority>
</properties>
<dependencies>

<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>8.5.1</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.logicsquad</groupId>
<artifactId>nanocaptcha</artifactId>
<version>1.5</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail-junit5</artifactId>
<version>2.0.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.github.thmarx.cms</groupId>
<artifactId>cms-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.thmarx.cms.modules.framework</groupId>
<artifactId>modules-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<finalName>${module.id}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
31 changes: 31 additions & 0 deletions modules/forms-module/src/main/assembly/assembly.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>bin</id>
<formats>
<format>zip</format>
</formats>
<files>
<file>
<source>target/${project.build.finalName}.${project.packaging}</source>
<outputDirectory>libs/</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>module.properties</include>
</includes>
<filtered>true</filtered>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>libs</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.thmarx.cms.modules.forms;

/*-
* #%L
* forms-module
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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 java.util.List;
import java.util.Optional;
import lombok.Data;

/**
*
* @author t.marx
*/
@Data
public class FormsConfig {

private Mail mail;

private List<Form> forms;

public Optional<Form> findForm (final String name) {
return forms.stream().filter(form -> form.getName().equals(name)).findFirst();
}

@Data
public static class Form {
private String name;
private String to;
private String subject;
}

@Data
public static class Mail {
private Smtp smtp;
}

@Data
public static class Smtp {
private String hostname;
private int port;
private String username;
private String password;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.thmarx.cms.modules.forms;

/*-
* #%L
* forms-module
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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.github.thmarx.cms.api.extensions.JettyHttpHandlerExtensionPoint;
import com.github.thmarx.cms.api.extensions.Mapping;
import com.github.thmarx.cms.modules.forms.handler.GenerateCaptchaHandler;
import com.github.thmarx.cms.modules.forms.handler.SubmitFormHandler;
import com.github.thmarx.modules.api.annotation.Extension;
import org.eclipse.jetty.http.pathmap.PathSpec;

/**
*
* @author t.marx
*/
@Extension(JettyHttpHandlerExtensionPoint.class)
public class FormsHttpHandlerExtension extends JettyHttpHandlerExtensionPoint {

@Override
public Mapping getMapping() {
Mapping mapping = new Mapping();

mapping.add(PathSpec.from("/captcha/generate"), new GenerateCaptchaHandler());
mapping.add(PathSpec.from("/form/submit"), new SubmitFormHandler());

return mapping;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.github.thmarx.cms.modules.forms;

/*-
* #%L
* forms-module
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.thmarx.cms.api.module.CMSModuleContext;
import com.github.thmarx.modules.api.ModuleLifeCycleExtension;
import com.github.thmarx.modules.api.annotation.Extension;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import lombok.extern.slf4j.Slf4j;
import org.simplejavamail.api.mailer.Mailer;
import org.simplejavamail.mailer.MailerBuilder;
import org.yaml.snakeyaml.Yaml;

/**
*
* @author t.marx
*/
@Slf4j
@Extension(ModuleLifeCycleExtension.class)
public class FormsLifecycleExtension extends ModuleLifeCycleExtension<CMSModuleContext> {

public static Cache<String, String> CAPTCHAS;
public static FormsConfig FORMSCONFIG;
public static Mailer MAILER;

@Override
public void init() {

}

@Override
public void activate() {
CAPTCHAS = Caffeine.newBuilder()
.maximumSize(10_000)
.expireAfterWrite(Duration.ofMinutes(1))
.build();

Path formsConfig = getContext().getDb().getFileSystem().resolve("config/forms.yaml");
try {
FORMSCONFIG = new Yaml().loadAs(Files.readString(formsConfig, StandardCharsets.UTF_8), FormsConfig.class);

MAILER = MailerBuilder.withSMTPServer(FORMSCONFIG.getMail().getSmtp().getHostname(), FORMSCONFIG.getMail().getSmtp().getPort()).buildMailer();

} catch (IOException ex) {
log.error(null, ex);
}
}

}
Loading

0 comments on commit 2235d16

Please sign in to comment.