use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class OptionsResponseDecoder method wrapResponse.
@Override
public OptionsResponse wrapResponse(DataMap dataMap, Map<String, String> headers, ProtocolVersion version) throws IOException {
if (dataMap == null) {
return null;
}
DataMap resources = dataMap.getDataMap(RESOURCES);
if (resources == null)
resources = new DataMap();
HashMap<String, ResourceSchema> resourceMap = new HashMap<String, ResourceSchema>(resources.size());
for (Map.Entry<String, Object> entry : resources.entrySet()) {
resourceMap.put(entry.getKey(), new ResourceSchema((DataMap) entry.getValue()));
}
DataMap schemas = dataMap.getDataMap(MODELS);
if (schemas == null)
schemas = new DataMap();
HashMap<String, DataSchema> dataSchemaMap = new HashMap<String, DataSchema>(schemas.size());
for (Map.Entry<String, Object> entry : schemas.entrySet()) {
String schemaText = CODEC.mapToString((DataMap) entry.getValue());
dataSchemaMap.put(entry.getKey(), DataTemplateUtil.parseSchema(schemaText));
}
return new OptionsResponse(resourceMap, dataSchemaMap);
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class FinderTest method test.
@Test
public void test() throws IOException {
final String findersFilename = IDLS_DIR + FINDERS_FILE;
final ResourceSchema findersIdl = _codec.readResourceSchema(new FileInputStream(findersFilename));
final FinderSchemaArray finders = findersIdl.getCollection().getFinders();
for (FinderSchema finder : finders) {
if ("searchWithoutMetadata".equals(finder.getName())) {
Assert.assertFalse(finder.hasMetadata());
} else if ("searchWithMetadata".equals(finder.getName())) {
Assert.assertEquals(finder.getMetadata().getType(), SearchMetadata.class.getName());
} else if ("basicSearch".equals(finder.getName())) {
Assert.assertFalse(finder.hasMetadata());
} else if ("predefinedSearch".equals(finder.getName())) {
Assert.assertFalse(finder.hasMetadata());
} else {
throw new RuntimeException("Unknown finder is added to com.linkedin.restli.examples.greetings.server.FindersResource");
}
}
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class ResourceModelEncoder method loadOrBuildResourceSchema.
/**
* Checks if a matching .restspec.json file exists in the classpath for the given {@link ResourceModel}.
* If one is found it is loaded. If not, one is built from the {@link ResourceModel}.
*
* The .restspec.json is preferred because it contains the exact idl that was generated for the resource
* and also includees includes javadoc from the server class in the restspec.json.
*
* @param resourceModel provides the name and namespace of the schema to load or build
* @return the {@link ResourceSchema} for the given {@link ResourceModel}
*/
public ResourceSchema loadOrBuildResourceSchema(final ResourceModel resourceModel) {
StringBuilder resourceFilePath = new StringBuilder();
if (resourceModel.getNamespace() != null) {
resourceFilePath.append(resourceModel.getNamespace());
resourceFilePath.append(".");
}
resourceFilePath.append(resourceModel.getName());
resourceFilePath.append(RestConstants.RESOURCE_MODEL_FILENAME_EXTENSION);
try {
InputStream stream = this.getClass().getClassLoader().getResourceAsStream(resourceFilePath.toString());
if (stream == null) {
// restspec.json file not found, building one instead
return buildResourceSchema(resourceModel);
} else {
DataMap resourceSchemaDataMap = codec.bytesToMap(IOUtils.toByteArray(stream));
return new ResourceSchema(resourceSchemaDataMap);
}
} catch (IOException e) {
throw new RuntimeException("Failed to read " + resourceFilePath.toString() + " from classpath.", e);
}
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class ResourceModelEncoder method buildResourceSchema.
/**
* @param resourceModel {@link ResourceModel} to build the schema for
* @return {@link ResourceSchema} for the provided resource model
*/
public ResourceSchema buildResourceSchema(final ResourceModel resourceModel) {
ResourceSchema rootNode = new ResourceSchema();
switch(resourceModel.getResourceType()) {
case ACTIONS:
appendActionsModel(rootNode, resourceModel);
break;
case SIMPLE:
appendSimple(rootNode, resourceModel);
break;
default:
appendCollection(rootNode, resourceModel);
break;
}
final DataMap customAnnotation = resourceModel.getCustomAnnotationData();
if (!customAnnotation.isEmpty()) {
rootNode.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
}
return rootNode;
}
use of com.linkedin.restli.restspec.ResourceSchema in project rest.li by linkedin.
the class ResourceModelEncoder method buildEntitySchema.
private EntitySchema buildEntitySchema(ResourceModel resourceModel) {
EntitySchema entityNode = new EntitySchema();
entityNode.setPath(buildPathForEntity(resourceModel));
if (resourceModel.getResourceLevel() == ResourceLevel.COLLECTION) {
ActionSchemaArray actions = createActions(resourceModel, ResourceLevel.ENTITY);
if (actions.size() > 0) {
entityNode.setActions(actions);
}
}
// subresources
ResourceSchemaArray subresources = new ResourceSchemaArray();
for (ResourceModel subResourceModel : resourceModel.getSubResources()) {
ResourceSchema subresource = new ResourceSchema();
switch(subResourceModel.getResourceType()) {
case COLLECTION:
case ASSOCIATION:
appendCollection(subresource, subResourceModel);
break;
case SIMPLE:
appendSimple(subresource, subResourceModel);
break;
default:
break;
}
final DataMap customAnnotation = subResourceModel.getCustomAnnotationData();
if (!customAnnotation.isEmpty()) {
subresource.setAnnotations(new CustomAnnotationContentSchemaMap(customAnnotation));
}
subresources.add(subresource);
}
if (subresources.size() > 0) {
Collections.sort(subresources, new Comparator<ResourceSchema>() {
@Override
public int compare(ResourceSchema resourceSchema, ResourceSchema resourceSchema2) {
return resourceSchema.getName().compareTo(resourceSchema2.getName());
}
});
entityNode.setSubresources(subresources);
}
return entityNode;
}
Aggregations