use of com.sun.syndication.feed.synd.SyndFeed in project archiva by apache.
the class RssFeedServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String repoId = null;
String groupId = null;
String artifactId = null;
String url = StringUtils.removeEnd(req.getRequestURL().toString(), "/");
if (StringUtils.countMatches(StringUtils.substringAfter(url, "feeds/"), "/") > 0) {
artifactId = StringUtils.substringAfterLast(url, "/");
groupId = StringUtils.substringBeforeLast(StringUtils.substringAfter(url, "feeds/"), "/");
groupId = StringUtils.replaceChars(groupId, '/', '.');
} else if (StringUtils.countMatches(StringUtils.substringAfter(url, "feeds/"), "/") == 0) {
// we receive feeds?babla=ded which is not correct
if (StringUtils.countMatches(url, "feeds?") > 0) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request url.");
return;
}
repoId = StringUtils.substringAfterLast(url, "/");
} else {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request url.");
return;
}
RssFeedProcessor processor = null;
try {
Map<String, String> map = new HashMap<>();
SyndFeed feed = null;
if (isAllowed(req, repoId, groupId, artifactId)) {
if (repoId != null) {
// new artifacts in repo feed request
processor = newArtifactsprocessor;
map.put(RssFeedProcessor.KEY_REPO_ID, repoId);
} else if ((groupId != null) && (artifactId != null)) {
// TODO: this only works for guest - we could pass in the list of repos
// new versions of artifact feed request
processor = newVersionsprocessor;
map.put(RssFeedProcessor.KEY_GROUP_ID, groupId);
map.put(RssFeedProcessor.KEY_ARTIFACT_ID, artifactId);
}
} else {
res.sendError(HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED);
return;
}
RepositorySession repositorySession = repositorySessionFactory.createSession();
try {
feed = processor.process(map, repositorySession.getRepository());
} finally {
repositorySession.close();
}
if (feed == null) {
res.sendError(HttpServletResponse.SC_NO_CONTENT, "No information available.");
return;
}
res.setContentType(MIME_TYPE);
if (repoId != null) {
feed.setLink(req.getRequestURL().toString());
} else if ((groupId != null) && (artifactId != null)) {
feed.setLink(req.getRequestURL().toString());
}
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, res.getWriter());
} catch (UserNotFoundException unfe) {
log.debug(COULD_NOT_AUTHENTICATE_USER, unfe);
res.sendError(HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER);
} catch (AccountLockedException acce) {
res.sendError(HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER);
} catch (AuthenticationException authe) {
log.debug(COULD_NOT_AUTHENTICATE_USER, authe);
res.sendError(HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER);
} catch (FeedException ex) {
log.debug(COULD_NOT_GENERATE_FEED_ERROR, ex);
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR);
} catch (MustChangePasswordException e) {
res.sendError(HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER);
} catch (UnauthorizedException e) {
log.debug(e.getMessage());
if (repoId != null) {
res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository");
} else {
res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId);
}
res.sendError(HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED);
}
}
use of com.sun.syndication.feed.synd.SyndFeed in project archiva by apache.
the class NewVersionsOfArtifactRssFeedProcessorTest method testProcess.
@SuppressWarnings("unchecked")
@Test
public void testProcess() throws Exception {
Date whenGathered = new Date(123456789);
ArtifactMetadata artifact1 = createArtifact(whenGathered, "1.0.1");
ArtifactMetadata artifact2 = createArtifact(whenGathered, "1.0.2");
Date whenGatheredNext = new Date(345678912);
ArtifactMetadata artifact3 = createArtifact(whenGatheredNext, "1.0.3-SNAPSHOT");
Map<String, String> reqParams = new HashMap<>();
reqParams.put(RssFeedProcessor.KEY_GROUP_ID, GROUP_ID);
reqParams.put(RssFeedProcessor.KEY_ARTIFACT_ID, ARTIFACT_ID);
expect(metadataRepository.getRepositories()).andReturn(Collections.singletonList(TEST_REPO));
expect(metadataRepository.getProjectVersions(TEST_REPO, GROUP_ID, ARTIFACT_ID)).andReturn(Arrays.asList("1.0.1", "1.0.2", "1.0.3-SNAPSHOT"));
expect(metadataRepository.getArtifacts(TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.1")).andReturn(Collections.singletonList(artifact1));
expect(metadataRepository.getArtifacts(TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.2")).andReturn(Collections.singletonList(artifact2));
expect(metadataRepository.getArtifacts(TEST_REPO, GROUP_ID, ARTIFACT_ID, "1.0.3-SNAPSHOT")).andReturn(Collections.singletonList(artifact3));
metadataRepositoryControl.replay();
SyndFeed feed = newVersionsProcessor.process(reqParams, metadataRepository);
assertEquals("New Versions of Artifact 'org.apache.archiva:artifact-two'", feed.getTitle());
assertEquals("New versions of artifact 'org.apache.archiva:artifact-two' found during repository scan.", feed.getDescription());
assertEquals("en-us", feed.getLanguage());
assertEquals(whenGatheredNext, feed.getPublishedDate());
List<SyndEntry> entries = feed.getEntries();
assertEquals(2, entries.size());
assertEquals("New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGathered, entries.get(0).getTitle());
assertEquals(whenGathered, entries.get(0).getPublishedDate());
assertEquals("New Versions of Artifact 'org.apache.archiva:artifact-two' as of " + whenGatheredNext, entries.get(1).getTitle());
assertEquals(whenGatheredNext, entries.get(1).getPublishedDate());
metadataRepositoryControl.verify();
}
use of com.sun.syndication.feed.synd.SyndFeed in project archiva by apache.
the class RssFeedGeneratorTest method testNewFeed.
@SuppressWarnings("unchecked")
@Test
public void testNewFeed() throws Exception {
List<RssFeedEntry> entries = new ArrayList<>();
RssFeedEntry entry = new RssFeedEntry("Item 1");
Date whenGathered = new Date(System.currentTimeMillis());
entry.setDescription("RSS 2.0 feed item 1.");
entry.setPublishedDate(whenGathered);
entries.add(entry);
entry = new RssFeedEntry("Item 2");
entry.setDescription("RSS 2.0 feed item 2.");
entry.setPublishedDate(whenGathered);
entries.add(entry);
entry = new RssFeedEntry("Item 3");
entry.setDescription("RSS 2.0 feed item 3.");
entry.setPublishedDate(whenGathered);
entries.add(entry);
SyndFeed feed = generator.generateFeed("Test Feed", "The test feed from Archiva.", entries);
assertEquals("Test Feed", feed.getTitle());
assertEquals("The test feed from Archiva.", feed.getDescription());
assertEquals("en-us", feed.getLanguage());
assertEquals(entries.get(2).getPublishedDate(), feed.getPublishedDate());
List<SyndEntry> syndEntries = feed.getEntries();
assertEquals(3, syndEntries.size());
assertEquals("Item 1", syndEntries.get(0).getTitle());
assertEquals("Item 2", syndEntries.get(1).getTitle());
assertEquals("Item 3", syndEntries.get(2).getTitle());
}
use of com.sun.syndication.feed.synd.SyndFeed in project archiva by apache.
the class RssFeedGeneratorTest method testNoDataEntries.
@Test
public void testNoDataEntries() throws Exception {
List<RssFeedEntry> entries = new ArrayList<>();
SyndFeed feed = generator.generateFeed("Test Feed", "The test feed from Archiva.", entries);
assertNull(feed);
}
use of com.sun.syndication.feed.synd.SyndFeed in project ofbiz-framework by apache.
the class BlogRssServices method generateBlogRssFeed.
public static Map<String, Object> generateBlogRssFeed(DispatchContext dctx, Map<String, ? extends Object> context) {
GenericValue userLogin = (GenericValue) context.get("userLogin");
String contentId = (String) context.get("blogContentId");
String entryLink = (String) context.get("entryLink");
String feedType = (String) context.get("feedType");
Locale locale = (Locale) context.get("locale");
// create the main link
String mainLink = (String) context.get("mainLink");
mainLink = mainLink + "?blogContentId=" + contentId;
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
// get the main blog content
GenericValue content = null;
try {
content = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
if (content == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentCannotGenerateBlogRssFeed", UtilMisc.toMap("contentId", contentId), locale));
}
// create the feed
SyndFeed feed = new SyndFeedImpl();
feed.setFeedType(feedType);
feed.setLink(mainLink);
feed.setTitle(content.getString("contentName"));
feed.setDescription(content.getString("description"));
feed.setEntries(generateEntryList(dispatcher, delegator, contentId, entryLink, locale, userLogin));
Map<String, Object> resp = ServiceUtil.returnSuccess();
resp.put("wireFeed", feed.createWireFeed());
return resp;
}
Aggregations