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));
}
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);
}
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");
}
}
Aggregations