Adaptive Form - 'File Attachment' component leads to 'Encountered an internal error while submitting the form' on submit | Community
Skip to main content
New Participant
August 12, 2022

Adaptive Form - 'File Attachment' component leads to 'Encountered an internal error while submitting the form' on submit

  • August 12, 2022
  • 4 replies
  • 3743 views

Hi All,

I am getting an error while submitting any attachment from Adaptive form

Error:

Adaptive Form :

 

Has anyone has suggestions on this process.

 

Thanks

Rakesh

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

4 replies

Vijay_Katoch
New Participant
August 16, 2022

Add below code in post.POST.jsp file while creating custom submit action.

"/bin/storeafsubmission" this is my servlet path.

This code works for me in aem 6.4 version

 

<%@include file="/libs/fd/af/components/guidesglobal.jsp"%>
<%@include file="/libs/foundation/global.jsp"%>

<%@ page import="org.apache.sling.api.request.RequestParameter,
com.day.cq.wcm.api.WCMMode,
com.day.cq.wcm.foundation.forms.FormsHelper,
org.apache.sling.api.resource.ResourceUtil,
org.apache.sling.api.resource.ValueMap,
com.adobe.forms.common.submitutils.CustomParameterRequest,
com.adobe.aemds.guide.utils.*" %>

<%@ page import="org.apache.sling.api.request.RequestParameter,
com.day.cq.wcm.api.WCMMode" %>

<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0"%>

<cq:defineObjects/>
<sling:defineObjects/>
<%@page session="false" %>
<%

com.adobe.aemds.guide.utils.GuideSubmitUtils.setForwardPath(slingRequest,"/bin/storeafsubmission",null,null);

%>

 

Links to create custom submit actions:

https://experienceleague.adobe.com/docs/experience-manager-learn/forms/adaptive-forms/custom-submit-aem-forms-article.html?lang=en

Employee
August 22, 2022

Can you please copy and paste your servlet code?

Vijay_Katoch
New Participant
August 22, 2022
package com.aem.vk.core.servlets;

import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.json.JSONException;
import org.json.JSONObject;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.ValueFormatException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


@Component(service = Servlet.class, property = { "sling.servlet.methods=get", "sling.servlet.methods=post",
"sling.servlet.paths=/bin/storeafsubmission" })
public class HandleAdaptiveFormSubmission extends SlingAllMethodsServlet {

//private static final Logger log = LoggerFactory.getLogger(HandleAdaptiveFormSubmission.class);
private static final long serialVersionUID = 1L;

@Reference(target = "(&(objectclass=javax.sql.DataSource)(datasource.name=AEM_MYSQL_DB))")
private DataSource dataSource;

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
JSONObject afSubmittedData;
try {
afSubmittedData = new JSONObject(request.getParameter("jcr:data"));
// we will only store the data bound to schema
JSONObject dataToStore = afSubmittedData.getJSONObject("afData").getJSONObject("afBoundData")
.getJSONObject("data");
String formName = afSubmittedData.getJSONObject("afData").getJSONObject("afSubmissionInfo")
.getString("afPath");
//log.debug("The form name is " + formName);
insertData(dataToStore, formName);

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void insertData(org.json.JSONObject jsonData, String formName) throws ClassNotFoundException, SQLException {
//log.debug("The json object I got to insert was " + jsonData.toString());
String insertTableSQL = "INSERT INTO db_vk.formsubmissions(formdata,formname) VALUES(?,?)";
//log.debug("The query is " + insertTableSQL);
Connection c = getConnection();
//Connection c = getConn();
PreparedStatement pstmt = null;
try {
pstmt = null;
pstmt = c.prepareStatement(insertTableSQL);
pstmt.setString(1, jsonData.toString());
pstmt.setString(2, formName);
//log.debug("Executing the insert statment " + pstmt.executeUpdate());
pstmt.executeUpdate();
c.commit();
} catch (SQLException e) {

//log.error("Getting errors", e);
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (c != null) {
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

public Connection getConnection() {
//log.debug("Getting Connection ");
Connection con = null;
try {

con = dataSource.getConnection();
// log.debug("got connection");
return con;
} catch (Exception e) {
// log.error("not able to get connection ", e);
}
return null;
}

public String getJSONSchema(String afPath) {
// TODO Auto-generated method stub
afPath = afPath.replaceAll("/content/dam/formsanddocuments/", "/content/forms/af/");
Resource afResource = getResolver.getServiceResolver().getResource(afPath + "/jcr:content/guideContainer");
Node resNode = afResource.adaptTo(Node.class);
String schemaNode = null;
try {
schemaNode = resNode.getProperty("schemaRef").getString();
} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (schemaNode.startsWith("/content/dam")) {
//log.debug("The schema is in the dam");
afResource = getResolver.getServiceResolver()
.getResource(schemaNode + "/jcr:content/renditions/original/jcr:content");
resNode = afResource.adaptTo(Node.class);
InputStream jsonSchemaStream = null;
try {
jsonSchemaStream = resNode.getProperty("jcr:data").getBinary().getStream();
Charset charset = StandardCharsets.UTF_8;
String jasonSchemaString = IOUtils.toString(jsonSchemaStream, charset);
//log.debug("The Schema is " + jasonSchemaString);
return jasonSchemaString;
} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (schemaNode.startsWith("/assets")) {
afResource = getResolver.getServiceResolver()
.getResource(afPath + "/jcr:content/guideContainer/" + schemaNode + "/jcr:content");
resNode = afResource.adaptTo(Node.class);
InputStream jsonSchemaStream = null;
try {
jsonSchemaStream = resNode.getProperty("jcr:data").getBinary().getStream();
Charset charset = StandardCharsets.UTF_8;
String jasonSchemaString = IOUtils.toString(jsonSchemaStream, charset);
//log.debug("The Schema is " + jasonSchemaString);
return jasonSchemaString;
} catch (ValueFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (PathNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RepositoryException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}

}
New Participant
August 16, 2022

What I see on your screenshot looks ok to me.

Just some usual questions:

1) What AEM version are you on?

2) What happens when you check your servlet in Adobe Experience Manager Web Console - Sling Servlet Resolver (http://localhost:4502/system/console/servletresolver)

3) Do you get to the Thank You Page with this submit action ?

Mayank_Gandhi
Employee
August 12, 2022

@rakesh443 where is your rest endpoint hosted? and on what web server?

rakesh443Author
New Participant
August 12, 2022

endpoint point will be the servlet in the same server where AEM is hosted.

right now I am testing in the my local instance

endpoint : localhost:4502/bin/myServlet

Employee
August 12, 2022
Pulkit_Jain_
Employee
August 12, 2022

@rakesh443 

I have seen this issue earlier, so asking a couple of additional questions:

1. Are you seeing this issue with the "Submit to REST endpoint with POST" submission ONLY?

2. Can you replicate the issue with a direct author/publish URL?

 

Please share the error log for this attempt for a quick review. Just a hunch, are you seeing "org.apache.http.ProtocolException: Target host is not specified" in the logs?

 

rakesh443Author
New Participant
August 12, 2022

Yes I am getting error only I use "Submit to REST endpoint with POST" by checking the check box

 

Error Log:

12.08.2022 18:49:50.150 *ERROR* [[0:0:0:0:0:0:0:1] [1660310389726] POST /content/forms/af/finalform/jcr:content/guideContainer.af.submit.jsp HTTP/1.1] com.adobe.aemds.guide.service.impl.RESTSubmitActionService Failed to make REST call
org.apache.http.client.ClientProtocolException: null
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187) [org.apache.httpcomponents.httpclient:4.5.12]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) [org.apache.httpcomponents.httpclient:4.5.12]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) [org.apache.httpcomponents.httpclient:4.5.12]

Pulkit_Jain_
Employee
August 12, 2022

@rakesh443 

Thanks for confirming! It's the same issue wherein the target host is missing.

1. Can you provide the complete URL in the endpoint - http://host:port/path_to_rest_end_point and 

2. Exclude the path_to_rest_end_point from the authentication requirement under the "Sling authentication service" under http://host:port/system/console/configMgr

 

Hope this helps!