Skip to content

Commit

Permalink
Cleanup and review javadoc & license "function-web" module
Browse files Browse the repository at this point in the history
  • Loading branch information
ilopmar committed Apr 4, 2018
1 parent d2a0d4a commit 4c7cb87
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,20 @@
/*
* Copyright 2017 original authors
*
* Copyright 2017-2018 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/
package io.micronaut.function.web;

import io.micronaut.context.ExecutionHandleLocator;
import io.micronaut.context.annotation.Replaces;
import io.micronaut.context.annotation.Value;
import io.micronaut.context.processor.ExecutableMethodProcessor;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.naming.NameUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.discovery.metadata.ServiceInstanceMetadataContributor;
import io.micronaut.function.DefaultLocalFunctionRegistry;
import io.micronaut.function.LocalFunctionRegistry;
import io.micronaut.http.MediaType;
import io.micronaut.http.codec.MediaTypeCodec;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.ExecutableMethod;
import io.micronaut.context.ExecutionHandleLocator;
import io.micronaut.context.annotation.Replaces;
import io.micronaut.context.annotation.Value;
Expand All @@ -44,14 +29,19 @@
import io.micronaut.function.FunctionBean;
import io.micronaut.function.LocalFunctionRegistry;
import io.micronaut.http.MediaType;
import io.micronaut.http.codec.MediaTypeCodec;
import io.micronaut.http.codec.MediaTypeCodecRegistry;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.ExecutableMethod;
import io.micronaut.web.router.DefaultRouteBuilder;
import io.micronaut.web.router.UriRoute;

import javax.inject.Singleton;
import java.net.URI;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
Expand All @@ -68,20 +58,19 @@
@Singleton
@Replaces(DefaultLocalFunctionRegistry.class)
public class AnnotatedFunctionRouteBuilder
extends DefaultRouteBuilder
implements ExecutableMethodProcessor<FunctionBean>, LocalFunctionRegistry, ServiceInstanceMetadataContributor, MediaTypeCodecRegistry {

extends DefaultRouteBuilder
implements ExecutableMethodProcessor<FunctionBean>, LocalFunctionRegistry, ServiceInstanceMetadataContributor, MediaTypeCodecRegistry {

private final LocalFunctionRegistry localFunctionRegistry;
private final String contextPath;
private final Map<String, URI> availableFunctions = new ConcurrentHashMap<>();

public AnnotatedFunctionRouteBuilder(
ExecutionHandleLocator executionHandleLocator,
UriNamingStrategy uriNamingStrategy,
ConversionService<?> conversionService,
MediaTypeCodecRegistry codecRegistry,
@Value("${function.contextPath:/}") String contextPath) {
ExecutionHandleLocator executionHandleLocator,
UriNamingStrategy uriNamingStrategy,
ConversionService<?> conversionService,
MediaTypeCodecRegistry codecRegistry,
@Value("${function.contextPath:/}") String contextPath) {
super(executionHandleLocator, uriNamingStrategy, conversionService);
this.localFunctionRegistry = new DefaultLocalFunctionRegistry(codecRegistry);
this.contextPath = contextPath.endsWith("/") ? contextPath : contextPath + '/';
Expand All @@ -91,47 +80,43 @@ public AnnotatedFunctionRouteBuilder(
@Override
public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
FunctionBean annotation = method.getAnnotation(FunctionBean.class);
if(annotation != null) {
if (annotation != null) {
String functionName = annotation.value();
String functionPath = functionName;
Class<?> declaringType = method.getDeclaringType();
if(StringUtils.isEmpty(functionPath)) {
if (StringUtils.isEmpty(functionPath)) {
String typeName = declaringType.getSimpleName();
if(typeName.contains("$")) {
if (typeName.contains("$")) {
// generated lambda
functionPath = contextPath + NameUtils.hyphenate(method.getMethodName());
}
else {
} else {
functionPath = contextPath + NameUtils.hyphenate(typeName);
}
}
else {
} else {
functionPath = contextPath + functionPath;
}

UriRoute route = null;
if(Stream.of(java.util.function.Function.class, Consumer.class, BiFunction.class, BiConsumer.class).anyMatch(type -> type.isAssignableFrom(declaringType))) {
if (Stream.of(java.util.function.Function.class, Consumer.class, BiFunction.class, BiConsumer.class).anyMatch(type -> type.isAssignableFrom(declaringType))) {
route = POST(functionPath, method);
}
else if(Supplier.class.isAssignableFrom(declaringType)) {
} else if (Supplier.class.isAssignableFrom(declaringType)) {
route = GET(functionPath, method);
}

if(route != null) {
if (route != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Created Route to Function: {}", route);
}

availableFunctions.put(functionName, URI.create(functionPath));
Class[] argumentTypes = method.getArgumentTypes();
int argCount = argumentTypes.length;
if(argCount > 0) {
if(argCount == 2 || !ClassUtils.isJavaLangType(argumentTypes[0])) {
if (argCount > 0) {
if (argCount == 2 || !ClassUtils.isJavaLangType(argumentTypes[0])) {
route.consumes(MediaType.APPLICATION_JSON_TYPE);
}
else {
} else {
route.body(method.getArgumentNames()[0])
.acceptAll();
.acceptAll();
}
}
((ExecutableMethodProcessor) localFunctionRegistry).process(beanDefinition, method);
Expand Down Expand Up @@ -189,23 +174,23 @@ public void contribute(ServiceInstance instance, Map<String, String> metadata) {

@Override
public Optional<MediaTypeCodec> findCodec(MediaType mediaType) {
if(localFunctionRegistry instanceof MediaTypeCodecRegistry) {
if (localFunctionRegistry instanceof MediaTypeCodecRegistry) {
return ((MediaTypeCodecRegistry) localFunctionRegistry).findCodec(mediaType);
}
return Optional.empty();
}

@Override
public Optional<MediaTypeCodec> findCodec(MediaType mediaType, Class<?> type) {
if(localFunctionRegistry instanceof MediaTypeCodecRegistry) {
if (localFunctionRegistry instanceof MediaTypeCodecRegistry) {
return ((MediaTypeCodecRegistry) localFunctionRegistry).findCodec(mediaType, type);
}
return Optional.empty();
}

@Override
public Collection<MediaTypeCodec> getCodecs() {
if(localFunctionRegistry instanceof MediaTypeCodecRegistry) {
if (localFunctionRegistry instanceof MediaTypeCodecRegistry) {
return ((MediaTypeCodecRegistry) localFunctionRegistry).getCodecs();
}
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/*
* Copyright 2017 original authors
*
* Copyright 2017-2018 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/

/**
* <p>Classes to support exposing {@link io.micronaut.function.FunctionBean} instances over the web</p>
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
* Copyright 2017 original authors
*
* Copyright 2017-2018 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/
package io.micronaut.function.web

Expand Down

0 comments on commit 4c7cb87

Please sign in to comment.