-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RMB-253: JSON schema and RAML multiple interface implementation (#326)
- Loading branch information
1 parent
eeac8df
commit c2c4fa1
Showing
11 changed files
with
635 additions
and
19 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
Submodule raml-util
updated
4 files
+169 −0 | examples/mod-users/userdata.json.sample | |
+53 −0 | examples/mod-users/users.raml.sample | |
+53 −0 | ramls/jsonSchemas.raml | |
+49 −0 | ramls/ramls.raml |
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,15 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"_id": { | ||
"type": "string" | ||
}, | ||
"name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"name" | ||
] | ||
} |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#%RAML 1.0 | ||
|
||
title: Jobs API | ||
title: Tests API | ||
baseUri: http://localhost:8081/v1 | ||
version: v1 | ||
|
||
|
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
121 changes: 121 additions & 0 deletions
121
domain-models-runtime/src/main/java/org/folio/rest/impl/JsonSchemasAPI.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,121 @@ | ||
package org.folio.rest.impl; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.folio.rest.annotations.Validate; | ||
import org.folio.rest.jaxrs.resource.JsonSchemas; | ||
import org.folio.rest.tools.GenerateRunner; | ||
import org.folio.util.ResourceUtil; | ||
|
||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.logging.Logger; | ||
import io.vertx.core.logging.LoggerFactory; | ||
|
||
public class JsonSchemasAPI implements JsonSchemas { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(JsonSchemasAPI.class); | ||
|
||
private static final Pattern REF_MATCH_PATTERN = Pattern.compile("\\\"\\$ref\\\"\\s*:\\s*\\\"(.*?)\\\""); | ||
|
||
private static final String OKAPI_URL_HEADER = "x-okapi-url"; | ||
private static final String RAMLS_PATH = System.getProperty("raml_files", GenerateRunner.SOURCES_DEFAULT) + File.separator; | ||
private static final String HASH_TAG = "#"; | ||
|
||
private static final List<String> JSON_SCHEMAS = getJsonSchemasList(); | ||
|
||
@Validate | ||
@Override | ||
public void getJsonSchemas( | ||
String path, | ||
Map<String, String> okapiHeaders, | ||
Handler<AsyncResult<Response>> asyncResultHandler, | ||
Context vertxContext | ||
) { | ||
vertxContext.runOnContext(v -> { | ||
try { | ||
if (path == null) { | ||
List<String> schemas = getSchemas(); | ||
asyncResultHandler.handle( | ||
Future.succeededFuture( | ||
GetJsonSchemasResponse.respond200WithApplicationJson(schemas) | ||
) | ||
); | ||
} else { | ||
String okapiUrl = okapiHeaders.get(OKAPI_URL_HEADER); | ||
String schema = getJsonSchemaByPath(path, okapiUrl); | ||
if (schema != null) { | ||
asyncResultHandler.handle( | ||
Future.succeededFuture( | ||
GetJsonSchemasResponse.respond200WithApplicationSchemaJson(schema) | ||
) | ||
); | ||
} else { | ||
String notFoundMessage = "Schema " + path + " not found"; | ||
asyncResultHandler.handle( | ||
Future.succeededFuture( | ||
GetJsonSchemasResponse.respond404WithTextPlain(notFoundMessage) | ||
) | ||
); | ||
} | ||
} | ||
} catch (Exception e) { | ||
log.error(e.getMessage(), e); | ||
asyncResultHandler.handle( | ||
Future.succeededFuture( | ||
GetJsonSchemasResponse.respond500WithTextPlain(e.getMessage()) | ||
) | ||
); | ||
} | ||
}); | ||
} | ||
|
||
private static List<String> getJsonSchemasList() { | ||
try { | ||
return Arrays.asList(ResourceUtil.asString(RAMLS_PATH + GenerateRunner.JSON_SCHEMA_LIST).split("\\r?\\n")); | ||
} catch (IOException e) { | ||
log.warn("Unable to get JSON Schemas list!", e); | ||
return new ArrayList<>(); | ||
} | ||
} | ||
|
||
private List<String> getSchemas() { | ||
return JSON_SCHEMAS; | ||
} | ||
|
||
private String getJsonSchemaByPath(String path, String okapiUrl) { | ||
try { | ||
return replaceReferences(ResourceUtil.asString(RAMLS_PATH + path), okapiUrl); | ||
} catch (IOException e) { | ||
return null; | ||
} | ||
} | ||
|
||
String replaceReferences(String schema, String okapiUrl) { | ||
Matcher matcher = REF_MATCH_PATTERN.matcher(schema); | ||
StringBuffer sb = new StringBuffer(schema.length()); | ||
while (matcher.find()) { | ||
String path = matcher.group(1); | ||
if (!path.startsWith(HASH_TAG)) { | ||
if (path.contains(RAMLS_PATH)) { | ||
path = path.substring(path.lastIndexOf(RAMLS_PATH) + RAMLS_PATH.length()); | ||
} | ||
matcher.appendReplacement(sb, Matcher.quoteReplacement("\"$ref\":\"" + okapiUrl + "/_/jsonSchemas?path=" + path + "\"")); | ||
} | ||
} | ||
matcher.appendTail(sb); | ||
return sb.toString(); | ||
} | ||
|
||
} |
Oops, something went wrong.