use of org.jdom2.JDOMException in project JMRI by JMRI.
the class WebServerPreferences method openFile.
public final void openFile(String fileName) throws FileNotFoundException {
WebServerPreferencesXml prefsXml = new WebServerPreferencesXml();
File file = new File(fileName);
Element root;
try {
root = prefsXml.rootFromFile(file);
} catch (FileNotFoundException ex) {
log.debug("Could not find Web Server preferences file. Normal if preferences have not been saved before.");
throw ex;
} catch (IOException | JDOMException ex) {
log.error("Exception while loading web server preferences: " + ex);
root = null;
}
if (root != null) {
load(root);
}
}
use of org.jdom2.JDOMException in project JMRI by JMRI.
the class RosterServlet method doPost.
/**
* Handle any POST request as an upload of a roster file from client.
*
* @param request servlet request
* @param response servlet response
* @throws javax.servlet.ServletException if unable to process uploads
* @throws java.io.IOException if communications is cut with
* client
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
OutputStream out = null;
InputStream fileContent = null;
String rosterFolderName = Roster.getDefault().getRosterLocation() + "roster" + File.separator;
String tempFolderName = System.getProperty("java.io.tmpdir");
if (!tempFolderName.endsWith(File.separator)) {
//make sure path ends with a separator
tempFolderName += File.separator;
}
Locale rl = request.getLocale();
//get the uploaded file(s)
List<FileMeta> files = MultipartRequestHandler.uploadByJavaServletAPI(request);
List<String> msgList = new ArrayList<>();
//loop thru files returned and validate and (if ok) save each
for (FileMeta fm : files) {
log.debug("processing uploaded '{}' file '{}' ({}), group='{}', roster='{}', temp='{}'", fm.getFileType(), fm.getFileName(), fm.getFileSize(), fm.getRosterGroup(), rosterFolderName, tempFolderName);
//only allow xml files or image files
if (!fm.getFileType().equals("text/xml") && !fm.getFileType().startsWith("image")) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorInvalidFileType"), fm.getFileName(), fm.getFileType());
log.error(m);
msgList.add(m);
//stop processing this one
break;
}
//save received file to temporary folder
File fileTemp = new File(tempFolderName + fm.getFileName());
try {
out = new FileOutputStream(fileTemp);
fileContent = fm.getContent();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = fileContent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
log.debug("file '{}' of type '{}' temp saved to {}", fm.getFileType(), fm.getFileName(), tempFolderName);
} catch (IOException e) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorSavingFile"), fm.getFileName());
log.error(m);
msgList.add(m);
//stop processing this one
break;
} finally {
if (out != null) {
out.close();
}
if (fileContent != null) {
fileContent.close();
}
}
//finally
//reference to target file name and location
File fileNew = new File(rosterFolderName + fm.getFileName());
//save image file, replacing if parm is set that way. return appropriate message
if (fm.getFileType().startsWith("image")) {
if (fileNew.exists()) {
if (!fm.getFileReplace()) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorFileExists"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
} else {
//delete the old file
fileNew.delete();
if (fileTemp.renameTo(fileNew)) {
String m = String.format(rl, Bundle.getMessage(rl, "FileReplaced"), fm.getFileName());
log.debug(m);
msgList.add(m);
} else {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorRenameFailed"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
}
}
} else {
if (fileTemp.renameTo(fileNew)) {
String m = String.format(rl, Bundle.getMessage(rl, "FileAdded"), fm.getFileName());
log.debug(m);
msgList.add(m);
} else {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorRenameFailed"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
}
}
} else {
// create a temp rosterentry to check, based on uploaded file
RosterEntry reTemp = null;
try {
reTemp = RosterEntry.fromFile(new File(tempFolderName + fm.getFileName()));
} catch (JDOMException e) {
//handle XML failures
String m = String.format(rl, Bundle.getMessage(rl, "ErrorInvalidXML"), fm.getFileName(), e.getMessage());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
break;
}
//get existing entry if found
RosterEntry reOld = Roster.getDefault().getEntryForId(reTemp.getId());
if (reOld != null) {
if (!fm.getFileReplace()) {
String m = String.format(rl, Bundle.getMessage(rl, "ErrorFileExists"), fm.getFileName());
log.error(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
} else {
//replace specified
//remove the old entry from roster
Roster.getDefault().removeEntry(reOld);
//saves XML file to roster folder and makes backup
reTemp.updateFile();
//add the new entry to roster
Roster.getDefault().addEntry(reTemp);
//save modified roster.xml file
Roster.getDefault().writeRoster();
String m = String.format(rl, Bundle.getMessage(rl, "RosterEntryReplaced"), fm.getFileName(), reTemp.getDisplayName());
log.debug(m);
msgList.add(m);
//get rid of temp file
fileTemp.delete();
}
} else {
//move the file to proper location
fileTemp.renameTo(fileNew);
Roster.getDefault().addEntry(reTemp);
Roster.getDefault().writeRoster();
String m = String.format(rl, Bundle.getMessage(rl, "RosterEntryAdded"), fm.getFileName(), reTemp.getId());
log.debug(m);
msgList.add(m);
}
}
}
//for FileMeta
//respond with a json list of messages from the upload attempts
response.setContentType("application/json");
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(response.getOutputStream(), msgList);
}
Aggregations