]> source.dussan.org Git - archiva.git/blob
4cca3aa94c487babad30b933e05f89d0637dd25a
[archiva.git] /
1 package org.apache.maven.archiva.web.rss;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
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;
38
39 import com.sun.syndication.feed.synd.SyndFeed;
40 import com.sun.syndication.io.FeedException;
41 import com.sun.syndication.io.SyndFeedOutput;
42
43 /**
44  * Servlet for handling rss feed requests.
45  * 
46  * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
47  * @version
48  */
49 public class RssFeedServlet
50     extends HttpServlet
51 {
52     public static final String MIME_TYPE = "application/xml; charset=UTF-8";
53
54     private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
55
56     private Logger log = LoggerFactory.getLogger( RssFeedGenerator.class );
57
58     private RssFeedProcessor processor;
59
60     private WebApplicationContext wac;
61
62     public void init( javax.servlet.ServletConfig servletConfig )
63         throws ServletException
64     {
65         super.init( servletConfig );
66         wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
67     }
68
69     public void doGet( HttpServletRequest req, HttpServletResponse res )
70         throws ServletException, IOException
71     {
72         log.info( "Request URL: " + req.getRequestURL() );
73         try
74         {
75             Map<String, String> map = new HashMap<String, String>();
76             SyndFeed feed = null;
77             
78             if ( req.getParameter( "repoId" ) != null )
79             {
80                 if ( isAuthorized() )
81                 {
82                  // new artifacts in repo feed request
83                     processor =
84                         (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
85                                                                                            RssFeedProcessor.class.getName(),
86                                                                                            "new-artifacts" ) );
87                     map.put( RssFeedProcessor.KEY_REPO_ID, req.getParameter( "repoId" ) );
88                 }
89                 else
90                 {
91                     res.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Request is not authorized." );
92                     return;
93                 }
94             }
95             else if ( ( req.getParameter( "groupId" ) != null ) && ( req.getParameter( "artifactId" ) != null ) )
96             {
97                 if ( isAuthorized() )
98                 {
99                  // new versions of artifact feed request
100                     processor =
101                         (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
102                                                                                            RssFeedProcessor.class.getName(),
103                                                                                            "new-versions" ) );
104                     map.put( RssFeedProcessor.KEY_GROUP_ID, req.getParameter( "groupId" ) );
105                     map.put( RssFeedProcessor.KEY_ARTIFACT_ID, req.getParameter( "artifactId" ) );
106                 }
107                 else
108                 {
109                     res.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Request is not authorized." );
110                     return;
111                 }
112             }
113             else
114             {
115                 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Required fields not found in request." );
116                 return;
117             }
118
119             feed = processor.process( map );
120             res.setContentType( MIME_TYPE );
121
122             SyndFeedOutput output = new SyndFeedOutput();
123             output.output( feed, res.getWriter() );
124         }
125         catch ( FeedException ex )
126         {
127             String msg = COULD_NOT_GENERATE_FEED_ERROR;
128             log.error( msg, ex );
129             res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg );
130         }
131     }
132
133     private boolean isAuthorized()
134     {
135         return true;
136     }
137 }