use of org.alfresco.rest.framework.core.ResourceWithMetadata in project alfresco-remote-api by Alfresco.
the class SerializeTests method testInvokeRelation.
@Test
public void testInvokeRelation() throws IOException {
ResourceWithMetadata relationResource = locator.locateRelationResource(api, "sheep", "baaahh", HttpMethod.GET);
assertNotNull(relationResource);
RelationshipResourceAction.Read<?> getter = (RelationshipResourceAction.Read<?>) relationResource.getResource();
String out = writeResponse(helper.processAdditionsToTheResponse(mock(WebScriptResponse.class), api, null, Params.valueOf("notUsed", null, null), getter.readAll("1234A3", Params.valueOf("notUsed", null, null))));
assertTrue("There must be json output", StringUtils.isNotBlank(out));
}
use of org.alfresco.rest.framework.core.ResourceWithMetadata in project alfresco-remote-api by Alfresco.
the class SerializeTests method testInvokeVersions.
@Test
public void testInvokeVersions() throws IOException {
final Map<String, Object> respons = new HashMap<String, Object>();
ResourceWithMetadata entityResource = locator.locateEntityResource(api, "goat", HttpMethod.GET);
assertNotNull(entityResource);
EntityResourceAction.ReadById<?> getter = (ReadById<?>) entityResource.getResource();
Object readById = getter.readById("1234A3", NOT_USED);
assertTrue("Version 1 must be a goat.", Goat.class.equals(readById.getClass()));
String out = writeResponse(readById);
assertNotNull(out);
Api v3 = Api.valueOf(api.getName(), api.getScope().toString(), "3");
entityResource = locator.locateEntityResource(v3, "goat", HttpMethod.GET);
assertNotNull(entityResource);
getter = (ReadById<?>) entityResource.getResource();
Object readByIdForNewVersion = getter.readById("1234A3", NOT_USED);
assertTrue("Version 3 must be a slim goat.", SlimGoat.class.equals(readByIdForNewVersion.getClass()));
respons.put("v3Goat", readByIdForNewVersion);
out = writeResponse(readByIdForNewVersion);
entityResource = locator.locateEntityResource(api, "grass", HttpMethod.GET);
// ok for version 1
assertNotNull(entityResource);
try {
entityResource = locator.locateEntityResource(v3, "grass", HttpMethod.GET);
fail("Should throw an UnsupportedResourceOperationException");
} catch (UnsupportedResourceOperationException error) {
// this is correct
}
}
use of org.alfresco.rest.framework.core.ResourceWithMetadata in project alfresco-remote-api by Alfresco.
the class ExecutionTests method testInvokePut.
@Test
public void testInvokePut() throws IOException {
ResourceWithMetadata entityResource = locator.locateEntityResource(api, "sheep", HttpMethod.PUT);
AbstractResourceWebScript executor = getExecutor("executorOfPut");
final Sheep aSheep = new Sheep("xyz");
Object result = executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(aSheep, ((ExecutionResult) result).getRoot());
final Goat goat = new Goat("xyz");
ResourceWithMetadata cowResource = locator.locateEntityResource(api, "cow", HttpMethod.PUT);
result = executor.execute(cowResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(goat, ((ExecutionResult) result).getRoot());
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.PUT);
result = executor.execute(resource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(aSheep, ((ExecutionResult) result).getRoot());
ResourceWithMetadata baaPhoto = locator.locateRelationResource(api, "sheep/{entityId}/baaahh", "photo", HttpMethod.PUT);
result = executor.execute(baaPhoto, Params.valueOf("4", "56", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
ResourceWithMetadata flockEntityResource = locator.locateRelationResource(api3, "flock", "photo", HttpMethod.PUT);
result = executor.execute(flockEntityResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
ResourceWithMetadata calf = locator.locateRelationResource(api, "cow", "calf", HttpMethod.PUT);
result = executor.execute(calf, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNotNull(result);
assertEquals(goat, ((ExecutionResult) result).getRoot());
cowResource = locator.locateRelationResource(api, "cow", "photo", HttpMethod.PUT);
result = executor.execute(cowResource, Params.valueOf("654", null, NULL_PARAMS, goat, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
calf = locator.locateRelationResource(api, "cow/{entityId}/calf", "photo", HttpMethod.PUT);
result = executor.execute(calf, Params.valueOf("4", "56", mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
assertNull(result);
}
use of org.alfresco.rest.framework.core.ResourceWithMetadata in project alfresco-remote-api by Alfresco.
the class PublicApiDeclarativeRegistry method findWebScript.
/* (non-Javadoc)
* @see org.alfresco.web.scripts.Registry#findWebScript(java.lang.String, java.lang.String)
*/
public Match findWebScript(String method, String uri) {
Match match;
HttpMethod httpMethod = HttpMethod.valueOf(method);
if (HttpMethod.GET.equals(httpMethod)) {
if (uri.equals(PublicApiTenantWebScriptServletRequest.NETWORKS_PATH)) {
Map<String, String> templateVars = new HashMap<>();
templateVars.put("apiScope", "public");
templateVars.put("apiVersion", "1");
templateVars.put("apiName", "networks");
match = new Match("", templateVars, "", getNetworksWebScript);
} else if (uri.equals(PublicApiTenantWebScriptServletRequest.NETWORK_PATH)) {
Map<String, String> templateVars = new HashMap<>();
templateVars.put("apiScope", "public");
templateVars.put("apiVersion", "1");
templateVars.put("apiName", "network");
match = new Match("", templateVars, "", getNetworkWebScript);
} else {
match = super.findWebScript(method, uri);
if (match == null) {
return null;
}
Map<String, String> templateVars = match.getTemplateVars();
ResourceWithMetadata rwm = getResourceWithMetadataOrNull(templateVars, httpMethod);
if (rwm != null) {
Class<? extends ResourceAction> resAction = null;
String entityId = templateVars.get(ResourceLocator.ENTITY_ID);
String relationshipId = templateVars.get(ResourceLocator.RELATIONSHIP_ID);
switch(rwm.getMetaData().getType()) {
case ENTITY:
if (StringUtils.isNotBlank(entityId)) {
if (EntityResourceAction.ReadById.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = EntityResourceAction.ReadById.class;
}
} else {
if (EntityResourceAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = EntityResourceAction.Read.class;
}
}
break;
case PROPERTY:
if (StringUtils.isNotBlank(entityId)) {
if (BinaryResourceAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = BinaryResourceAction.Read.class;
} else if (RelationshipResourceBinaryAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = RelationshipResourceBinaryAction.Read.class;
}
}
break;
case RELATIONSHIP:
if (StringUtils.isNotBlank(relationshipId)) {
if (RelationshipResourceAction.ReadById.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = RelationshipResourceAction.ReadById.class;
}
} else {
if (RelationshipResourceAction.Read.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = RelationshipResourceAction.Read.class;
}
}
break;
default:
break;
}
final boolean noAuth = (resAction != null && rwm.getMetaData().isNoAuth(resAction));
if (noAuth) {
// override match with noAuth
match = overrideMatch(match);
}
}
}
} else if (HttpMethod.POST.equals(httpMethod)) {
match = super.findWebScript(method, uri);
if (match != null) {
ResourceWithMetadata rwm = getResourceWithMetadataOrNull(match.getTemplateVars(), httpMethod);
if (rwm != null) {
Class<? extends ResourceAction> resAction = null;
Boolean noAuth = null;
switch(rwm.getMetaData().getType()) {
case ENTITY:
if (EntityResourceAction.Create.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = EntityResourceAction.Create.class;
} else if (EntityResourceAction.CreateWithResponse.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = EntityResourceAction.CreateWithResponse.class;
}
break;
case RELATIONSHIP:
if (RelationshipResourceAction.Create.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = RelationshipResourceAction.Create.class;
} else if (RelationshipResourceAction.CreateWithResponse.class.isAssignableFrom(rwm.getResource().getClass())) {
resAction = RelationshipResourceAction.CreateWithResponse.class;
}
break;
case OPERATION:
noAuth = rwm.getMetaData().isNoAuth(null);
break;
default:
break;
}
if (noAuth == null) {
noAuth = (resAction != null && rwm.getMetaData().isNoAuth(resAction));
}
if (noAuth) {
// override match with noAuth
match = overrideMatch(match);
}
}
}
} else {
match = super.findWebScript(method, uri);
}
if (match == null) {
throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID, new String[] { uri });
}
return match;
}
use of org.alfresco.rest.framework.core.ResourceWithMetadata in project alfresco-remote-api by Alfresco.
the class WebScriptOptionsMetaData method execute.
@Override
public void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException {
final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
ResourceDictionary resourceDic = lookupDictionary.getDictionary();
Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
if (apiResources == null) {
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
}
String collectionName = templateVars.get(ResourceLocator.COLLECTION_RESOURCE);
String resourceName = templateVars.get(ResourceLocator.RELATIONSHIP_RESOURCE);
String resourceKey = ResourceDictionary.resourceKey(collectionName, resourceName);
if (logger.isDebugEnabled()) {
logger.debug("Locating resource :" + resourceKey);
}
ResourceWithMetadata resource = apiResources.get(resourceKey);
if (resource == null) {
// Get entity resource and check if we are referencing a property on it.
resourceKey = ResourceDictionary.propertyResourceKey(collectionName, resourceName);
resource = apiResources.get(resourceKey);
}
ResourceMetaDataWriter writer = chooseWriter(req);
writer.writeMetaData(res.getOutputStream(), resource, apiResources);
}
Aggregations