added new rest api

This commit is contained in:
Ralph Soika 2024-09-11 21:06:00 +02:00
parent c7d9fcc212
commit 7d350efa65
4 changed files with 434 additions and 1 deletions

View file

@ -368,7 +368,7 @@
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.30.1</version>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
@ -377,6 +377,19 @@
<version>3.3.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>3.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View file

@ -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.
* <p>
* In case the current request contains an authentication token we propagate
* this token to the service.
* <p>
* 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<ItemCollection> result = client.searchDocuments(query);
if (result != null && result.size() > 0) {
// return first match
return result.get(0);
}
return null;
}
}

View file

@ -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());
}
}
}

View file

@ -0,0 +1,134 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<item name="$created">
<value xsi:type="xs:dateTime">2024-09-10T14:40:40.689+02:00</value>
</item>
<item name="$file">
<value xsi:nil="true" />
</item>
<item name="$isauthor">
<value xsi:type="xs:boolean">true</value>
</item>
<item name="$modified">
<value xsi:type="xs:dateTime">2024-09-10T15:05:09.442+02:00</value>
</item>
<item name="$snapshot.history">
<value xsi:type="xs:int">1</value>
</item>
<item name="$snapshot.overwritefilecontent">
<value xsi:type="xs:string"></value>
</item>
<item name="$snapshotid">
<value xsi:type="xs:string">20837c95-4f6d-4dc6-9767-39f9006c3177-1725973509454</value>
</item>
<item name="$uniqueid">
<value xsi:type="xs:string">20837c95-4f6d-4dc6-9767-39f9006c3177</value>
</item>
<item name="$version">
<value xsi:type="xs:int">10</value>
</item>
<item name="attributes">
<value xsi:type="xmlItem">
<value xsi:type="xs:string">$created</value>
<value xsi:type="xs:string">Erfasst am</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string">&lt;format locale="DE"&gt;dd.MM.yyyy&lt;/format&gt;</value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">invoice.number</value>
<value xsi:type="xs:string">Rech.Nr.</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">invoice.date</value>
<value xsi:type="xs:string">Belegdatum</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string">&lt;format locale="DE"&gt;dd.MM.yyyy&lt;/format&gt;</value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">space.name</value>
<value xsi:type="xs:string">Abteilung</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">invoice.total.net</value>
<value xsi:type="xs:string">Betrag</value>
<value xsi:type="xs:string">xs:decimal</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">invoice.saldo</value>
<value xsi:type="xs:string">Saldo</value>
<value xsi:type="xs:string">xs:decimal</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">invoice.atc.number</value>
<value xsi:type="xs:string">ATC Nr.</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">invoice.booking_date</value>
<value xsi:type="xs:string">Buchungsdatum</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">invoice.text</value>
<value xsi:type="xs:string">Positionsnummer</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">_childitems~activity.type</value>
<value xsi:type="xs:string">Art</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
<value xsi:type="xmlItem">
<value xsi:type="xs:string">_childitems~amount</value>
<value xsi:type="xs:string">Betrag</value>
<value xsi:type="xs:string">xs:decimal</value>
<value xsi:type="xs:string"></value>
<value xsi:type="xs:string"></value>
</value>
</item>
<item name="contenttype">
<value xsi:type="xs:string">application/vnd.ms-excel</value>
</item>
<item name="encoding">
<value xsi:type="xs:string">UTF-8</value>
</item>
<item name="txtdescription">
<value xsi:type="xs:string">Rechnungs Export für Mandanten</value>
</item>
<item name="txtname">
<value xsi:type="xs:string">Steuerbescheide</value>
</item>
<item name="txtquery">
<value xsi:type="xs:string">(type:"workitem")&#13;
AND ($modelversion:approval* OR $modelversion:steuerbescheid*)&#13;
</value>
</item>
<item name="type">
<value xsi:type="xs:string">ReportEntity</value>
</item>
<item name="xslresource">
<value xsi:type="xs:string"></value>
</item>
</document>