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));
}
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);
}
}
}
Aggregations