1 package org.apache.maven.archiva.web.rss;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import java.io.IOException;
23 import java.util.HashMap;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
31 import org.apache.archiva.rss.RssFeedGenerator;
32 import org.apache.archiva.rss.processor.RssFeedProcessor;
33 import org.codehaus.plexus.spring.PlexusToSpringUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.web.context.WebApplicationContext;
37 import org.springframework.web.context.support.WebApplicationContextUtils;
39 import com.sun.syndication.feed.synd.SyndFeed;
40 import com.sun.syndication.io.FeedException;
41 import com.sun.syndication.io.SyndFeedOutput;
44 * Servlet for handling rss feed requests.
46 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
49 public class RssFeedServlet
52 public static final String MIME_TYPE = "application/xml; charset=UTF-8";
54 private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
56 private Logger log = LoggerFactory.getLogger( RssFeedGenerator.class );
58 private RssFeedProcessor processor;
60 private WebApplicationContext wac;
62 public void init( javax.servlet.ServletConfig servletConfig )
63 throws ServletException
65 super.init( servletConfig );
66 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
69 public void doGet( HttpServletRequest req, HttpServletResponse res )
70 throws ServletException, IOException
72 log.info( "Request URL: " + req.getRequestURL() );
75 Map<String, String> map = new HashMap<String, String>();
78 if ( req.getParameter( "repoId" ) != null )
82 // new artifacts in repo feed request
84 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
85 RssFeedProcessor.class.getName(),
87 map.put( RssFeedProcessor.KEY_REPO_ID, req.getParameter( "repoId" ) );
91 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Request is not authorized." );
95 else if ( ( req.getParameter( "groupId" ) != null ) && ( req.getParameter( "artifactId" ) != null ) )
99 // new versions of artifact feed request
101 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
102 RssFeedProcessor.class.getName(),
104 map.put( RssFeedProcessor.KEY_GROUP_ID, req.getParameter( "groupId" ) );
105 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, req.getParameter( "artifactId" ) );
109 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Request is not authorized." );
115 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Required fields not found in request." );
119 feed = processor.process( map );
120 res.setContentType( MIME_TYPE );
122 SyndFeedOutput output = new SyndFeedOutput();
123 output.output( feed, res.getWriter() );
125 catch ( FeedException ex )
127 String msg = COULD_NOT_GENERATE_FEED_ERROR;
128 log.error( msg, ex );
129 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg );
133 private boolean isAuthorized()