use of org.alfresco.rest.framework.Api in project alfresco-remote-api by Alfresco.
the class SerializeTests method testFilter.
// note: exposed as "properties" query param
@Test
public void testFilter() throws IOException, JSONException {
assertNotNull(helper);
BeanPropertiesFilter theFilter = getFilter("age");
Object res = new ExecutionResult(new Sheep("bob"), theFilter);
String out = writeResponse(res);
assertTrue("Filter must only return the age.", StringUtils.contains(out, "{\"age\":3}"));
theFilter = getFilter("age,name");
res = new ExecutionResult(new Sheep("bob"), theFilter);
out = writeResponse(res);
JSONObject jsonRsp = new JSONObject(new JSONTokener(out));
assertEquals(1, jsonRsp.length());
JSONObject entry = jsonRsp.getJSONObject("entry");
assertEquals(2, entry.length());
assertEquals("The name should be 'Dolly'", "Dolly", entry.getString("name"));
assertTrue("The age should be 3", entry.getInt("age") == 3);
// unit test filter with "include" taking precendence over "fields" filter
List<String> theInclude = getIncludeClause("name");
theFilter = getFilter("age", theInclude);
res = new ExecutionResult(new Sheep("bob"), theFilter);
out = writeResponse(res);
jsonRsp = new JSONObject(new JSONTokener(out));
assertEquals(1, jsonRsp.length());
entry = jsonRsp.getJSONObject("entry");
assertEquals(2, entry.length());
assertEquals("The name should be 'Dolly'", "Dolly", entry.getString("name"));
assertTrue("The age should be 3", entry.getInt("age") == 3);
Api v3 = Api.valueOf(api.getName(), api.getScope().toString(), "3");
Map<String, BeanPropertiesFilter> relFiler = getRelationFilter("herd");
res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3, "goat", ParamsExtender.valueOf(relFiler, "notUsed"), new SlimGoat());
out = writeResponse(res);
jsonRsp = new JSONObject(new JSONTokener(out));
entry = jsonRsp.getJSONObject("relations").getJSONObject("herd").getJSONObject("list").getJSONArray("entries").getJSONObject(0).getJSONObject("entry");
assertEquals("The name should be 'bigun'", "bigun", entry.getString("name"));
assertTrue("The quantity should be 56", entry.getInt("quantity") == 56);
relFiler = getRelationFilter("herd(name)");
res = helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), v3, "goat", ParamsExtender.valueOf(relFiler, "notUsed"), new SlimGoat());
out = writeResponse(res);
assertTrue("Must return only the herd name.", StringUtils.contains(out, "{\"name\":\"bigun\"}"));
}
use of org.alfresco.rest.framework.Api in project alfresco-remote-api by Alfresco.
the class InspectorTests method testInspectApi.
@Test
public void testInspectApi() {
Api api = ResourceInspector.inspectApi(SheepBlackSheepResource.class);
assertNotNull(api);
assertEquals(Api.SCOPE.PRIVATE, api.getScope());
assertEquals(1, api.getVersion());
api = ResourceInspector.inspectApi(String.class);
assertNull(api);
api = ResourceInspector.inspectApi(NodeCommentsRelation.class);
assertNotNull(api);
assertEquals(Api.SCOPE.PUBLIC, api.getScope());
assertEquals(1, api.getVersion());
}
use of org.alfresco.rest.framework.Api in project alfresco-remote-api by Alfresco.
the class ResourceDictionary method prettyPrint.
/*
* Prints a String representation of the Resource Dictionary
*/
public String prettyPrint() {
StringBuilder builder = new StringBuilder();
builder.append("*******Resources********:").append(NEW_LINE);
builder.append("**Public Apis **").append(NEW_LINE);
for (Api api : this.publicApis) {
printApi(builder, api);
}
builder.append(NEW_LINE);
builder.append("**Private Apis **").append(NEW_LINE);
for (Api api : this.privateApis) {
printApi(builder, api);
}
builder.append("*******End of Resources ********:");
return builder.toString();
}
use of org.alfresco.rest.framework.Api in project alfresco-remote-api by Alfresco.
the class ResourceInspector method inspectRelationship.
/**
* Inspects the relationship resource and returns meta data about it
*
* @param annot RelationshipResource
* @param resource Class<?>
*/
private static List<ResourceMetadata> inspectRelationship(RelationshipResource annot, Class<?> resource) {
Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot);
String urlPath = String.valueOf(annotAttribs.get("name"));
String entityPath = findEntityNameByAnnotationAttributes(annotAttribs);
String relationshipKey = ResourceDictionary.resourceKey(entityPath, urlPath);
Api api = inspectApi(resource);
List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
MetaHelper helper = new MetaHelper(resource);
findOperation(RelationshipResourceAction.Create.class, HttpMethod.POST, helper);
findOperation(RelationshipResourceAction.Read.class, HttpMethod.GET, helper);
findOperation(RelationshipResourceAction.ReadById.class, HttpMethod.GET, helper);
findOperation(RelationshipResourceAction.Update.class, HttpMethod.PUT, helper);
findOperation(RelationshipResourceAction.Delete.class, HttpMethod.DELETE, helper);
findOperation(RelationshipResourceAction.DeleteSet.class, HttpMethod.DELETE, helper);
findOperation(RelationshipResourceAction.CreateWithResponse.class, HttpMethod.POST, helper);
findOperation(RelationshipResourceAction.ReadWithResponse.class, HttpMethod.GET, helper);
findOperation(RelationshipResourceAction.ReadByIdWithResponse.class, HttpMethod.GET, helper);
findOperation(RelationshipResourceAction.UpdateWithResponse.class, HttpMethod.PUT, helper);
findOperation(RelationshipResourceAction.DeleteWithResponse.class, HttpMethod.DELETE, helper);
findOperation(RelationshipResourceAction.DeleteSetWithResponse.class, HttpMethod.DELETE, helper);
findOperation(MultiPartRelationshipResourceAction.Create.class, HttpMethod.POST, helper);
boolean noAuth = resource.isAnnotationPresent(WebApiNoAuth.class);
if (noAuth) {
throw new IllegalArgumentException("@WebApiNoAuth should not be on all (relationship resource class) - only on methods: " + urlPath);
}
Set<Class<? extends ResourceAction>> apiNoAuth = helper.apiNoAuth;
if (resource.isAnnotationPresent(WebApiDeleted.class)) {
metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, null, inspectApi(resource), ALL_RELATIONSHIP_RESOURCE_INTERFACES, apiNoAuth, entityPath));
} else {
metainfo.add(new ResourceMetadata(relationshipKey, RESOURCE_TYPE.RELATIONSHIP, helper.operations, inspectApi(resource), helper.apiDeleted, apiNoAuth, entityPath));
}
inspectAddressedProperties(api, resource, relationshipKey, metainfo);
inspectOperations(api, resource, relationshipKey, metainfo);
return metainfo;
}
use of org.alfresco.rest.framework.Api in project alfresco-remote-api by Alfresco.
the class ResourceInspector method inspectEntity.
/**
* Inspects the entity resource and returns meta data about it
*
* @param annot EntityResource
* @param resource Class<?>
*/
private static List<ResourceMetadata> inspectEntity(EntityResource annot, Class<?> resource) {
String urlPath = findEntityName(annot);
logger.debug("Found EntityResource: " + urlPath);
List<ResourceMetadata> metainfo = new ArrayList<ResourceMetadata>();
Api api = inspectApi(resource);
MetaHelper helper = new MetaHelper(resource);
findOperation(EntityResourceAction.Create.class, HttpMethod.POST, helper);
findOperation(EntityResourceAction.Read.class, HttpMethod.GET, helper);
findOperation(EntityResourceAction.ReadById.class, HttpMethod.GET, helper);
findOperation(EntityResourceAction.Update.class, HttpMethod.PUT, helper);
findOperation(EntityResourceAction.Delete.class, HttpMethod.DELETE, helper);
findOperation(EntityResourceAction.DeleteSet.class, HttpMethod.DELETE, helper);
findOperation(EntityResourceAction.CreateWithResponse.class, HttpMethod.POST, helper);
findOperation(EntityResourceAction.ReadWithResponse.class, HttpMethod.GET, helper);
findOperation(EntityResourceAction.ReadByIdWithResponse.class, HttpMethod.GET, helper);
findOperation(EntityResourceAction.UpdateWithResponse.class, HttpMethod.PUT, helper);
findOperation(EntityResourceAction.DeleteWithResponse.class, HttpMethod.DELETE, helper);
findOperation(EntityResourceAction.DeleteSetWithResponse.class, HttpMethod.DELETE, helper);
findOperation(MultiPartResourceAction.Create.class, HttpMethod.POST, helper);
boolean noAuth = resource.isAnnotationPresent(WebApiNoAuth.class);
if (noAuth) {
throw new IllegalArgumentException("@WebApiNoAuth should not be on all (entity resource class) - only on individual methods: " + urlPath);
}
Set<Class<? extends ResourceAction>> apiNoAuth = helper.apiNoAuth;
if (resource.isAnnotationPresent(WebApiDeleted.class)) {
metainfo.add(new ResourceMetadata(ResourceDictionary.resourceKey(urlPath, null), RESOURCE_TYPE.ENTITY, null, api, ALL_ENTITY_RESOURCE_INTERFACES, apiNoAuth, null));
} else {
if (!helper.apiDeleted.isEmpty() || !helper.operations.isEmpty()) {
metainfo.add(new ResourceMetadata(ResourceDictionary.resourceKey(urlPath, null), RESOURCE_TYPE.ENTITY, helper.operations, api, helper.apiDeleted, apiNoAuth, null));
}
}
inspectAddressedProperties(api, resource, urlPath, metainfo);
inspectOperations(api, resource, urlPath, metainfo);
return metainfo;
}
Aggregations