use of org.alfresco.rest.framework.core.ResourceOperation in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testPostExtractor.
@SuppressWarnings("unchecked")
@Test
public void testPostExtractor() throws IOException {
// Put together the stubs
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
extractor.setAssistant(assistant);
Map<String, String> templateVars = new HashMap<String, String>();
Content content = mock(Content.class);
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
when(request.getContent()).thenReturn(content);
Params params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
Object passed = params.getPassedIn();
assertNotNull(passed);
assertTrue(List.class.isAssignableFrom(passed.getClass()));
List<Object> passedObjs = (List<Object>) passed;
assertTrue(passedObjs.size() == 1);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
// No entity id for POST
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
try {
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here. No entity id for POST");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
passed = params.getPassedIn();
assertNotNull(passed);
passedObjs = (List<Object>) passed;
assertTrue(passedObjs.size() == 1);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass()));
try {
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "45678");
params = extractor.extractParams(mockRelationship(), request);
fail("Should not get here.");
} catch (UnsupportedResourceOperationException iae) {
// Must throw this exception
assertNotNull("POSTING to a relationship collection by id is not correct.", iae);
}
templateVars.clear();
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "codfish");
try {
// POST does not support addressed parameters.
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here.");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
testExtractOperationParams(templateVars, request, extractor);
templateVars.clear();
Method aMethod = ResourceInspector.findMethod(EntityResourceAction.Create.class, GrassEntityResource.class);
ResourceOperation op = ResourceInspector.inspectOperation(GrassEntityResource.class, aMethod, HttpMethod.POST);
List<ResourceMetadata> metainfo = ResourceInspector.inspect(GrassEntityResource.class);
assertNotNull(op);
assertTrue("Create method should have two params", op.getParameters().size() == 2);
ResourceParameter singleParam = op.getParameters().get(0);
assertTrue(ResourceParameter.KIND.HTTP_BODY_OBJECT.equals(singleParam.getParamType()));
assertFalse("Create grass does not support multiple grass creations", singleParam.isAllowMultiple());
assertFalse(singleParam.isRequired());
// Test context when the request body is null and 'required' webApiParam is false
when(request.getHeader("content-length")).thenReturn("0");
params = extractor.extractParams(metainfo.get(0), request);
assertNotNull(params);
// Test context when the request body is provided and 'required' property is false
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.GRASS_JSON));
params = extractor.extractParams(metainfo.get(0), request);
assertNotNull(params);
}
use of org.alfresco.rest.framework.core.ResourceOperation in project alfresco-remote-api by Alfresco.
the class InspectorTests method testInspectOperation.
@Test
public void testInspectOperation() {
Method aMethod = ResourceInspector.findMethod(EntityResourceAction.Read.class, SheepEntityResource.class);
ResourceOperation op = ResourceInspector.inspectOperation(SheepEntityResource.class, aMethod, HttpMethod.GET);
assertNotNull(op);
assertTrue(HttpMethod.GET.equals(op.getHttpMethod()));
assertTrue("Gets all the Sheep".equals(op.getTitle()));
assertEquals("Sheep ReadALL should return ACCEPTED", Status.STATUS_ACCEPTED, op.getSuccessStatus());
assertTrue("".equals(op.getDescription()));
assertNotNull(op.getParameters());
assertTrue(op.getParameters().size() == 7);
for (ResourceParameter aParam : op.getParameters()) {
assertNotNull(aParam.getName());
assertNotNull(aParam.getDescription());
assertNotNull(aParam.getTitle());
switch(aParam.getParamType()) {
case QUERY_STRING:
if (("requiredParam".equals(aParam.getName()))) {
assertTrue(aParam.isRequired());
}
break;
case URL_PATH:
assertTrue(("siteId".equals(aParam.getName())));
assertFalse(aParam.isRequired());
break;
case HTTP_BODY_OBJECT:
assertTrue(("body".equals(aParam.getName())));
assertFalse(aParam.isRequired());
break;
case HTTP_HEADER:
assertTrue(("who".equals(aParam.getName())));
assertFalse(aParam.isRequired());
}
}
// No @WebApiDescription or param (in future WebApiDescription will be mandatory)
aMethod = ResourceInspector.findMethod(EntityResourceAction.ReadById.class, SheepEntityResource.class);
op = ResourceInspector.inspectOperation(SheepEntityResource.class, aMethod, HttpMethod.GET);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Missing @WebApiDescription annotation"));
aMethod = ResourceInspector.findMethod(EntityResourceAction.ReadById.class, GrassEntityResource.class);
op = ResourceInspector.inspectOperation(GrassEntityResource.class, aMethod, HttpMethod.GET);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Gets grass by id"));
assertTrue("readById method should have 1 url param", op.getParameters().size() == 1);
ResourceParameter singleParam = op.getParameters().get(0);
assertTrue(ResourceParameter.KIND.URL_PATH.equals(singleParam.getParamType()));
assertFalse("URL paths can never suport multiple params, its always FALSE", singleParam.isAllowMultiple());
assertNotNull(singleParam.getDescription());
assertNotNull(singleParam.getTitle());
assertNotNull(singleParam.getName());
assertTrue(singleParam.isRequired());
aMethod = ResourceInspector.findMethod(BinaryResourceAction.Read.class, FlockEntityResource.class);
op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.GET);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Reads a photo as a Stream"));
aMethod = ResourceInspector.findMethod(BinaryResourceAction.Delete.class, FlockEntityResource.class);
op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.DELETE);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Deletes a photo"));
aMethod = ResourceInspector.findMethod(BinaryResourceAction.Update.class, FlockEntityResource.class);
op = ResourceInspector.inspectOperation(FlockEntityResource.class, aMethod, HttpMethod.PUT);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Updates a photo"));
aMethod = ResourceInspector.findMethod(BinaryResourceAction.Read.class, SheepBaaaahResource.class);
op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.GET);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Reads a photo"));
aMethod = ResourceInspector.findMethod(BinaryResourceAction.Delete.class, SheepBaaaahResource.class);
op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.DELETE);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Deletes a photo"));
aMethod = ResourceInspector.findMethod(BinaryResourceAction.Update.class, SheepBaaaahResource.class);
op = ResourceInspector.inspectOperation(SheepBaaaahResource.class, aMethod, HttpMethod.PUT);
assertNotNull(op);
assertTrue(op.getTitle().startsWith("Updates a photo"));
}
use of org.alfresco.rest.framework.core.ResourceOperation in project alfresco-remote-api by Alfresco.
the class AbstractResourceWebScript method execute.
public Object execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) {
final String entityCollectionName = ResourceInspector.findEntityCollectionNameName(resource.getMetaData());
final ResourceOperation operation = resource.getMetaData().getOperation(getHttpMethod());
final WithResponse callBack = new WithResponse(operation.getSuccessStatus(), DEFAULT_JSON_CONTENT, CACHE_NEVER);
Object toReturn = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Object>() {
@Override
public Object execute() throws Throwable {
Object result = executeAction(resource, params, callBack);
if (result instanceof BinaryResource) {
// don't postprocess it.
return result;
}
return helper.processAdditionsToTheResponse(res, resource.getMetaData().getApi(), entityCollectionName, params, result);
}
}, isReadOnly, true);
setResponse(res, callBack);
return toReturn;
}
use of org.alfresco.rest.framework.core.ResourceOperation in project alfresco-remote-api by Alfresco.
the class ResourceWebScriptDelete method execute.
@Override
public Void execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly) {
final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.DELETE);
final WithResponse callBack = new WithResponse(operation.getSuccessStatus(), DEFAULT_JSON_CONTENT, CACHE_NEVER);
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
// ignore return result
executeAction(resource, params, callBack);
return null;
}
}, false, true);
setResponse(res, callBack);
return null;
}
use of org.alfresco.rest.framework.core.ResourceOperation in project alfresco-remote-api by Alfresco.
the class ResourceWebScriptPost method extractParams.
@Override
public Params extractParams(ResourceMetadata resourceMeta, WebScriptRequest req) {
final RecognizedParams params = getRecognizedParams(req);
final String entityId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.ENTITY_ID);
final String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
final ResourceOperation operation = resourceMeta.getOperation(HttpMethod.POST);
switch(resourceMeta.getType()) {
case ENTITY:
if (StringUtils.isNotBlank(entityId)) {
throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
} else {
Object postedObj = processRequest(resourceMeta, operation, req);
return Params.valueOf(null, params, postedObj, req);
}
case RELATIONSHIP:
if (StringUtils.isNotBlank(relationshipId)) {
throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
} else {
Object postedRel = processRequest(resourceMeta, operation, req);
return Params.valueOf(entityId, params, postedRel, req);
}
case OPERATION:
final String operationName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_RESOURCE);
final String propertyName = req.getServiceMatch().getTemplateVars().get(ResourceLocator.PROPERTY);
if (StringUtils.isNotBlank(entityId) && StringUtils.isNotBlank(operationName)) {
Object postedObj = processRequest(resourceMeta, operation, req);
if (StringUtils.isNotBlank(propertyName)) {
return Params.valueOf(entityId, relationshipId, params, postedObj, req);
} else {
return Params.valueOf(entityId, params, postedObj, req);
}
}
// Fall through to unsupported.
default:
throw new UnsupportedResourceOperationException("POST not supported for Actions");
}
}
Aggregations