Search in sources :

Example 11 with Match

use of org.springframework.extensions.webscripts.Match in project alfresco-remote-api by Alfresco.

the class ExecutionTests method mockRequest.

private WebScriptRequest mockRequest(Map<String, String> templateVars, final Map<String, List<String>> params) {
    final String[] paramNames = params.keySet().toArray(new String[] {});
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
    when(request.getParameterNames()).thenReturn(paramNames);
    when(request.getParameterValues(anyString())).thenAnswer(new Answer<String[]>() {

        @Override
        public String[] answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            return params.get((String) args[0]).toArray(new String[] {});
        }
    });
    return request;
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) Match(org.springframework.extensions.webscripts.Match)

Example 12 with Match

use of org.springframework.extensions.webscripts.Match in project alfresco-remote-api by Alfresco.

the class WebScriptSSOAuthenticationFilter method doFilter.

/* (non-Javadoc)
     * @see org.alfresco.repo.webdav.auth.BaseNTLMAuthenticationFilter#doFilter(javax.servlet.ServletContext, javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
public void doFilter(ServletContext context, ServletRequest sreq, ServletResponse sresp, FilterChain chain) throws IOException, ServletException {
    // Get the HTTP request/response
    HttpServletRequest req = (HttpServletRequest) sreq;
    // find a webscript match for the requested URI
    String requestURI = req.getRequestURI();
    String pathInfo = requestURI.substring((req.getContextPath() + req.getServletPath()).length());
    if (getLogger().isTraceEnabled()) {
        getLogger().trace("Processing request: " + requestURI + " SID:" + (req.getSession(false) != null ? req.getSession().getId() : null));
    }
    Match match = container.getRegistry().findWebScript(req.getMethod(), URLDecoder.decode(pathInfo));
    if (match != null && match.getWebScript() != null) {
        // the filters down the chain to require any authentication checks
        if (RequiredAuthentication.none == match.getWebScript().getDescription().getRequiredAuthentication()) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Found webscript with no authentication - set NO_AUTH_REQUIRED flag.");
            }
            req.setAttribute(NO_AUTH_REQUIRED, Boolean.TRUE);
        }
    }
    chain.doFilter(sreq, sresp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Match(org.springframework.extensions.webscripts.Match)

Example 13 with Match

use of org.springframework.extensions.webscripts.Match 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);
    extractor.setLocator(locator);
    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 14 with Match

use of org.springframework.extensions.webscripts.Match 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);
    extractor.setLocator(locator);
    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 15 with Match

use of org.springframework.extensions.webscripts.Match in project alfresco-remote-api by Alfresco.

the class BlockingRemoteUserMapper method checkExtAuthStillWorks.

private void checkExtAuthStillWorks(RequiredAuthentication required, Set<String> families) {
    blockingRemoteUserMapper.reset();
    DefaultRemoteUserMapper defaultRemoteUserMapper = new DefaultRemoteUserMapper();
    defaultRemoteUserMapper.setActive(true);
    defaultRemoteUserMapper.setProxyUserName(null);
    defaultRemoteUserMapper.setPersonService(personService);
    remoteUserAuthenticatorFactory.setRemoteUserMapper(defaultRemoteUserMapper);
    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
    when(mockHttpRequest.getScheme()).thenReturn("http");
    final String userName = "RAFACAT_usr_" + (int) (Math.random() * 1000);
    when(mockHttpRequest.getHeader(proxyHeader)).thenReturn(userName);
    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);
    WebScript mockWebScript = mock(WebScript.class);
    Match mockMatch = new Match("fake", Collections.EMPTY_MAP, "whatever", mockWebScript);
    when(mockRequest.getServiceMatch()).thenReturn(mockMatch);
    Description mockDescription = mock(Description.class);
    when(mockWebScript.getDescription()).thenReturn(mockDescription);
    when(mockDescription.getFamilys()).thenReturn(families);
    WebScriptServletResponse mockResponse = prepareMockResponse();
    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    final boolean authenticated = authenticator.authenticate(required, false);
    assertTrue("This should be authenticating with external auth", authenticated);
    assertFalse("We have been using the DefaultRemoteUserMapper, so our BlockingRemoteUserMapper shouldn't have been called", blockingRemoteUserMapper.isWasInterrupted());
    assertEquals("BlockingRemoteUserMapper shouldn't have been called", blockingRemoteUserMapper.getTimePassed(), 0);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Description(org.springframework.extensions.webscripts.Description) DefaultRemoteUserMapper(org.alfresco.repo.security.authentication.external.DefaultRemoteUserMapper) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) WebScript(org.springframework.extensions.webscripts.WebScript) Authenticator(org.springframework.extensions.webscripts.Authenticator) Match(org.springframework.extensions.webscripts.Match)

Aggregations

Match (org.springframework.extensions.webscripts.Match)15 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)8 HashMap (java.util.HashMap)6 Params (org.alfresco.rest.framework.resource.parameters.Params)6 Test (org.junit.Test)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Content (org.springframework.extensions.surf.util.Content)4 StringReader (java.io.StringReader)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 List (java.util.List)2 ResourceWebScriptGet (org.alfresco.rest.framework.webscripts.ResourceWebScriptGet)2 Matchers.anyString (org.mockito.Matchers.anyString)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Description (org.springframework.extensions.webscripts.Description)2 WebScript (org.springframework.extensions.webscripts.WebScript)2 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)2 File (java.io.File)1 PrintWriter (java.io.PrintWriter)1