Tuesday, April 18, 2023

AEM(Adobe Experience Manager) - com.day.cq.wcm.foundation.forms.impl.MailServlet Invalid redirect specified:

 You may get the following error while using the AEM mail action to send the details captured through an AEM form(form built based on AEM foundation or core form components)


org.apache.sling.auth.core.AuthUtil isRedirectValid: Redirect target must not be an URL 

com.day.cq.wcm.foundation.forms.impl.MailServlet Invalid redirect specified:


This error can happen in multiple scenarios; some are below


Defining the external page as the Thank you page for the form - AEM Mail Servlet won't support external URLs as the thank you page due to security restriction


Defining the internal AEM page with complete URL, including protocol and domain - Even though the AEM page sites inside the AEM, as you specified the full URL, the mail servlet treat the URL as external.


Mismatch in the etc. map configuration - The etc. map configuration will help you to map the content path to the complete URLs - domain mapping with shortened URL based on your setup; if your etc. map configuration is enabled with the upper case for the domain names(e.g., www.Test.com), then you will see this issue even though only the content path specified as the thank you page. The reason is the Mail servlet URL comparison is case sensitive, even though the form URL and the thank you page URL are enabled through the same domain. 

Monday, April 10, 2023

What is Apache Sling :: Connection Timeout Agent and How to use it in AEM?

Apache Sling :: Connection Timeout Agent provides a Java agent that uses the instrumentation API to add connect and read timeouts to connections made via HTTP or HTTPs. It only applies these timeouts if none were set explicitly.

The agent is intended as an additional layer of control when running untrusted client code that may make calls without explicitly setting timeouts. Setting timeouts in client code is always recommended rather than relying on this agent.

It currently supports setting timeouts for HTTP connections done using:

  • java.net.URL and/or java.net.URLConnection
  • Apache Commons HttpClient 3.x
  • Apache HttpComponents Client 4.x
  • OK Http

Enable the agent configurations through Java CLI.

You can check out the code from Tags · apache/sling-org-apache-sling-connection-timeout-agent (github.com) and build locally (mvn clean install)

I am using the snapshot version for testing with Connect timeout as 20 secs and Read time out as 60 secs (Copy org.apache.sling.connection-timeout-agent-1.0.3-SNAPSHOT-jar-with-dependencies.jar next to the AEM jar file)

java -javaagent:org.apache.sling.connection-timeout-agent-1.0.3-SNAPSHOT-jar-with-dependencies.jar=20000,60000 -jar aem-sdk-quickstart-2022.9.8722.20220912T101352Z-220800.jar
  • <agent-connect-timeout> - connection timeout in milliseconds to apply via the agent
  • <agent-read-timeout>- read timeout in milliseconds to apply via the agent
  • <logspec> - if set to v, it will enter verbose mode and print additional information to System.out
The Connection Timeout Agent is beneficial to handle some worst-case scenarios — e.g., we were using Adobe Search and Promote to implement the content search for websites; when the Adobe Search and Promote was retired, we migrated to a different search engine, but the S&P code and configurations were not cleaned up; we faced huge performance issue on the day the S&P API’s were stopped working the actual root cause was due to missing connect timeout all the reads were struck indefinitely that caused other requests to get slowed down. The issue could have been arrested if this agent had been configured already.

For testing purposes, the servlet code enabled as part of my last post How to Connect Adobe Experience Manager (AEM) with ChatGPT | by Albin Issac | Apr, 2023 | Medium was not enabled with connection/read timeouts; enabled the connection timeout as one means the connection should fail all the times

java -javaagent:org.apache.sling.connection-timeout-agent-1.0.3-SNAPSHOT-jar-with-dependencies.jar=1,60000 -jar aem-sdk-quickstart-2022.9.8722.20220912T101352Z-220800.jar

Yes, the connection started failing every time; it started working without any issue when I set 20 seconds as the Connection timeout and 60 seconds as the read timeout.

org.apache.http.conn.ConnectTimeoutException: Connect to api.openai.com:443 [api.openai.com/, api.openai.com/] failed: connect timed out

The configurations can be monitored through JMX — org.apache.sling.cta

The AMS started enabling the connection timeout agent for the customers hosted on the AMS platform — The default connection timeout will be set to 20 seconds, and the default read timeout will be set to 60 seconds. But you can work with the AMS team to adjust the timeout values based on your custom need. The agent can be configured in on-prem/custom cloud-hosted versions of AEM if required.

I assume AEM as Cloud Services by default enabled with the Connection Time out agent — These timeout values are 10 seconds for connect calls and 60 seconds for read calls for connections. Refer to AEM as a Cloud Service Development Guidelines | Adobe Experience Manager (Outgoing HTTP Connections) for more details; not sure if the customers can adjust the global timeout values.

Anyhow recommended to always set the connection and read time outs in the code as the global configurations can lead to different issues.

Refer to Apache Sling :: Connection Timeout Agent for more details on Connection Timeout Agent.

Wednesday, April 5, 2023

How to Connect Adobe Experience Manager (AEM) with ChatGPT

 ChatGPT is an AI language model developed by OpenAI, capable of generating human-like text based on input. The model is trained on a large corpus of text data and can generate responses to questions, summarize long texts, write stories, and much more.

The OpenAI API can integrate AI models with different systems.

AEM already enables some of the Generative AI capabilities like — Content Creation, Summarization, rewriting, etc., also Adobe Integrate Adobe Firefly (a range of creative AI models to AEM DAM) to simplify content creation and management. I will explain this in more detail in another post.

Integrating AEM with ChatGPT empowers content authors to easily create content that meets the SEO requirements, e.g., create a title/description for a page, create a story on a topic, summarize the content, etc.; the ChatGPT generated content can be optimized further based on the need.

In this post, let us see how to integrate AEM with ChatGPT to empower the authors with content authoring.

As a first step, enable a servlet connecting the chatgpt model with the user prompt and returning the response.

ChatServlet.java

Change the gpt model based on your need, gpt-3.5-turbo is the latest model currently available for API access; the model details can be found Models — OpenAI API

The ‘role’ can take one of three values: ‘system’, ‘user’ or the ‘assistant’.

These roles help define the context of the conversation; even you can specify the previous conversation context by adding messages with different roles, user — the message sent by the user, and assistant — the response from API.

Not mandatory to specify all the roles; I have used only the user role to generate the response; you can use other roles to give more context to the conversation.

ChatGptRequest.java

max_tokens — specifies the maximum number of tokens the API should generate in response to a prompt. Tokens are the individual words and punctuation marks that make up the text. Adjust the max_tokens based on your prompts and the expected response.

ChatGptResponse.java

Message.java

Now Generate the OpenAI API Key through Account API Keys — OpenAI API

Replace <Open API Token> in the servlet with the actual token; ensure the token is secured, and you can keep the token as an OSGI configuration.

The API is billed based on the token usage; some free tokens are enabled for initial API testing; after that, you should enable the billing details to use the API.

Now the servlet can be invoked as http://localhost:4502/bin/chat.json?prompt=<<Prompt>> e.g., http://localhost:4502/bin/chat.json?prompt=what is AEM

The servlet can be invoked from components, pages, or toolbars to generate the required content.

In this post lets us see how to add a button on the title component edit toolbar to integrate with the above servlet to generate the content whenever required — the condition on the action can be changed to enable the chatgpt action on all the components or specific components (also the servlet can be invoked in multiple other ways).

Create a client library under your project with the below JS and add cq.authoring.editor.sites.page.hook for the categories.

ToolbarAction.CHATGPT.js

js.txt

Now the Title component edit toolbar displays the chatgpt actions.

Clicking on the action will display the dialog to generate the required content through the chatgpt integration.

Enter the prompt for generating the content and Click on Generate button.

Now the authors can use the chatgpt integration to perform the different content operations through ChatGPT —Generate the content, Summarize, generate a title, generate a description, etc.; modify the content based on the business context.

AEM already enables some of the Generative AI capabilities like — Content Creation, Summarization, rewriting, etc., also Adobe Integrate Adobe Firefly (a range of creative AI models to AEM DAM) to simplify content creation and management. I will explain this in more detail in another post.

AEM Project Reference— youttubedata/aem/chatgptintegration at master · techforum-repo/youttubedata (github.com)