Search in sources :

Example 1 with RssFeedProcessor

use of org.apache.archiva.rss.processor.RssFeedProcessor 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);
    }
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) AccountLockedException(org.apache.archiva.redback.policy.AccountLockedException) HashMap(java.util.HashMap) AuthenticationException(org.apache.archiva.redback.authentication.AuthenticationException) FeedException(com.sun.syndication.io.FeedException) SyndFeedOutput(com.sun.syndication.io.SyndFeedOutput) RepositorySession(org.apache.archiva.metadata.repository.RepositorySession) MustChangePasswordException(org.apache.archiva.redback.policy.MustChangePasswordException) SyndFeed(com.sun.syndication.feed.synd.SyndFeed) RssFeedProcessor(org.apache.archiva.rss.processor.RssFeedProcessor) UnauthorizedException(org.apache.archiva.redback.authorization.UnauthorizedException)

Aggregations

SyndFeed (com.sun.syndication.feed.synd.SyndFeed)1 FeedException (com.sun.syndication.io.FeedException)1 SyndFeedOutput (com.sun.syndication.io.SyndFeedOutput)1 HashMap (java.util.HashMap)1 RepositorySession (org.apache.archiva.metadata.repository.RepositorySession)1 AuthenticationException (org.apache.archiva.redback.authentication.AuthenticationException)1 UnauthorizedException (org.apache.archiva.redback.authorization.UnauthorizedException)1 AccountLockedException (org.apache.archiva.redback.policy.AccountLockedException)1 MustChangePasswordException (org.apache.archiva.redback.policy.MustChangePasswordException)1 UserNotFoundException (org.apache.archiva.redback.users.UserNotFoundException)1 RssFeedProcessor (org.apache.archiva.rss.processor.RssFeedProcessor)1