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.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServlet;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.apache.archiva.rss.processor.RssFeedProcessor;
35 import org.apache.commons.codec.Decoder;
36 import org.apache.commons.codec.DecoderException;
37 import org.apache.commons.codec.binary.Base64;
38 import org.apache.maven.archiva.database.ArchivaDatabaseException;
39 import org.apache.maven.archiva.security.AccessDeniedException;
40 import org.apache.maven.archiva.security.ArchivaRoleConstants;
41 import org.apache.maven.archiva.security.ArchivaSecurityException;
42 import org.apache.maven.archiva.security.PrincipalNotFoundException;
43 import org.apache.maven.archiva.security.ServletAuthenticator;
44 import org.apache.maven.archiva.security.UserRepositories;
45 import org.codehaus.plexus.redback.authentication.AuthenticationException;
46 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
47 import org.codehaus.plexus.redback.authorization.AuthorizationException;
48 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
49 import org.codehaus.plexus.redback.policy.AccountLockedException;
50 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
51 import org.codehaus.plexus.redback.system.SecuritySession;
52 import org.codehaus.plexus.redback.users.UserNotFoundException;
53 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
54 import org.codehaus.plexus.spring.PlexusToSpringUtils;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.web.context.WebApplicationContext;
58 import org.springframework.web.context.support.WebApplicationContextUtils;
60 import com.sun.syndication.feed.synd.SyndFeed;
61 import com.sun.syndication.io.FeedException;
62 import com.sun.syndication.io.SyndFeedOutput;
65 * Servlet for handling rss feed requests.
67 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
70 public class RssFeedServlet
73 public static final String MIME_TYPE = "application/rss+xml; charset=UTF-8";
75 private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
77 private static final String COULD_NOT_AUTHENTICATE_USER = "Could not authenticate user";
79 private static final String USER_NOT_AUTHORIZED = "User not authorized to access feed.";
81 private Logger log = LoggerFactory.getLogger( RssFeedServlet.class );
83 private RssFeedProcessor processor;
85 private WebApplicationContext wac;
87 private UserRepositories userRepositories;
89 private ServletAuthenticator servletAuth;
91 private HttpAuthenticator httpAuth;
93 public void init( javax.servlet.ServletConfig servletConfig )
94 throws ServletException
96 super.init( servletConfig );
97 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
99 (UserRepositories) wac.getBean( PlexusToSpringUtils.buildSpringId( UserRepositories.class.getName() ) );
101 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
103 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
106 public void doGet( HttpServletRequest req, HttpServletResponse res )
107 throws ServletException, IOException
109 String repoId = req.getParameter( "repoId" );
110 String groupId = req.getParameter( "groupId" );
111 String artifactId = req.getParameter( "artifactId" );
115 Map<String, String> map = new HashMap<String, String>();
116 SyndFeed feed = null;
118 if ( ( repoId == null ) && ( groupId == null && artifactId == null ) )
120 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Required fields not found in request." );
124 if ( isAllowed( req ) )
126 if ( repoId != null )
128 // new artifacts in repo feed request
130 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
131 RssFeedProcessor.class.getName(),
133 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
135 else if ( ( groupId != null ) && ( artifactId != null ) )
137 // new versions of artifact feed request
139 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
140 RssFeedProcessor.class.getName(),
142 map.put( RssFeedProcessor.KEY_GROUP_ID, groupId );
143 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId );
148 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
152 feed = processor.process( map );
153 res.setContentType( MIME_TYPE );
155 if ( repoId != null )
157 feed.setLink( req.getRequestURL() + "?repoId=" + repoId );
159 else if ( ( groupId != null ) && ( artifactId != null ) )
161 feed.setLink( req.getRequestURL() + "?groupId=" + groupId + "&artifactId=" + artifactId );
164 SyndFeedOutput output = new SyndFeedOutput();
165 output.output( feed, res.getWriter() );
167 catch ( ArchivaDatabaseException e )
169 log.debug( COULD_NOT_GENERATE_FEED_ERROR, e );
170 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
172 catch ( UserNotFoundException unfe )
174 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
175 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
177 catch ( AccountLockedException acce )
179 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
181 catch ( AuthenticationException authe )
183 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
184 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
186 catch ( FeedException ex )
188 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
189 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
191 catch ( MustChangePasswordException e )
193 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
195 catch ( UnauthorizedException e )
197 log.debug( e.getMessage() );
198 if ( repoId != null )
200 res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" );
204 res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId );
207 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
212 * Basic authentication.
217 private boolean isAllowed( HttpServletRequest req )
218 throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException,
219 UnauthorizedException
221 String auth = req.getHeader( "Authorization" );
222 List<String> repoIds = new ArrayList<String>();
224 if ( req.getParameter( "repoId" ) != null )
226 repoIds.add( req.getParameter( "repoId" ) );
228 else if ( req.getParameter( "artifactId" ) != null && req.getParameter( "groupId" ) != null )
232 if ( !auth.toUpperCase().startsWith( "BASIC " ) )
237 Decoder dec = new Base64();
238 String usernamePassword = "";
242 usernamePassword = new String( (byte[]) dec.decode( auth.substring( 6 ).getBytes() ) );
244 catch ( DecoderException ie )
246 log.warn( "Error decoding username and password.", ie.getMessage() );
249 if ( usernamePassword == null || usernamePassword.trim().equals( "" ) )
251 repoIds = getObservableRepos( ArchivaRoleConstants.PRINCIPAL_GUEST );
255 String[] userCredentials = usernamePassword.split( ":" );
256 repoIds = getObservableRepos( userCredentials[0] );
261 repoIds = getObservableRepos( ArchivaRoleConstants.PRINCIPAL_GUEST );
269 for ( String repoId : repoIds )
273 AuthenticationResult result = httpAuth.getAuthenticationResult( req, null );
274 SecuritySession securitySession = httpAuth.getSecuritySession();
276 if ( servletAuth.isAuthenticated( req, result ) &&
277 servletAuth.isAuthorized( req, securitySession, repoId, false ) )
282 catch ( AuthorizationException e )
286 catch ( UnauthorizedException e )
292 throw new UnauthorizedException( "Access denied." );
295 private List<String> getObservableRepos( String principal )
299 return userRepositories.getObservableRepositoryIds( principal );
301 catch ( PrincipalNotFoundException e )
303 log.warn( e.getMessage(), e );
305 catch ( AccessDeniedException e )
307 log.warn( e.getMessage(), e );
309 catch ( ArchivaSecurityException e )
311 log.warn( e.getMessage(), e );
314 return Collections.emptyList();