Search in sources :

Example 11 with WebScriptServletRequest

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

the class RemoteAuthenticatorFactoryTest method testEnabledUser.

@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));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) Authenticator(org.springframework.extensions.webscripts.Authenticator) Test(org.junit.Test)

Example 12 with WebScriptServletRequest

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

the class RemoteAuthenticatorFactoryTest method testDisabledUser.

@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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) Authenticator(org.springframework.extensions.webscripts.Authenticator) Test(org.junit.Test)

Example 13 with WebScriptServletRequest

use of org.springframework.extensions.webscripts.servlet.WebScriptServletRequest in project records-management by Alfresco.

the class ImportPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    // Unwrap to a WebScriptServletRequest if we have one
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    // get the content type of request and ensure it's multipart/form-data
    String contentType = req.getContentType();
    if (MULTIPART_FORMDATA.equals(contentType) && webScriptServletRequest != null) {
        String nodeRef = req.getParameter(PARAM_DESTINATION);
        if (nodeRef == null || nodeRef.length() == 0) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'destination' parameter was not provided in form data");
        }
        // create and check noderef
        final NodeRef destination = new NodeRef(nodeRef);
        if (nodeService.exists(destination)) {
            // check the destination is an RM container
            if (!nodeService.hasAspect(destination, RecordsManagementModel.ASPECT_FILE_PLAN_COMPONENT) || !dictionaryService.isSubClass(nodeService.getType(destination), ContentModel.TYPE_FOLDER)) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "NodeRef '" + destination + "' does not represent an Records Management container node.");
            }
        } else {
            status.setCode(HttpServletResponse.SC_NOT_FOUND, "NodeRef '" + destination + "' does not exist.");
        }
        // as there is no 'import capability' and the RM admin user is different from
        // the DM admin user (meaning the webscript 'admin' authentication can't be used)
        // perform a manual check here to ensure the current user has the RM admin role.
        boolean isAdmin = filePlanRoleService.hasRMAdminRole(filePlanService.getFilePlan(destination), AuthenticationUtil.getRunAsUser());
        if (!isAdmin) {
            throw new WebScriptException(Status.STATUS_FORBIDDEN, "Access Denied");
        }
        File acpFile = null;
        try {
            // create a temporary file representing uploaded ACP file
            FormField acpContent = webScriptServletRequest.getFileField(PARAM_ARCHIVE);
            if (acpContent == null) {
                acpContent = webScriptServletRequest.getFileField(PARAM_FILEDATA);
                if (acpContent == null) {
                    throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'archive' file content was not provided in form data");
                }
            }
            acpFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, "." + ACPExportPackageHandler.ACP_EXTENSION);
            // copy contents of uploaded file to temp ACP file
            FileOutputStream fos = new FileOutputStream(acpFile);
            // NOTE: this method closes both streams
            FileCopyUtils.copy(acpContent.getInputStream(), fos);
            if (logger.isDebugEnabled()) {
                logger.debug("Importing uploaded ACP (" + acpFile.getAbsolutePath() + ") into " + nodeRef);
            }
            // setup the import handler
            final ACPImportPackageHandler importHandler = new ACPImportPackageHandler(acpFile, "UTF-8");
            // import the ACP file as the system user
            AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

                public NodeRef doWork() {
                    importerService.importView(importHandler, new Location(destination), null, null);
                    return null;
                }
            }, AuthenticationUtil.getSystemUserName());
            // create and return model
            Map<String, Object> model = new HashMap<String, Object>(1);
            model.put("success", true);
            return model;
        } catch (FileNotFoundException fnfe) {
            throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Failed to import ACP file", fnfe);
        } catch (IOException ioe) {
            throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Failed to import ACP file", ioe);
        } finally {
            if (acpFile != null) {
                acpFile.delete();
            }
        }
    } else {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Request is not " + MULTIPART_FORMDATA + " encoded");
    }
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ACPImportPackageHandler(org.alfresco.repo.importer.ACPImportPackageHandler) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) FileOutputStream(java.io.FileOutputStream) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) File(java.io.File) FormField(org.springframework.extensions.webscripts.servlet.FormData.FormField) Location(org.alfresco.service.cmr.view.Location)

Aggregations

WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)9 WrappingWebScriptRequest (org.springframework.extensions.webscripts.WrappingWebScriptRequest)9 TransferException (org.alfresco.service.cmr.transfer.TransferException)7 StringWriter (java.io.StringWriter)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Test (org.junit.Test)3 Authenticator (org.springframework.extensions.webscripts.Authenticator)3 JSONWriter (org.springframework.extensions.webscripts.json.JSONWriter)3 WebScriptServletResponse (org.springframework.extensions.webscripts.servlet.WebScriptServletResponse)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 HashMap (java.util.HashMap)2 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)2