}
buf.append(ch);
}
final String displayUrl = buf.toString();
log.info("Getting subscribed calendar #" + teamCalDo.getId() + " from: " + displayUrl);
final CalendarBuilder builder = new CalendarBuilder();
byte[] bytes = null;
try {
// Create a method instance.
final GetMethod method = new GetMethod(url);
final int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
log.error("Unable to gather subscription calendar #"
+ teamCalDo.getId()
+ " information, using database from url '"
+ displayUrl
+ "'. Received statusCode: "
+ statusCode);
return;
}
final MessageDigest md = MessageDigest.getInstance("MD5");
// Read the response body.
final InputStream stream = method.getResponseBodyAsStream();
bytes = IOUtils.toByteArray(stream);
final String md5 = calcHexHash(md.digest(bytes));
if (StringUtils.equals(md5, teamCalDo.getExternalSubscriptionHash()) == false) {
teamCalDo.setExternalSubscriptionHash(md5);
teamCalDo.setExternalSubscriptionCalendarBinary(bytes);
// internalUpdate is valid at this point, because we are calling this method in an async thread
teamCalDao.internalUpdate(teamCalDo);
}
} catch (final Exception e) {
bytes = teamCalDo.getExternalSubscriptionCalendarBinary();
log.error("Unable to gather subscription calendar #"
+ teamCalDo.getId()
+ " information, using database from url '"
+ displayUrl
+ "': "
+ e.getMessage(), e);
}
if (bytes == null) {
log.error("Unable to use database subscription calendar #" + teamCalDo.getId() + " information, quit from url '" + displayUrl + "'.");
return;
}
if (currentInitializedHash != null && StringUtils.equals(currentInitializedHash, teamCalDo.getExternalSubscriptionHash()) == true) {
// nothing to do here if the hashes are equal
log.info("No modification of subscribed calendar #" + teamCalDo.getId() + " found from: " + displayUrl + " (OK, nothing to be done).");
return;
}
try {
final Date timeInPast = new Date(System.currentTimeMillis() - TIME_IN_THE_PAST);
final Calendar calendar = builder.build(new ByteArrayInputStream(bytes));
@SuppressWarnings("unchecked")
final List<Component> list = calendar.getComponents(Component.VEVENT);
final List<VEvent> vEvents = new ArrayList<VEvent>();
for (final Component c : list) {
final VEvent event = (VEvent) c;