Search in sources :

Example 1 with Consumes

use of io.micronaut.http.annotation.Consumes in project check-ins by objectcomputing.

the class LocalLoginController method auth.

@Consumes({ MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON })
@Post
public Single<MutableHttpResponse<?>> auth(HttpRequest<?> request, String email, String role) {
    UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials(email, role);
    Flowable<AuthenticationResponse> authenticationResponseFlowable = Flowable.fromPublisher(authenticator.authenticate(request, usernamePasswordCredentials));
    return authenticationResponseFlowable.map(authenticationResponse -> {
        if (authenticationResponse.isAuthenticated() && authenticationResponse.getUserDetails().isPresent()) {
            UserDetails userDetails = authenticationResponse.getUserDetails().get();
            // Get member profile by work email
            MemberProfile memberProfile = currentUserServices.findOrSaveUser("", "", email);
            String firstName = memberProfile.getFirstName() != null ? memberProfile.getFirstName() : "";
            String lastName = memberProfile.getLastName() != null ? memberProfile.getLastName() : "";
            userDetails.setAttributes(Map.of("email", memberProfile.getWorkEmail(), "name", firstName + ' ' + lastName, "picture", ""));
            eventPublisher.publishEvent(new LoginSuccessfulEvent(userDetails));
            return loginHandler.loginSuccess(userDetails, request);
        } else {
            eventPublisher.publishEvent(new LoginFailedEvent(authenticationResponse));
            return loginHandler.loginFailed(authenticationResponse, request);
        }
    }).first(HttpResponse.status(HttpStatus.UNAUTHORIZED));
}
Also used : UsernamePasswordCredentials(io.micronaut.security.authentication.UsernamePasswordCredentials) View(io.micronaut.views.View) Single(io.reactivex.Single) Environments(com.objectcomputing.checkins.Environments) LoginSuccessfulEvent(io.micronaut.security.event.LoginSuccessfulEvent) Flowable(io.reactivex.Flowable) HttpStatus(io.micronaut.http.HttpStatus) MediaType(io.micronaut.http.MediaType) Map(java.util.Map) Requires(io.micronaut.context.annotation.Requires) HttpResponse(io.micronaut.http.HttpResponse) HttpRequest(io.micronaut.http.HttpRequest) LoginFailedEvent(io.micronaut.security.event.LoginFailedEvent) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) Get(io.micronaut.http.annotation.Get) Controller(io.micronaut.http.annotation.Controller) LoginHandler(io.micronaut.security.handlers.LoginHandler) Secured(io.micronaut.security.annotation.Secured) ApplicationEventPublisher(io.micronaut.context.event.ApplicationEventPublisher) MutableHttpResponse(io.micronaut.http.MutableHttpResponse) SecurityRule(io.micronaut.security.rules.SecurityRule) Post(io.micronaut.http.annotation.Post) Authenticator(io.micronaut.security.authentication.Authenticator) UserDetails(io.micronaut.security.authentication.UserDetails) Consumes(io.micronaut.http.annotation.Consumes) CurrentUserServices(com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) Collections(java.util.Collections) UserDetails(io.micronaut.security.authentication.UserDetails) MemberProfile(com.objectcomputing.checkins.services.memberprofile.MemberProfile) LoginSuccessfulEvent(io.micronaut.security.event.LoginSuccessfulEvent) AuthenticationResponse(io.micronaut.security.authentication.AuthenticationResponse) LoginFailedEvent(io.micronaut.security.event.LoginFailedEvent) UsernamePasswordCredentials(io.micronaut.security.authentication.UsernamePasswordCredentials) Consumes(io.micronaut.http.annotation.Consumes) Post(io.micronaut.http.annotation.Post)

Example 2 with Consumes

use of io.micronaut.http.annotation.Consumes in project micronaut-core by micronaut-projects.

the class AnnotatedFunctionRouteBuilder method process.

@SuppressWarnings("unchecked")
@Override
public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> method) {
    if (beanDefinition.hasAnnotation(FunctionBean.class)) {
        String methodName = method.getMethodName();
        Class<?> declaringType = method.getDeclaringType();
        String functionName = beanDefinition.stringValue(FunctionBean.class).orElse(methodName);
        String functionMethod = beanDefinition.stringValue(FunctionBean.class, "method").orElse(null);
        List<UriRoute> routes = new ArrayList<>(2);
        MediaType[] consumes = Arrays.stream(method.stringValues(Consumes.class)).map(MediaType::of).toArray(MediaType[]::new);
        MediaType[] produces = Arrays.stream(method.stringValues(Produces.class)).map(MediaType::of).toArray(MediaType[]::new);
        if (Stream.of(java.util.function.Function.class, Consumer.class, BiFunction.class, BiConsumer.class).anyMatch(type -> type.isAssignableFrom(declaringType))) {
            if (methodName.equals("accept") || methodName.equals("apply") || methodName.equals(functionMethod)) {
                String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
                String[] argumentNames = method.getArgumentNames();
                String argumentName = argumentNames[0];
                int argCount = argumentNames.length;
                UriRoute route = POST(functionPath, beanDefinition, method);
                routes.add(route);
                if (argCount == 1) {
                    route.body(argumentName);
                }
                List<Argument<?>> typeArguments = beanDefinition.getTypeArguments();
                if (!typeArguments.isEmpty()) {
                    int size = typeArguments.size();
                    Argument<?> firstArgument = typeArguments.get(0);
                    if (size < 3 && ClassUtils.isJavaLangType(firstArgument.getType()) && consumes == null) {
                        consumes = new MediaType[] { MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_JSON_TYPE };
                    }
                    if (size < 3) {
                        route.body(Argument.of(firstArgument.getType(), argumentName));
                    }
                    if (size > 1) {
                        Argument<?> argument = typeArguments.get(size == 3 ? 2 : 1);
                        if (ClassUtils.isJavaLangType(argument.getType()) && produces == null) {
                            produces = new MediaType[] { MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_JSON_TYPE };
                        }
                    }
                } else {
                    if (argCount == 1 && ClassUtils.isJavaLangType(method.getArgumentTypes()[0]) && consumes == null) {
                        consumes = new MediaType[] { MediaType.TEXT_PLAIN_TYPE, MediaType.APPLICATION_JSON_TYPE };
                    }
                }
            }
        } else if (Supplier.class.isAssignableFrom(declaringType) && methodName.equals("get")) {
            String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
            routes.add(GET(functionPath, beanDefinition, method));
            routes.add(HEAD(functionPath, beanDefinition, method));
        } else {
            if (StringUtils.isNotEmpty(functionMethod) && functionMethod.equals(methodName)) {
                Argument[] argumentTypes = method.getArguments();
                int argCount = argumentTypes.length;
                if (argCount < 3) {
                    String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
                    if (argCount == 0) {
                        routes.add(GET(functionPath, beanDefinition, method));
                        routes.add(HEAD(functionPath, beanDefinition, method));
                    } else {
                        UriRoute route = POST(functionPath, beanDefinition, method);
                        routes.add(route);
                        if (argCount == 2 || !ClassUtils.isJavaLangType(argumentTypes[0].getType())) {
                            if (consumes == null) {
                                consumes = new MediaType[] { MediaType.APPLICATION_JSON_TYPE };
                            }
                        } else {
                            route.body(method.getArgumentNames()[0]).consumesAll();
                        }
                    }
                }
            }
        }
        if (!routes.isEmpty()) {
            for (UriRoute route : routes) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Created Route to Function: {}", route);
                }
                if (consumes != null) {
                    route.consumes(consumes);
                }
                if (produces != null) {
                    route.produces(produces);
                }
            }
            String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
            availableFunctions.put(functionName, URI.create(functionPath));
            ((ExecutableMethodProcessor) localFunctionRegistry).process(beanDefinition, method);
        }
    }
}
Also used : FunctionBean(io.micronaut.function.FunctionBean) Argument(io.micronaut.core.type.Argument) BiFunction(java.util.function.BiFunction) UriRoute(io.micronaut.web.router.UriRoute) java.util(java.util) BiConsumer(java.util.function.BiConsumer) Consumer(java.util.function.Consumer) Consumes(io.micronaut.http.annotation.Consumes) Produces(io.micronaut.http.annotation.Produces) BiFunction(java.util.function.BiFunction) MediaType(io.micronaut.http.MediaType) BiConsumer(java.util.function.BiConsumer) ExecutableMethodProcessor(io.micronaut.context.processor.ExecutableMethodProcessor)

Aggregations

MediaType (io.micronaut.http.MediaType)2 Consumes (io.micronaut.http.annotation.Consumes)2 Environments (com.objectcomputing.checkins.Environments)1 MemberProfile (com.objectcomputing.checkins.services.memberprofile.MemberProfile)1 CurrentUserServices (com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices)1 Requires (io.micronaut.context.annotation.Requires)1 ApplicationEventPublisher (io.micronaut.context.event.ApplicationEventPublisher)1 ExecutableMethodProcessor (io.micronaut.context.processor.ExecutableMethodProcessor)1 Argument (io.micronaut.core.type.Argument)1 FunctionBean (io.micronaut.function.FunctionBean)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 HttpStatus (io.micronaut.http.HttpStatus)1 MutableHttpResponse (io.micronaut.http.MutableHttpResponse)1 Controller (io.micronaut.http.annotation.Controller)1 Get (io.micronaut.http.annotation.Get)1 Post (io.micronaut.http.annotation.Post)1 Produces (io.micronaut.http.annotation.Produces)1 Secured (io.micronaut.security.annotation.Secured)1 AuthenticationResponse (io.micronaut.security.authentication.AuthenticationResponse)1