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

The following examples show how to use org.springframework.extensions.webscripts.servlet.WebScriptServletResponse. 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: 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 #2
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 #3
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 #4
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 #5
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 #6
Source File: ResponseWriter.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Sets the response headers with any information we know about the content
 *
 * @param res         WebScriptResponse
 * @param contentInfo Content Information
 */
default void setContentInfoOnResponse(WebScriptResponse res, ContentInfo contentInfo)
{
    if (contentInfo != null)
    {
        //Set content info on the response
        res.setContentType(contentInfo.getMimeType());
        res.setContentEncoding(contentInfo.getEncoding());

        if (res instanceof WrappingWebScriptResponse)
        {
            WrappingWebScriptResponse wrappedRes = ((WrappingWebScriptResponse) res);
            res = wrappedRes.getNext();
        }

        if (res instanceof WebScriptServletResponse)
        {
            WebScriptServletResponse servletResponse = (WebScriptServletResponse) res;
            if (contentInfo.getLength() > 0)
            {
                if (contentInfo.getLength() > 0 && contentInfo.getLength() < Integer.MAX_VALUE)
                {
                    servletResponse.getHttpServletResponse().setContentLength((int) contentInfo.getLength());
                }
            }
            if (contentInfo.getLocale() != null)
            {
                servletResponse.getHttpServletResponse().setLocale(contentInfo.getLocale());
            }
        }
    }
}
 
Example #7
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 #8
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 #9
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 #10
Source File: RemoteAuthenticatorFactoryAdminConsoleAccessTest.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
private WebScriptServletResponse prepareMockResponse()
{
    HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
    WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
    when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);
    doAnswer(new Answer()
    {
        public Object answer(InvocationOnMock invocation)
        {
            Object[] args = invocation.getArguments();
            if (args != null && args.length == 1)
            {
                setStatusCode = -1;
                try
                {
                    setStatusCode = (int) args[0];
                }
                catch (Exception e)
                {
                    logger.error("Could not get the status code: " + e.getMessage(), e);
                }
            }
            return null;
        }
    }).when(mockHttpResponse).setStatus(anyInt());
    return mockResponse;
}
 
Example #11
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 #12
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);
}
 
Example #13
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 #14
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 #15
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 #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);
}