-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #443 from FraunhoferISST/feat/erpadapter_request
Feat/erpadapter request client
- Loading branch information
Showing
18 changed files
with
545 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../src/main/java/org/eclipse/tractusx/puris/backend/erpadapter/ErpAdapterConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.eclipse.tractusx.puris.backend.erpadapter; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@Getter | ||
public class ErpAdapterConfiguration { | ||
|
||
/** | ||
* Toggles usage of the ERP adapter | ||
*/ | ||
@Value("${puris.erpadapter.enabled}") | ||
private boolean erpAdapterEnabled; | ||
|
||
/** | ||
* The URL of the ERP adapter | ||
*/ | ||
@Value("${puris.erpadapter.url}") | ||
private String erpAdapterUrl; | ||
|
||
/** | ||
* The URL under which we expect responses from | ||
* the ERP adapter | ||
*/ | ||
@Value("${puris.baseurl}" + "${server.servlet.context-path}" + "/erp-adapter") | ||
private String erpResponseUrl; | ||
|
||
/** | ||
* The auth-key used when accessing the ERP adapter's | ||
* request interface | ||
*/ | ||
@Value("${puris.erpadapter.authkey}") | ||
private String erpAdapterAuthKey; | ||
|
||
/** | ||
* The auth-secret used when accessing the ERP adapter's | ||
* request interface | ||
*/ | ||
@Value("${puris.erpadapter.authsecret}") | ||
private String erpAdapterAuthSecret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
.../org/eclipse/tractusx/puris/backend/erpadapter/logic/service/ErpAdapterRequestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2024 Volkswagen AG | ||
* Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.eclipse.tractusx.puris.backend.erpadapter.logic.service; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import okhttp3.*; | ||
import org.eclipse.tractusx.puris.backend.erpadapter.ErpAdapterConfiguration; | ||
import org.eclipse.tractusx.puris.backend.erpadapter.domain.model.ErpAdapterRequest; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class ErpAdapterRequestClient { | ||
|
||
private final OkHttpClient client = new OkHttpClient(); | ||
|
||
private final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
private final ErpAdapterConfiguration erpAdapterConfiguration; | ||
|
||
public Integer sendRequest(ErpAdapterRequest erpAdapterRequest){ | ||
HttpUrl.Builder urlBuilder = HttpUrl.parse(erpAdapterConfiguration.getErpAdapterUrl()).newBuilder(); | ||
urlBuilder.addQueryParameter("bpnl", erpAdapterRequest.getPartnerBpnl()); | ||
urlBuilder.addQueryParameter("request-type", erpAdapterRequest.getRequestType()); | ||
urlBuilder.addQueryParameter("request-id", erpAdapterRequest.getId().toString()); | ||
urlBuilder.addQueryParameter("samm-version", erpAdapterRequest.getSammVersion()); | ||
urlBuilder.addQueryParameter("request-timestamp", String.valueOf(erpAdapterRequest.getRequestDate().getTime())); | ||
|
||
ObjectNode requestBody = mapper.createObjectNode(); | ||
|
||
requestBody.put("material", erpAdapterRequest.getOwnMaterialNumber()); | ||
requestBody.put("direction", erpAdapterRequest.getDirectionCharacteristic().toString()); | ||
requestBody.put("responseUrl", erpAdapterConfiguration.getErpResponseUrl()); | ||
|
||
RequestBody body = RequestBody.create(requestBody.toString(), MediaType.parse("application/json")); | ||
|
||
Request request = new Request.Builder() | ||
.post(body) | ||
.url(urlBuilder.build()) | ||
.header(erpAdapterConfiguration.getErpAdapterAuthKey(), erpAdapterConfiguration.getErpAdapterAuthSecret()) | ||
.header("Content-Type", "application/json") | ||
.build(); | ||
try (var response = client.newCall(request).execute()) { | ||
return response.code(); | ||
} catch (IOException e) { | ||
log.error("Error while sending ErpAdapterRequest", e); | ||
return null; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.