Skip to content

Commit

Permalink
Importer code imporvements discovered while writing api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe authored and jodygarnett committed Apr 13, 2017
1 parent 6dcd4f8 commit c57f776
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 102 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public ResponseEntity<Object> postImports(
} }


@GetMapping @GetMapping
public ImportWrapper getImports() { public ImportWrapper getImports(@RequestParam(required=false) String expand) {
Object lookupContext = context(null, true, true); Object lookupContext = context(null, true, true);
if (lookupContext == null) { if (lookupContext == null) {
// this means a specific lookup failed // this means a specific lookup failed
throw new RestException("Failed to find import context", HttpStatus.NOT_FOUND); throw new RestException("Failed to find import context", HttpStatus.NOT_FOUND);
} else { } else {
return (writer, builder, converter) -> converter.contexts(builder,(Iterator<ImportContext>)lookupContext, converter.expand(0)); return (writer, builder, converter) -> converter.contexts(builder,(Iterator<ImportContext>)lookupContext, converter.expand(expand, 0));
} }
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public ImportData getDirectory(




//We need to force spring to ignore the .shp here (we don't want a .shp encoded response! //We need to force spring to ignore the .shp here (we don't want a .shp encoded response!
@DeleteMapping(value = { @DeleteMapping(value = {"/data/files/{fileName:.+}", "/tasks/{taskId}/data/files/{fileName:\\.+}" })
"/data/files", "/tasks/{taskId}/data/files",
"/data/files/{fileName:.+}", "/tasks/{taskId}/data/files/{fileName:\\.+}" })
public ResponseEntity deleteDirectory( public ResponseEntity deleteDirectory(
@PathVariable Long importId, @PathVariable Long importId,
@PathVariable(required = false) Integer taskId, @PathVariable(required = false) Integer taskId,
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ protected ImportTaskController(Importer importer) {
} }


@GetMapping @GetMapping
public ImportWrapper tasksGet(@PathVariable Long id) { public ImportWrapper tasksGet(@PathVariable Long id, @RequestParam(required=false) String expand) {
return (writer, builder, converter) -> converter.tasks(builder,context(id).getTasks(), true, converter.expand(0)); return (writer, builder, converter) -> converter.tasks(builder,context(id).getTasks(), true, converter.expand(expand, 0));
} }


@GetMapping(path = "/{taskId}") @GetMapping(path = "/{taskId}")
Expand Down Expand Up @@ -96,23 +96,23 @@ public ImportWrapper progressGet(@PathVariable Long id, @PathVariable Integer ta
} }


@GetMapping(path = "/{taskId}/target") @GetMapping(path = "/{taskId}/target")
public ImportWrapper targetGet(@PathVariable Long id, @PathVariable Integer taskId) { public ImportWrapper targetGet(@PathVariable Long id, @PathVariable Integer taskId, @RequestParam(required=false) String expand) {
final ImportTask task = task(id, taskId); final ImportTask task = task(id, taskId);
if (task.getStore() == null) { if (task.getStore() == null) {
throw new RestException("Task has no target store", HttpStatus.NOT_FOUND); throw new RestException("Task has no target store", HttpStatus.NOT_FOUND);
} }
return (writer, builder, converter) -> converter.store(builder,task.getStore(), task, true, converter.expand(1)); return (writer, builder, converter) -> converter.store(builder,task.getStore(), task, true, converter.expand(expand, 1));


} }


@GetMapping(path = "/{taskId}/layer") @GetMapping(path = "/{taskId}/layer")
public ImportWrapper layersGet(@PathVariable Long id, @PathVariable Integer taskId) { public ImportWrapper layersGet(@PathVariable Long id, @PathVariable Integer taskId, @RequestParam(required=false) String expand) {
ImportTask task = task(id, taskId); ImportTask task = task(id, taskId);
return (writer, builder, converter) -> converter.layer(builder,task, true, converter.expand(1)); return (writer, builder, converter) -> converter.layer(builder,task, true, converter.expand(expand, 1));
} }


@PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE}) @PostMapping(consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public Object taskPost(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) { public Object taskPost(@PathVariable Long id, @RequestParam(required=false) String expand, HttpServletRequest request, HttpServletResponse response) {
ImportData data = null; ImportData data = null;


LOGGER.info("Handling POST of " + request.getContentType()); LOGGER.info("Handling POST of " + request.getContentType());
Expand All @@ -133,7 +133,7 @@ public Object taskPost(@PathVariable Long id, HttpServletRequest request, HttpSe
} }


//Construct response //Construct response
return acceptData(data, context(id), response); return acceptData(data, context(id), response, expand);
} }


@PutMapping(path = "/{taskId}", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaTypeExtensions.TEXT_JSON_VALUE}) @PutMapping(path = "/{taskId}", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaTypeExtensions.TEXT_JSON_VALUE})
Expand Down Expand Up @@ -161,11 +161,12 @@ PutIgnoringExtensionContentNegotiationStrategy importTaskPutContentNegotiationSt
public Object taskPutFile( public Object taskPutFile(
@PathVariable Long id, @PathVariable Long id,
@PathVariable Object taskId, @PathVariable Object taskId,
@RequestParam(required=false) String expand,
HttpServletRequest request, HttpServletResponse response) { HttpServletRequest request, HttpServletResponse response) {


ImportContext context = context(id); ImportContext context = context(id);
//TODO: Task id is the file name here. This functionality is completely undocumented //TODO: Task id is the file name here. This functionality is completely undocumented
return acceptData(handleFileUpload(context, taskId, request), context, response); return acceptData(handleFileUpload(context, taskId, request), context, response, expand);
} }




Expand All @@ -187,13 +188,13 @@ public void targetPut(


@PutMapping(path = "/{taskId}/layer") @PutMapping(path = "/{taskId}/layer")
@ResponseStatus(HttpStatus.ACCEPTED) @ResponseStatus(HttpStatus.ACCEPTED)
public ImportWrapper layerPut(@PathVariable Long id, @PathVariable Integer taskId, @RequestBody LayerInfo layer) { public ImportWrapper layerPut(@PathVariable Long id, @PathVariable Integer taskId, @RequestParam(required=false) String expand, @RequestBody LayerInfo layer) {
ImportTask task = task(id, taskId); ImportTask task = task(id, taskId);


return (writer, builder, converter) -> { return (writer, builder, converter) -> {
updateLayer(task, layer, importer, converter); updateLayer(task, layer, importer, converter);
importer.changed(task); importer.changed(task);
converter.task(builder,task, true, converter.expand(1)); converter.task(builder,task, true, converter.expand(expand, 1));
}; };
} }


Expand All @@ -205,7 +206,7 @@ public void taskDelete(@PathVariable Long id, @PathVariable Integer taskId) {
importer.changed(task.getContext()); importer.changed(task.getContext());
} }


public Object acceptData(ImportData data, ImportContext context, HttpServletResponse response) { public Object acceptData(ImportData data, ImportContext context, HttpServletResponse response, String expand) {
List<ImportTask> newTasks = null; List<ImportTask> newTasks = null;
try { try {
newTasks = importer.update(context, data); newTasks = importer.update(context, data);
Expand All @@ -224,10 +225,10 @@ public Object acceptData(ImportData data, ImportContext context, HttpServletResp


return (ImportWrapper) (writer, builder,converter) -> { return (ImportWrapper) (writer, builder,converter) -> {
if (result.size() == 1) { if (result.size() == 1) {
converter.task(builder,result.get(0), true, converter.expand(1)); converter.task(builder,result.get(0), true, converter.expand(expand, 1));
} }
else { else {
converter.tasks(builder,result, true, converter.expand(0)); converter.tasks(builder,result, true, converter.expand(expand, 0));
} }
}; };
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ public ImportWrapper getTransform(
return (writer, builder, converter) -> { return (writer, builder, converter) -> {
ImportTransform tx = transform(importId, taskId, transformId, true); ImportTransform tx = transform(importId, taskId, transformId, true);
if (tx == null) { if (tx == null) {
converter.transformChain(builder,task(importId, taskId), true, converter.expand(1)); converter.transformChain(builder,task(importId, taskId), true, converter.expand(expand, 1));
} else { } else {
ImportTask task = task(importId, taskId); ImportTask task = task(importId, taskId);
int index = task.getTransform().getTransforms().indexOf(tx); int index = task.getTransform().getTransforms().indexOf(tx);


converter.transform(builder, tx, index, task, true, converter.expand(1)); converter.transform(builder, tx, index, task, true, converter.expand(expand, 1));
} }
}; };
} }
Expand All @@ -96,7 +96,7 @@ public ImportWrapper putTransform(
ImportTask task = task(importId, taskId); ImportTask task = task(importId, taskId);
int index = task.getTransform().getTransforms().indexOf(orig); int index = task.getTransform().getTransforms().indexOf(orig);


converter.transform(builder, orig, index, task, true, converter.expand(1)); converter.transform(builder, orig, index, task, true, converter.expand(expand, 1));
}; };
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -90,89 +90,21 @@ public class ImportJSONWriter {


Importer importer; Importer importer;


// FlushableJSONBuilder json;

private Callback callback; private Callback callback;


@Autowired @Autowired
public ImportJSONWriter(Importer importer) { public ImportJSONWriter(Importer importer) {
this.importer = importer; this.importer = importer;
} }

// public ImportContextJSONConverterWriter(Importer importer, OutputStream out) {
// super(MediaType.APPLICATION_JSON,AbstractCatalogController.TEXT_JSON,MediaType.TEXT_HTML);
// this.importer = importer;
// this.json = new FlushableJSONBuilder(new OutputStreamWriter(out));
// }

// @Override
protected boolean supports(Class<?> clazz) {
return ImportContext.class.isAssignableFrom(clazz)
|| ImportTask.class.isAssignableFrom(clazz)
|| ImportWrapper.class.isAssignableFrom(clazz)
|| ImportData.class.isAssignableFrom(clazz);
}

// @Override
protected void writeInternal(FlushableJSONBuilder json, Object t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();

// if (!contentType.equals(MediaType.TEXT_HTML)) {
// outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
// } else {
// outputMessage.getHeaders().setContentType(MediaType.TEXT_HTML);
// }
// RequestInfo page = RequestInfo.get();
// String path = page.getPagePath();
OutputStream out = outputMessage.getBody();
OutputStreamWriter writer = new OutputStreamWriter(out);
if (contentType.equals(MediaType.TEXT_HTML)) {
writer.write("<html><body><pre>");
}

// json = new FlushableJSONBuilder(writer);
if (t instanceof ImportContext) {
this.context(json, (ImportContext) t, true, expand(1));
} else if (t instanceof ImportTask) {
this.task(json, (ImportTask) t, true, expand(1));

} else if (ImportData.class.isAssignableFrom(t.getClass())) {
ImportData data = (ImportData)t;
Object parent = data.getParent();
int expand = expand(1);
if (data instanceof FileData) {
if (data instanceof Directory) {
if (data instanceof Mosaic) {
mosaic(json, (Mosaic) data, parent ,expand );
}
else {
directory(json, (Directory) data, parent, expand);
}
} else {
file(json, (FileData) data, parent, expand, false);
}
} else if (data instanceof Database) {
database(json, (Database) data, parent, expand);
} else if (data instanceof Table) {
table(json, (Table)data, parent, expand);
} else if (data instanceof RemoteData) {
remote(json, (RemoteData) data, parent, expand);
}
} else if (ImportWrapper.class.isAssignableFrom(t.getClass())) {
((ImportWrapper) t).write(writer,json, this);
} else {
throw new RestException("Trying to write an unknown object " + t,
HttpStatus.I_AM_A_TEAPOT);
}
// insert new objects here

if (contentType.equals(MediaType.TEXT_HTML)) {
writer.write("</pre></body></html>");
}
writer.flush();
}


//TODO: Document this behavior
/**
* Determines the number of levels to expand the JSON result, by parsing the "expand" parameter from the
* query map.
*
* @param def The default value to fall back on
* @return
*/
public int expand(int def) { public int expand(int def) {
String ex = null; String ex = null;
Map<String, String[]> queryMap = RequestInfo.get().getQueryMap(); Map<String, String[]> queryMap = RequestInfo.get().getQueryMap();
Expand All @@ -182,14 +114,25 @@ public int expand(int def) {
ex = params[0]; ex = params[0];
} }
} }
if (ex == null) { return expand(ex, def);
}

/**
* Determines the number of levels to expand the JSON result
*
* @param expand The value of the "expand" parameter
* @param def The default value to fall back on
* @return
*/
public int expand(String expand, int def) {
if (expand == null) {
return def; return def;
} }


try { try {
return "self".equalsIgnoreCase(ex) ? 1 return "self".equalsIgnoreCase(expand) ? 1
: "all".equalsIgnoreCase(ex) ? Integer.MAX_VALUE : "all".equalsIgnoreCase(expand) ? Integer.MAX_VALUE
: "none".equalsIgnoreCase(ex) ? 0 : Integer.parseInt(ex); : "none".equalsIgnoreCase(expand) ? 0 : Integer.parseInt(expand);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
return def; return def;
} }
Expand Down

0 comments on commit c57f776

Please sign in to comment.