use of org.folio.rest.jaxrs.model.Metadata in project raml-module-builder by folio-org.
the class RestVerticle method populateMetaData.
private void populateMetaData(Object entity, Map<String, String> okapiHeaders, String path) {
// try to populate meta data section of the passed in json (converted to pojo already as this stage)
// will only succeed if the pojo (json schema) has a reference to the metaData schema.
// there should not be a metadata schema declared in the json schema unless it is the OOTB meta data schema
// the created date and by fields are stored in the db in separate columns on insert trigger so that even if
// we overwrite them here, the correct value will be set in the db level via a trigger on update
String json;
try {
String userId = okapiHeaders.get(OKAPI_USERID_HEADER);
if (userId == null) {
String token = okapiHeaders.get(OKAPI_HEADER_TOKEN);
String[] split = token.split("\\.");
// the split array contains the 3 parts of the token - the body is the middle part
json = JwtUtils.getJson(split[1]);
JsonObject j = new JsonObject(json);
userId = j.getString("user_id");
}
if (userId != null) {
Metadata md = new Metadata();
md.setUpdatedDate(new Date());
md.setCreatedDate(new Date());
md.setCreatedByUserId(userId);
md.setUpdatedByUserId(userId);
try {
/* if a metadata section is passed in by client, we cannot assume it is correct.
* entity.getClass().getMethod("getMetaData",
new Class[] { }).invoke(entity);*/
entity.getClass().getMethod("setMetadata", new Class[] { Metadata.class }).invoke(entity, md);
} catch (Exception e) {
// do nothing - if this is thrown then the setMetaData() failed, assume pojo
// (aka) json schema - didnt include a reference to it.
log.debug(e.getMessage(), e);
}
}
} catch (Exception e) {
log.warn("Problem parsing " + OKAPI_HEADER_TOKEN + " header, for path " + path + " - " + e.getMessage());
}
}
use of org.folio.rest.jaxrs.model.Metadata in project raml-module-builder by folio-org.
the class DemoRamlRestTest method postData.
/**
* for POST
*/
private void postData(TestContext context, String url, Buffer buffer, int errorCode, int mode, String contenttype, String tenant, boolean userIdHeader) {
// save stacktrace for async handler
Exception stacktrace = new RuntimeException();
Async async = context.async();
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = null;
if (mode == 0) {
request = client.putAbs(url);
} else if (mode == 1) {
request = client.postAbs(url);
} else {
request = client.deleteAbs(url);
}
request.exceptionHandler(error -> {
async.complete();
context.fail(new RuntimeException(error.getMessage(), stacktrace));
}).handler(response -> {
int statusCode = response.statusCode();
// is it 2XX
log.info(statusCode + ", " + errorCode + " expected status at " + System.currentTimeMillis() + " mode " + mode + " for " + url);
if (statusCode == errorCode) {
if (statusCode == 422) {
String str = response.getHeader("Content-type");
if (str != null && str.contains("application/json")) {
context.assertTrue(true);
} else {
context.fail(new RuntimeException("422 response code should contain a content type header of application/json", stacktrace));
}
} else if (statusCode == 201) {
response.bodyHandler(responseData -> {
String date = (String) new JsonPathParser(responseData.toJsonObject()).getValueAt("metadata.createdDate");
if (date == null && userIdHeader) {
context.fail(new RuntimeException("metaData schema createdDate missing from returned json", stacktrace));
}
});
}
context.assertTrue(true);
} else {
response.bodyHandler(responseData -> {
context.fail(new RuntimeException("got unexpected response code, expected: " + errorCode + ", received code: " + statusCode + " mode " + mode + " for url " + url + "\ndata:" + responseData.toString(), stacktrace));
});
}
if (!async.isCompleted()) {
async.complete();
}
});
request.setChunked(true);
request.putHeader("X-Okapi-Request-Id", "999999999999");
if (tenant != null) {
request.putHeader("x-okapi-tenant", tenant);
}
request.putHeader("Accept", "application/json,text/plain");
if (userIdHeader) {
request.putHeader("X-Okapi-User-Id", "af23adf0-61ba-4887-bf82-956c4aae2260");
}
if (contenttype != null) {
request.putHeader("Content-type", contenttype);
} else {
if (mode == 0 || mode == 2) {
request.putHeader("Content-type", "application/json");
} else {
request.putHeader("Content-type", "multipart/form-data; boundary=MyBoundary");
}
}
if (buffer != null) {
request.write(buffer);
}
request.end();
}
use of org.folio.rest.jaxrs.model.Metadata in project raml-module-builder by folio-org.
the class MetadataUtil method createMetadata.
/**
* @return Metadata where createdDate and updatedDate are set to current time and
* createdByUserId and updatedByUserId are set to the
* {@link RestVerticle.OKAPI_USERID_HEADER} header, using {@code user_id} from the
* {@link RestVerticle.OKAPI_HEADER_TOKEN} as a fall-back. The token is used without
* validation.
*/
public static Metadata createMetadata(Map<String, String> okapiHeaders) {
String userId = okapiHeaders.get(RestVerticle.OKAPI_USERID_HEADER);
if (userId == null) {
try {
userId = new OkapiToken(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TOKEN)).getUserIdWithoutValidation();
} catch (Exception e) {
// ignore, userId remains null
}
}
Metadata md = new Metadata();
md.setUpdatedDate(new Date());
md.setCreatedDate(md.getUpdatedDate());
md.setCreatedByUserId(userId);
md.setUpdatedByUserId(userId);
return md;
}
use of org.folio.rest.jaxrs.model.Metadata in project raml-module-builder by folio-org.
the class MetadataUtilTest method createMetadataFromToken.
@Test
void createMetadataFromToken() {
Metadata metadata = MetadataUtil.createMetadata(headers(RestVerticle.OKAPI_HEADER_TOKEN, token));
assertThat(metadata.getCreatedByUserId(), is("bar"));
assertThat(metadata.getUpdatedByUserId(), is("bar"));
assertNow(metadata.getCreatedDate());
assertNow(metadata.getUpdatedDate());
}
use of org.folio.rest.jaxrs.model.Metadata in project raml-module-builder by folio-org.
the class MetadataUtilTest method createMetadataFromUserId.
@Test
void createMetadataFromUserId() {
Metadata metadata = MetadataUtil.createMetadata(headers(RestVerticle.OKAPI_HEADER_TOKEN, token, RestVerticle.OKAPI_USERID_HEADER, "foo"));
assertThat(metadata.getCreatedByUserId(), is("foo"));
assertThat(metadata.getUpdatedByUserId(), is("foo"));
assertNow(metadata.getCreatedDate());
assertNow(metadata.getUpdatedDate());
}
Aggregations