You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2022/10/04 17:34:56 UTC

[isis-app-simpleapp] branch jpa-SNAPSHOT updated: Example for resources in Wicket.

This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch jpa-SNAPSHOT
in repository https://gitbox.apache.org/repos/asf/isis-app-simpleapp.git


The following commit(s) were added to refs/heads/jpa-SNAPSHOT by this push:
     new 7c790f4  Example for resources in Wicket.
7c790f4 is described below

commit 7c790f40190cb1b29a4cfb523112fa8e96501f85
Author: Uwe Jäger <uw...@googlemail.com>
AuthorDate: Tue Oct 4 17:43:56 2022 +0200

    Example for resources in Wicket.
---
 .../modules/simple/service/SimpleEchoService.java  | 18 +++++++++++++
 .../main/java/domainapp/webapp/AppManifest.java    |  2 ++
 .../domainapp/webapp/resource/CustomResource.java  | 31 ++++++++++++++++++++++
 .../webapp/resource/CustomResourceInitializer.java | 18 +++++++++++++
 .../webapp/resource/CustomResourceReference.java   | 20 ++++++++++++++
 5 files changed, 89 insertions(+)

diff --git a/module-simple/src/main/java/domainapp/modules/simple/service/SimpleEchoService.java b/module-simple/src/main/java/domainapp/modules/simple/service/SimpleEchoService.java
new file mode 100644
index 0000000..2ffe103
--- /dev/null
+++ b/module-simple/src/main/java/domainapp/modules/simple/service/SimpleEchoService.java
@@ -0,0 +1,18 @@
+package domainapp.modules.simple.service;
+
+import org.apache.isis.applib.services.user.UserService;
+import org.springframework.stereotype.Service;
+
+import javax.inject.Inject;
+
+@Service
+public class SimpleEchoService {
+    @Inject
+    private UserService userService;
+
+    public String greet(String greeting) {
+        String username = userService.currentUserName().orElse("unknown user");
+
+        return greeting + ", " + username + "!";
+    }
+}
diff --git a/webapp/src/main/java/domainapp/webapp/AppManifest.java b/webapp/src/main/java/domainapp/webapp/AppManifest.java
index 4329382..1d3e77d 100644
--- a/webapp/src/main/java/domainapp/webapp/AppManifest.java
+++ b/webapp/src/main/java/domainapp/webapp/AppManifest.java
@@ -1,5 +1,6 @@
 package domainapp.webapp;
 
+import domainapp.webapp.resource.CustomResourceInitializer;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
 import org.springframework.context.annotation.PropertySource;
@@ -67,6 +68,7 @@ import domainapp.webapp.quartz.QuartzModule;
 
         ApplicationModule.class,
         CustomModule.class,
+        CustomResourceInitializer.class,
         QuartzModule.class,
 
         // discoverable fixtures
diff --git a/webapp/src/main/java/domainapp/webapp/resource/CustomResource.java b/webapp/src/main/java/domainapp/webapp/resource/CustomResource.java
new file mode 100644
index 0000000..0d81ee6
--- /dev/null
+++ b/webapp/src/main/java/domainapp/webapp/resource/CustomResource.java
@@ -0,0 +1,31 @@
+package domainapp.webapp.resource;
+
+import domainapp.modules.simple.service.SimpleEchoService;
+import org.apache.wicket.request.resource.AbstractResource;
+
+import javax.inject.Inject;
+import java.io.IOException;
+
+public class CustomResource extends AbstractResource {
+
+    @Inject private SimpleEchoService simpleEchoService;
+
+    @Override
+    protected ResourceResponse newResourceResponse(Attributes attributes) {
+        String greeting = attributes.getParameters().get("greeting").toString();
+
+        ResourceResponse resourceResponse = new ResourceResponse();
+
+        resourceResponse.setStatusCode(200);
+        resourceResponse.setContentType("text/plain");
+        resourceResponse.disableCaching();
+        resourceResponse.setWriteCallback(new WriteCallback() {
+            @Override
+            public void writeData(Attributes attributes) throws IOException {
+                attributes.getResponse().write(simpleEchoService.greet(greeting));
+            }
+        });
+
+        return resourceResponse;
+    }
+}
diff --git a/webapp/src/main/java/domainapp/webapp/resource/CustomResourceInitializer.java b/webapp/src/main/java/domainapp/webapp/resource/CustomResourceInitializer.java
new file mode 100644
index 0000000..2998041
--- /dev/null
+++ b/webapp/src/main/java/domainapp/webapp/resource/CustomResourceInitializer.java
@@ -0,0 +1,18 @@
+package domainapp.webapp.resource;
+
+import org.apache.isis.viewer.wicket.model.isis.WicketApplicationInitializer;
+import org.apache.wicket.protocol.http.WebApplication;
+import org.springframework.context.annotation.Configuration;
+
+import javax.inject.Inject;
+
+@Configuration
+public class CustomResourceInitializer implements WicketApplicationInitializer {
+
+    @Inject CustomResourceReference customResourceReference;
+
+    @Override
+    public void init(WebApplication webApplication) {
+        webApplication.mountResource("/custom-resource-greet/${greeting}", customResourceReference);
+    }
+}
diff --git a/webapp/src/main/java/domainapp/webapp/resource/CustomResourceReference.java b/webapp/src/main/java/domainapp/webapp/resource/CustomResourceReference.java
new file mode 100644
index 0000000..9581057
--- /dev/null
+++ b/webapp/src/main/java/domainapp/webapp/resource/CustomResourceReference.java
@@ -0,0 +1,20 @@
+package domainapp.webapp.resource;
+
+import org.apache.wicket.request.resource.IResource;
+import org.apache.wicket.request.resource.ResourceReference;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class CustomResourceReference extends ResourceReference {
+
+    public CustomResourceReference() {
+        super(CustomResourceReference.class, "custom-resource");
+    }
+
+    @Override
+    @Bean
+    public IResource getResource() {
+        return new CustomResource();
+    }
+}