Skip to content

Commit

Permalink
Import Task GET
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe committed Mar 31, 2017
1 parent 42c7e3f commit 2e19301
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 52 deletions.

This file was deleted.

Expand Up @@ -5,6 +5,7 @@
package org.geoserver.importer.rest;

import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;

Expand All @@ -15,6 +16,7 @@
import org.geoserver.importer.ImportData;
import org.geoserver.importer.ImportFilter;
import org.geoserver.importer.Importer;
import org.geoserver.importer.rest.converters.ImportContextJSONConverterWriter;
import org.geoserver.rest.RequestInfo;
import org.geoserver.rest.RestBaseController;
import org.geoserver.rest.RestException;
Expand Down Expand Up @@ -77,14 +79,14 @@ public ResponseEntity<ImportContext> postImports(@PathVariable(name="id",require

@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE,
CatalogController.TEXT_JSON })
public ImportContextCollectionWrapper<ImportContext> getImports() {
public ImportWrapper getImports() {

Object lookupContext = context(null, true, true);
if (lookupContext == null) {
// this means a specific lookup failed
throw new RestException("Failed to find import context", HttpStatus.NOT_FOUND);
} else {
return new ImportContextCollectionWrapper<>((Iterator<ImportContext>) lookupContext, ImportContext.class);
return (writer, converter) -> converter.contexts((Iterator<ImportContext>)lookupContext, converter.expand(0));
}
}

Expand Down
@@ -0,0 +1,20 @@
package org.geoserver.importer.rest;

import net.sf.json.JSONObject;
import org.geoserver.importer.rest.converters.ImportContextJSONConverterWriter;

import java.io.IOException;
import java.io.Writer;

public class ImportJSONWrapper implements ImportWrapper {
JSONObject json;

public ImportJSONWrapper(JSONObject json) {
this.json = json;
}

@Override
public void write(Writer writer, ImportContextJSONConverterWriter converter) throws IOException {
writer.write(json.toString());
}
}
@@ -0,0 +1,26 @@
package org.geoserver.importer.rest;

import org.geoserver.catalog.StoreInfo;
import org.geoserver.importer.ImportTask;

/**
* Created by tbarsballe on 2017-03-31.
*/
public class ImportStoreWrapper {
StoreInfo store ;

ImportTask task;

public ImportStoreWrapper(StoreInfo store, ImportTask task) {
this.store = store;
this.task = task;
}

public StoreInfo getStore() {
return store;
}

public ImportTask getTask() {
return task;
}
}
Expand Up @@ -4,17 +4,31 @@
*/
package org.geoserver.importer.rest;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.geoserver.catalog.CoverageStoreInfo;
import org.geoserver.catalog.DataStoreInfo;
import org.geoserver.catalog.StoreInfo;
import org.geoserver.catalog.WMSStoreInfo;
import org.geoserver.catalog.rest.CatalogController;
import org.geoserver.importer.ImportTask;
import org.geoserver.importer.Importer;
import org.geoserver.importer.rest.converters.ImportContextJSONConverterWriter;
import org.geoserver.rest.RestBaseController;
import org.geoserver.rest.RestException;
import org.geoserver.rest.wrapper.RestWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.Writer;
import java.util.List;

@RestController
@RequestMapping(path = RestBaseController.ROOT_PATH+"/imports/{id}/tasks", produces = {
MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_HTML_VALUE })
Expand All @@ -27,8 +41,8 @@ protected ImportTaskController(Importer importer) {

@GetMapping(path = "", produces = { MediaType.APPLICATION_JSON_VALUE,
CatalogController.TEXT_JSON , MediaType.TEXT_HTML_VALUE})
public ImportContextCollectionWrapper<ImportTask> tasksGet(@PathVariable Long id) {
return new ImportContextCollectionWrapper<>(context(id).getTasks(), ImportTask.class);
public ImportWrapper tasksGet(@PathVariable Long id) {
return (writer, converter) -> converter.tasks(context(id).getTasks(), true, converter.expand(0));
}

@GetMapping(path = "/{taskId}", produces = { MediaType.APPLICATION_JSON_VALUE,
Expand All @@ -38,9 +52,40 @@ public ImportTask taskGet(@PathVariable Long id, @PathVariable Integer taskId) {
}


@GetMapping(path = {"/progress","/{taskId}/progress"}, produces = { MediaType.APPLICATION_JSON_VALUE,
@GetMapping(path = {"/{taskId}/progress"}, produces = { MediaType.APPLICATION_JSON_VALUE,
CatalogController.TEXT_JSON , MediaType.TEXT_HTML_VALUE})
public ImportJSONWrapper progressGet(@PathVariable Long id, @PathVariable Integer taskId) {

JSONObject progress = new JSONObject();
ImportTask inProgress = importer.getCurrentlyProcessingTask(id);
try {
if (inProgress != null) {
progress.put("progress", inProgress.getNumberProcessed());
progress.put("total", inProgress.getTotalToProcess());
progress.put("state", inProgress.getState().toString());
} else {
ImportTask task = task(id, taskId);
progress.put("state", task.getState().toString());
if (task.getState() == ImportTask.State.ERROR) {
if (task.getError() != null) {
progress.put("message", task.getError().getMessage());
}
}
}
} catch (JSONException jex) {
throw new RestException("Internal Error", HttpStatus.INTERNAL_SERVER_ERROR, jex);
}
return new ImportJSONWrapper(progress);
}

@GetMapping(path = {"/{taskId}/target"}, produces = { MediaType.APPLICATION_JSON_VALUE,
CatalogController.TEXT_JSON , MediaType.TEXT_HTML_VALUE})
public void progressGet(@PathVariable Long id, @PathVariable(required = false) Integer taskId) {
//getResponse().setEntity(createProgressRepresentation());
public ImportWrapper targetGet(@PathVariable Long id, @PathVariable Integer taskId) {
final ImportTask task = task(id, taskId);
if (task.getStore() == null) {
throw new RestException("Task has no target store", HttpStatus.NOT_FOUND);
}
return (writer, converter) -> converter.store(task.getStore(), task, true, converter.expand(1));

}
}
@@ -0,0 +1,13 @@
package org.geoserver.importer.rest;

import org.geoserver.importer.rest.converters.ImportContextJSONConverterWriter;

import java.io.IOException;
import java.io.Writer;

/**
* Created by tbarsballe on 2017-03-31.
*/
public interface ImportWrapper {
void write(Writer writer, ImportContextJSONConverterWriter converter) throws IOException;
}
Expand Up @@ -39,8 +39,7 @@
import org.geoserver.importer.Table;
import org.geoserver.importer.mosaic.Granule;
import org.geoserver.importer.mosaic.Mosaic;
import org.geoserver.importer.rest.ImportContextCollectionWrapper;
import org.geoserver.importer.rest.JSONRepresentation;
import org.geoserver.importer.rest.*;
import org.geoserver.importer.transform.AttributeRemapTransform;
import org.geoserver.importer.transform.AttributesToPointGeometryTransform;
import org.geoserver.importer.transform.CreateIndexTransform;
Expand Down Expand Up @@ -104,8 +103,7 @@ public boolean canRead(Class clazz, MediaType mediaType) {
@Override
public boolean canWrite(Class clazz, MediaType mediaType) {
final boolean importControllers = ImportContext.class.isAssignableFrom(clazz)
|| ImportTask.class.isAssignableFrom(clazz)
|| ImportContextCollectionWrapper.class.isAssignableFrom(clazz);
|| ImportTask.class.isAssignableFrom(clazz) || ImportWrapper.class.isAssignableFrom(clazz);
final boolean dataControllers = ImportData.class.isAssignableFrom(clazz);
return (importControllers || dataControllers) && getSupportedMediaTypes().contains(mediaType);
}
Expand Down Expand Up @@ -144,12 +142,7 @@ public void write(Object t, MediaType contentType, HttpOutputMessage outputMessa
this.context((ImportContext) t, true, expand(1));
} else if (t instanceof ImportTask) {
this.task((ImportTask) t, true, expand(1));
} else if(t instanceof ImportContextCollectionWrapper) {
if (ImportContext.class.isAssignableFrom(((ImportContextCollectionWrapper) t).getCollectionClass())) {
this.contexts((Iterator<ImportContext>) ((ImportContextCollectionWrapper) t).getCollection(), expand(0));
} else if (ImportTask.class.isAssignableFrom(((ImportContextCollectionWrapper) t).getCollectionClass())) {
this.tasks((List<ImportTask>) ((ImportContextCollectionWrapper) t).getCollection(), true, expand(0));
}

} else if (ImportData.class.isAssignableFrom(t.getClass())) {
ImportData data = (ImportData)t;
Object parent = data.getParent();
Expand All @@ -172,15 +165,18 @@ public void write(Object t, MediaType contentType, HttpOutputMessage outputMessa
} else if (data instanceof RemoteData) {
remote((RemoteData) data, parent, expand);
}
} else {
throw new RestException("Trying to write an unknow object " + t,
} else if (ImportWrapper.class.isAssignableFrom(t.getClass())) {
((ImportWrapper) t).write(writer, 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();

}

Expand All @@ -197,7 +193,7 @@ public void write(Object t, MediaType contentType, HttpOutputMessage outputMessa

private Callback callback;

protected int expand(int def) {
public int expand(int def) {
String ex = null;

Map<String, String[]> queryMap = page.getQueryMap();
Expand Down Expand Up @@ -346,14 +342,14 @@ public void task(ImportTask task, boolean top, int expand) throws IOException {
json.flush();
}

void store(StoreInfo store, ImportTask task, boolean top, int expand) throws IOException {
public void store(StoreInfo store, ImportTask task, boolean top, int expand) throws IOException {

String type = store instanceof DataStoreInfo ? "dataStore"
: store instanceof CoverageStoreInfo ? "coverageStore" : "store";

json.object();
if (task != null) {
json.key("href").value(page.pageURI(pathTo(task) + "/target"));
json.key("href").value(page.pageURI("/target"));
}

if (expand > 0) {
Expand Down
Expand Up @@ -173,6 +173,13 @@ public void testGetTask() throws Exception {
assertTrue(task.getString("href").endsWith("/imports/0/tasks/0"));
}

@Test
public void testGetTaskProgress() throws Exception {
JSONObject json = (JSONObject) getAsJSON(RestBaseController.ROOT_PATH+"/imports/0/tasks/0/progress");
assertEquals("READY", json.get("state"));
//TODO: trigger import and check progress
}

@Test
public void testDeleteTask() throws Exception {
MockHttpServletResponse resp = postAsServletResponse(RestBaseController.ROOT_PATH+"/imports", "");
Expand Down

0 comments on commit 2e19301

Please sign in to comment.