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

#6294 error when adding a new comment to an event #6310

Merged
merged 21 commits into from
Sep 26, 2023
Merged
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
8 changes: 4 additions & 4 deletions core/src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
#RestTemplate
greencityuser.server.address=http://localhost:8080

# Email for service-to-service communication with GreenCityUser
[email protected]

# Token expiration time
tokenKey=1
accessTokenValidTimeInMinutes=120
refreshTokenValidTimeInMinutes=600
verifyEmailTimeHour=24



verifyEmailTimeHour=24
63 changes: 51 additions & 12 deletions service-api/src/main/java/greencity/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import greencity.dto.eventcomment.EventCommentForSendEmailDto;
import greencity.message.SendEventCreationNotification;
import greencity.security.jwt.JwtTool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Pageable;
Expand All @@ -22,6 +23,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

Expand All @@ -36,19 +38,42 @@
import greencity.message.SendChangePlaceStatusEmailMessage;
import greencity.message.SendHabitNotification;
import greencity.message.SendReportEmailMessage;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import static greencity.constant.AppConstant.AUTHORIZATION;

@RequiredArgsConstructor
@Component
public class RestClient {
private final RestTemplate restTemplate;
@Setter
@Value("${greencityuser.server.address}")
private String greenCityUserServerAddress;
private final String greenCityUserServerAddress;
private final HttpServletRequest httpServletRequest;
private final JwtTool jwtTool;
private final String systemEmail;

/**
* Constructs a new instance of the RestClient class.
*
* @param restTemplate The RestTemplate to be used for making HTTP
* requests to GreenCityUser.
* @param greenCityUserServerAddress The address of the GreenCityUser server.
* @param httpServletRequest The HttpServletRequest object contains data
* related to the current http request.
* @param jwtTool The JwtTool is used to create JWT tokens
* for system requests to GreenCityUser.
* @param systemEmail The system email address used to creat JWT
* tokens for system requests to
* GreenCityUser.
*/
public RestClient(RestTemplate restTemplate,
@Value("${greencityuser.server.address}") String greenCityUserServerAddress,
HttpServletRequest httpServletRequest,
JwtTool jwtTool,
@Value("${spring.liquibase.parameters.service-email}") String systemEmail) {
this.restTemplate = restTemplate;
this.greenCityUserServerAddress = greenCityUserServerAddress;
this.httpServletRequest = httpServletRequest;
this.jwtTool = jwtTool;
this.systemEmail = systemEmail;
}

/**
* Method for getting all users by their {@link EmailNotification}.
Expand Down Expand Up @@ -390,7 +415,7 @@ public void managementRegisterUser(UserManagementDto userDto) {
* @author Taras Kavkalo
*/
public void addEcoNews(EcoNewsForSendEmailDto message) {
HttpHeaders headers = new HttpHeaders();
HttpHeaders headers = setHeader();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<EcoNewsForSendEmailDto> entity = new HttpEntity<>(message, headers);
restTemplate.exchange(greenCityUserServerAddress
Expand Down Expand Up @@ -421,8 +446,8 @@ public void sendNewEventComment(EventCommentForSendEmailDto message) {
* @author Taras Kavkalo
*/
public void sendReport(SendReportEmailMessage reportEmailMessage) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
HttpHeaders headers = setHeader();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<SendReportEmailMessage> entity = new HttpEntity<>(reportEmailMessage, headers);
restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.SEND_REPORT, HttpMethod.POST, entity, Object.class)
Expand Down Expand Up @@ -450,8 +475,10 @@ public void scheduleDeleteDeactivatedUsers() {
* @author Taras Kavkalo
*/
public void changePlaceStatus(SendChangePlaceStatusEmailMessage changePlaceStatusEmailMessage) {
HttpHeaders headers = setHeader();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<SendChangePlaceStatusEmailMessage> entity =
new HttpEntity<>(changePlaceStatusEmailMessage, new HttpHeaders());
new HttpEntity<>(changePlaceStatusEmailMessage, headers);
restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.CHANGE_PLACE_STATUS, HttpMethod.POST, entity, Object.class)
.getBody();
Expand All @@ -465,7 +492,9 @@ public void changePlaceStatus(SendChangePlaceStatusEmailMessage changePlaceStatu
* @author Taras Kavkalo
*/
public void sendHabitNotification(SendHabitNotification sendHabitNotification) {
HttpEntity<SendHabitNotification> entity = new HttpEntity<>(sendHabitNotification, new HttpHeaders());
HttpHeaders headers = setHeader();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<SendHabitNotification> entity = new HttpEntity<>(sendHabitNotification, headers);
restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.SEND_HABIT_NOTIFICATION, HttpMethod.POST, entity, Object.class)
.getBody();
Expand Down Expand Up @@ -534,12 +563,22 @@ public PageableAdvancedDto<UserManagementVO> search(Pageable pageable, UserManag
* @return {@link HttpEntity}
*/
private HttpHeaders setHeader() {
String accessToken = httpServletRequest.getHeader(AUTHORIZATION);
String accessToken = null;
Cookie[] cookies = httpServletRequest.getCookies();
String uri = httpServletRequest.getRequestURI();

if (cookies != null && uri.startsWith("/management")) {
accessToken = getTokenFromCookies(cookies);
}

if (StringUtils.isEmpty(accessToken)) {
accessToken = httpServletRequest.getHeader(AUTHORIZATION);
}

if (StringUtils.isEmpty(accessToken)) {
accessToken = "Bearer " + jwtTool.createAccessToken(systemEmail, Role.ROLE_ADMIN);
}

HttpHeaders headers = new HttpHeaders();
headers.set(AUTHORIZATION, accessToken);
return headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public class EventCommentForSendEmailDto {
private EventAuthorDto organizer;

private String email;

private Long eventId;
}
11 changes: 11 additions & 0 deletions service-api/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import greencity.message.SendReportEmailMessage;
import greencity.message.SendHabitNotification;
import greencity.message.AddEcoNewsMessage;
import greencity.message.SendReportEmailMessage;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -78,6 +81,13 @@ public static SendHabitNotification getSendHabitNotification() {
.build();
}

public static HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer accessToken");
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}

public static NewsSubscriberResponseDto getNewsSubscriberResponseDto() {
return NewsSubscriberResponseDto.builder()
.email("[email protected]")
Expand Down Expand Up @@ -134,6 +144,7 @@ public static EventCommentForSendEmailDto getEventCommentForSendEmailDto() {
.createdDate(LocalDateTime.now())
.author(ModelUtils.getEventCommentAuthorDto())
.text("text")
.eventId(1L)
.build();
}

Expand Down
Loading