Search in sources :

Example 21 with Params

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);
    }
}
Also used : HashMap(java.util.HashMap) Params(org.alfresco.rest.framework.resource.parameters.Params) BinaryResourceAction(org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction) RelationshipResourceBinaryAction(org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) FileBinaryResource(org.alfresco.rest.framework.resource.content.FileBinaryResource) NodeBinaryResource(org.alfresco.rest.framework.resource.content.NodeBinaryResource) BinaryResource(org.alfresco.rest.framework.resource.content.BinaryResource) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) TenantUtil(org.alfresco.repo.tenant.TenantUtil) ResourceWithMetadata(org.alfresco.rest.framework.core.ResourceWithMetadata) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException)

Example 22 with Params

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());
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) FormData(org.springframework.extensions.webscripts.servlet.FormData) HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Params(org.alfresco.rest.framework.resource.parameters.Params) MultiPartRequest(org.alfresco.rest.api.tests.util.MultiPartBuilder.MultiPartRequest) Match(org.springframework.extensions.webscripts.Match) ResourceWebScriptPost(org.alfresco.rest.framework.webscripts.ResourceWebScriptPost) File(java.io.File) FileData(org.alfresco.rest.api.tests.util.MultiPartBuilder.FileData) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 23 with Params

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());
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) HashMap(java.util.HashMap) UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) Params(org.alfresco.rest.framework.resource.parameters.Params) Match(org.springframework.extensions.webscripts.Match) Content(org.springframework.extensions.surf.util.Content) StringReader(java.io.StringReader) ResourceWebScriptPut(org.alfresco.rest.framework.webscripts.ResourceWebScriptPut) Farmer(org.alfresco.rest.framework.tests.api.mocks.Farmer) Test(org.junit.Test)

Example 24 with Params

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;
}
Also used : Params(org.alfresco.rest.framework.resource.parameters.Params)

Example 25 with 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;
}
Also used : Params(org.alfresco.rest.framework.resource.parameters.Params)

Aggregations

Params (org.alfresco.rest.framework.resource.parameters.Params)30 Test (org.junit.Test)23 BaseUnitTest (org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest)13 HashMap (java.util.HashMap)8 Parameters (org.alfresco.rest.framework.resource.parameters.Parameters)8 RMSite (org.alfresco.rm.rest.api.model.RMSite)8 SiteUpdate (org.alfresco.rest.api.model.SiteUpdate)7 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)7 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)6 Match (org.springframework.extensions.webscripts.Match)6 StringReader (java.io.StringReader)3 InvalidParameterException (java.security.InvalidParameterException)3 List (java.util.List)3 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)3 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)3 ResourceWebScriptPost (org.alfresco.rest.framework.webscripts.ResourceWebScriptPost)3 Content (org.springframework.extensions.surf.util.Content)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BeanPropertiesFilter (org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter)2