use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class AbstractResourceWebScript method execute.
@SuppressWarnings("rawtypes")
@Override
public void execute(final Api api, final WebScriptRequest req, final WebScriptResponse res) throws IOException {
try {
final Map<String, Object> respons = new HashMap<String, Object>();
final Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
final ResourceWithMetadata resource = locator.locateResource(api, templateVars, httpMethod);
final Params params = paramsExtractor.extractParams(resource.getMetaData(), req);
final boolean isReadOnly = HttpMethod.GET == httpMethod;
// This execution usually takes place in a Retrying Transaction (see subclasses)
final Object toSerialize = execute(resource, params, res, isReadOnly);
// Outside the transaction.
if (toSerialize != null) {
if (toSerialize instanceof BinaryResource) {
// TODO review (experimental) - can we move earlier & wrap complete execute ? Also for QuickShare (in MT/Cloud) needs to be tenant for the nodeRef (TBC).
boolean noAuth = false;
if (BinaryResourceAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
noAuth = resource.getMetaData().isNoAuth(BinaryResourceAction.Read.class);
} else if (RelationshipResourceBinaryAction.Read.class.isAssignableFrom(resource.getResource().getClass())) {
noAuth = resource.getMetaData().isNoAuth(RelationshipResourceBinaryAction.Read.class);
} else {
logger.warn("Unexpected");
}
if (noAuth) {
String networkTenantDomain = TenantUtil.getCurrentDomain();
TenantUtil.runAsSystemTenant(new TenantUtil.TenantRunAsWork<Void>() {
public Void doWork() throws Exception {
streamResponse(req, res, (BinaryResource) toSerialize);
return null;
}
}, networkTenantDomain);
} else {
streamResponse(req, res, (BinaryResource) toSerialize);
}
} else {
renderJsonResponse(res, toSerialize, assistant.getJsonHelper());
}
}
} catch (AlfrescoRuntimeException | ApiException | WebScriptException xception) {
renderException(xception, res, assistant);
} catch (RuntimeException runtimeException) {
renderException(runtimeException, res, assistant);
}
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testMultiPartPostExtractor.
@Test
public void testMultiPartPostExtractor() throws Exception {
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
extractor.setAssistant(assistant);
Map<String, String> templateVars = new HashMap<String, String>();
WebScriptRequest request = mock(WebScriptRequest.class);
when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
File file = TempFileProvider.createTempFile("ParamsExtractorTests-", ".txt");
PrintWriter writer = new PrintWriter(file);
writer.println("Multipart Mock test.");
writer.close();
MultiPartRequest reqBody = MultiPartBuilder.create().setFileData(new FileData(file.getName(), file, MimetypeMap.MIMETYPE_TEXT_PLAIN)).build();
MockHttpServletRequest mockRequest = new MockHttpServletRequest("POST", "");
mockRequest.setContent(reqBody.getBody());
mockRequest.setContentType(reqBody.getContentType());
when(request.getContentType()).thenReturn("multipart/form-data");
when(request.parseContent()).thenReturn(new FormData(mockRequest));
Params params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
Object passed = params.getPassedIn();
assertNotNull(passed);
assertTrue(FormData.class.isAssignableFrom(passed.getClass()));
FormData formData = (FormData) passed;
assertTrue(formData.getIsMultiPart());
// 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) {
assertNotNull(uoe);
}
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
passed = params.getPassedIn();
assertNotNull(passed);
assertTrue(FormData.class.isAssignableFrom(passed.getClass()));
formData = (FormData) passed;
assertTrue(formData.getIsMultiPart());
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testPutExtractor.
@Test
public void testPutExtractor() throws IOException {
// Put together the stubs
ResourceWebScriptPut extractor = new ResourceWebScriptPut();
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);
when(request.getContentType()).thenReturn("application/pdf; charset=UTF-16BE");
Params params = null;
try {
params = extractor.extractParams(mockEntity(), request);
fail("Should not get here. PUT is executed against the instance URL");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
try {
params = extractor.extractParams(mockRelationship(), request);
fail("Should not get here. PUT is executed against the instance URL");
} catch (UnsupportedResourceOperationException uoe) {
// Must throw this exception
assertNotNull(uoe);
}
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
// Put single entity wrapped in array
params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
Object passed = params.getPassedIn();
assertNotNull(passed);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passed.getClass()));
assertNotNull(params.getFilter());
assertTrue("Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass()));
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
params = extractor.extractParams(mockEntity(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
passed = params.getPassedIn();
assertNotNull(passed);
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "67890");
params = extractor.extractParams(mockRelationship(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
passed = params.getPassedIn();
assertNotNull(passed);
assertTrue("A Farmer was passed in.", Farmer.class.equals(passed.getClass()));
Farmer aFarmer = (Farmer) passed;
assertEquals("Relationship id should be automatically set on the object passed in.", aFarmer.getId(), "67890");
// reset the reader
when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON));
params = testExtractAddressedParams(templateVars, request, extractor);
assertEquals("UTF-16BE", params.getContentInfo().getEncoding());
assertEquals(MimetypeMap.MIMETYPE_PDF, params.getContentInfo().getMimeType());
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testExtractOperationParams.
private Params testExtractOperationParams(Map<String, String> templateVars, WebScriptRequest request, ParamsExtractor extractor) {
templateVars.clear();
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "codfish");
Params params = extractor.extractParams(mockOperation(), request);
assertNotNull(params);
assertNull("For a Collection there should be no relationshipId params.", params.getRelationshipId());
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "9865");
templateVars.put(ResourceLocator.PROPERTY, "monkFish");
params = extractor.extractParams(mockOperation(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
assertEquals("9865", params.getRelationshipId());
return params;
}
use of org.alfresco.rest.framework.resource.parameters.Params in project alfresco-remote-api by Alfresco.
the class ParamsExtractorTests method testExtractAddressedParams.
private Params testExtractAddressedParams(Map<String, String> templateVars, WebScriptRequest request, ParamsExtractor extractor) {
templateVars.clear();
templateVars.put(ResourceLocator.ENTITY_ID, "1234");
templateVars.put(ResourceLocator.RELATIONSHIP_RESOURCE, "codfish");
Params params = extractor.extractParams(mockProperty(), request);
assertNotNull(params);
assertTrue(params.hasBinaryProperty("codfish"));
assertFalse(params.hasBinaryProperty("something"));
assertEquals("codfish", params.getBinaryProperty());
templateVars.put(ResourceLocator.RELATIONSHIP_ID, "9865");
templateVars.put(ResourceLocator.PROPERTY, "monkFish");
params = extractor.extractParams(mockProperty(), request);
assertNotNull(params);
assertEquals("1234", params.getEntityId());
assertEquals("9865", params.getRelationshipId());
assertTrue(params.hasBinaryProperty("monkFish"));
return params;
}
Aggregations