From 961c878cbfecb55b0f7be3e16c4494208ca65e59 Mon Sep 17 00:00:00 2001 From: Yvan Date: Fri, 24 Jan 2025 11:06:37 +0000 Subject: [PATCH] now we can serve json or html dependent on the req accept header --- Java/Spring/activitypubbot/build.gradle | 1 + .../ActivityPubBotApplication.java | 26 +++++++++----- .../activitypubbot/HomeController.java | 35 +++++++++++++------ .../activitypubbot/MvcController.java | 20 +++++++++++ .../src/main/resources/application.properties | 3 +- 5 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/MvcController.java diff --git a/Java/Spring/activitypubbot/build.gradle b/Java/Spring/activitypubbot/build.gradle index e21eea4..3d5e023 100644 --- a/Java/Spring/activitypubbot/build.gradle +++ b/Java/Spring/activitypubbot/build.gradle @@ -23,6 +23,7 @@ ext { } dependencies { + implementation 'org.apache.tomcat.embed:tomcat-embed-jasper' implementation 'org.springframework.boot:spring-boot-starter-data-rest' implementation 'org.springframework.boot:spring-boot-starter-groovy-templates' implementation 'org.springframework.boot:spring-boot-starter-web' diff --git a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/ActivityPubBotApplication.java b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/ActivityPubBotApplication.java index b62e146..7a9c1e0 100644 --- a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/ActivityPubBotApplication.java +++ b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/ActivityPubBotApplication.java @@ -2,9 +2,6 @@ package dev.activitypub.activitypubbot; import java.util.Arrays; -//import org.springframework.boot.SpringApplication; -//import org.springframework.boot.autoconfigure.SpringBootApplication; - import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -13,9 +10,22 @@ import org.springframework.context.annotation.Bean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; -@SpringBootApplication -public class ActivityPubBotApplication { +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +@SpringBootApplication +public class ActivityPubBotApplication extends SpringBootServletInitializer { + + //comment below if deploying outside web container --> + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { + return builder.sources(ActivityPubBotApplication.class); + } + + public static void main(String[] args) { + SpringApplication.run(ActivityPubBotApplication.class); + } + /* public static void main(String[] args) { SpringApplication.run(ActivityPubBotApplication.class, args); } @@ -26,11 +36,11 @@ public class ActivityPubBotApplication { return args -> { System.out.println("Beany McBeanface"); - /*String[] beanNames = ctx.getBeanDefinitionNames(); + /* String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); - }*/ + }* / }; - } + } */ } diff --git a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/HomeController.java b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/HomeController.java index 0b30f21..9d729e5 100644 --- a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/HomeController.java +++ b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/HomeController.java @@ -1,6 +1,7 @@ package dev.activitypub.activitypubbot; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; @@ -9,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; * extending this to handle the basic ActivityPub implementation. */ @RestController +@RequestMapping( headers = "accept=application/json" ) public class HomeController { @Autowired @@ -20,19 +22,11 @@ public class HomeController { return apProps.getKeyFilePath(); } - /** - * Keeping / mysterious... - */ - @GetMapping("/") - public String index() { - return "Parsnip!"; - } - /** * Really just an alias to /user/springbot * TODO: how to auto-map @ to /users/ */ - @GetMapping("/@springbot") + @RequestMapping(value = "/@springbot", headers = "accept=application/json") public String atactor() { return this.actor(); } @@ -41,7 +35,7 @@ public class HomeController { * Access the bot/user - ultimately this should check the database for * /user/ to pull out the relevant data. */ - @GetMapping("/users/springbot") + @RequestMapping(value = "/users/springbot", headers = "accept=application/json") public String actor() { // TODO: this needs some sort of object wrapper or template, and data in database return """ @@ -64,8 +58,27 @@ public class HomeController { "publicKey": { "id": "https://springbot.seth.id.au/users/springbot#main-key", "owner": "https://springap.seth.id.au/users/springbot", - "publicKeyPem": "-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----" + "publicKeyPem": "-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0A5W6M6b+3meJAU0/Fki\\nkMSrEZ6EEThAv4NmyCeDlRbmFQWbh5rWtb69TxkGkkiSFNM3sgg+RSW44Ehn10mL\\nTptfk6oSWFnFHw9MPxmwlWm1Xw8zmp2OMUlI82w11PECFdITJw/1HW73JSVQYfFq\\nWo9rD6nI9G3LPpAB16015NJ9hyeMvz5RA9p9UE540q0l5iJD/l7bxCjHglOQInQX\\neCiR2ErzQSVq3AMhBehoP7HuhKjs8swi8dOgjO3sawqxUyv2+lkesFD2rvxCcXRO\\nBkg/Y7nmJSEcqtcmKYQdObPCIt/wCZNAihJz7dwnGKLE2+JJqPZMer9fAj077OkQ\\neQIDAQAB\\n-----END PUBLIC KEY-----" } +}"""; + } + + /** + * Webfinger the user... + */ + @GetMapping("/.well-known/webfinger") + public String webfinger() { + return """ +{ + "subject": "acct:springbot@springbot.seth.id.au", + + "links": [ + { + "rel": "self", + "type": "application/activity+json", + "href": "https://springbot.seth.id.au/users/springbot" + } + ] }"""; } } diff --git a/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/MvcController.java b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/MvcController.java new file mode 100644 index 0000000..d31bfbd --- /dev/null +++ b/Java/Spring/activitypubbot/src/main/java/dev/activitypub/activitypubbot/MvcController.java @@ -0,0 +1,20 @@ +package dev.activitypub.activitypubbot; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class MvcController { + + @RequestMapping("/@springbot") + public String home() { + System.out.println("Bottty McBotface..."); + return "index"; + } + + @RequestMapping("/") + public String root() { + System.out.println("Going home..."); + return "index"; + } +} diff --git a/Java/Spring/activitypubbot/src/main/resources/application.properties b/Java/Spring/activitypubbot/src/main/resources/application.properties index 4992f85..cbcf4e3 100644 --- a/Java/Spring/activitypubbot/src/main/resources/application.properties +++ b/Java/Spring/activitypubbot/src/main/resources/application.properties @@ -1,5 +1,6 @@ spring.application.name=activitypubbot -ap.keyFilePath=doodle.de.do +spring.mvc.view.prefix: /WEB-INF/views/ +spring.mvc.view.suffix: .jsp server.ssl.key-store-type=PKCS12 server.ssl.key-store=classpath:keys/activitypubbot.p12 server.ssl.key-store-password=password