mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-10-20 19:20:04 +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.io.UnsupportedEncodingException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.apache.commons.fileupload.FileItem;
|
import org.apache.commons.fileupload.FileItem;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -152,7 +153,8 @@ public class GPSFixImportTest {
|
|||||||
public void delete() {
|
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
|
// 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.Collection;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -14,8 +13,6 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.apache.commons.fileupload.FileItem;
|
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.InvalidSyntaxException;
|
||||||
import org.osgi.framework.ServiceReference;
|
import org.osgi.framework.ServiceReference;
|
||||||
|
|
||||||
@@ -144,52 +141,4 @@ public class SensorDataImportServlet extends AbstractFileUploadServlet {
|
|||||||
}
|
}
|
||||||
return result;
|
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.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -80,9 +79,8 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Iterable<TrackFileImportDeviceIdentifier> importFiles(Iterable<Pair<String, FileItem>> files,
|
protected void importFiles(Iterable<Pair<String, FileItem>> files,
|
||||||
GPSFixImporter preferredImporter) throws IOException {
|
JsonHolder jsonResult, GPSFixImporter preferredImporter) throws IOException {
|
||||||
final Set<TrackFileImportDeviceIdentifier> deviceIds = new HashSet<>();
|
|
||||||
for (Pair<String, FileItem> pair : files) {
|
for (Pair<String, FileItem> pair : files) {
|
||||||
final String fileName = pair.getA();
|
final String fileName = pair.getA();
|
||||||
final FileItem fileItem = pair.getB();
|
final FileItem fileItem = pair.getB();
|
||||||
@@ -113,14 +111,15 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
|
|||||||
importer.importFixes(in, new Callback() {
|
importer.importFixes(in, new Callback() {
|
||||||
@Override
|
@Override
|
||||||
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
|
public void addFix(GPSFix fix, TrackFileImportDeviceIdentifier device) {
|
||||||
deviceIds.add(device);
|
|
||||||
storeFix(fix, device);
|
storeFix(fix, device);
|
||||||
|
jsonResult.add(device);
|
||||||
}
|
}
|
||||||
}, true, fileName);
|
}, true, fileName);
|
||||||
succeeded = true;
|
succeeded = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.INFO, "Failed with " + e.getClass().getSimpleName()
|
logger.log(Level.INFO, "Failed with " + e.getClass().getSimpleName()
|
||||||
+ " while importing file using " + importer.getType());
|
+ " while importing file using " + importer.getType());
|
||||||
|
jsonResult.add(importer.getClass().getName(), fileName, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (succeeded) {
|
if (succeeded) {
|
||||||
@@ -129,35 +128,36 @@ public class TrackFilesImportServlet extends AbstractFileUploadServlet {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return deviceIds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void process(List<FileItem> fileItems, HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
protected void process(List<FileItem> fileItems, HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
||||||
String prefImporterType = null;
|
JsonHolder jsonResult = new JsonHolder(logger);
|
||||||
List<Pair<String, FileItem>> files = new ArrayList<>();
|
try {
|
||||||
for (FileItem item : fileItems) {
|
String prefImporterType = null;
|
||||||
if (!item.isFormField())
|
List<Pair<String, FileItem>> files = new ArrayList<>();
|
||||||
files.add(new Pair<String, FileItem>(item.getName(), item));
|
for (FileItem item : fileItems) {
|
||||||
else {
|
if (!item.isFormField())
|
||||||
if (item.getFieldName() != null && item.getFieldName().equals(PREFERRED_IMPORTER)) {
|
files.add(new Pair<String, FileItem>(item.getName(), item));
|
||||||
prefImporterType = item.getString();
|
else {
|
||||||
|
if (item.getFieldName() != null && item.getFieldName().equals(PREFERRED_IMPORTER)) {
|
||||||
|
prefImporterType = item.getString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
GPSFixImporter preferredImporter = null;
|
||||||
GPSFixImporter preferredImporter = null;
|
if (prefImporterType != null && !prefImporterType.isEmpty()) {
|
||||||
if (prefImporterType != null && !prefImporterType.isEmpty()) {
|
preferredImporter = getServiceFinderFactory().createServiceFinder(GPSFixImporter.class)
|
||||||
preferredImporter = getServiceFinderFactory().createServiceFinder(GPSFixImporter.class)
|
.findService(prefImporterType);
|
||||||
.findService(prefImporterType);
|
}
|
||||||
}
|
importFiles(files, jsonResult, preferredImporter);
|
||||||
final Iterable<TrackFileImportDeviceIdentifier> mappingList = importFiles(files, preferredImporter);
|
// setJsonResponseHeader(resp);
|
||||||
// setJsonResponseHeader(resp);
|
// DO NOT set a JSON response header. This causes the browser to wrap the response in a
|
||||||
// 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.
|
||||||
// <pre> tag when uploading from GWT, as this is an AJAX-request inside an iFrame.
|
} catch (Exception e) {
|
||||||
resp.setContentType("text/html");
|
jsonResult.add(e);
|
||||||
for (TrackFileImportDeviceIdentifier mapping : mappingList) {
|
} finally {
|
||||||
String stringRep = mapping.getId().toString();
|
jsonResult.writeJSONString(resp);
|
||||||
resp.getWriter().println(stringRep);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user