Skip to content

#620_refactoring_method #624

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -91,7 +91,7 @@ public RobotsTxtController getRobotsTxtController() {
@Bean
public ReportController getReportController() {
return new ReportController(
servicesConfig.getMailService(),
servicesConfig.getReportService(),
servicesConfig.getCronService()
);
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/ru/mystamps/web/config/ServicesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,29 @@ public MailService getMailService() {
boolean enableTestMode = !isProductionEnvironment;

return new MailServiceImpl(
getReportService(),
mailSender,
messageSource,
env.getProperty("app.mail.admin.email", "root@localhost"),
new Locale(env.getProperty("app.mail.admin.lang", "en")),
env.getRequiredProperty("app.mail.robot.email"),
enableTestMode);
enableTestMode
);
}

@Bean
public UsersActivationService getUsersActivationService() {
return new UsersActivationServiceImpl(daoConfig.getUsersActivationDao(), getMailService());
}

@Bean
public ReportService getReportService() {
return new ReportServiceImpl(
messageSource,
new Locale(env.getProperty("app.mail.admin.lang", "en"))
);
}

@Bean
public SeriesService getSeriesService() {
return new SeriesServiceImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import ru.mystamps.web.Url;
import ru.mystamps.web.service.CronService;
import ru.mystamps.web.service.MailService;
import ru.mystamps.web.service.ReportService;

/**
* @author Maxim Shestakov
Expand All @@ -34,13 +34,13 @@
@RequiredArgsConstructor
public class ReportController {

private final MailService mailService;
private final ReportService reportService;
private final CronService cronService;

@GetMapping(path = Url.DAILY_STATISTICS, produces = "text/plain; charset=UTF-8")
@ResponseBody
public String showDailyReport() {
return mailService.prepareDailyStatistics(
return reportService.prepareDailyStatistics(
cronService.getDailyReport()
);
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/ru/mystamps/web/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@
public interface MailService {
void sendActivationKeyToUser(SendUsersActivationDto activation);
void sendDailyStatisticsToAdmin(AdminDailyReport report);
String prepareDailyStatistics(AdminDailyReport report);
}
38 changes: 4 additions & 34 deletions src/main/java/ru/mystamps/web/service/MailServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,10 @@
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.annotation.Async;

import org.springframework.security.access.prepost.PreAuthorize;

import ru.mystamps.web.Url;
import ru.mystamps.web.service.dto.AdminDailyReport;
import ru.mystamps.web.service.dto.SendUsersActivationDto;
import ru.mystamps.web.service.exception.EmailSendingException;
import ru.mystamps.web.support.spring.security.HasAuthority;

public class MailServiceImpl implements MailService {
private static final Logger LOG = LoggerFactory.getLogger(MailServiceImpl.class);
Expand All @@ -57,15 +54,18 @@ public class MailServiceImpl implements MailService {
private final String robotEmail;
private final boolean testMode;
private final DatePrinter shortDatePrinter;
private final ReportService reportService;

public MailServiceImpl(
ReportService reportService,
JavaMailSender mailer,
MessageSource messageSource,
String adminEmail,
Locale adminLang,
String robotEmail,
boolean testMode) {

this.reportService = reportService;
this.mailer = mailer;
this.messageSource = messageSource;
this.adminEmail = adminEmail;
Expand Down Expand Up @@ -104,7 +104,7 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
sendMail(
adminEmail,
getSubjectOfDailyStatisticsMail(report),
prepareDailyStatistics(report),
reportService.prepareDailyStatistics(report),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that it won't fail because of the @PreAuthorize? Have you test it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it doesn't work:

2017-08-03 22:13:56.007 ERROR 19147 --- [cTaskExecutor-4] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected error occurred invoking async method 'public void ru.mystamps.web.service.MailServiceImpl.sendDailyStatisticsToAdmin(ru.mystamps.web.service.dto.AdminDailyReport)'.

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

Copy link
Contributor Author

@bodom91 bodom91 Aug 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do i can run these test ?
I tried run "mvn test" , but i get no error.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no test for that. You can modify CronServiceImpl:

-private static final String EVERY_DAY_AT_00_00 = "0 0 0 * * *";
+private static final String EVERY_DAY_AT_00_00 = "* * * * * *";

this will run sendDailyStatistics() every second. Now start the server and look at the console.

(Note that when you fix this error it will still fail because there is no SMTP server configured.)

"daily_statistics"
);

Expand All @@ -118,36 +118,6 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
);
}

@Override
@PreAuthorize(HasAuthority.VIEW_DAILY_STATS)
public String prepareDailyStatistics(AdminDailyReport report) {
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
String fromDate = shortDatePrinter.format(report.getStartDate());
String tillDate = shortDatePrinter.format(report.getEndDate());

Map<String, String> ctx = new HashMap<>();
ctx.put("from_date", fromDate);
ctx.put("to_date", tillDate);

put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
put(ctx, "events_cnt", report.countEvents());
put(ctx, "not_found_cnt", report.getNotFoundCounter());
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
put(ctx, "bad_request_cnt", -1L); // TODO: #122

return new StrSubstitutor(ctx).replace(template);
}

@SuppressWarnings("PMD.UseObjectForClearerAPI")
private void sendMail(
final String email,
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/ru/mystamps/web/service/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2009-2017 Slava Semushin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.service;

import ru.mystamps.web.service.dto.AdminDailyReport;

/**
*@author Maxim Shestakov
*/
public interface ReportService {
String prepareDailyStatistics(AdminDailyReport report);
}
85 changes: 85 additions & 0 deletions src/main/java/ru/mystamps/web/service/ReportServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2009-2017 Slava Semushin <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package ru.mystamps.web.service;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.text.StrSubstitutor;
import org.apache.commons.lang3.time.DatePrinter;
import org.apache.commons.lang3.time.FastDateFormat;

import org.springframework.context.MessageSource;

import ru.mystamps.web.service.dto.AdminDailyReport;

/**
* @author Maxim Shestakov
*/
public class ReportServiceImpl implements ReportService {

private final MessageSource messageSource;
private final DatePrinter shortDatePrinter;
private final Locale adminLang;

public ReportServiceImpl(
MessageSource messageSource,
Locale adminLang) {

this.messageSource = messageSource;
this.adminLang = adminLang;

this.shortDatePrinter = FastDateFormat.getInstance("dd.MM.yyyy", adminLang);
}

// This method should have @PreAuthorize(VIEW_DAILY_STATS) but we can't put it here because it
// breaks MailServiceImpl.sendDailyStatisticsToAdmin() method that is being executed by cron.
@Override
public String prepareDailyStatistics(AdminDailyReport report) {
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
String fromDate = shortDatePrinter.format(report.getStartDate());
String tillDate = shortDatePrinter.format(report.getEndDate());

Map<String, String> ctx = new HashMap<>();
ctx.put("from_date", fromDate);
ctx.put("to_date", tillDate);

put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
put(ctx, "events_cnt", report.countEvents());
put(ctx, "not_found_cnt", report.getNotFoundCounter());
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
put(ctx, "bad_request_cnt", -1L); // TODO: #122

return new StrSubstitutor(ctx).replace(template);
}

private static void put(Map<String, String> ctx, String key, long value) {
ctx.put(key, String.valueOf(value));
}
}