use of org.openntf.nsfodp.exporter.ODPExporter in project org.openntf.nsfodp by OpenNTF.
the class ExporterApplication method start.
@Override
public Object start(IApplicationContext context) throws Exception {
String notesIni = System.getenv(NSFODPConstants.PROP_NOTESINI);
if (notesIni != null && !notesIni.isEmpty()) {
// $NON-NLS-1$
String execDir = System.getenv("Notes_ExecDirectory");
try (NotesAPI api = NotesAPI.get()) {
// $NON-NLS-1$
api.NotesInitExtended(execDir, "=" + notesIni);
}
}
String databasePath = System.getenv(NSFODPConstants.PROP_EXPORTER_DATABASE_PATH);
if (databasePath == null) {
throw new IllegalArgumentException(MessageFormat.format(Messages.ExporterApplication_dbPathCannotBeEmpty, NSFODPConstants.PROP_EXPORTER_DATABASE_PATH));
}
Path odpDir = Paths.get(System.getenv(NSFODPConstants.PROP_OUTPUTFILE));
// $NON-NLS-1$
boolean binaryDxl = "true".equals(System.getenv(NSFODPConstants.PROP_EXPORTER_BINARY_DXL));
// $NON-NLS-1$
boolean swiperFilter = "true".equals(System.getenv(NSFODPConstants.PROP_EXPORTER_SWIPER_FILTER));
// $NON-NLS-1$
boolean richTextAsItemData = "true".equals(System.getenv(NSFODPConstants.PROP_RICH_TEXT_AS_ITEM_DATA));
String projectName = System.getenv(NSFODPConstants.PROP_PROJECT_NAME);
NotesThread runner = new NotesThread(() -> {
try (NotesAPI session = NotesAPI.get()) {
try (NDatabase database = session.openDatabase(databasePath)) {
ODPExporter exporter = new ODPExporter(database);
exporter.setBinaryDxl(binaryDxl);
exporter.setSwiperFilter(swiperFilter);
exporter.setRichTextAsItemData(richTextAsItemData);
exporter.setProjectName(projectName);
Path result = exporter.export();
// $NON-NLS-1$
Path eclipseProject = odpDir.resolve(".project");
if (Files.exists(eclipseProject)) {
// $NON-NLS-1$ //$NON-NLS-2$
Path tempPath = Files.createTempFile("nsfodp", ".project");
Files.move(eclipseProject, tempPath, StandardCopyOption.REPLACE_EXISTING);
eclipseProject = tempPath;
} else {
eclipseProject = null;
}
if (Files.exists(odpDir)) {
NSFODPUtil.deltree(Collections.singleton(odpDir));
}
Files.createDirectories(odpDir);
NSFODPUtil.moveDirectory(result, odpDir);
if (eclipseProject != null) {
// $NON-NLS-1$
Files.move(eclipseProject, odpDir.resolve(".project"), StandardCopyOption.REPLACE_EXISTING);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
});
runner.run();
runner.join();
return EXIT_OK;
}
use of org.openntf.nsfodp.exporter.ODPExporter in project org.openntf.nsfodp by OpenNTF.
the class ODPExporterServlet method handle.
protected void handle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// $NON-NLS-1$
boolean post = "POST".equals(req.getMethod());
Principal user = req.getUserPrincipal();
resp.setBufferSize(0);
resp.setStatus(HttpServletResponse.SC_OK);
ServletOutputStream os = resp.getOutputStream();
Set<Path> cleanup = new HashSet<>();
try {
if (!ALLOW_ANONYMOUS && "Anonymous".equalsIgnoreCase(user.getName())) {
// $NON-NLS-1$
resp.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// $NON-NLS-1$
resp.setContentType("text/plain");
os.println(Messages.ODPExporterServlet_anonymousAccessDisallowed);
return;
}
try (NotesAPI session = NotesAPI.get()) {
NDatabase database;
if (post) {
// Then read the NSF from the body
String contentType = req.getContentType();
if (!"application/octet-stream".equals(contentType)) {
// $NON-NLS-1$
throw new IllegalArgumentException(MessageFormat.format(Messages.ODPExporterServlet_mismatchedContentType, NSFODPConstants.HEADER_DATABASE_PATH, contentType));
}
// $NON-NLS-1$
Path nsfFile = Files.createTempFile(NSFODPUtil.getTempDirectory(), getClass().getName(), ".nsf");
cleanup.add(nsfFile);
try (InputStream reqInputStream = req.getInputStream()) {
Files.copy(reqInputStream, nsfFile, StandardCopyOption.REPLACE_EXISTING);
}
database = session.openDatabase(nsfFile.toString());
} else {
// Then look for an NSF path in the headers
String databasePath = req.getHeader(NSFODPConstants.HEADER_DATABASE_PATH);
if (StringUtil.isEmpty(databasePath)) {
throw new IllegalArgumentException(MessageFormat.format(Messages.ODPExporterServlet_dbPathMissing, NSFODPConstants.HEADER_DATABASE_PATH));
}
// Verify that the user can indeed export this DB
try (NotesAPI userApi = NotesAPI.get(user.getName(), false, false)) {
try (NDatabase userDb = userApi.openDatabase(databasePath)) {
if (userDb.getCurrentAccessLevel() < 5) {
// Designer access
throw new UnsupportedOperationException(MessageFormat.format(Messages.ODPExporterServlet_insufficientAccess, user.getName(), databasePath));
}
}
}
database = session.openDatabase(databasePath);
}
try {
IProgressMonitor mon = new LineDelimitedJsonProgressMonitor(os);
ODPExporter exporter = new ODPExporter(database);
String binaryDxl = req.getHeader(NSFODPConstants.HEADER_BINARY_DXL);
if ("true".equals(binaryDxl)) {
// $NON-NLS-1$
exporter.setBinaryDxl(true);
}
String swiperFilter = req.getHeader(NSFODPConstants.HEADER_SWIPER_FILTER);
if ("true".equals(swiperFilter)) {
// $NON-NLS-1$
exporter.setSwiperFilter(true);
}
String richTextAsItemData = req.getHeader(NSFODPConstants.HEADER_RICH_TEXT_AS_ITEM_DATA);
if ("true".equals(richTextAsItemData)) {
// $NON-NLS-1$
exporter.setRichTextAsItemData(true);
}
exporter.setProjectName(req.getHeader(NSFODPConstants.HEADER_PROJECT_NAME));
exporter.setOdpType(ODPType.ZIP);
Path result = exporter.export();
cleanup.add(result);
mon.done();
Files.copy(result, os);
} finally {
if (post) {
String filePath = database.getFilePath();
database.close();
session.deleteDatabase(filePath);
}
}
}
} catch (Throwable e) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(baos);
e.printStackTrace(out);
out.flush();
os.println(LineDelimitedJsonProgressMonitor.message(// $NON-NLS-1$ //$NON-NLS-2$
"type", // $NON-NLS-1$ //$NON-NLS-2$
"error", // $NON-NLS-1$
"stackTrace", // $NON-NLS-1$
baos.toString()));
} finally {
NSFODPUtil.deltree(cleanup);
}
}
Aggregations