Skip to content

Commit

Permalink
[25458] replace old blue medication swagger api client with new openapi
Browse files Browse the repository at this point in the history
  • Loading branch information
huthomas committed Aug 23, 2023
1 parent ed35ee1 commit 3b32025
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 150 deletions.
13 changes: 8 additions & 5 deletions bundles/at.medevit.elexis.bluemedication.core/.classpath
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="lib/kotlin-stdlib-1.6.20.jar"/>
<classpathentry exported="true" kind="lib" path="lib/kotlin-stdlib-common-1.5.31.jar"/>
<classpathentry exported="true" kind="lib" path="lib/kotlin-stdlib-jdk8-1.6.10.jar"/>
<classpathentry exported="true" kind="lib" path="lib/okio-jvm-3.0.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/gson-2.9.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/gson-fire-1.8.3.jar"/>
<classpathentry exported="true" kind="lib" path="lib/okhttp-4.10.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/openapi-java-client-1.7.0.jar" sourcepath="lib/openapi-java-client-1.7.0-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="lib/gson-fire-1.8.3.jar"/>
<classpathentry exported="true" kind="lib" path="lib/okhttp-2.7.5.jar"/>
<classpathentry exported="true" kind="lib" path="lib/okio-1.6.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/swagger-java-client-1.0.0.jar" sourcepath="lib/swagger-java-client-1.0.0-sources.jar"/>
<classpathentry exported="true" kind="lib" path="lib/threetenbp-1.3.5.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
19 changes: 11 additions & 8 deletions bundles/at.medevit.elexis.bluemedication.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ Require-Bundle: ch.rgw.utility;bundle-version="3.9.100",
org.apache.httpcomponents.httpclient;bundle-version="4.5.10",
org.apache.httpcomponents.httpcore;bundle-version="4.4.12",
com.google.gson;bundle-version="2.8.6"
Bundle-ClassPath: lib/gson-fire-1.8.3.jar,
lib/okhttp-2.7.5.jar,
lib/okio-1.6.0.jar,
lib/swagger-java-client-1.0.0.jar,
lib/threetenbp-1.3.5.jar,
.
Bundle-ClassPath: .,
lib/openapi-java-client-1.7.0.jar,
lib/gson-2.9.1.jar,
lib/gson-fire-1.8.3.jar,
lib/okhttp-4.10.0.jar,
lib/okio-jvm-3.0.0.jar,
lib/kotlin-stdlib-1.6.20.jar,
lib/kotlin-stdlib-common-1.5.31.jar,
lib/kotlin-stdlib-jdk8-1.6.10.jar
Export-Package: at.medevit.elexis.bluemedication.core
Service-Component: OSGI-INF/at.medevit.elexis.bluemedication.core.internal.BlueMedicationServiceImpl.xml,
OSGI-INF/at.medevit.elexis.bluemedication.core.BlueMedicationServiceHolder.xml
Bundle-Vendor: medevit.at
Service-Component: OSGI-INF/at.medevit.elexis.bluemedication.core.BlueMedicationServiceHolder.xml,
OSGI-INF/at.medevit.elexis.bluemedication.core.internal.BlueMedicationServiceImpl.xml
10 changes: 9 additions & 1 deletion bundles/at.medevit.elexis.bluemedication.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ output.. = bin/
bin.includes = META-INF/,\
.,\
lib/,\
OSGI-INF/
OSGI-INF/,\
lib/openapi-java-client-1.7.0.jar,\
lib/gson-2.9.1.jar,\
lib/gson-fire-1.8.3.jar,\
lib/okhttp-4.10.0.jar,\
lib/okio-jvm-3.0.0.jar,\
lib/kotlin-stdlib-1.6.20.jar,\
lib/kotlin-stdlib-common-1.5.31.jar,\
lib/kotlin-stdlib-jdk8-1.6.10.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,4 @@
public class BlueMedicationConstants {
public static final String CFG_URL_STAGING = "at.medevit.elexis.emediplan/bluemedication/staging"; //$NON-NLS-1$
public static final String CFG_USE_IMPORT = "at.medevit.elexis.emediplan/bluemedication/useimport"; //$NON-NLS-1$

public static final String CFG_HIN_PROXY_HOST = "at.medevit.elexis.emediplan/hin/proxy.host"; //$NON-NLS-1$
public static final String CFG_HIN_PROXY_PORT = "at.medevit.elexis.emediplan/hin/proxy.port"; //$NON-NLS-1$

public static final String DEFAULT_HIN_PROXY_HOST = "localhost"; //$NON-NLS-1$
public static final String DEFAULT_HIN_PROXY_PORT = "5016"; //$NON-NLS-1$
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.message.BasicHeader;
import org.openapitools.client.ApiClient;
import org.openapitools.client.ApiException;
import org.openapitools.client.ApiResponse;
import org.openapitools.client.api.EMediplanGenerationApi;
import org.openapitools.client.api.ExtractionAndConsolidationApi;
import org.openapitools.client.api.MediCheckApi;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -34,12 +42,8 @@
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.threeten.bp.LocalDate;

import com.google.gson.Gson;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import at.medevit.ch.artikelstamm.IArtikelstammItem;
import at.medevit.elexis.bluemedication.core.BlueMedicationConstants;
Expand All @@ -59,12 +63,8 @@
import ch.elexis.core.utils.OsgiServiceUtil;
import ch.rgw.tools.Result;
import ch.rgw.tools.Result.SEVERITY;
import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.ApiResponse;
import io.swagger.client.api.EMediplanGenerationApi;
import io.swagger.client.api.ExtractionAndConsolidationApi;
import io.swagger.client.api.MediCheckApi;
import okhttp3.OkHttpClient;
import okhttp3.Response;

@Component(property = EventConstants.EVENT_TOPIC + "=" + ElexisEventTopics.BASE + "emediplan/ui/create")
public class BlueMedicationServiceImpl implements BlueMedicationService, EventHandler {
Expand Down Expand Up @@ -97,46 +97,12 @@ private void initProxyOrOauth() {
if(hinAuthService == null) {
hinAuthService = OsgiServiceUtil.getService(IHinAuthService.class);
}
if (!hinAuthService.isPresent()) {
if (!proxyActive) {
// get proxy settings and store old values
Properties systemSettings = System.getProperties();
oldProxyHost = systemSettings.getProperty("http.proxyHost"); //$NON-NLS-1$
oldProxyPort = systemSettings.getProperty("http.proxyPort"); //$NON-NLS-1$

// set new values
systemSettings.put("http.proxyHost", ConfigServiceHolder.get().getLocal( //$NON-NLS-1$
BlueMedicationConstants.CFG_HIN_PROXY_HOST, BlueMedicationConstants.DEFAULT_HIN_PROXY_HOST));
systemSettings.put("http.proxyPort", ConfigServiceHolder.get().getLocal( //$NON-NLS-1$
BlueMedicationConstants.CFG_HIN_PROXY_PORT, BlueMedicationConstants.DEFAULT_HIN_PROXY_PORT));
System.setProperties(systemSettings);
proxyActive = true;
}
}
}

/**
* Reset the proxy values in the system properties.
*/
private void deInitProxy() {
if (proxyActive) {
Properties systemSettings = System.getProperties();
if (oldProxyHost != null) {
systemSettings.put("http.proxyHost", oldProxyHost); //$NON-NLS-1$
}
if (oldProxyPort != null) {
systemSettings.put("http.proxyPort", oldProxyPort); //$NON-NLS-1$
}
System.setProperties(systemSettings);
proxyActive = false;
}
}

@Override
public Result<UploadResult> uploadDocument(IPatient patient, File document, String resulttyp) {
initProxyOrOauth();
workaroundGet();
try {
ExtractionAndConsolidationApi apiInstance = new ExtractionAndConsolidationApi();
configureApiClient(apiInstance.getApiClient());

Expand Down Expand Up @@ -168,8 +134,9 @@ public Result<UploadResult> uploadDocument(IPatient patient, File document, Stri
}
}
}
ApiResponse<?> response = apiInstance.dispatchPostWithHttpInfo(internalData, externalData,
patientFirstName, patientLastName, patientSex, patientBirthdate, StringUtils.EMPTY,
ApiResponse<?> response = apiInstance.dispatchPostWithHttpInfo(externalData, internalData,
patientFirstName, patientLastName, patientSex,
DateTimeFormatter.ofPattern("dd.MM.yyyy").format(patientBirthdate), StringUtils.EMPTY,
StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY);
if (response.getStatusCode() >= 300) {
return new Result<UploadResult>(SEVERITY.ERROR, 0,
Expand All @@ -180,7 +147,7 @@ public Result<UploadResult> uploadDocument(IPatient patient, File document, Stri
}
// successful upload
@SuppressWarnings("unchecked")
io.swagger.client.model.UploadResult data = ((ApiResponse<io.swagger.client.model.UploadResult>) response)
org.openapitools.client.model.UploadResult data = ((ApiResponse<org.openapitools.client.model.UploadResult>) response)
.getData();
return new Result<UploadResult>(
new UploadResult(appendPath(getBasePath(), data.getUrl() + "&mode=embed"), data.getId(),
Expand All @@ -190,8 +157,8 @@ public Result<UploadResult> uploadDocument(IPatient patient, File document, Stri
// error result code should be evaluated
try {
Gson gson = new Gson();
io.swagger.client.model.ErrorResult[] mcArray = gson.fromJson(e.getResponseBody(),
io.swagger.client.model.ErrorResult[].class);
org.openapitools.client.model.ErrorResult[] mcArray = gson.fromJson(e.getResponseBody(),
org.openapitools.client.model.ErrorResult[].class);
if (mcArray != null && mcArray.length > 0) {
return new Result<UploadResult>(SEVERITY.ERROR, 0,
"Error result code [" + mcArray[0].getCode() + "]", null, false);
Expand All @@ -203,9 +170,6 @@ public Result<UploadResult> uploadDocument(IPatient patient, File document, Stri
logger.error("Error uploading Document", e);
return new Result<UploadResult>(SEVERITY.ERROR, 0, e.getMessage(), null, false);
}
} finally {
deInitProxy();
}
}

private void configureApiClient(ApiClient client) {
Expand All @@ -224,25 +188,13 @@ private class CheckApiClient extends ApiClient {
private String redirectUrl;

public CheckApiClient() {
super();
// do not follow redirects
getHttpClient().setFollowRedirects(false);
getHttpClient().setFollowSslRedirects(false);
super(new OkHttpClient.Builder().followRedirects(false).followSslRedirects(false).build());
}

public String selectHeaderContentType(String[] contentTypes) {
return "application/x-chmed16a";
};

@Override
public RequestBody serialize(Object obj, String contentType) throws ApiException {
if (obj instanceof String) {
return RequestBody.create(MediaType.parse(contentType), (String) obj);
} else {
throw new ApiException("Content type \"" + contentType + "\" is not supported");
}
}

@Override
public <T> T handleResponse(Response response, Type returnType) throws ApiException {
if (response.code() == 302) {
Expand All @@ -269,10 +221,12 @@ public Result<UploadResult> uploadCheck(IPatient patient) {
IMandator mandant = ContextServiceHolder.get().getActiveMandator().orElse(null);
if (mandant != null) {
try {
ByteArrayOutputStream jsonOutput = new ByteArrayOutputStream();
eMediplanService.exportEMediplanChmed(mandant, patient, getPrescriptions(patient, "all"), true,
jsonOutput);
apiInstance.checkPostWithHttpInfo(new String(jsonOutput.toByteArray(), "UTF-8"));
File tmpFile = File.createTempFile("bluemedication" + System.currentTimeMillis(), ".tmp");
try (FileOutputStream fout = new FileOutputStream(tmpFile)) {
eMediplanService.exportEMediplanChmed(mandant, patient, getPrescriptions(patient, "all"), true,
fout);
}
apiInstance.checkPostWithHttpInfo(tmpFile);
if (client.getRedirectUrl() != null) {
return new Result<UploadResult>(
new UploadResult(client.getRedirectUrl(), StringUtils.EMPTY, "check", true));
Expand All @@ -291,8 +245,8 @@ public Result<UploadResult> uploadCheck(IPatient patient) {
// error result code should be evaluated
try {
Gson gson = new Gson();
io.swagger.client.model.ErrorResult[] mcArray = gson.fromJson(e.getResponseBody(),
io.swagger.client.model.ErrorResult[].class);
org.openapitools.client.model.ErrorResult[] mcArray = gson.fromJson(e.getResponseBody(),
org.openapitools.client.model.ErrorResult[].class);
if (mcArray != null && mcArray.length > 0) {
return new Result<UploadResult>(SEVERITY.ERROR, 0,
"Error result code [" + mcArray[0].getCode() + "]", null, false);
Expand All @@ -303,8 +257,6 @@ public Result<UploadResult> uploadCheck(IPatient patient) {
}
logger.error("Error uploading Document", e);
return new Result<UploadResult>(SEVERITY.ERROR, 0, e.getMessage(), null, false);
} finally {
deInitProxy();
}
}

Expand All @@ -322,15 +274,16 @@ public Result<String> emediplanNotification(IPatient patient) {
: null);

ApiResponse<?> response = apiInstance.notificationEmediplanPostWithHttpInfo(patient.getFirstName(),
patient.getLastName(), patient.getGender().name(), birthDate);
patient.getLastName(), patient.getGender().name(),
DateTimeFormatter.ofPattern("dd.MM.yyyy").format(birthDate));
return Result.OK(response.toString());
} catch (ApiException e) {
if (e.getCode() == 400 || e.getCode() == 422) {
// error result code should be evaluated
try {
Gson gson = new Gson();
io.swagger.client.model.ErrorResult[] mcArray = gson.fromJson(e.getResponseBody(),
io.swagger.client.model.ErrorResult[].class);
org.openapitools.client.model.ErrorResult[] mcArray = gson.fromJson(e.getResponseBody(),
org.openapitools.client.model.ErrorResult[].class);
if (mcArray != null && mcArray.length > 0) {
return new Result<String>(SEVERITY.ERROR, 0, "Error result code [" + mcArray[0].getCode() + "]",
null, false);
Expand All @@ -341,8 +294,6 @@ public Result<String> emediplanNotification(IPatient patient) {
}
logger.error("Error performing notification", e);
return new Result<String>(SEVERITY.ERROR, 0, e.getMessage(), null, false);
} finally {
deInitProxy();
}
}

Expand Down Expand Up @@ -423,8 +374,6 @@ public Result<String> downloadEMediplan(UploadResult uploadResult) {
} catch (ApiException e) {
logger.error("Error downloading Document", e);
return Result.ERROR(e.getMessage());
} finally {
deInitProxy();
}
}

Expand All @@ -434,8 +383,7 @@ public Result<String> downloadPdf(UploadResult uploadResult) {
try {
ExtractionAndConsolidationApi apiInstance = new ExtractionAndConsolidationApi();
configureApiClient(apiInstance.getApiClient());
ApiResponse<File> response = apiInstance
.downloadIdExtractionExtendedpdfGetWithHttpInfo(uploadResult.getId(), true);
ApiResponse<File> response = apiInstance.downloadIdExtractionGetWithHttpInfo(uploadResult.getId(), true);
if (response.getStatusCode() >= 300) {
return Result.ERROR("Response status code was [" + response.getStatusCode() + "]");
}
Expand All @@ -446,8 +394,6 @@ public Result<String> downloadPdf(UploadResult uploadResult) {
} catch (ApiException e) {
logger.error("Error downloading Document Pdf", e);
return Result.ERROR(e.getMessage());
} finally {
deInitProxy();
}
}

Expand Down Expand Up @@ -525,14 +471,22 @@ public void startPollForResult(final Object object, final UploadResult uploadRes
}
logger.info("Start polling for [" + uploadResult.getId() + "]");
// configure HIN proxy for apache http client
HttpHost proxy = new HttpHost(
ConfigServiceHolder.get().getLocal(BlueMedicationConstants.CFG_HIN_PROXY_HOST,
BlueMedicationConstants.DEFAULT_HIN_PROXY_HOST),
Integer.parseInt(ConfigServiceHolder.get().getLocal(BlueMedicationConstants.CFG_HIN_PROXY_PORT,
BlueMedicationConstants.DEFAULT_HIN_PROXY_PORT)),
"http");
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
HttpClient httpclient = HttpClients.custom().setRoutePlanner(routePlanner).build();
// HttpHost proxy = new HttpHost(
// ConfigServiceHolder.get().getLocal(BlueMedicationConstants.CFG_HIN_PROXY_HOST,
// BlueMedicationConstants.DEFAULT_HIN_PROXY_HOST),
// Integer.parseInt(ConfigServiceHolder.get().getLocal(BlueMedicationConstants.CFG_HIN_PROXY_PORT,
// BlueMedicationConstants.DEFAULT_HIN_PROXY_PORT)),
// "http");
// DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
Collection<BasicHeader> headers = new ArrayList<>();
if (hinAuthService.isPresent()) {
Optional<String> authToken = hinAuthService.get()
.getToken(Collections.singletonMap(IHinAuthService.TOKEN_GROUP, "BlueMedication"));
if (authToken.isPresent()) {
headers.add(new BasicHeader("Authorization", "Bearer " + authToken.get()));
}
}
HttpClient httpclient = HttpClients.custom().setDefaultHeaders(headers).build();

HttpGet httpget = new HttpGet(getAppBasePath() + "/status/" + uploadResult.getId());
int maxRetry = 30; // default timeout 30 sec -> 15 min.
Expand Down
Loading

0 comments on commit 3b32025

Please sign in to comment.