Commit 6f3d64ab authored by Gradl, Tobias's avatar Gradl, Tobias
Browse files

410: Convert to Spring Boot library and application (OPENED)

Task-Url: #410
parent 9565156b
Pipeline #22568 passed with stage
in 27 seconds
......@@ -9,7 +9,7 @@ import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import eu.dariah.de.dariahsp.model.User;
import eu.dariah.de.search.dao.UserDao;
import eu.dariah.de.search.dao.db.UserDao;
public class UserServiceImpl implements UserService {
......@@ -19,7 +19,7 @@ public class UserServiceImpl implements UserService {
return userDetailsDao.findByUsername(domain, username);
}
protected void innerSaveUser(User persistedUser) {
public void saveUser(User persistedUser) {
userDetailsDao.save(persistedUser);
}
......
......@@ -28,7 +28,7 @@ import com.mongodb.client.MongoCursor;
import de.unibamberg.minf.dme.model.version.VersionInfo;
import de.unibamberg.minf.dme.model.version.VersionInfoImpl;
import eu.dariah.de.search.dao.VersionDao;
import eu.dariah.de.search.dao.db.VersionDao;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
......
apply plugin: "org.springframework.boot"
apply plugin: "nebula.ospackage"
apply plugin: "war"
ext {
hibernateValidatorVersion = "5.2.2.Final"
tilesVersion = "3.0.5"
tilesVersion = "3.0.8"
jodaJspTagsVersion = "1.1.1"
}
dependencies {
implementation project(":search-core")
compile "org.springframework:spring-context:$springVersion"
compile "org.springframework:spring-beans:$springVersion"
compile "org.springframework:spring-core:$springVersion"
compile "org.springframework:spring-webmvc:$springVersion"
compile "org.springframework:spring-web:$springVersion"
// Spring boot
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.boot:spring-boot-starter-webflux"
implementation "org.springframework.boot:spring-boot-starter-validation"
implementation "org.springframework.boot:spring-boot-starter-data-mongodb"
developmentOnly "org.springframework.boot:spring-boot-devtools"
// Web
implementation "org.apache.tomcat.embed:tomcat-embed-jasper"
implementation "javax.servlet:jstl"
implementation "joda-time:joda-time-jsptags:$jodaJspTagsVersion"
// Apache Tiles
implementation "org.apache.tiles:tiles-jsp:$tilesVersion"
// Testing
implementation "org.springframework.boot:spring-boot-starter-test"
/*testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "org.junit.jupiter:junit-jupiter-engine"
testImplementation "org.mockito:mockito-core:$mockitoVersion"
testImplementation "org.mockito:mockito-junit-jupiter:$mockitoVersion"*/
// Lombok
compileOnly "org.projectlombok:lombok"
annotationProcessor "org.projectlombok:lombok"
testCompileOnly "org.projectlombok:lombok"
// =======================
// Old ... remove
// =======================
//compile "org.springframework:spring-context:$springVersion"
//compile "org.springframework:spring-beans:$springVersion"
//compile "org.springframework:spring-core:$springVersion"
//compile "org.springframework:spring-webmvc:$springVersion"
//compile "org.springframework:spring-web:$springVersion"
//compile "org.springframework.security:spring-security-core:4.2.1.RELEASE"
//compile "org.springframework.security:spring-security-web:4.2.1.RELEASE"
//compile "org.springframework.security:spring-security-config:4.2.1.RELEASE"
compile "org.apache.httpcomponents:httpclient:$httpComponentsVersion"
compile "org.apache.tiles:tiles-core:$tilesVersion"
compile "org.apache.tiles:tiles-jsp:$tilesVersion"
compile "org.apache.tiles:tiles-api:$tilesVersion"
compile "org.slf4j:slf4j-api:$slf4jVersion"
//compile "org.apache.httpcomponents:httpclient:$httpComponentsVersion"
//compile "org.apache.tiles:tiles-core:$tilesVersion"
//compile "org.apache.tiles:tiles-jsp:$tilesVersion"
//compile "org.apache.tiles:tiles-api:$tilesVersion"
/* compile "org.slf4j:slf4j-api:$slf4jVersion"
compile "ch.qos.logback:logback-core:$logbackVersion"
compile "ch.qos.logback:logback-classic:$logbackVersion"
compile "javax.servlet:jstl:1.2"
......@@ -32,67 +64,71 @@ dependencies {
testImplementation "junit:junit:4.12"
testImplementation "org.mockito:mockito-all:1.10.19"
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile "javax.servlet.jsp:jsp-api:2.1"
providedCompile "javax.servlet.jsp:jsp-api:2.1"*/
}
tasks.withType(Deb) {
dependsOn(classes)
doLast {
project.debFile = archiveName;
}
}
tasks.withType(Rpm) {
dependsOn(classes)
doLast {
project.rpmFile = archiveName;
}
bootWar {
launchScript()
}
ospackage {
os = LINUX
os = LINUX
arch = 'all'
packageName "dariah-$project.name"
replaces "$project.name"
maintainer 'info@de.dariah.eu'
requires('openjdk-8-jdk').or('openjdk-11-jdk')
version = "${project.version}"
release = '1'
signingKeyId('CB012F105B632B46C2A7B4918FC46DAC1BC3E238')
into "var/dfa/webapps/$project.name/"
from(configurations.runtime) {
into "WEB-INF/lib"
//installUtils file('scripts/utils.sh')
//preInstall file('scripts/search.preinst')
postInstall file('scripts/search.postinst')
//preUninstall file('scripts/preUninstall.sh')
//postUninstall file('scripts/postUninstall.sh')
into "/var/dfa/webapps/$project.name/"
// Copy the actual .jar file
from(war.outputs.files) {
include '*.war'
rename { String filename ->
filename.replace("-${project.version}.war", ".war")
}
fileMode 0755
}
from("$buildDir/classes/java/main") {
into "WEB-INF/classes"
}
from ("src/main/resources") {
into "WEB-INF/classes"
}
from ("src/main/webapp/WEB-INF") {
into "WEB-INF"
}
from ("src/main/webapp/resources") {
into "resources"
/*from("scripts/search.service") {
into "/etc/systemd/system/"
}*/
from("src/main/resources/application.deb.yml") {
into "/etc/dfa/search"
rename { String filename ->
filename.replace("application.deb.yml", "search.yml")
}
}
}
from ("src/main/webapp/themes") {
into "themes"
tasks.withType(Deb) {
dependsOn(build)
doLast {
project.debFile = archiveName;
}
}
from ("src/main/webapp/META-INF") {
into "META-INF"
buildDeb {
requires('default-jre').or('default-jre-headless')
requires('mongodb').or('mongodb-org')
from("scripts/search.service") {
into "/etc/systemd/system/"
}
}
publishing {
publications {
maven(MavenPublication) {
from(components.web)
artifact(bootJar) {}
artifact(sourcesJar) {}
artifact(javadocJar) {}
}
......@@ -104,7 +140,8 @@ task publishDebPackage {
def distroPath = "$buildDir/distributions/"
doLast {
if (version.endsWith('SNAPSHOT') || version.endsWith('RELEASE')) {
println "Publishing Debian package $debFile as user $repoUser";
aptRepo = version.endsWith('SNAPSHOT') ? aptTestingRepo : aptReleasesRepo
println "Publishing Debian package $debFile as user $repoUser to $aptRepo";
exec {
executable "curl"
args "-f", "-u", "$repoUser:$repoPass", "-H", "Content-Type: multipart/form-data", "--data-binary", "@$distroPath$debFile", "$aptRepo";
......
#!/bin/sh
# preinst script for search-ui
#
# create a search-ui group and user
if ! getent passwd search >/dev/null 2>&1; then
adduser --system --no-create-home --home /var/lib/dfa/search search
addgroup --system search
adduser search search
fi
# create db -- note: this should agree with dbpath in mongod.conf
if [ ! -d /var/lib/dfa/search ]; then
mkdir -p /var/lib/dfa/search
chown search:search /var/lib/dfa/search
fi
# create logdir -- note: this should agree with logpath in mongod.conf
if [ ! -d /var/log/search ]; then
mkdir -p /var/log/search
chown search:search /var/log/search
fi
# Check for changes to the service file
systemctl daemon-reload
[Unit]
Description=search
After=syslog.target
[Service]
User=search
ExecStart=/var/dfa/webapps/search-ui/search-ui.war --spring.config.location=file:/etc/dfa/search/search.yml
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
package eu.dariah.de.colreg.profiles;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import eu.dariah.de.dariahsp.ProfileActionHandler;
import eu.dariah.de.dariahsp.model.ExtendedUserProfile;
import eu.dariah.de.dariahsp.model.User;
import eu.dariah.de.search.service.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class UserProfileActionHandler implements ProfileActionHandler {
@Autowired private UserServiceImpl userService;
@Override
public void handleLogin(ExtendedUserProfile profile) {
User u = userService.loadUserByUsername(profile.getIssuerId(), profile.getUsername());
if (u==null) {
u = profile.toUser();
}
u.setLastLogin(LocalDateTime.now());
userService.saveUser(u);
profile.setId(u.getId());
log.debug("User has logged in: {}=>{}", profile.getId(), u.getId());
}
@Override
public void handleLogout(ExtendedUserProfile profile) {
log.debug("User has logged out: {}", profile.getId());
}
}
\ No newline at end of file
package eu.dariah.de.search;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
@SpringBootApplication
@ConfigurationPropertiesScan
public class SearchApplication {
public static void main(String[] args) {
SpringApplication.run(SearchApplication.class, args);
}
}
\ No newline at end of file
package eu.dariah.de.search.config;
import org.apache.catalina.Context;
import org.apache.tomcat.util.scan.StandardJarScanner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import de.unibamberg.minf.core.web.controller.ImageController;
import eu.dariah.de.colreg.profiles.UserProfileActionHandler;
import eu.dariah.de.dariahsp.CustomizableProfileManager;
import eu.dariah.de.dariahsp.ProfileActionHandler;
import eu.dariah.de.dariahsp.web.controller.CommonLoginController;
import eu.dariah.de.dariahsp.web.controller.SAMLMetadataController;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
@ConfigurationProperties
public class ApplicationConfig {
private String contextPath = "";
public String getContextPath() { return contextPath; }
public void setContextPath(String contextPath) { this.contextPath = contextPath; }
/**
* WebServerFactoryCustomizer bean that adapts to a configured context path for the application. This adaption is not
* necessary for implementation of the dariahsp-core library, but helps with setting up the application as it might
* be available or proxied at their deployments
*
* @return WebServerFactoryCustomizer
*/
@Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> webServerFactoryCustomizer() {
log.info("Web server context path set to {}", contextPath.isEmpty() ? "/" : contextPath);
return factory -> factory.setContextPath(contextPath);
}
/**
* Provides tomcat factory bean disabling TLD scanning:
* @see https://stackoverflow.com/a/52229296
*
* @return TomcatServletWebServerFactory tomcatFactory
*/
@Bean
public TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
}
};
}
/**
* Bean that is injected into {@link CustomizableProfileManager} to facilitate observation of login and logout actions
* Implementations can provide custom implementations of the {@link ProfileActionHandler} interface e.g. to log such
* actions into a database
*
* @return SampleProfileActionHandler bean
*/
@Bean
public ProfileActionHandler profileActionPostprocessor() {
return new UserProfileActionHandler();
}
/**
* Controller bean that facilitates access to generated or stored SAML SP metadata
*
* @return SAMLMetadataController bean
*/
@Bean
public SAMLMetadataController samlMetadataController() {
return new SAMLMetadataController();
}
/**
* Controller that binds to common login mappings
*
* @return LoginLogoutController bean
*/
@Bean
public CommonLoginController loginLogoutController() {
return new CommonLoginController("common/login");
}
@Bean
public ImageController imageController() {
return new ImageController();
}
}
package eu.dariah.de.search.config;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import javax.annotation.PostConstruct;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.unibamberg.minf.core.web.service.ImageService;
import de.unibamberg.minf.core.web.service.ImageServiceImpl;
import de.unibamberg.minf.core.web.service.ImageServiceImpl.SizeBoundsType;
import de.unibamberg.minf.processing.git.adapter.GitRepositoryAdapterImpl;
import de.unibamberg.minf.processing.git.service.GitDataSourceService;
import de.unibamberg.minf.processing.git.service.GitDataSourceServiceImpl;
import eu.dariah.de.search.config.nested.ImagesConfigProperties;
import eu.dariah.de.search.config.nested.PathsConfigProperties;
import eu.dariah.de.search.config.nested.ThumbnailsConfigProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Data
@Slf4j
@Configuration
@ConfigurationProperties
public class MigratedConfig {
//private PathsConfigProperties paths;
//private ImagesConfigProperties images;
/*@PostConstruct
public void completeConfiguration() {
if (paths.getData()==null) {
paths.setData(Paths.get(paths.getMain(), "data").toString());
}
if (paths.getImages()==null) {
paths.setImages(Paths.get(paths.getMain(), "images").toString());
}
if (paths.getBackups()==null) {
paths.setBackups(Paths.get(paths.getMain(), "backups").toString());
}
if (paths.getTestdata()==null) {
paths.setTestdata(Paths.get(paths.getMain(), "testdata").toString());
}
if (images==null) {
images = new ImagesConfigProperties();
images.setThumbnails(new ThumbnailsConfigProperties());
}
}*/
/*@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{
//all the classes the context needs to know about
eu.dariah.de.colreg.model.api.ActionObject.class,
eu.dariah.de.colreg.model.Agent.class,
eu.dariah.de.colreg.model.Collection.class,
eu.dariah.de.colreg.model.api.repository.RepositoryDraftContainer.class,
eu.dariah.de.colreg.model.api.repository.RepositoryResponse.class,
eu.dariah.de.colreg.model.api.oaipmh.OaiPmhResponseContainer.class
});
marshaller.setMarshallerProperties(new HashMap<>() {{
put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true);
}});
return marshaller;
}
@Bean
public XMLConverter xmlConverter() {
XMLConverter xmlConverter = new XMLConverter();
xmlConverter.setMarshaller(jaxb2Marshaller());
xmlConverter.setUnmarshaller(jaxb2Marshaller());
return xmlConverter;
}*/
/*@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
RequestMappingHandlerAdapter requestMappingHandlerAdapter = new RequestMappingHandlerAdapter();
requestMappingHandlerAdapter.setMessageConverters(new ArrayList<>());
requestMappingHandlerAdapter.getMessageConverters().add(jsonConverter());
return requestMappingHandlerAdapter;
}*/
/*@Bean
public org.springframework.http.converter.json.MappingJackson2HttpMessageConverter jsonConverter() {
return new MappingJackson2HttpMessageConverter(objectMapper());
}*/
/*@Bean
public ImageService imageService() {
ImageServiceImpl imageService = new ImageServiceImpl();
imageService.setImagePath(paths.getImages());
imageService.setImagesHeight(images.getHeight());
imageService.setImagesWidth(images.getWidth());
imageService.setImagesSizeType(SizeBoundsType.MAX_LONGEST_SIDE);
imageService.setThumbnailsHeight(images.getThumbnails().getHeight());
imageService.setThumbnailsWidth(images.getThumbnails().getWidth());
imageService.setThumbnailsSizeType(SizeBoundsType.MAX_SHORTEST_SIDE);
return imageService;
}
@Bean
public GitDataSourceService gitDataSourceService() {
GitDataSourceServiceImpl gitDataSourceService = new GitDataSourceServiceImpl();
gitDataSourceService.setAdapter(new GitRepositoryAdapterImpl());
gitDataSourceService.setBaseDirectory(paths.getTestdata());
return gitDataSourceService;
}
@Bean
public AccessTestsCleanupService accessTestsCleanupService() {
AccessTestsCleanupService accessTestsCleanupService = new AccessTestsCleanupService();
accessTestsCleanupService.setRun(true);
accessTestsCleanupService.setBaseDirectory(paths.getTestdata());
return accessTestsCleanupService;
}*/
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
\ No newline at end of file
package eu.dariah.de.search.config;
import java.util.Collection;
import java.util.Collections;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Getter
@Setter
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "mongo")
@EnableMongoRepositories(basePackages = "eu.dariah.de.search.dao.db")
public class MongoConfig extends AbstractMongoClientConfiguration {
private String host = "localhost";
private Integer port = 27017;
private String database = "search";
@Override
protected String getDatabaseName() {
return database;
}
@Override
public MongoClient mongoClient() {
String connectionString = String.format("mongodb://%s:%s/%s", host, port, database);
log.debug("Mongo client initializing with connection: {}", connectionString);
ConnectionString connectionStringObj = new ConnectionString(connectionString);
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.applyConnectionString(connectionStringObj)
.build();
return MongoClients.create(mongoClientSettings);
}
@Override
public Collection<String> getMappingBasePackages() {
return Collections.singleton("eu.dariah.de.search.model");
}
}