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

Add support HttpExchange annotations #652

Open
wants to merge 1 commit into
base: 5.9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -32,6 +32,7 @@
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.annotation.Put;
import io.micronaut.http.annotation.Trace;
import io.micronaut.http.annotation.UriMapping;
import io.micronaut.inject.annotation.NamedAnnotationTransformer;
import io.micronaut.inject.visitor.VisitorContext;

Expand All @@ -46,6 +47,7 @@
* @since 1.0
*/
public class RequestMappingAnnotationTransformer implements NamedAnnotationTransformer {

@Override
public String getName() {
return "org.springframework.web.bind.annotation.RequestMapping";
Expand All @@ -67,8 +69,8 @@ protected boolean isHttpMethodMapping(@Nullable HttpMethod method) {
* @return The builder
*/
protected @NonNull AnnotationValueBuilder<?> newBuilder(
@Nullable HttpMethod httpMethod,
AnnotationValue<Annotation> annotation) {
@Nullable HttpMethod httpMethod,
AnnotationValue<Annotation> annotation) {

if (httpMethod != null) {
return switch (httpMethod) {
Expand All @@ -80,15 +82,15 @@ protected boolean isHttpMethodMapping(@Nullable HttpMethod method) {
case PUT -> AnnotationValue.builder(Put.class);
case PATCH -> AnnotationValue.builder(Patch.class);
case OPTIONS -> AnnotationValue.builder(Options.class);
default -> AnnotationValue.builder("io.micronaut.http.annotation.UriMapping");
default -> AnnotationValue.builder(UriMapping.class);
};
} else {
return AnnotationValue.builder("io.micronaut.http.annotation.UriMapping");
return AnnotationValue.builder(UriMapping.class);
}
}

private String computePath(AnnotationValue<Annotation> annotation) {
return annotation.stringValue().orElseGet(() -> annotation.stringValue("path").orElse("/"));
return annotation.stringValue().orElseGet(() -> annotation.stringValue("path").orElse(UriMapping.DEFAULT_URI));
}

@Override
Expand All @@ -101,11 +103,11 @@ public List<AnnotationValue<?>> transform(AnnotationValue<Annotation> annotation
annotations.add(newBuilder(method, annotation).value(path).build());

final String[] consumes = annotation.stringValues("consumes");
final String[] produces = annotation.stringValues("produces");

if (ArrayUtils.isNotEmpty(consumes)) {
annotations.add(AnnotationValue.builder(Consumes.class).member("value", consumes).build());
}

final String[] produces = annotation.stringValues("produces");
if (ArrayUtils.isNotEmpty(produces)) {
annotations.add(AnnotationValue.builder(Produces.class).member("value", produces).build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017-2020 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
*
* 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 io.micronaut.spring.web.annotation.exchange;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.http.annotation.Delete;

/**
* Maps Spring DeleteExchange to Micronaut.
*
* @since 5.10.0
*/
public class DeleteExchangeAnnotationTransformer extends HttpExchangeAnnotationTransformer {

@Override
public String getName() {
return "org.springframework.web.service.annotation.DeleteExchange";
}

@Override
protected AnnotationValueBuilder<?> newBuilder(String httpMethod) {
return AnnotationValue.builder(Delete.class);
}

@Override
protected boolean isHttpMethodMapping(String method) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017-2020 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
*
* 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 io.micronaut.spring.web.annotation.exchange;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.http.annotation.Get;

/**
* Maps Spring GetExchange to Micronaut.
*
* @since 5.10.0
*/
public class GetExchangeAnnotationTransformer extends HttpExchangeAnnotationTransformer {

@Override
public String getName() {
return "org.springframework.web.service.annotation.GetExchange";
}

@Override
protected AnnotationValueBuilder<?> newBuilder(String httpMethod) {
return AnnotationValue.builder(Get.class);
}

@Override
protected boolean isHttpMethodMapping(String method) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2017-2024 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
*
* 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 io.micronaut.spring.web.annotation.exchange;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Delete;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Head;
import io.micronaut.http.annotation.HttpMethodMapping;
import io.micronaut.http.annotation.Options;
import io.micronaut.http.annotation.Patch;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.annotation.Put;
import io.micronaut.http.annotation.Trace;
import io.micronaut.http.annotation.UriMapping;
import io.micronaut.inject.annotation.NamedAnnotationTransformer;
import io.micronaut.inject.visitor.VisitorContext;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

/**
* Maps Spring HttpExchange to Micronaut.
*
* @since 5.10.0
*/
public class HttpExchangeAnnotationTransformer implements NamedAnnotationTransformer {

@Override
public String getName() {
return "org.springframework.web.service.annotation.HttpExchange";
}

/**
* Whether the given method is an HTTP method mapping.
* @param method The method, can be null
* @return True if it is
*/
protected boolean isHttpMethodMapping(@Nullable String method) {
return method != null;
}

/**
* Construct a new builder for the given http method.
* @param method HTTP method
* @return The builder
*/
@NonNull
protected AnnotationValueBuilder<?> newBuilder(String method) {

if (method != null) {
return switch (method.toUpperCase()) {
case "GET" -> AnnotationValue.builder(Get.class);
case "POST" -> AnnotationValue.builder(Post.class);
case "PATCH" -> AnnotationValue.builder(Patch.class);
case "PUT" -> AnnotationValue.builder(Put.class);
case "DELETE" -> AnnotationValue.builder(Delete.class);
case "HEAD" -> AnnotationValue.builder(Head.class);
case "OPTIONS" -> AnnotationValue.builder(Options.class);
case "TRACE" -> AnnotationValue.builder(Trace.class);
default -> AnnotationValue.builder(UriMapping.class);
};
} else {
return AnnotationValue.builder(UriMapping.class);
}
}

private String computePath(AnnotationValue<Annotation> annotation) {
return annotation.stringValue().orElseGet(() -> annotation.stringValue("url").orElse(UriMapping.DEFAULT_URI));
}

@Override
public List<AnnotationValue<?>> transform(AnnotationValue<Annotation> annotation, VisitorContext visitorContext) {
var annotations = new ArrayList<AnnotationValue<?>>();

final String path = computePath(annotation);
var method = annotation.stringValue("method").orElse(null);

annotations.add(newBuilder(method).value(path).build());

var contentType = annotation.stringValue("contentType").orElse(null);
if (StringUtils.isNotEmpty(contentType)) {
annotations.add(AnnotationValue.builder(Consumes.class).member("contentType", contentType).build());
}

final String[] accept = annotation.stringValues("accept");
if (ArrayUtils.isNotEmpty(accept)) {
annotations.add(AnnotationValue.builder(Produces.class).member("value", accept).build());
}

if (isHttpMethodMapping(method)) {
annotations.add(AnnotationValue.builder(HttpMethodMapping.class).value(path).build());
}
return annotations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017-2020 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
*
* 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 io.micronaut.spring.web.annotation.exchange;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.http.annotation.Patch;

/**
* Maps Spring PatchExchange to Micronaut.
*
* @since 5.10.0
*/
public class PatchExchangeAnnotationTransformer extends HttpExchangeAnnotationTransformer {

@Override
public String getName() {
return "org.springframework.web.service.annotation.PatchExchange";
}

@Override
protected AnnotationValueBuilder<?> newBuilder(String httpMethod) {
return AnnotationValue.builder(Patch.class);
}

@Override
protected boolean isHttpMethodMapping(String method) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017-2020 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
*
* 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 io.micronaut.spring.web.annotation.exchange;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.http.annotation.Post;

/**
* Maps Spring PostExchange to Micronaut.
*
* @since 5.10.0
*/
public class PostExchangeAnnotationTransformer extends HttpExchangeAnnotationTransformer {

@Override
public String getName() {
return "org.springframework.web.service.annotation.PostExchange";
}

@Override
protected AnnotationValueBuilder<?> newBuilder(String httpMethod) {
return AnnotationValue.builder(Post.class);
}

@Override
protected boolean isHttpMethodMapping(String method) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017-2020 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
*
* 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 io.micronaut.spring.web.annotation.exchange;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.AnnotationValueBuilder;
import io.micronaut.http.annotation.Put;

/**
* Maps Spring PutExchange to Micronaut.
*
* @since 5.10.0
*/
public class PutExchangeAnnotationTransformer extends HttpExchangeAnnotationTransformer {

@Override
public String getName() {
return "org.springframework.web.service.annotation.PutExchange";
}

@Override
protected AnnotationValueBuilder<?> newBuilder(String httpMethod) {
return AnnotationValue.builder(Put.class);
}

@Override
protected boolean isHttpMethodMapping(String method) {
return true;
}
}
Loading
Loading