Skip to content
Permalink
Browse files

Add #4 - Atom-Feed

  • Loading branch information
Steven Schwenke Steven Schwenke
Steven Schwenke authored and Steven Schwenke committed Aug 5, 2019
1 parent 6e60aa7 commit b9ff846d99849af62b2c422ee4321bda62e454de
@@ -34,6 +34,7 @@ dependencies {
implementation('io.jsonwebtoken:jjwt-jackson:0.10.5')
implementation('org.apache.commons:commons-lang3')
implementation('org.postgresql:postgresql:42.2.4')
implementation('com.rometools:rome:1.12.1')

implementation('org.springframework.cloud:spring-cloud-spring-service-connector')
implementation('org.springframework.cloud:spring-cloud-cloudfoundry-connector')
@@ -0,0 +1,24 @@
package de.stevenschwenke.java.ithubbs.ithubbsbackend.atom;

import com.rometools.rome.feed.atom.Feed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/atom")
public class AtomController {

private final AtomService atomService;

@Autowired
public AtomController(AtomService atomService) {
this.atomService = atomService;
}

@GetMapping(path = "")
public Feed atom() {
return atomService.createFeed();
}
}
@@ -0,0 +1,65 @@
package de.stevenschwenke.java.ithubbs.ithubbsbackend.atom;

import com.rometools.rome.feed.atom.Content;
import com.rometools.rome.feed.atom.Entry;
import com.rometools.rome.feed.atom.Feed;
import com.rometools.rome.feed.atom.Link;
import de.stevenschwenke.java.ithubbs.ithubbsbackend.event.Event;
import de.stevenschwenke.java.ithubbs.ithubbsbackend.event.EventRepository;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

@Service
public class AtomService {

private EventRepository eventRepository;

public AtomService(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}

Feed createFeed() {
Feed feed = new Feed();
feed.setFeedType("atom_1.0");
feed.setTitle("IT Hub Braunschweig Veranstaltungen");
feed.setId("https://www.ithubbs.de/current");

Content subtitle = new Content();
subtitle.setType("text/plain");
subtitle.setValue("Aktuelle IT-Veranstaltungen in Braunschweig");
feed.setSubtitle(subtitle);

feed.setUpdated(new Date());

Stream<Event> eventStream = StreamSupport.stream(eventRepository.findAll().spliterator(), false);
List<Entry> entries = eventStream.map(this::createEntry).collect(Collectors.toList());

feed.setEntries(entries);
return feed;
}

private Entry createEntry(Event event) {

Entry entry = new Entry();

Link link = new Link();
link.setHref(event.getUrl());
entry.setAlternateLinks(Collections.singletonList(link));
entry.setId(event.getUrl());
entry.setTitle(event.getDatetime().format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)) + ": " + event.getName());

Content summary = new Content();
summary.setType("text/plain");
summary.setValue(event.getDatetime() + ": " + event.getUrl());
entry.setSummary(summary);
return entry;
}
}

0 comments on commit b9ff846

Please sign in to comment.
You can’t perform that action at this time.