From 7d350efa65732d492b5d752f3b713d4d216459b2 Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Wed, 11 Sep 2024 21:06:00 +0200 Subject: [PATCH] added new rest api --- office-alexander-logistics-app/pom.xml | 15 +- .../api/DataRestService.java | 193 ++++++++++++++++++ .../rest/TestDataClient.java | 93 +++++++++ reports/excel/Steuerbescheide.imixs-report | 134 ++++++++++++ 4 files changed, 434 insertions(+), 1 deletion(-) create mode 100644 office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/DataRestService.java create mode 100644 office-alexander-logistics-app/src/test/java/com/alexanderlogistics/rest/TestDataClient.java create mode 100644 reports/excel/Steuerbescheide.imixs-report diff --git a/office-alexander-logistics-app/pom.xml b/office-alexander-logistics-app/pom.xml index 7691d1d..f164950 100644 --- a/office-alexander-logistics-app/pom.xml +++ b/office-alexander-logistics-app/pom.xml @@ -368,7 +368,7 @@ org.glassfish.jersey.core jersey-client - 2.30.1 + 3.1.2 test @@ -377,6 +377,19 @@ 3.3.6 test + + org.glassfish.jersey.inject + jersey-hk2 + 3.1.2 + test + + + + jakarta.ws.rs + jakarta.ws.rs-api + 3.1.0 + test + \ No newline at end of file diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/DataRestService.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/DataRestService.java new file mode 100644 index 0000000..c28e7c3 --- /dev/null +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/api/DataRestService.java @@ -0,0 +1,193 @@ +/* + * Imixs-Workflow + * + * Copyright (C) 2001-2020 Imixs Software Solutions GmbH, + * http://www.imixs.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You can receive a copy of the GNU General Public + * License at http://www.gnu.org/licenses/gpl.html + * + * Project: + * https://www.imixs.org + * https://github.com/imixs/imixs-workflow + * + * Contributors: + * Imixs Software Solutions GmbH - Project Management + * Ralph Soika - Software Developer + */ + +package com.alexanderlogistics.api; + +import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.logging.Logger; + +import org.imixs.melman.CookieAuthenticator; +import org.imixs.melman.DocumentClient; +import org.imixs.melman.RestAPIException; +import org.imixs.workflow.ItemCollection; +import org.imixs.workflow.exceptions.QueryException; + +import jakarta.annotation.Resource; +import jakarta.ejb.SessionContext; +import jakarta.ejb.Stateless; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.QueryParam; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.Cookie; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +/** + * The api endpoint '/data' provides methods to read and post data via a access + * token + * + * @version 1.0 + * @author rsoika + * + */ +@Path("/data") +@Stateless +public class DataRestService { + + @Context + private HttpServletRequest servletRequest; + + @Context + HttpServletResponse servletResponse; + + @Resource + private SessionContext ctx; + + private static Logger logger = Logger.getLogger(DataRestService.class.getName()); + + @GET + @Path("/ping") + public String ping() { + logger.info("GET ping"); + return "ping: " + System.currentTimeMillis(); + } + + /** + * Test method + * + * @param workflowgroup + * @param task + * @return + * @throws QueryException + */ + @GET + @Path("/report/{name}") + @Produces({ MediaType.TEXT_HTML }) + public Response getReportData(@PathParam("name") String name, @QueryParam("token") String token) + throws QueryException { + + logger.info("Report Name=" + name); + String baseURI = servletRequest.getRequestURL().toString(); + + baseURI = baseURI.substring(0, baseURI.indexOf("/data/report/" + name)); + logger.info("Base URL=" + baseURI); + Client client = createClient(baseURI, token); + + if (client == null) { + return Response.ok().status(Response.Status.FORBIDDEN).build(); + } + // Specify the target URL + String url = baseURI + "/report/" + name + ".html"; + WebTarget target = client.target(url); + Response response = target.request().get(); + + // Check the response status and process the response + if (response.getStatus() == 200) { + String result = response.readEntity(String.class); + + return Response.ok(result).build(); + } + + return Response.ok().status(Response.Status.BAD_REQUEST).build(); + } + + /** + * creates a new Instance of a WorkflowClient. + *

+ * In case the current request contains an authentication token we propagate + * this token to the service. + *

+ * The method tests the access. If it is not possible the method returns null. + * + * @see DefaultAuthenicator + * @return + */ + protected Client createClient(String serviceAPI, String sessionID) { + + // Create a Cookie object with a name and a value + Cookie cookie = new Cookie.Builder("JSESSIONID") + .value(sessionID) + .path("/") + // .domain("domain.com") + .build(); + + DocumentClient client = new DocumentClient(serviceAPI.trim()); + CookieAuthenticator aut = new CookieAuthenticator(cookie); + client.registerClientRequestFilter(aut); + + Client rsClient = client.newClient(); + rsClient.register(aut); + + // Test access + String url = client.getBaseURI() + "documents/"; + WebTarget target = rsClient.target(url); + Response response = target.request().get(); + if (response.getStatus() == 200) { + String result = response.readEntity(String.class); + if (result.contains("j_password")) { + logger.info("Request failed - login failed"); + return null; + } + } else { + logger.info("Request failed with status code: " + response.getStatus()); + return null; + } + + return rsClient; + } + + /** + * Searches a report by name + * + * @param name + * @return + * @throws UnsupportedEncodingException + * @throws RestAPIException + */ + protected ItemCollection findReport(DocumentClient client, String name) + throws RestAPIException, UnsupportedEncodingException { + String query = "(type:\"ReportEntity\" AND txtname:\"" + name + "\")"; + List result = client.searchDocuments(query); + + if (result != null && result.size() > 0) { + // return first match + return result.get(0); + } + + return null; + } + +} diff --git a/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/rest/TestDataClient.java b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/rest/TestDataClient.java new file mode 100644 index 0000000..984c86e --- /dev/null +++ b/office-alexander-logistics-app/src/test/java/com/alexanderlogistics/rest/TestDataClient.java @@ -0,0 +1,93 @@ +package com.alexanderlogistics.rest; + +import java.util.logging.Logger; + +import org.imixs.melman.CookieAuthenticator; +import org.imixs.melman.DocumentClient; +import org.imixs.workflow.exceptions.PluginException; +import org.junit.Before; +import org.junit.Test; + +import com.oracle.truffle.api.library.ExportMessage.Ignore; + +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Cookie; +import jakarta.ws.rs.core.Response; + +/** + * Test to request data via a JSESSIONID only.... + * This technique is used by the DataRestService + * + */ +@Ignore +public class TestDataClient { + + private static Logger logger = Logger.getLogger(TestDataClient.class.getName()); + + DocumentClient client = null; + Client rsClient = null; + + String restAPI = "https://alexander-logistics.office-workflow.de/api"; + String sessionID = "rMxsrGJwT2GOisKrjEcaI5kpny_DnWieEPbIUvH2.imixs-office-workflow-8c9795689-d62t5"; + String reportName = "Steuerbescheide"; + + @Before + public void setup() throws PluginException { + // Create a Cookie object with a name and a value + Cookie cookie = new Cookie.Builder("JSESSIONID") + .value(sessionID) + .path("/") + .domain("alexander-logistics.office-workflow.de") + .build(); + + client = new DocumentClient(restAPI.trim()); + CookieAuthenticator aut = new CookieAuthenticator(cookie); + client.registerClientRequestFilter(aut); + rsClient = client.newClient(); + rsClient.register(aut); + } + + /** + * Test to read data.... + * + */ + @Test + public void testGetReportData() { + + // Test access + String url = client.getBaseURI() + "documents/"; + logger.info("send request " + url); + WebTarget target = rsClient.target(url); + // Send a GET request and get the response + Response response = target.request().get(); + + // Check the response status and process the response + if (response.getStatus() == 200) { + String result = response.readEntity(String.class); + if (result.contains("j_password")) { + System.out.println("login failed"); + } + System.out.println("Response: " + result); + } else { + System.out.println("Request failed with status code: " + response.getStatus()); + } + + // Specify the target URL + url = client.getBaseURI() + "report/Steuerbescheide.html?pageSize=100"; // + reportName; + logger.info("send request " + url); + target = rsClient.target(url); + // Send a GET request and get the response + response = target.request().get(); + + // Check the response status and process the response + if (response.getStatus() == 200) { + String result = response.readEntity(String.class); + System.out.println("Response: " + result); + } else { + System.out.println("Request failed with status code: " + response.getStatus()); + } + + } + +} diff --git a/reports/excel/Steuerbescheide.imixs-report b/reports/excel/Steuerbescheide.imixs-report new file mode 100644 index 0000000..ebba4f7 --- /dev/null +++ b/reports/excel/Steuerbescheide.imixs-report @@ -0,0 +1,134 @@ + + + + + 2024-09-10T14:40:40.689+02:00 + + + + + + true + + + 2024-09-10T15:05:09.442+02:00 + + + 1 + + + + + + 20837c95-4f6d-4dc6-9767-39f9006c3177-1725973509454 + + + 20837c95-4f6d-4dc6-9767-39f9006c3177 + + + 10 + + + + $created + Erfasst am + + <format locale="DE">dd.MM.yyyy</format> + + + + invoice.number + Rech.Nr. + + + + + + invoice.date + Belegdatum + + <format locale="DE">dd.MM.yyyy</format> + + + + space.name + Abteilung + + + + + + invoice.total.net + Betrag + xs:decimal + + + + + invoice.saldo + Saldo + xs:decimal + + + + + invoice.atc.number + ATC Nr. + + + + + + invoice.booking_date + Buchungsdatum + + + + + + invoice.text + Positionsnummer + + + + + + _childitems~activity.type + Art + + + + + + _childitems~amount + Betrag + xs:decimal + + + + + + application/vnd.ms-excel + + + UTF-8 + + + Rechnungs Export für Mandanten + + + Steuerbescheide + + + (type:"workitem") + AND ($modelversion:approval* OR $modelversion:steuerbescheid*) + + + + ReportEntity + + + + + \ No newline at end of file