This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-samples.git
commit 59fc3a15d17abee9189ef18034130004fd21b72a Author: Robert Munteanu <[email protected]> AuthorDate: Thu Nov 14 11:18:02 2024 +0100 feat(oauth): add error handling --- .../sling/samples/oauth_demo/ErrorHolderModel.java | 36 ++++++++++++++++++++++ oauth/ui.apps/Makefile | 2 +- .../src/main/content/META-INF/vault/filter.xml | 4 +++ .../apps/sling/servlet/errorhandler/.content.xml | 22 +++++++++++++ .../errorhandler/OAuthCallbackException.html | 20 ++++++++++++ .../errorhandler/OAuthEntryPointException.html | 20 ++++++++++++ .../content/oauth-demo/static/css/output.css | 19 ++++++++++++ 7 files changed, 122 insertions(+), 1 deletion(-) diff --git a/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/ErrorHolderModel.java b/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/ErrorHolderModel.java new file mode 100644 index 0000000..1f76b68 --- /dev/null +++ b/oauth/core/src/main/java/org/apache/sling/samples/oauth_demo/ErrorHolderModel.java @@ -0,0 +1,36 @@ +package org.apache.sling.samples.oauth_demo; + +import javax.annotation.PostConstruct; +import javax.servlet.http.HttpServletResponse; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.SlingHttpServletResponse; +import org.apache.sling.models.annotations.Model; +import org.apache.sling.models.annotations.injectorspecific.RequestAttribute; +import org.apache.sling.models.annotations.injectorspecific.SlingObject; + +@Model(adaptables = SlingHttpServletRequest.class) +public class ErrorHolderModel { + + @RequestAttribute(name = "javax.servlet.error.message") + private String message; + + @RequestAttribute(name = "javax.servlet.error.exception") + private Exception exception; + + @SlingObject + private SlingHttpServletResponse response; + + @PostConstruct + public void init() { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + + public String getMessage() { + return message ; + } + + public Exception getException() { + return exception; + } +} diff --git a/oauth/ui.apps/Makefile b/oauth/ui.apps/Makefile index 53fbd0e..f09c67a 100644 --- a/oauth/ui.apps/Makefile +++ b/oauth/ui.apps/Makefile @@ -1,5 +1,5 @@ css: src/main/content/jcr_root/content/oauth-demo/static/css/output.css .PHONY=css -src/main/content/jcr_root/content/oauth-demo/static/css/output.css: src/main/frontend/input.css tailwind.config.js src/main/content/jcr_root/apps/oauth-demo/components/welcome/welcome.html src/main/content/jcr_root/apps/oauth-demo/components/youtube-videos/show.html +src/main/content/jcr_root/content/oauth-demo/static/css/output.css: src/main/frontend/input.css tailwind.config.js src/main/content/jcr_root/apps/oauth-demo/components/welcome/welcome.html src/main/content/jcr_root/apps/oauth-demo/components/youtube-videos/show.html src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthCallbackException.html src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthEntryPointException.html tailwindcss --input $< --output $@ diff --git a/oauth/ui.apps/src/main/content/META-INF/vault/filter.xml b/oauth/ui.apps/src/main/content/META-INF/vault/filter.xml index 3f37a9f..ace73d8 100644 --- a/oauth/ui.apps/src/main/content/META-INF/vault/filter.xml +++ b/oauth/ui.apps/src/main/content/META-INF/vault/filter.xml @@ -36,4 +36,8 @@ <filter root="/apps/oauth-demo"> <exclude pattern="/apps/oauth-demo/install/.*"/> </filter> + <filter root="/apps/sling/servlet/errorhandler"> + <include pattern="/apps/sling/servlet/errorhandler/OAuthCallbackException.html"/> + <include pattern="/apps/sling/servlet/errorhandler/OAuthEntryPointException.html"/> + </filter> </workspaceFilter> diff --git a/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/.content.xml b/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/.content.xml new file mode 100644 index 0000000..20e138a --- /dev/null +++ b/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/.content.xml @@ -0,0 +1,22 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" + jcr:primaryType="sling:Folder" +/> diff --git a/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthCallbackException.html b/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthCallbackException.html new file mode 100644 index 0000000..06a9f08 --- /dev/null +++ b/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthCallbackException.html @@ -0,0 +1,20 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link href="/oauth-demo/static/css/output.css" rel="stylesheet"> + <link rel="icon" href="/oauth-demo/static/favicon.ico"> + <title>OAuth Error</title> +</head> +<body> + <main data-sly-use.errorHolder="org.apache.sling.samples.oauth_demo.ErrorHolderModel"> + <div class="bg-red-100 rounded border border-red-400 text-red-700 py-2 px-4"> + <h1>Error while processing the OAuth callback</h1> + <p>${errorHolder.message}.</p> + </div> + + <a class="underline" href="/">Return to the main page and try again.</a> + </main> +</body> +</html> \ No newline at end of file diff --git a/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthEntryPointException.html b/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthEntryPointException.html new file mode 100644 index 0000000..e86b466 --- /dev/null +++ b/oauth/ui.apps/src/main/content/jcr_root/apps/sling/servlet/errorhandler/OAuthEntryPointException.html @@ -0,0 +1,20 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link href="/oauth-demo/static/css/output.css" rel="stylesheet"> + <link rel="icon" href="/oauth-demo/static/favicon.ico"> + <title>OAuth Error</title> +</head> +<body> + <main data-sly-use.errorHolder="org.apache.sling.samples.oauth_demo.ErrorHolderModel"> + <div class="bg-red-100 rounded border border-red-400 text-red-700 py-2 px-4"> + <h1>Error while starting the OAuth process</h1> + <p>${errorHolder.message}.</p> + </div> + + <a class="underline" href="/">Return to the main page and try again.</a> + </main> +</body> +</html> \ No newline at end of file diff --git a/oauth/ui.apps/src/main/content/jcr_root/content/oauth-demo/static/css/output.css b/oauth/ui.apps/src/main/content/jcr_root/content/oauth-demo/static/css/output.css index 9127bf7..e6bab3c 100644 --- a/oauth/ui.apps/src/main/content/jcr_root/content/oauth-demo/static/css/output.css +++ b/oauth/ui.apps/src/main/content/jcr_root/content/oauth-demo/static/css/output.css @@ -857,11 +857,25 @@ p { border-radius: 0.25rem; } +.border { + border-width: 1px; +} + +.border-red-400 { + --tw-border-opacity: 1; + border-color: rgb(248 113 113 / var(--tw-border-opacity)); +} + .bg-blue-500 { --tw-bg-opacity: 1; background-color: rgb(59 130 246 / var(--tw-bg-opacity)); } +.bg-red-100 { + --tw-bg-opacity: 1; + background-color: rgb(254 226 226 / var(--tw-bg-opacity)); +} + .bg-white { --tw-bg-opacity: 1; background-color: rgb(255 255 255 / var(--tw-bg-opacity)); @@ -890,6 +904,11 @@ p { font-weight: 700; } +.text-red-700 { + --tw-text-opacity: 1; + color: rgb(185 28 28 / var(--tw-text-opacity)); +} + .text-white { --tw-text-opacity: 1; color: rgb(255 255 255 / var(--tw-text-opacity));
