use of io.micronaut.http.HttpResponse in project check-ins by objectcomputing.
the class CurrentUserController method currentUser.
/**
* Get user details from Google authentication
*
* @param authentication {@link Authentication} or null
* @return {@link HttpResponse<CurrentUserDTO>}
*/
@Get
public HttpResponse<CurrentUserDTO> currentUser(@Nullable Authentication authentication) {
if (authentication == null) {
return HttpResponse.unauthorized();
}
String workEmail = authentication.getAttributes().get("email").toString();
String imageUrl = authentication.getAttributes().get("picture") != null ? authentication.getAttributes().get("picture").toString() : "";
String name = authentication.getAttributes().get("name").toString().trim();
String firstName = name.substring(0, name.indexOf(' '));
String lastName = name.substring(name.indexOf(' ') + 1).trim();
MemberProfile user = currentUserServices.findOrSaveUser(firstName, lastName, workEmail);
List<Permission> permissions = permissionServices.findUserPermissions(user.getId());
Set<Role> roles = roleServices.findUserRoles(user.getId());
List<String> rolesAsString = roles.stream().map(o -> o.getRole()).collect(Collectors.toList());
return HttpResponse.ok().headers(headers -> headers.location(location(user.getId()))).body(fromEntity(user, imageUrl, permissions, rolesAsString));
}
use of io.micronaut.http.HttpResponse in project check-ins by objectcomputing.
the class GuildControllerTest method deleteGuildByAdmin.
@Test
void deleteGuildByAdmin() {
// setup guild
Guild guildEntity = createDefaultGuild();
// create members
MemberProfile memberProfileOfAdmin = createAnUnrelatedUser();
createAndAssignAdminRole(memberProfileOfAdmin);
// add members to guild
createDefaultGuildMember(guildEntity, memberProfileOfAdmin);
final MutableHttpRequest<?> request = HttpRequest.DELETE(String.format("/%s", guildEntity.getId())).basicAuth(memberProfileOfAdmin.getWorkEmail(), ADMIN_ROLE);
final HttpResponse response = client.toBlocking().exchange(request);
assertEquals(HttpStatus.OK, response.getStatus());
}
use of io.micronaut.http.HttpResponse in project check-ins by objectcomputing.
the class TeamControllerTest method deleteTeamByTeamLead.
@Test
void deleteTeamByTeamLead() {
// setup team
Team teamEntity = createDefaultTeam();
// create members
MemberProfile memberProfileofTeamLeadEntity = createADefaultMemberProfile();
// add members to team
createLeadTeamMember(teamEntity, memberProfileofTeamLeadEntity);
// createDefaultTeamMember(team, memberProfileOfTeamMember);
final MutableHttpRequest<?> request = HttpRequest.DELETE(String.format("/%s", teamEntity.getId())).basicAuth(memberProfileofTeamLeadEntity.getWorkEmail(), MEMBER_ROLE);
final HttpResponse response = client.toBlocking().exchange(request);
assertEquals(HttpStatus.OK, response.getStatus());
}
use of io.micronaut.http.HttpResponse in project akhq by tchiotludo.
the class StaticFilter method doFilter.
@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
return Publishers.map(chain.proceed(request), response -> {
boolean first = response.getBody(NettyStreamedFileCustomizableResponseType.class).filter(n -> n.getMediaType().getName().equals(MediaType.TEXT_HTML)).isPresent();
boolean second = response.getBody(NettySystemFileCustomizableResponseType.class).filter(n -> n.getFile().getAbsoluteFile().toString().endsWith("ui/index.html")).isPresent();
if (first || second) {
try {
InputStream inputStream = Objects.requireNonNull(StaticFilter.class.getClassLoader().getResourceAsStream("ui/index.html"));
String content;
try (Reader reader = new InputStreamReader(inputStream)) {
content = CharStreams.toString(reader);
}
String finalBody = replace(content);
return HttpResponse.<String>ok().body(finalBody).contentType(MediaType.TEXT_HTML).contentLength(finalBody.length());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return response;
});
}
use of io.micronaut.http.HttpResponse in project micronaut-core by micronaut-projects.
the class DefaultHttpClient method buildExchangeStreamPublisher.
/**
* @param request The request
* @param errorType The error type
* @param <I> The input type
* @return A {@link Function}
*/
protected <I> Function<URI, Publisher<HttpResponse<ByteBuffer<?>>>> buildExchangeStreamPublisher(@NonNull io.micronaut.http.HttpRequest<I> request, @NonNull Argument<?> errorType) {
final io.micronaut.http.HttpRequest<Object> parentRequest = ServerRequestContext.currentRequest().orElse(null);
return requestURI -> {
Flux<io.micronaut.http.HttpResponse<Object>> streamResponsePublisher = Flux.from(buildStreamExchange(parentRequest, request, requestURI, errorType));
return streamResponsePublisher.switchMap(response -> {
StreamedHttpResponse streamedHttpResponse = NettyHttpResponseBuilder.toStreamResponse(response);
Flux<HttpContent> httpContentReactiveSequence = Flux.from(streamedHttpResponse);
return httpContentReactiveSequence.filter(message -> !(message.content() instanceof EmptyByteBuf)).map(message -> {
ByteBuf byteBuf = message.content();
if (log.isTraceEnabled()) {
log.trace("HTTP Client Streaming Response Received Chunk (length: {}) for Request: {} {}", byteBuf.readableBytes(), request.getMethodName(), request.getUri());
traceBody("Response", byteBuf);
}
ByteBuffer<?> byteBuffer = byteBufferFactory.wrap(byteBuf);
NettyStreamedHttpResponse<ByteBuffer<?>> thisResponse = new NettyStreamedHttpResponse<>(streamedHttpResponse, response.status());
thisResponse.setBody(byteBuffer);
return (HttpResponse<ByteBuffer<?>>) new HttpResponseWrapper<>(thisResponse);
});
}).doOnTerminate(() -> {
final Object o = request.getAttribute(NettyClientHttpRequest.CHANNEL).orElse(null);
if (o instanceof Channel) {
final Channel c = (Channel) o;
if (c.isOpen()) {
c.close();
}
}
});
};
}
Aggregations