Skip to content

Commit

Permalink
Implement TimedDownloaderService.
Browse files Browse the repository at this point in the history
Fix #665

No functional changes.
  • Loading branch information
php-coder committed Jan 2, 2018
1 parent a15ea13 commit 829173f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/main/java/ru/mystamps/web/config/ServicesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,21 @@ public CronService getCronService() {

@Bean
public DownloaderService getImageDownloaderService() {
return new HttpURLConnectionDownloaderService(new String[]{"image/jpeg", "image/png"});
return new TimedDownloaderService(
LoggerFactory.getLogger(TimedDownloaderService.class),
new HttpURLConnectionDownloaderService(
new String[]{"image/jpeg", "image/png"}
)
);
}

@Bean
public DownloaderService getSeriesDownloaderService() {
return new HttpURLConnectionDownloaderService(
new String[]{"text/html", "image/jpeg", "image/png"}
return new TimedDownloaderService(
LoggerFactory.getLogger(TimedDownloaderService.class),
new HttpURLConnectionDownloaderService(
new String[]{"text/html", "image/jpeg", "image/png"}
)
);
}

Expand Down
61 changes: 61 additions & 0 deletions src/main/java/ru/mystamps/web/service/TimedDownloaderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2009-2018 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 org.apache.commons.lang3.time.StopWatch;

import org.slf4j.Logger;

import lombok.RequiredArgsConstructor;

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

@RequiredArgsConstructor
public class TimedDownloaderService implements DownloaderService {

private final Logger log;
private final DownloaderService service;

// @todo #665 TimedDownloaderService: add unit tests
@Override
public DownloadResult download(String url) {
// Why we don't use Spring's StopWatch?
// 1) because its javadoc says that it's not intended for production
// 2) because we don't want to have strong dependencies on the Spring Framework
StopWatch timer = new StopWatch();

// start() and stop() may throw IllegalStateException and in this case it's possible
// that we won't generate anything or lose already generated result. I don't want to
// make method body too complicated by adding many try/catches and I believe that such
// exception will never happen because it would mean that we're using API in a wrong way.
timer.start();
DownloadResult result = service.download(url);
timer.stop();

if (result.hasSucceeded()) {
log.debug(
"{} bytes have been downloaded in {} msecs",
result.getData().length,
timer.getTime()
);
}

return result;
}

}
4 changes: 4 additions & 0 deletions src/main/java/ru/mystamps/web/service/dto/DownloadResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public boolean hasFailed() {
return code != Code.SUCCESS;
}

public boolean hasSucceeded() {
return code == Code.SUCCESS;
}

public String getDataAsString() {
return new String(data, StandardCharsets.UTF_8);
}
Expand Down

1 comment on commit 829173f

@0pdd
Copy link

@0pdd 0pdd commented on 829173f Jan 2, 2018

Choose a reason for hiding this comment

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

Puzzle 665-32370c4c discovered in src/main/java/ru/mystamps/web/service/TimedDownloaderService.java and submitted as #800. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but
we discovered it only now.

Please sign in to comment.