org.springframework.extensions.webscripts.servlet.WebScriptServletRequest Java Examples

The following examples show how to use org.springframework.extensions.webscripts.servlet.WebScriptServletRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceWebScriptPut.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the input stream for the request
 * @param req WebScriptRequest
 * @return InputStream
 */
private InputStream getStream(WebScriptRequest req)
{
    try
    {
        if (req instanceof WebScriptServletRequest)
        {
            WebScriptServletRequest servletRequest = (WebScriptServletRequest) req;
            return servletRequest.getHttpServletRequest().getInputStream();
        }
        else if (req instanceof WrappingWebScriptRequest)
        {
            // eg. BufferredRequest
            WrappingWebScriptRequest wrappedRequest = (WrappingWebScriptRequest) req;
            return wrappedRequest.getContent().getInputStream();
        }
    }
    catch (IOException error)
    {
        logger.warn("Failed to get the input stream.", error);
    }

    return null;
}
 
Example #2
Source File: LocalWebScriptConnectorServiceImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Authenticator create(WebScriptServletRequest req, WebScriptServletResponse res)
{
    // Do we have current details?
    if (AuthenticationUtil.getFullyAuthenticatedUser() != null)
    {
        // There are already details existing
        // Allow these to be kept and used
        String fullUser = AuthenticationUtil.getFullyAuthenticatedUser();
        logger.debug("Existing Authentication found, remaining as " + fullUser);
        return new LocalTestRunAsAuthenticator(fullUser);
    }
    
    // Fall back to the http auth one
    logger.debug("No existing Authentication found, using regular HTTP Auth");
    return httpAuthFactory.create(req, res);
}
 
Example #3
Source File: RemoteAuthenticatorFactoryAdminConsoleAccessTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
private WebScriptServletRequest prepareMockRequest(Set<String> families, String headerToAdd)
{
    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
    when(mockHttpRequest.getScheme()).thenReturn("http");
    if (headerToAdd != null)
    {
        when(mockHttpRequest.getHeader("Authorization")).thenReturn(headerToAdd);
    }
    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);
    return mockRequest;
}
 
Example #4
Source File: RemoteAuthenticatorFactoryTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testLogInWithNonExistingPerson()
{
    // Random non existing person
    final String username = GUID.generate();

    // Mock a request with a username in the header
    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
    when(mockHttpRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(username);
    when(mockHttpRequest.getScheme()).thenReturn("http");
    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);

    HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
    WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
    when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);

    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    assertTrue("The non existing user should be authenticated.", authenticator.authenticate(RequiredAuthentication.user, false));
    assertTrue("The user should be auto created.", personService.personExists(username));
}
 
Example #5
Source File: LocalTestRunAsAuthenticatorFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Authenticator create(WebScriptServletRequest req, WebScriptServletResponse res)
{
    String runAsUser = AuthenticationUtil.getRunAsUser();
    if (runAsUser == null)
    {
        runAsUser = AuthenticationUtil.getSystemUserName();
    }
    return new LocalTestRunAsAuthenticator(runAsUser);
}
 
Example #6
Source File: BasicHttpAuthenticatorFactory.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Construct
 * 
 * @param req WebScriptServletRequest
 * @param res WebScriptServletResponse
 * @param listener AuthenticationListener
 */
public BasicHttpAuthenticator(WebScriptServletRequest req, WebScriptServletResponse res, AuthenticationListener listener)
{
    this.servletReq = req;
    this.servletRes = res;
    
    HttpServletRequest httpReq = servletReq.getHttpServletRequest();
    
    this.listener = listener;
    
    this.authorization = httpReq.getHeader("Authorization");
    this.ticket = httpReq.getParameter("alf_ticket");
}
 
Example #7
Source File: ParamsHandlerMethodArgumentResolver.java    From alfresco-mvc with Apache License 2.0 5 votes vote down vote up
@Override
public Params resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
		NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
	Object nativeRequest = webRequest.getNativeRequest();
	if (nativeRequest instanceof WebscriptRequestWrapper) {
		WebScriptServletRequest webScriptServletRequest = ((WebscriptRequestWrapper) nativeRequest)
				.getWebScriptServletRequest();
		return AlfrescoApiResponseInterceptor.getDefaultParameters(webScriptServletRequest);
	}
	return AlfrescoApiResponseInterceptor.getDefaultParameters(null);
}
 
Example #8
Source File: AlfrescoApiResponseInterceptor.java    From alfresco-mvc with Apache License 2.0 5 votes vote down vote up
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
		Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
		ServerHttpResponse response) {

	boolean useAlfrescoResponse = globalAlfrescoResponse;

	if (!useAlfrescoResponse) {
		AlfrescoRestResponse methodAnnotation = returnType.getMethodAnnotation(AlfrescoRestResponse.class);
		if (methodAnnotation == null) {
			methodAnnotation = returnType.getContainingClass().getAnnotation(AlfrescoRestResponse.class);
		}

		if (methodAnnotation != null) {
			useAlfrescoResponse = true;
		}
	}

	if (useAlfrescoResponse) {
		if (!(request instanceof ServletServerHttpRequest)) {
			throw new RuntimeException(
					"the request must be an instance of org.springframework.http.server.ServletServerHttpRequest");
		}

		HttpServletRequest r = ((ServletServerHttpRequest) request).getServletRequest();

		if (!(r instanceof WebscriptRequestWrapper)) {
			throw new RuntimeException(
					"the request must be an instance of com.gradecak.alfresco.mvc.webscript.DispatcherWebscript.WebscriptRequestWrapper. It seems the request is not coming from Alfresco @MVC");
		}

		WebScriptServletRequest a = ((WebscriptRequestWrapper) r).getWebScriptServletRequest();

		return webscriptHelper.processAdditionsToTheResponse(null, null, null, getDefaultParameters(a), body);
	}

	return body;
}
 
Example #9
Source File: PublicApiAuthenticatorFactory.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Construct
 * 
 * @param req WebScriptServletRequest
 * @param res WebScriptServletResponse
 * @param proxyListener ProxyListener
 */
public PublicApiAuthenticator(WebScriptServletRequest req, WebScriptServletResponse res, ProxyListener proxyListener)
{
    super(req, res, proxyListener);
    if (!(req instanceof TenantWebScriptServletRequest))
    {
        throw new WebScriptException("Request is not a tenant aware request");
    }
    servletReq = (TenantWebScriptServletRequest)req;
    this.proxyListener = proxyListener; 
}
 
Example #10
Source File: RemoteAuthenticatorFactoryAdminConsoleAccessTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean authenticate(Set<String> families, String headerToAdd)
{
    WebScriptServletRequest mockRequest = prepareMockRequest(families, headerToAdd);
    WebScriptServletResponse mockResponse = prepareMockResponse();

    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    return authenticator.authenticate(RequiredAuthentication.admin, false);
}
 
Example #11
Source File: RemoteAuthenticatorFactoryAdminConsoleAccessTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean authenticateWithGuestParameters(RequiredAuthentication required, boolean isGuest)
{
    blockingRemoteUserMapper.reset();

    WebScriptServletRequest mockRequest = prepareMockRequest(Collections.emptySet(), null);
    WebScriptServletResponse mockResponse = prepareMockResponse();

    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    return authenticator.authenticate(required, isGuest);
}
 
Example #12
Source File: RemoteAuthenticatorFactoryAdminConsoleAccessTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #13
Source File: RemoteAuthenticatorFactoryTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testEnabledUser() throws Exception
{
    final String username = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<String>()
    {
        @Override
        public String execute() throws Throwable
        {
            return AuthenticationUtil.runAs(new RunAsWork<String>()
            {
                @Override
                public String doWork() throws Exception
                {
                    return createPerson(true);
                }
            }, AuthenticationUtil.SYSTEM_USER_NAME);
        }
    }, false, true);

    // Mock a request with a username in the header
    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
    when(mockHttpRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(username);
    when(mockHttpRequest.getScheme()).thenReturn("http");
    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);

    HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
    WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
    when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);

    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    assertTrue(authenticator.authenticate(RequiredAuthentication.user, false));
}
 
Example #14
Source File: DispatcherWebscript.java    From alfresco-mvc with Apache License 2.0 4 votes vote down vote up
public WebScriptServletRequest getWebScriptServletRequest() {
	return origReq;
}
 
Example #15
Source File: DispatcherWebscript.java    From alfresco-mvc with Apache License 2.0 4 votes vote down vote up
public WebscriptRequestWrapper(WebScriptServletRequest request) {
	super(request.getHttpServletRequest());
	this.origReq = request;
}
 
Example #16
Source File: RemoteAuthenticatorFactoryTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Test
public void testDisabledUser() throws Exception
{
    final String username = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<String>()
    {
        @Override
        public String execute() throws Throwable
        {
            return AuthenticationUtil.runAs(new RunAsWork<String>()
            {
                @Override
                public String doWork() throws Exception
                {
                    return createPerson(false);
                }
            }, AuthenticationUtil.SYSTEM_USER_NAME);
        }
    }, false, true);

    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>()
    {
        @Override
        public Void execute() throws Throwable
        {
            return AuthenticationUtil.runAs(new RunAsWork<Void>()
            {
                @Override
                public Void doWork() throws Exception
                {
                    // Mock a request with a username in the header
                    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
                    when(mockHttpRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(username);
                    when(mockHttpRequest.getScheme()).thenReturn("http");
                    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
                    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);

                    HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
                    WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
                    when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);

                    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
                    assertFalse(authenticator.authenticate(RequiredAuthentication.user, false));

                    return null;
                }
            }, AuthenticationUtil.SYSTEM_USER_NAME);
        }
    }, false, true);
}
 
Example #17
Source File: PublicApiAuthenticatorFactory.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Authenticator create(WebScriptServletRequest req, WebScriptServletResponse res)
{
    return new PublicApiAuthenticator(req, res, new ProxyListener());
}
 
Example #18
Source File: RemoteUserAuthenticatorFactory.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
public RemoteUserAuthenticator(WebScriptServletRequest req, WebScriptServletResponse res, AuthenticationListener listener)
{
    super(req, res, listener);
}
 
Example #19
Source File: RemoteUserAuthenticatorFactory.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Authenticator create(WebScriptServletRequest req, WebScriptServletResponse res)
{
    return new RemoteUserAuthenticator(req, res, this.listener);
}
 
Example #20
Source File: BasicHttpAuthenticatorFactory.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
public Authenticator create(WebScriptServletRequest req, WebScriptServletResponse res)
{
    return new BasicHttpAuthenticator(req, res, listener);
}