mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-19 10:39:59 +00:00
added jsonresult structure to gps import servlet
This commit is contained in:
1 parent
6b279c7910
commit
d8369aab93
4 files changed
+91
-80
No files matched your search
+3
-1
@@ -11,6 +11,7 @@ import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.junit.Test;
|
||||
@@ -152,7 +153,8 @@ public class GPSFixImportTest {
|
||||
public void delete() {
|
||||
}
|
||||
};
|
||||
servlet.importFiles(Arrays.asList(new Pair<>("test.gpx", fi)), new AlwaysFailingGPSFixImporter(-1));
|
||||
JsonHolder holder = new JsonHolder(Logger.getLogger(GPSFixImportTest.class.getName()));
|
||||
servlet.importFiles(Arrays.asList(new Pair<>("test.gpx", fi)), holder, new AlwaysFailingGPSFixImporter(-1));
|
||||
// getting to here without errors is good enough
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.sap.sailing.server.gateway.trackfiles.impl;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import com.sap.sailing.domain.trackfiles.TrackFileImportDeviceIdentifier;
|
||||
|
||||
public class JsonHolder {
|
||||
private final JSONObject jsonResponseObj = new JSONObject();
|
||||
private final JSONArray jsonErrorObj = new JSONArray();
|
||||
private final JSONArray jsonUuidObj = new JSONArray();
|
||||
private final Logger logger;
|
||||
|
||||
public JsonHolder(Logger logger) {
|
||||
this.logger = logger;
|
||||
jsonResponseObj.put("errors", jsonErrorObj);
|
||||
jsonResponseObj.put("uploads", jsonUuidObj);
|
||||
}
|
||||
|
||||
public void add(String requestedImporterName, String filename,
|
||||
Exception exception) {
|
||||
logger.log(Level.SEVERE, "Sensordata import importer: " + requestedImporterName);
|
||||
logger.log(Level.SEVERE, "Sensordata import filename: " + filename);
|
||||
JSONObject jsonExceptionObj = logException(exception);
|
||||
jsonExceptionObj.put("filename", filename);
|
||||
jsonExceptionObj.put("requestedImporter", requestedImporterName);
|
||||
|
||||
}
|
||||
|
||||
public void add(Exception exception) {
|
||||
logException(exception);
|
||||
}
|
||||
|
||||
private JSONObject logException(Exception e) {
|
||||
final String exUUID = UUID.randomUUID().toString();
|
||||
logger.log(Level.SEVERE, "Sensordata import ExUUID: " + exUUID, e);
|
||||
JSONObject jsonExceptionObj = new JSONObject();
|
||||
jsonErrorObj.add(jsonExceptionObj);
|
||||
jsonExceptionObj.put("exUUID", exUUID);
|
||||
jsonExceptionObj.put("className", e.getClass().getName());
|
||||
jsonExceptionObj.put("message", e.getMessage());
|
||||
return jsonExceptionObj;
|
||||
}
|
||||
|
||||
public void add(TrackFileImportDeviceIdentifier mapping) {
|
||||
String stringRep = mapping.getId().toString();
|
||||
jsonUuidObj.add(stringRep);
|
||||
}
|
||||
|
||||
public void writeJSONString(HttpServletResponse resp) throws IOException {
|
||||
resp.setContentType("text/html");
|
||||
jsonResponseObj.writeJSONString(resp.getWriter());
|
||||
}
|
||||
}
|
||||
-51
@@ -6,7 +6,6 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -14,8 +13,6 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.osgi.framework.InvalidSyntaxException;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
|
||||
@@ -144,52 +141,4 @@ public class SensorDataImportServlet extends AbstractFileUploadServlet {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static class JsonHolder {
|
||||
private final JSONObject jsonResponseObj = new JSONObject();
|
||||
private final JSONArray jsonErrorObj = new JSONArray();
|
||||
private final JSONArray jsonUuidObj = new JSONArray();
|
||||
private final Logger logger;
|
||||
|
||||
public JsonHolder(Logger logger) {
|
||||
this.logger = logger;
|
||||
jsonResponseObj.put("errors", jsonErrorObj);
|
||||
jsonResponseObj.put("uploads", jsonUuidObj);
|
||||
}
|
||||
|
||||
public void add(String requestedImporterName, String filename,
|
||||
Exception exception) {
|
||||
logger.log(Level.SEVERE, "Sensordata import importer: " + requestedImporterName);
|
||||
logger.log(Level.SEVERE, "Sensordata import filename: " + filename);
|
||||
JSONObject jsonExceptionObj = logException(exception);
|
||||
jsonExceptionObj.put("filename", filename);
|
||||
jsonExceptionObj.put("requestedImporter", requestedImporterName);
|
||||
|
||||
}
|
||||
|
||||
public void add(Exception exception) {
|
||||
logException(exception);
|
||||
}
|
||||
|
||||
private JSONObject logException(Exception e) {
|
||||
final String exUUID = UUID.randomUUID().toString();
|
||||
logger.log(Level.SEVERE, "Sensordata import ExUUID: " + exUUID, e);
|
||||
JSONObject jsonExceptionObj = new JSONObject();
|
||||
jsonErrorObj.add(jsonExceptionObj);
|
||||
jsonExceptionObj.put("exUUID", exUUID);
|
||||
jsonExceptionObj.put("className", e.getClass().getName());
|
||||
jsonExceptionObj.put("message", e.getMessage());
|
||||
return jsonExceptionObj;
|
||||
}
|
||||
|
||||
public void add(TrackFileImportDeviceIdentifier mapping) {
|
||||
String stringRep = mapping.getId().toString();
|
||||
jsonUuidObj.add(stringRep);
|
||||
}
|
||||
|
||||
public void writeJSONString(HttpServletResponse resp) throws IOException {
|
||||
resp.setContentType("text/html");
|
||||
jsonResponseObj.writeJSONString(resp.getWriter());
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
-28
@@ -4,7 +4,6 @@ import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -80,9 +79,8 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Iterable<TrackFileImportDeviceIdentifier> importFiles(Iterable<Pair<String, FileItem>> files,
|
||||
GPSFixImporter preferredImporter) throws IOException {
|
||||
final Set<TrackFileImportDeviceIdentifier> deviceIds = new HashSet<>();
|
||||
protected void importFiles(Iterable<Pair<String, FileItem>> files,
|
||||
JsonHolder jsonResult, GPSFixImporter preferredImporter) throws IOException {
|
||||
for (Pair<String, FileItem> pair : files) {
|
||||
final String fileName = pair.getA();
|
||||
final FileItem fileItem = pair.getB();
|
||||
@@ -113,14 +111,15 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
|
||||
importer.importFixes(in, new Callback() {
|
||||
@Override
|
||||
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
|
||||
deviceIds.add(device);
|
||||
storeFix(fix, device);
|
||||
jsonResult.add(device);
|
||||
}
|
||||
}, true, fileName);
|
||||
succeeded = true;
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.INFO, "Failed with " + e.getClass().getSimpleName()
|
||||
+ " while importing file using " + importer.getType());
|
||||
jsonResult.add(importer.getClass().getName(), fileName, e);
|
||||
}
|
||||
}
|
||||
if (succeeded) {
|
||||
@@ -129,35 +128,36 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
|
||||
}
|
||||
}
|
||||
}
|
||||
return deviceIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<FileItem> fileItems, HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||
String prefImporterType = null;
|
||||
List<Pair<String, FileItem>> files = new ArrayList<>();
|
||||
for (FileItem item : fileItems) {
|
||||
if (!item.isFormField())
|
||||
files.add(new Pair<String, FileItem>(item.getName(), item));
|
||||
else {
|
||||
if (item.getFieldName() != null && item.getFieldName().equals(PREFERRED_IMPORTER)) {
|
||||
prefImporterType = item.getString();
|
||||
JsonHolder jsonResult = new JsonHolder(logger);
|
||||
try {
|
||||
String prefImporterType = null;
|
||||
List<Pair<String, FileItem>> files = new ArrayList<>();
|
||||
for (FileItem item : fileItems) {
|
||||
if (!item.isFormField())
|
||||
files.add(new Pair<String, FileItem>(item.getName(), item));
|
||||
else {
|
||||
if (item.getFieldName() != null && item.getFieldName().equals(PREFERRED_IMPORTER)) {
|
||||
prefImporterType = item.getString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
GPSFixImporter preferredImporter = null;
|
||||
if (prefImporterType != null && !prefImporterType.isEmpty()) {
|
||||
preferredImporter = getServiceFinderFactory().createServiceFinder(GPSFixImporter.class)
|
||||
.findService(prefImporterType);
|
||||
}
|
||||
final Iterable<TrackFileImportDeviceIdentifier> mappingList = importFiles(files, preferredImporter);
|
||||
// setJsonResponseHeader(resp);
|
||||
// DO NOT set a JSON response header. This causes the browser to wrap the response in a
|
||||
// <pre> tag when uploading from GWT, as this is an AJAX-request inside an iFrame.
|
||||
resp.setContentType("text/html");
|
||||
for (TrackFileImportDeviceIdentifier mapping : mappingList) {
|
||||
String stringRep = mapping.getId().toString();
|
||||
resp.getWriter().println(stringRep);
|
||||
GPSFixImporter preferredImporter = null;
|
||||
if (prefImporterType != null && !prefImporterType.isEmpty()) {
|
||||
preferredImporter = getServiceFinderFactory().createServiceFinder(GPSFixImporter.class)
|
||||
.findService(prefImporterType);
|
||||
}
|
||||
importFiles(files, jsonResult, preferredImporter);
|
||||
// setJsonResponseHeader(resp);
|
||||
// DO NOT set a JSON response header. This causes the browser to wrap the response in a
|
||||
// <pre> tag when uploading from GWT, as this is an AJAX-request inside an iFrame.
|
||||
} catch (Exception e) {
|
||||
jsonResult.add(e);
|
||||
} finally {
|
||||
jsonResult.writeJSONString(resp);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user