Skip to content

Commit

Permalink
Adding callback support for Operation, clean up leftovers argument re…
Browse files Browse the repository at this point in the history
…solvers
  • Loading branch information
aaime committed Jun 23, 2019
1 parent 4820695 commit b83b7f1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.geoserver.api;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.logging.Logger;
import org.geoserver.ows.Dispatcher;
import org.geoserver.ows.DispatcherCallback;
import org.geoserver.ows.Request;
import org.geoserver.platform.Operation;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod;

public class APIConfigurationSupport extends WebMvcConfigurationSupport {

static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geoserver.api");

List<DispatcherCallback> callbacks;

class APIRequestMappingHandlerAdapter extends RequestMappingHandlerAdapter {
@Override
protected ServletInvocableHandlerMethod createInvocableHandlerMethod(
HandlerMethod handlerMethod) {
return new APIInvocableHandlerMethod(handlerMethod);
}
}

class APIInvocableHandlerMethod extends ServletInvocableHandlerMethod {

public APIInvocableHandlerMethod(HandlerMethod handlerMethod) {
super(handlerMethod);
}

@Override
protected Object doInvoke(Object... args) throws Exception {
Request request = Dispatcher.REQUEST.get();
Operation operation =
new Operation(
request.getRequest(),
request.getServiceDescriptor(),
getBridgedMethod(),
args);
operation = fireOperationDispatchedCallback(request, operation);
try {
return operation
.getMethod()
.invoke(operation.getService().getService(), operation.getParameters());
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
if (targetException instanceof RuntimeException) {
throw (RuntimeException) targetException;
} else if (targetException instanceof Error) {
throw (Error) targetException;
} else if (targetException instanceof Exception) {
throw (Exception) targetException;
} else {
throw new IllegalStateException(e);
}
}
}
}

@Override
protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter() {
return new APIRequestMappingHandlerAdapter();
}

public List<DispatcherCallback> getCallbacks() {
return callbacks;
}

public void setCallbacks(List<DispatcherCallback> callbacks) {
this.callbacks = callbacks;
}

// SHARE (or move to a callback handler/list class of sort?)
Operation fireOperationDispatchedCallback(Request req, Operation op) {
for (DispatcherCallback cb : callbacks) {
Operation o = cb.operationDispatched(req, op);
op = o != null ? o : op;
}
return op;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.DispatcherServletWebRequest;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
Expand All @@ -71,7 +70,7 @@ public class APIDispatcher extends AbstractController

static final Charset UTF8 = Charset.forName("UTF-8");

static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geoserver.ows");
static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geoserver.api");

// SHARE
/** list of callbacks */
Expand Down Expand Up @@ -104,9 +103,10 @@ protected boolean isHandler(Class<?> beanType) {
this.mappingHandler.getUrlPathHelper().setAlwaysUseFullPath(true);

// create the one handler adapter we need similar to how DispatcherServlet does it
WebMvcConfigurationSupport configurationSupport =
context.getAutowireCapableBeanFactory()
.createBean(WebMvcConfigurationSupport.class);
// but with a special implementation that supports callbacks for the operation
APIConfigurationSupport configurationSupport =
context.getAutowireCapableBeanFactory().createBean(APIConfigurationSupport.class);
configurationSupport.setCallbacks(callbacks);
handlerAdapter = configurationSupport.requestMappingHandlerAdapter();
handlerAdapter.setApplicationContext(context);
handlerAdapter.afterPropertiesSet();
Expand All @@ -116,21 +116,13 @@ protected boolean isHandler(Class<?> beanType) {
// add all registered converters before the Spring ones too
List<HttpMessageConverter> extensionConverters =
GeoServerExtensions.extensions(HttpMessageConverter.class);
// add them in reverse order to the head, so that they will have the same order as extension
// priority commands
ListIterator<HttpMessageConverter> itr =
extensionConverters.listIterator(extensionConverters.size());
while (itr.hasPrevious()) {
handlerAdapter.getMessageConverters().add(0, itr.previous());
}
addToListBackwards(extensionConverters, handlerAdapter.getMessageConverters());
this.messageConverters = handlerAdapter.getMessageConverters();

// add custom argument resolvers
List<HandlerMethodArgumentResolver> argumentResolvers =
new ArrayList<>(handlerAdapter.getArgumentResolvers());
argumentResolvers.add(0, new BaseURLArgumentResolver());
argumentResolvers.add(0, new NegotiatedContentTypeArgumentResolver());
handlerAdapter.setArgumentResolvers(argumentResolvers);
List<HandlerMethodArgumentResolver> argumentResolves =
GeoServerExtensions.extensions(HandlerMethodArgumentResolver.class);
addToListBackwards(argumentResolves, handlerAdapter.getArgumentResolvers());

// default treatment of "f" parameter and headers, defaulting to JSON if nothing else has
// been provided
Expand Down Expand Up @@ -179,6 +171,15 @@ public void handleReturnValue(
}));
}

private void addToListBackwards(List source, List target) {
// add them in reverse order to the head, so that they will have the same order as extension
// priority commands
ListIterator arIterator = source.listIterator(source.size());
while (arIterator.hasPrevious()) {
target.add(0, arIterator.previous());
}
}

@Override
public void onApplicationEvent(ContextStartedEvent event) {}

Expand Down
14 changes: 0 additions & 14 deletions src/community/ogcapi/src/main/java/org/geoserver/api/BaseURL.java

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.logging.Logger;
import org.geoserver.api.APIDispatcher;
import org.geoserver.api.APIService;
import org.geoserver.api.BaseURL;
import org.geoserver.api.HTMLResponseBody;
import org.geoserver.api.OpenAPIMessageConverter;
import org.geoserver.catalog.Catalog;
Expand Down Expand Up @@ -58,7 +57,7 @@ private Catalog getCatalog() {
@GetMapping(name = "landingPage")
@ResponseBody
@HTMLResponseBody(templateName = "landingPage.ftl", fileName = "landingPage.html")
public LandingPageDocument getLandingPage(@BaseURL String baseURL) {
public LandingPageDocument getLandingPage() {
return new LandingPageDocument(getService(), getCatalog(), "ogc/features");
}

Expand All @@ -80,7 +79,7 @@ public OpenAPI api() {
@GetMapping(path = "collections", name = "collections")
@ResponseBody
@HTMLResponseBody(templateName = "collections.ftl", fileName = "collections.html")
public CollectionsDocument getCollections(@BaseURL String baseURL) {
public CollectionsDocument getCollections() {
return new CollectionsDocument(geoServer);
}

Expand Down

0 comments on commit b83b7f1

Please sign in to comment.