====== Java Integration Example ======
Example demostrates use of Web services for ADUCID (aka WSA) and Web integration API for ADUCID. Example is based on Servlet 3.0 specification and tested on Tomcat 9.0 application server.
===== Project Structure =====
Image below shows Java web application project structure:
{{:web-integration:example_ws_structure.png?nolink&300|}}
Let's describe project files:
* ''OpenCheckServlet.java'' - servlet running at ''/openCheck'' and processing authentication result
* ''OpenServlet.java'' - servlet running at ''/open'' and starting authentication operation
* ''ProxyServlet.java'' - servlet running at ''/proxy'' and checking authentication status
* ''running.gif'' - picture representing running authentication
* ''aducid-api.js'' - see [[web-integration:client-side|Client API for Web Integration]]
* ''jquery.min.js'' - required by ''aducid-api.js''
* ''web.xml'' - web application configuration file
* ''index.jsp'' - page starting authentication operation
* ''secure.jsp'' - page showing authentication result
* ''build.bat'' - batch file generating WS classes and building web application
* ''pom.xml'' - maven configuration file
===== Java Servlets =====
Use of [[web-integration:client-side|Client API for Web Integration]] requires preparation of 3 servlets.
==== Open ====
First one starts authentication process:
@WebServlet("/open")
public class OpenServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
PrintWriter out = response.getWriter();
// initialize client
URL url = new URL(getServletContext().getInitParameter("wsUrl"));
QName qname = new QName("http://impl.ws.sdk.aducid.com/", "AducidApiServiceImplService");
Service service = Service.create(url, qname);
AducidApiService client = service.getPort(AducidApiService.class);
try {
// generate unique browser tab identifier - necessary not to share session attributes
String id = UUID.randomUUID().toString();
// start operation
RequestOperationArguments arguments = new RequestOperationArguments();
arguments.setPeigReturnName(request.getRequestURL() + "Check?id=" + id); // required by iOS
arguments.setGenerateQrCodePicture(true);
arguments.setQrCodePictureWidth(300);
arguments.setQrCodePictureHeight(300);
RequestOperationResult result = client.startAuthenticationSession(arguments);
result.setId(id);
System.out.println("authId: " + result.getAuthId());
// save authId with id prefix to session
request.getSession().setAttribute(id + "_authId", result.getAuthId());
// success - send JSON response to integration API
out.print(client.toJson(result, null));
} catch (AducidClientException exception) {
// error - send JSON response to integration API
out.print(client.toErrorJson(exception.getFaultInfo().getResult(), "index.jsp"));
}
out.flush();
out.close();
}
}
Some minor notes to source code:
* way to get ''wsUrl'' is described in [[web-integration:java-example#web_application_configuration_file|Web Application Configuration File]] chapter
* code works with unique ''id'', we can start authentication in more than one browser tab, then ''authId'' value in session would be overwritten by last tab ''authId'' value and authentication in previous tabs would stop working
* value set in ''setPeigReturnName'' must be set to authentication result URL, here it is ''/openCheck'' with ''id'' query parameter
==== Proxy ====
This servlet asks for authentication operation status:
@WebServlet("/proxy")
public class ProxyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
PrintWriter out = response.getWriter();
// initialize client
URL url = new URL(getServletContext().getInitParameter("wsUrl"));
QName qname = new QName("http://impl.ws.sdk.aducid.com/", "AducidApiServiceImplService");
Service service = Service.create(url, qname);
AducidApiService client = service.getPort(AducidApiService.class);
try {
// read authId with id prefix from session
String authId = (String) request.getSession().getAttribute(request.getParameter("id") + "_authId");
// ask for operation status
String status = client.getStatus(authId);
System.out.println("status: " + status);
// success - send JSON response to integration API
out.print(client.toJson(status, null));
} catch (AducidClientException exception) {
// error - send JSON response to integration API
out.print(client.toErrorJson(exception.getFaultInfo().getResult(), "index.jsp"));
}
out.flush();
out.close();
}
}
Notes to source code:
* way to get ''wsUrl'' is described in [[web-integration:java-example#web_application_configuration_file|Web Application Configuration File]] chapter
==== Open Check ====
Servlet evaluating authentication operation result:
@WebServlet("/openCheck")
public class OpenCheckServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
PrintWriter out = response.getWriter();
// initialize client
URL url = new URL(getServletContext().getInitParameter("wsUrl"));
QName qname = new QName("http://impl.ws.sdk.aducid.com/", "AducidApiServiceImplService");
Service service = Service.create(url, qname);
AducidApiService client = service.getPort(AducidApiService.class);
try {
// read authId with id prefix from session
String authId = (String) request.getSession().getAttribute(request.getParameter("id") + "_authId");
// ask for operation result
GetPSLAttributesResponse result = client.getResult(authId, request.getParameter("authKey"));
System.out.println("status: " + result.getStatusAIM().name().toLowerCase());
System.out.println("authentication: " + result.getStatusAuth());
// save result to session
request.getSession().setAttribute("result", result);
// success - send JSON response to integration API
out.print(client.toJson(null, "secure.jsp"));
} catch (AducidClientException exception) {
// error - send JSON response to integration API
out.print(client.toErrorJson(exception.getFaultInfo().getResult(), "index.jsp"));
}
out.flush();
out.close();
}
}
Some notes to source code:
* way to get ''wsUrl'' is described in [[web-integration:java-example#web_application_configuration_file|Web Application Configuration File]] chapter
===== Web Integration =====
==== Start Page ====
Servlets described earlier are used in ''index.jsp'' page:
LOGIN PAGE
==== Result Page ====
After successful authentication, ''UDI'' value is printed at ''result.jsp'' page:
SECURE PAGE
UDI: ${result.userDatabaseIndex}
Try again
===== Web Application Configuration File =====
This file is required by Servlet API. File provides ''wsUrl'' configuration value:
Example WSwsUrl${ws.url}/aducid-client-api-ws/wsaindex.jsp
Value ''${ws.url}'' is replaced at application building phase. See [[web-integration:java-example#building_application|Building Application]] chapter.
===== Maven Configuration File =====
Maven configuration file contents definition of plugin to generate WS classes and instruction to filter ''${ws.url}'' value:
4.0.0com.aducid1.DEV-SNAPSHOTwarexample_wsdemo: example wsexample-wsorg.apache.maven.pluginsmaven-compiler-plugin3.8.11.8UTF-8truetrueorg.jvnet.jax-ws-commonsjaxws-maven-plugin2.3client${ws.url}/aducid-client-api-ws/wsa?wsdl-Djavax.xml.accessExternalSchema=allUTF-8wsimportorg.apache.maven.pluginsmaven-war-plugin3.2.2truesrc/main/webappWEB-INF/web.xml${project.build.sourceDirectory}sourcesjavax.servletjavax.servlet-api3.0.1provided
===== Building Application =====
There is prepared batch file ''build.bat'' to build application. It is neccessary to define right ''ws.url'' value as an address to AIM server:
set MAVEN_OPTS="-Xmx512m"
mvn -Dws.url=https://myaim.aducid.com -Dfile.encoding=UTF-8 clean install