use of won.owner.web.events.OnExportUserEvent in project webofneeds by researchstudio-sat.
the class RestUserController method exportAccount.
@ResponseBody
@RequestMapping(value = "/exportAccount", method = RequestMethod.POST)
public ResponseEntity exportAccount(@RequestParam(name = "keyStorePassword", required = false) String keyStorePassword) {
logger.debug("processing request to /exportAccount");
SecurityContext securityContext = SecurityContextHolder.getContext();
User authUser = ((KeystoreEnabledUserDetails) securityContext.getAuthentication().getPrincipal()).getUser();
User user = userService.getByUsername(authUser.getUsername());
String responseEmail = null;
if (user.isEmailVerified()) {
responseEmail = user.getEmail();
} else {
return generateStatusResponse(RestStatusResponse.EXPORT_NOT_VERIFIED);
}
eventPublisher.publishEvent(new OnExportUserEvent(securityContext.getAuthentication(), keyStorePassword, responseEmail));
return generateStatusResponse(RestStatusResponse.EXPORT_SUCCESS);
}
use of won.owner.web.events.OnExportUserEvent in project webofneeds by researchstudio-sat.
the class ExportListener method onApplicationEvent.
@Override
public void onApplicationEvent(OnExportUserEvent onExportUserEvent) {
Authentication authentication = onExportUserEvent.getAuthentication();
KeystoreEnabledUserDetails userDetails = ((KeystoreEnabledUserDetails) authentication.getPrincipal());
String password = onExportUserEvent.getKeyStorePassword();
User user = userService.getByUsername(userDetails.getUsername());
String responseMail = onExportUserEvent.getResponseEmail();
File tmpFile = null;
try {
tmpFile = File.createTempFile("won", null);
tmpFile.deleteOnExit();
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(tmpFile), Charset.forName("UTF-8"));
ZipEntry atomsEntry = new ZipEntry("export.nq");
zip.putNextEntry(atomsEntry);
user.getUserAtoms().stream().parallel().map(userAtom -> fetchAtomData(authentication, userAtom.getUri())).forEach(dataset -> {
RDFDataMgr.write(zip, dataset, RDFFormat.NQUADS_UTF8);
});
zip.closeEntry();
ZipEntry keystoreEntry = new ZipEntry("keystore.jks");
zip.putNextEntry(keystoreEntry);
if (password != null && !password.isEmpty()) {
ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
userDetails.getKeyStore().store(tmpStream, password.toCharArray());
tmpStream.writeTo(zip);
} else {
zip.write("You need to supply a keyStorePassword to get your keystore for security reasons".getBytes());
}
zip.closeEntry();
zip.close();
emailSender.sendExportMessage(onExportUserEvent.getResponseEmail(), tmpFile);
} catch (LinkedDataFetchingException e) {
logger.warn(e.getMessage());
emailSender.sendExportFailedMessage(responseMail);
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
emailSender.sendExportFailedMessage(responseMail);
throw new RuntimeException(e);
} catch (Exception e) {
emailSender.sendExportFailedMessage(responseMail);
throw e;
} finally {
if (tmpFile != null) {
tmpFile.delete();
}
}
}
Aggregations