Skip to content

Commit

Permalink
MODINV-1134 Missing x-okapi-user-id header in communications with inv…
Browse files Browse the repository at this point in the history
…entory-storage (#792)

* MODINV-1134 Missing x-okapi-user-id header in communications with inventory-storage

* upd contextConstruct

* upd contextConstruct
  • Loading branch information
JavokhirAbdullayev authored Dec 19, 2024
1 parent 64bda1c commit 01f58cd
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 50 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 21.1.0-SNAPSHOT 2024-xx-xx
* Provide consistent handling with concurrency two or more Marc Bib Update events for the same bib record [MODINV-1100](https://folio-org.atlassian.net/browse/MODINV-1100)

* Missing x-okapi-user-id header in communications with inventory-storage [MODINV-1134](https://folio-org.atlassian.net/browse/MODINV-1134)
## 21.0.0 2024-10-29
* Existing "035" field is not retained the original position in imported record [MODINV-1049](https://folio-org.atlassian.net/browse/MODINV-1049)
* Update Data Import logic to normalize OCLC 035 values [MODINV-949](https://folio-org.atlassian.net/browse/MODINV-949)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/folio/inventory/client/OrdersClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public CompletableFuture<Optional<JsonArray>> getPoLineCollection(String cql, Co
@SneakyThrows
private OkapiHttpClient createOkapiHttpClient(Context context) {
return new OkapiHttpClient(webClient, new URL(context.getOkapiLocation()),
context.getTenantId(), context.getToken(), null, null, null);
context.getTenantId(), context.getToken(), context.getUserId(), context.getRequestId(), null);
}
}
1 change: 1 addition & 0 deletions src/main/java/org/folio/inventory/common/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public interface Context {
String getToken();
String getOkapiLocation();
String getUserId();
String getRequestId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class MessagingContext implements Context {
public static final String TOKEN = "token";
public static final String OKAPI_LOCATION = "okapiLocation";
public static final String JOB_ID = "jobId";
public static final String USER_ID = "userId";
public static final String REQUEST_ID = "requestId";

private final MultiMap headers;

Expand All @@ -32,7 +34,12 @@ public String getOkapiLocation() {

@Override
public String getUserId() {
return null;
return getHeader(USER_ID);
}

@Override
public String getRequestId() {
return getHeader(REQUEST_ID);
}

private String getHeader(String header) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/folio/inventory/common/WebContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public String getUserId() {
return getHeader(OKAPI_USER_ID_HEADER, "");
}

@Override
public String getRequestId() {
return getHeader(OKAPI_REQUEST_ID);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class MarcBibUpdateKafkaHandler implements AsyncRecordHandler<String, Str
private static final String MAPPING_PARAMS_KEY = "MAPPING_PARAMS";
private static final String CURRENT_RETRY_NUMBER = "CURRENT_RETRY_NUMBER";
private static final String OKAPI_USER_ID = "x-okapi-user-id";
private static final String OKAPI_REQUEST_ID = "x-okapi-request-id";
private static final int MAX_RETRIES_COUNT = Integer.parseInt(System.getenv().getOrDefault("inventory.di.ol.retry.number", "1"));

private final InstanceUpdateDelegate instanceUpdateDelegate;
Expand Down Expand Up @@ -90,7 +91,7 @@ public Future<String> handle(KafkaConsumerRecord<String, String> consumerRecord)
LOGGER.error(message);
return Future.failedFuture(message);
}
Context context = EventHandlingUtil.constructContext(instanceEvent.getTenant(), headersMap.get(OKAPI_TOKEN_HEADER), headersMap.get(OKAPI_URL_HEADER), headersMap.get(OKAPI_USER_ID));
Context context = EventHandlingUtil.constructContext(instanceEvent.getTenant(), headersMap.get(OKAPI_TOKEN_HEADER), headersMap.get(OKAPI_URL_HEADER), headersMap.get(OKAPI_USER_ID), headersMap.get(OKAPI_REQUEST_ID));
Record marcBibRecord = instanceEvent.getRecord();

io.vertx.core.Context vertxContext = Vertx.currentContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public final class EventHandlingUtil {
private EventHandlingUtil() {}

public static Context constructContext(String tenantId, String token, String okapiUrl) {
return constructContext(tenantId, token, okapiUrl, null);
return constructContext(tenantId, token, okapiUrl, null, null);
}

public static Context constructContext(String tenantId, String token, String okapiUrl, String userId) {
public static Context constructContext(String tenantId, String token, String okapiUrl, String userId, String requestId) {
return new Context() {
@Override
public String getTenantId() {
Expand All @@ -44,6 +44,11 @@ public String getOkapiLocation() {
public String getUserId() {
return Optional.ofNullable(userId).orElse("");
}

@Override
public String getRequestId() {
return Optional.ofNullable(requestId).orElse("");
}
};
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/folio/inventory/domain/CollectionProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import org.folio.inventory.domain.user.UserCollection;

public interface CollectionProvider {
ItemCollection getItemCollection(String tenantId, String token);
HoldingsRecordCollection getHoldingsRecordCollection(String tenantId, String token);
InstanceCollection getInstanceCollection(String tenantId, String token);
AuthorityRecordCollection getAuthorityCollection(String tenantId, String token);
UserCollection getUserCollection(String tenantId, String token);
HoldingsRecordsSourceCollection getHoldingsRecordsSourceCollection(String tenantId, String token);
ItemCollection getItemCollection(String tenantId, String token, String userId, String requestId);
HoldingsRecordCollection getHoldingsRecordCollection(String tenantId, String token, String userId, String requestId);
InstanceCollection getInstanceCollection(String tenantId, String token, String userId, String requestId);
AuthorityRecordCollection getAuthorityCollection(String tenantId, String token, String userId, String requestId);
UserCollection getUserCollection(String tenantId, String token, String userId, String requestId);
HoldingsRecordsSourceCollection getHoldingsRecordsSourceCollection(String tenantId, String token, String userId, String requestId);
}
12 changes: 6 additions & 6 deletions src/main/java/org/folio/inventory/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,32 @@ public static Storage basedUpon(JsonObject config, HttpClient client) {

public ItemCollection getItemCollection(Context context) {
return providerFactory.apply(context).getItemCollection(
context.getTenantId(), context.getToken());
context.getTenantId(), context.getToken(), context.getUserId(), context.getRequestId());
}

public InstanceCollection getInstanceCollection(Context context) {
return providerFactory.apply(context).getInstanceCollection(
context.getTenantId(), context.getToken());
context.getTenantId(), context.getToken(), context.getUserId(), context.getRequestId());
}

public HoldingsRecordCollection getHoldingsRecordCollection(Context context) {
return providerFactory.apply(context).getHoldingsRecordCollection(
context.getTenantId(), context.getToken());
context.getTenantId(), context.getToken(), context.getUserId(), context.getRequestId());
}

public HoldingsRecordsSourceCollection getHoldingsRecordsSourceCollection (Context context){
return providerFactory.apply(context).getHoldingsRecordsSourceCollection(
context.getTenantId(), context.getToken()
context.getTenantId(), context.getToken(), context.getUserId(), context.getRequestId()
);
}

public AuthorityRecordCollection getAuthorityRecordCollection(Context context) {
return providerFactory.apply(context).getAuthorityCollection(
context.getTenantId(), context.getToken());
context.getTenantId(), context.getToken(), context.getUserId(), context.getRequestId());
}

public UserCollection getUserCollection(Context context) {
return providerFactory.apply(context).getUserCollection(
context.getTenantId(), context.getToken());
context.getTenantId(), context.getToken(), context.getUserId(), context.getRequestId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,37 @@ public ExternalStorageCollections(String baseAddress, HttpClient client) {
}

@Override
public ItemCollection getItemCollection(String tenantId, String token) {
return new ExternalStorageModuleItemCollection(baseAddress, tenantId, token,
client);
public ItemCollection getItemCollection(String tenantId, String token, String userId, String requestId) {
return new ExternalStorageModuleItemCollection(baseAddress, tenantId, token, userId, requestId, client);
}

@Override
public HoldingsRecordCollection getHoldingsRecordCollection(String tenantId, String token) {
public HoldingsRecordCollection getHoldingsRecordCollection(String tenantId, String token, String userId, String requestId) {
return new ExternalStorageModuleHoldingsRecordCollection(baseAddress,
tenantId, token, client);
tenantId, token, userId, requestId, client);
}

@Override
public InstanceCollection getInstanceCollection(String tenantId, String token) {
public InstanceCollection getInstanceCollection(String tenantId, String token, String userId, String requestId) {
return new ExternalStorageModuleInstanceCollection(baseAddress,
tenantId, token, client);
tenantId, token, userId, requestId, client);
}

@Override
public AuthorityRecordCollection getAuthorityCollection(String tenantId, String token) {
public AuthorityRecordCollection getAuthorityCollection(String tenantId, String token, String userId, String requestId) {
return new ExternalStorageModuleAuthorityRecordCollection(baseAddress,
tenantId, token, client);
tenantId, token, userId, requestId, client);
}

@Override
public UserCollection getUserCollection(String tenantId, String token) {
public UserCollection getUserCollection(String tenantId, String token, String userId, String requestId) {
return new ExternalStorageModuleUserCollection(baseAddress,
tenantId, token, client);
tenantId, token, userId, requestId, client);
}

@Override
public HoldingsRecordsSourceCollection getHoldingsRecordsSourceCollection(String tenantId, String token) {
public HoldingsRecordsSourceCollection getHoldingsRecordsSourceCollection(String tenantId, String token, String userId, String requestId) {
return new ExternalStorageModuleHoldingsRecordsSourceCollection(baseAddress,
tenantId, token, client);
tenantId, token, userId, requestId, client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public class ExternalStorageModuleAuthorityRecordCollection
String baseAddress,
String tenant,
String token,
String userId,
String requestId,
HttpClient client) {

super(String.format("%s/%s", baseAddress, "authority-storage/authorities"),
tenant, token, "authorities", client);
tenant, token, userId, requestId,"authorities", client);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
abstract class ExternalStorageModuleCollection<T> {
private static final String TENANT_HEADER = "X-Okapi-Tenant";
private static final String TOKEN_HEADER = "X-Okapi-Token";
private static final String USER_ID_HEADER = "X-Okapi-User-Id";
private static final String REQUEST_ID_HEADER = "X-Okapi-Request-Id";

private static final Logger LOGGER = LogManager.getLogger(ExternalStorageModuleCollection.class);

Expand All @@ -43,17 +45,23 @@ abstract class ExternalStorageModuleCollection<T> {
protected final String token;
protected final String collectionWrapperPropertyName;
protected final WebClient webClient;
protected final String userId;
protected final String requestId;

ExternalStorageModuleCollection(
String storageAddress,
String tenant,
String token,
String userId,
String requestId,
String collectionWrapperPropertyName,
HttpClient client) {

this.storageAddress = storageAddress;
this.tenant = tenant;
this.token = token;
this.userId = userId;
this.requestId = requestId;
this.collectionWrapperPropertyName = collectionWrapperPropertyName;
this.webClient = WebClient.wrap(client);
}
Expand Down Expand Up @@ -211,7 +219,9 @@ protected HttpRequest<Buffer> withStandardHeaders(HttpRequest<Buffer> request) {
return request
.putHeader(ACCEPT, "application/json, text/plain")
.putHeader(TENANT_HEADER, tenant)
.putHeader(TOKEN_HEADER, token);
.putHeader(TOKEN_HEADER, token)
.putHeader(USER_ID_HEADER, userId)
.putHeader(REQUEST_ID_HEADER, requestId);
}

protected CompletionStage<Response> mapAsyncResultToCompletionStage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class ExternalStorageModuleHoldingsRecordCollection
ExternalStorageModuleHoldingsRecordCollection(String baseAddress,
String tenant,
String token,
String userId,
String requestId,
HttpClient client) {

super(String.format("%s/%s", baseAddress, "holdings-storage/holdings"),
tenant, token, "holdingsRecords", client);
tenant, token, userId, requestId, "holdingsRecords", client);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public class ExternalStorageModuleHoldingsRecordsSourceCollection
String baseAddress,
String tenant,
String token,
String userId,
String requestId,
HttpClient client) {

super(String.format("%s/%s", baseAddress, "holdings-sources"),
tenant, token, "holdingsRecordsSources", client);
tenant, token, userId, requestId, "holdingsRecordsSources", client);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ class ExternalStorageModuleInstanceCollection
String baseAddress,
String tenant,
String token,
String userId,
String requestId,
HttpClient client) {

super(format("%s/%s", baseAddress, "instance-storage/instances"),
tenant, token, "instances", client);
tenant, token, userId, requestId, "instances", client);

batchAddress = format("%s/%s", baseAddress, "instance-storage/batch/instances");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class ExternalStorageModuleItemCollection
implements ItemCollection {

ExternalStorageModuleItemCollection(String baseAddress, String tenant,
String token, HttpClient client) {
String token, String userId, String requestId, HttpClient client) {

super(String.format("%s/%s", baseAddress, "item-storage/items"),
tenant, token, "items", client);
tenant, token, userId, requestId, "items", client);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class ExternalStorageModuleUserCollection
String baseAddress,
String tenant,
String token,
String userId,
String requestId,
HttpClient client) {

super(String.format("%s/%s", baseAddress, "users"),
tenant, token, "users", client);
tenant, token, userId, requestId, "users", client);
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/test/java/api/ApiTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class ApiTestSuite {
public static final String USER_ID = "7e115dfb-d1d6-46ac-b2dc-2b3e74cda694";
public static final String CENTRAL_TENANT_ID_FIELD = "centralTenantId";
public static final String CONSORTIUM_ID_FIELD = "consortiumId";
public static final String REQUEST_ID = "test_request_1234";

private static String bookMaterialTypeId;
private static String dvdMaterialTypeId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.folio.inventory.storage.external;

import static api.ApiTestSuite.REQUEST_ID;
import static api.ApiTestSuite.USER_ID;
import static org.folio.inventory.common.FutureAssistance.fail;
import static org.folio.inventory.common.FutureAssistance.getOnCompletion;
import static org.folio.inventory.common.FutureAssistance.succeed;
Expand Down Expand Up @@ -35,7 +37,7 @@ public class ExternalInstanceCollectionExamples extends ExternalStorageTests {

private final InstanceCollection collection =
useHttpClient(client -> new ExternalStorageModuleInstanceCollection(
getStorageAddress(), TENANT_ID, TENANT_TOKEN, client));
getStorageAddress(), TENANT_ID, TENANT_TOKEN, USER_ID, REQUEST_ID, client));

@Before
@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.folio.inventory.storage.external;

import static api.ApiTestSuite.REQUEST_ID;
import static api.ApiTestSuite.USER_ID;
import static org.folio.inventory.common.FutureAssistance.fail;
import static org.folio.inventory.common.FutureAssistance.getOnCompletion;
import static org.folio.inventory.common.FutureAssistance.succeed;
Expand Down Expand Up @@ -33,7 +35,7 @@ public class ExternalItemCollectionExamples extends ExternalStorageTests {

private final ItemCollection collection = useHttpClient(
client -> new ExternalStorageModuleItemCollection(getStorageAddress(),
TENANT_ID, TENANT_TOKEN, client));
TENANT_ID, TENANT_TOKEN, USER_ID, REQUEST_ID, client));

private final Item smallAngryPlanet = smallAngryPlanet();
private final Item nod = nod();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.folio.inventory.storage.external;

import static api.ApiTestSuite.REQUEST_ID;
import static api.ApiTestSuite.USER_ID;
import static org.folio.inventory.common.FutureAssistance.fail;
import static org.folio.inventory.common.FutureAssistance.getOnCompletion;
import static org.folio.inventory.common.FutureAssistance.succeed;
Expand Down Expand Up @@ -31,7 +33,7 @@ public class ExternalStorageModuleAuthorityRecordCollectionExamples extends Exte

private final ExternalStorageModuleAuthorityRecordCollection storage =
useHttpClient(client -> new ExternalStorageModuleAuthorityRecordCollection(
getStorageAddress(), TENANT_ID, TENANT_TOKEN, client));
getStorageAddress(), TENANT_ID, TENANT_TOKEN, USER_ID, REQUEST_ID, client));

@Test
public void shouldMapFromJson() {
Expand Down
Loading

0 comments on commit 01f58cd

Please sign in to comment.