Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AnnotatedService public #5628

Merged
merged 26 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2d75886
Make AnnotatedService public
chickenchickenlove Apr 22, 2024
51424ed
fix dependencies
chickenchickenlove Apr 22, 2024
8f785a9
apply review
chickenchickenlove Apr 24, 2024
15b1fb5
rename to .
chickenchickenlove Apr 24, 2024
4fed8e3
apply review
chickenchickenlove Apr 24, 2024
93a11dc
Update core/src/main/java/com/linecorp/armeria/internal/server/annota…
chickenchickenlove Apr 28, 2024
dabc97b
Update core/src/main/java/com/linecorp/armeria/internal/server/annota…
chickenchickenlove Apr 28, 2024
31811e0
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
68d422a
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
e2989b0
Update core/src/main/java/com/linecorp/armeria/server/DefaultServiceC…
chickenchickenlove Apr 28, 2024
23e41d5
Update core/src/main/java/com/linecorp/armeria/server/DefaultServiceC…
chickenchickenlove Apr 28, 2024
db4cbb0
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
ce2d651
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
ee3baae
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
f13f91a
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
ffb97bb
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
3424468
Update core/src/main/java/com/linecorp/armeria/server/annotation/Anno…
chickenchickenlove Apr 28, 2024
d622251
Update core/src/main/java/com/linecorp/armeria/internal/server/annota…
chickenchickenlove Apr 28, 2024
2bdfcd1
Update core/src/main/java/com/linecorp/armeria/internal/server/annota…
chickenchickenlove Apr 28, 2024
12b7179
Apply reveiw
chickenchickenlove Apr 28, 2024
698579f
Update core/src/main/java/com/linecorp/armeria/server/DefaultServiceC…
chickenchickenlove Apr 30, 2024
6cc6923
Update core/src/main/java/com/linecorp/armeria/internal/common/util/S…
chickenchickenlove Apr 30, 2024
180d110
apply review
chickenchickenlove Apr 30, 2024
68e509f
minor cleanups
jrhee17 May 3, 2024
c42fb4d
DefaultAnnotatedService can be package-private
jrhee17 May 3, 2024
3dadf29
Update core/src/main/java/com/linecorp/armeria/internal/server/annota…
minwoox May 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.linecorp.armeria.internal.common.util;

import com.linecorp.armeria.common.util.Unwrappable;
import com.linecorp.armeria.internal.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.annotation.AnnotatedService;

public final class ServiceNamingUtil {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import com.linecorp.armeria.server.RoutePathType;
import com.linecorp.armeria.server.Service;
import com.linecorp.armeria.server.ServiceConfig;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.Header;
import com.linecorp.armeria.server.annotation.Param;
import com.linecorp.armeria.server.annotation.RequestObject;
Expand Down Expand Up @@ -144,7 +145,7 @@ public ServiceSpecification generateSpecification(Set<ServiceConfig> serviceConf
serviceConfigs.forEach(sc -> {
final AnnotatedService service = sc.service().as(AnnotatedService.class);
if (service != null) {
final Class<?> serviceClass = ClassUtil.getUserClass(service.object().getClass());
final Class<?> serviceClass = ClassUtil.getUserClass(service.serviceObject().getClass());
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
final String className = serviceClass.getName();
final String methodName = service.method().getName();
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
if (!filter.test(name(), className, methodName)) {
Expand All @@ -171,7 +172,8 @@ private static void addMethodInfo(Map<Class<?>, Set<MethodInfo>> methodInfos,
final Method method = service.method();
final int overloadId = service.overloadId();
final TypeSignature returnTypeSignature = getReturnTypeSignature(method);
final List<FieldInfo> fieldInfos = fieldInfos(service.annotatedValueResolvers());
final List<FieldInfo> fieldInfos = fieldInfos(service.as(DefaultAnnotatedService.class)
.annotatedValueResolvers());
route.methods().forEach(
httpMethod -> {
final MethodInfo methodInfo = new MethodInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.Route;
import com.linecorp.armeria.server.annotation.AnnotatedService;

/**
* Details of an annotated HTTP service method.
Expand Down Expand Up @@ -78,7 +79,8 @@ public HttpService buildSafeDecoratedService(
HttpService decoratedService = decorator.apply(service);
// Apply localDecorator passed in through method parameter
decoratedService = decoratedService.decorate(localDecorator);
return service.withExceptionHandler(decoratedService);
return service.as(DefaultAnnotatedService.class)
.withExceptionHandler(decoratedService);
trustin marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import com.linecorp.armeria.server.Route;
import com.linecorp.armeria.server.annotation.AdditionalHeader;
import com.linecorp.armeria.server.annotation.AdditionalTrailer;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.Blocking;
import com.linecorp.armeria.server.annotation.Consumes;
import com.linecorp.armeria.server.annotation.Decorator;
Expand Down Expand Up @@ -291,8 +292,10 @@ static List<AnnotatedServiceElement> create(String pathPrefix, Object object, Me
queryDelimiter);
return new AnnotatedServiceElement(
route,
new AnnotatedService(object, method, overloadId, resolvers, eh, res, route, defaultStatus,
responseHeaders, responseTrailers, needToUseBlockingTaskExecutor),
new DefaultAnnotatedService(object, method, overloadId,
resolvers, eh, res, route, defaultStatus,
responseHeaders, responseTrailers,
needToUseBlockingTaskExecutor),
decorator(method, clazz, dependencyInjector));
}).collect(toImmutableList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import com.linecorp.armeria.internal.server.FileAggregatedMultipart;
import com.linecorp.armeria.internal.server.annotation.AnnotatedBeanFactoryRegistry.BeanFactoryId;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.ByteArrayRequestConverterFunction;
import com.linecorp.armeria.server.annotation.Default;
import com.linecorp.armeria.server.annotation.Delimiter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.linecorp.armeria.server.RoutingContext;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.SimpleDecoratingHttpService;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.ExceptionHandlerFunction;
import com.linecorp.armeria.server.annotation.ExceptionVerbosity;
import com.linecorp.armeria.server.annotation.FallthroughException;
Expand All @@ -76,8 +77,8 @@
* This class is not supposed to be instantiated by a user. Please check out the documentation
* <a href="https://armeria.dev/docs/server-annotated-service">Annotated HTTP Service</a> to use this.
*/
public final class AnnotatedService implements HttpService {
private static final Logger logger = LoggerFactory.getLogger(AnnotatedService.class);
public final class DefaultAnnotatedService implements AnnotatedService {
private static final Logger logger = LoggerFactory.getLogger(DefaultAnnotatedService.class);

private static final MethodHandles.Lookup lookup = MethodHandles.lookup();

Expand Down Expand Up @@ -106,18 +107,19 @@ public final class AnnotatedService implements HttpService {

private final ResponseType responseType;
private final boolean useBlockingTaskExecutor;
private final String serviceName;
@Nullable
private String serviceName;
private final boolean serviceNameSetByAnnotation;

AnnotatedService(Object object, Method method,
int overloadId, List<AnnotatedValueResolver> resolvers,
List<ExceptionHandlerFunction> exceptionHandlers,
List<ResponseConverterFunction> responseConverters,
Route route,
HttpStatus defaultStatus,
HttpHeaders defaultHttpHeaders,
HttpHeaders defaultHttpTrailers,
boolean useBlockingTaskExecutor) {
DefaultAnnotatedService(Object object, Method method,
int overloadId, List<AnnotatedValueResolver> resolvers,
List<ExceptionHandlerFunction> exceptionHandlers,
List<ResponseConverterFunction> responseConverters,
Route route,
HttpStatus defaultStatus,
HttpHeaders defaultHttpHeaders,
HttpHeaders defaultHttpTrailers,
boolean useBlockingTaskExecutor) {
this.object = requireNonNull(object, "object");
this.method = requireNonNull(method, "method");
checkArgument(overloadId >= 0, "overloadId: %s (expected: >= 0)", overloadId);
Expand Down Expand Up @@ -168,7 +170,6 @@ public final class AnnotatedService implements HttpService {
this.serviceName = serviceName.value();
serviceNameSetByAnnotation = true;
} else {
this.serviceName = ClassUtil.getUserClass(object.getClass()).getName();
trustin marked this conversation as resolved.
Show resolved Hide resolved
serviceNameSetByAnnotation = false;
}

Expand Down Expand Up @@ -221,39 +222,52 @@ private static void warnIfHttpResponseArgumentExists(Type returnType,
}

public String serviceName() {
if (serviceName == null) {
trustin marked this conversation as resolved.
Show resolved Hide resolved
return serviceClass().getName();
}
return serviceName;
}

public boolean serviceNameSetByAnnotation() {
return serviceNameSetByAnnotation;
}
trustin marked this conversation as resolved.
Show resolved Hide resolved

@Override
public String methodName() {
return method.getName();
}

Object object() {
@Override
public Object serviceObject() {
return object;
}

Method method() {
@Override
public Class<?> serviceClass() {
return ClassUtil.getUserClass(object.getClass());
trustin marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public Method method() {
return method;
}

int overloadId() {
@Override
public int overloadId() {
return overloadId;
}

List<AnnotatedValueResolver> annotatedValueResolvers() {
return resolvers;
}

Route route() {
@Override
public Route route() {
return route;
}

// TODO: Expose through `AnnotatedServiceConfig`, see #5382.
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
HttpStatus defaultStatus() {
@Override
public HttpStatus defaultStatus() {
return defaultStatus;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.linecorp.armeria.common.ResponseHeaders;
import com.linecorp.armeria.common.ResponseHeadersBuilder;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.HttpResult;

final class HttpResultUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.linecorp.armeria.common.SuccessFunction;
import com.linecorp.armeria.common.annotation.UnstableApi;
import com.linecorp.armeria.common.util.BlockingTaskExecutor;
import com.linecorp.armeria.internal.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.ExceptionHandlerFunction;
import com.linecorp.armeria.server.annotation.RequestConverterFunction;
import com.linecorp.armeria.server.annotation.ResponseConverterFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.Exceptions;
import com.linecorp.armeria.internal.common.util.TemporaryThreadLocals;
import com.linecorp.armeria.internal.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.AnnotatedService;

/**
* The default {@link ServerErrorHandler} that is used when a user didn't specify one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.util.BlockingTaskExecutor;
import com.linecorp.armeria.common.util.EventLoopGroups;
import com.linecorp.armeria.internal.server.annotation.AnnotatedService;
import com.linecorp.armeria.internal.server.annotation.DefaultAnnotatedService;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.logging.AccessLogWriter;

import io.netty.channel.EventLoopGroup;
Expand Down Expand Up @@ -338,7 +339,8 @@ ServiceConfigBuilder toServiceConfigBuilder(Route route, String contextPath, Htt
} else {
// Set the default service name only when the service name is set using @ServiceName.
// If it's not, the global defaultServiceNaming is used.
if (annotatedService != null && annotatedService.serviceNameSetByAnnotation()) {
if (annotatedService != null && annotatedService.as(DefaultAnnotatedService.class)
.serviceNameSetByAnnotation()) {
trustin marked this conversation as resolved.
Show resolved Hide resolved
serviceConfigBuilder.defaultServiceName(annotatedService.serviceName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.linecorp.armeria.common.SuccessFunction;
import com.linecorp.armeria.common.annotation.UnstableApi;
import com.linecorp.armeria.common.util.BlockingTaskExecutor;
import com.linecorp.armeria.internal.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.ExceptionHandlerFunction;
import com.linecorp.armeria.server.annotation.RequestConverterFunction;
import com.linecorp.armeria.server.annotation.ResponseConverterFunction;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2017 LINE Corporation
*
* LINE Corporation licenses this file to you 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:
*
* https://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.
*/

package com.linecorp.armeria.server.annotation;

import java.lang.reflect.Method;

import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.Route;
import com.linecorp.armeria.server.ServerBuilder;

/**
* An {@link HttpService} which is defined by a {@link Path} or HTTP method annotations.
* This class is designed to provide a common interface for {@link AnnotatedService}
* internally. Please check out the documentation at
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
* <a href="https://armeria.dev/docs/server-annotated-service">Annotated HTTP Service</a> to use this.
*/
public interface AnnotatedService extends HttpService {
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns service name for this {@link AnnotatedService}.
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
*/
String serviceName();
trustin marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns method name which is annotated in {@link AnnotatedService}.
*/
String methodName();
trustin marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the annotated service object specified with {@link ServerBuilder#annotatedService(Object)}.
*/
Object serviceObject();

/**
* Returns the annotated service object specified with {@link ServerBuilder#annotatedService(Object)}.
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
*/
Class<?> serviceClass();

/**
* Returns the target {@link Method} invoked when the request is received.
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
*/
Method method();

/**
* Returns a unique ID to distinguish overloaded methods.
* If the method is not overloaded, it should return 0.
*/
int overloadId();
trustin marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns {@link Route} for this {@link AnnotatedService}.
chickenchickenlove marked this conversation as resolved.
Show resolved Hide resolved
*/
Route route();

/**
* Returns the default {@link HttpStatus} specified with {@link StatusCode}.
* If {@link StatusCode} is not given, {@link HttpStatus#OK} is returned by default.
* If the method returns a void type such as {@link Void} or Kotlin Unit, {@link HttpStatus#NO_CONTENT} is
* returned.
*/
HttpStatus defaultStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import com.linecorp.armeria.common.RequestContext;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.internal.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.annotation.AnnotatedService;
import com.linecorp.armeria.server.kotlin.CoroutineContextService;

import io.netty.util.AttributeKey;
Expand Down
Loading