Skip to content

Commit 829173f

Browse files
committed
Implement TimedDownloaderService.
Fix #665 No functional changes.
1 parent a15ea13 commit 829173f

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

src/main/java/ru/mystamps/web/config/ServicesConfig.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,21 @@ public CronService getCronService() {
9393

9494
@Bean
9595
public DownloaderService getImageDownloaderService() {
96-
return new HttpURLConnectionDownloaderService(new String[]{"image/jpeg", "image/png"});
96+
return new TimedDownloaderService(
97+
LoggerFactory.getLogger(TimedDownloaderService.class),
98+
new HttpURLConnectionDownloaderService(
99+
new String[]{"image/jpeg", "image/png"}
100+
)
101+
);
97102
}
98103

99104
@Bean
100105
public DownloaderService getSeriesDownloaderService() {
101-
return new HttpURLConnectionDownloaderService(
102-
new String[]{"text/html", "image/jpeg", "image/png"}
106+
return new TimedDownloaderService(
107+
LoggerFactory.getLogger(TimedDownloaderService.class),
108+
new HttpURLConnectionDownloaderService(
109+
new String[]{"text/html", "image/jpeg", "image/png"}
110+
)
103111
);
104112
}
105113

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2009-2018 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service;
19+
20+
import org.apache.commons.lang3.time.StopWatch;
21+
22+
import org.slf4j.Logger;
23+
24+
import lombok.RequiredArgsConstructor;
25+
26+
import ru.mystamps.web.service.dto.DownloadResult;
27+
28+
@RequiredArgsConstructor
29+
public class TimedDownloaderService implements DownloaderService {
30+
31+
private final Logger log;
32+
private final DownloaderService service;
33+
34+
// @todo #665 TimedDownloaderService: add unit tests
35+
@Override
36+
public DownloadResult download(String url) {
37+
// Why we don't use Spring's StopWatch?
38+
// 1) because its javadoc says that it's not intended for production
39+
// 2) because we don't want to have strong dependencies on the Spring Framework
40+
StopWatch timer = new StopWatch();
41+
42+
// start() and stop() may throw IllegalStateException and in this case it's possible
43+
// that we won't generate anything or lose already generated result. I don't want to
44+
// make method body too complicated by adding many try/catches and I believe that such
45+
// exception will never happen because it would mean that we're using API in a wrong way.
46+
timer.start();
47+
DownloadResult result = service.download(url);
48+
timer.stop();
49+
50+
if (result.hasSucceeded()) {
51+
log.debug(
52+
"{} bytes have been downloaded in {} msecs",
53+
result.getData().length,
54+
timer.getTime()
55+
);
56+
}
57+
58+
return result;
59+
}
60+
61+
}

src/main/java/ru/mystamps/web/service/dto/DownloadResult.java

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public boolean hasFailed() {
4545
return code != Code.SUCCESS;
4646
}
4747

48+
public boolean hasSucceeded() {
49+
return code == Code.SUCCESS;
50+
}
51+
4852
public String getDataAsString() {
4953
return new String(data, StandardCharsets.UTF_8);
5054
}

0 commit comments

Comments
 (0)