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.commons.lang.StringUtils;
39 import org.apache.maven.archiva.database.ArchivaDatabaseException;
40 import org.apache.maven.archiva.security.AccessDeniedException;
41 import org.apache.maven.archiva.security.ArchivaRoleConstants;
42 import org.apache.maven.archiva.security.ArchivaSecurityException;
43 import org.apache.maven.archiva.security.PrincipalNotFoundException;
44 import org.apache.maven.archiva.security.ServletAuthenticator;
45 import org.apache.maven.archiva.security.UserRepositories;
46 import org.codehaus.plexus.redback.authentication.AuthenticationException;
47 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
48 import org.codehaus.plexus.redback.authorization.AuthorizationException;
49 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
50 import org.codehaus.plexus.redback.policy.AccountLockedException;
51 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
52 import org.codehaus.plexus.redback.system.SecuritySession;
53 import org.codehaus.plexus.redback.users.UserManager;
54 import org.codehaus.plexus.redback.users.UserNotFoundException;
55 import org.codehaus.plexus.spring.PlexusToSpringUtils;
56 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.springframework.web.context.WebApplicationContext;
60 import org.springframework.web.context.support.WebApplicationContextUtils;
62 import com.sun.syndication.feed.synd.SyndFeed;
63 import com.sun.syndication.io.FeedException;
64 import com.sun.syndication.io.SyndFeedOutput;
67 * Servlet for handling rss feed requests.
71 public class RssFeedServlet
74 public static final String MIME_TYPE = "application/rss+xml; charset=UTF-8";
76 private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
78 private static final String COULD_NOT_AUTHENTICATE_USER = "Could not authenticate user";
80 private static final String USER_NOT_AUTHORIZED = "User not authorized to access feed.";
82 private Logger log = LoggerFactory.getLogger( RssFeedServlet.class );
84 private RssFeedProcessor processor;
86 private WebApplicationContext wac;
88 private UserRepositories userRepositories;
90 private ServletAuthenticator servletAuth;
92 private HttpAuthenticator httpAuth;
94 public void init( javax.servlet.ServletConfig servletConfig )
95 throws ServletException
97 super.init( servletConfig );
98 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
100 (UserRepositories) wac.getBean( PlexusToSpringUtils.buildSpringId( UserRepositories.class.getName() ) );
102 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
104 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
107 public void doGet( HttpServletRequest req, HttpServletResponse res )
108 throws ServletException, IOException
110 String repoId = null;
111 String groupId = null;
112 String artifactId = null;
114 String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" );
115 if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 )
117 artifactId = StringUtils.substringAfterLast( url, "/" );
118 groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/");
119 groupId = StringUtils.replaceChars( groupId, '/', '.' );
121 else if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 )
123 repoId = StringUtils.substringAfterLast( url, "/" );
127 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." );
133 Map<String, String> map = new HashMap<String, String>();
134 SyndFeed feed = null;
136 if ( isAllowed( req, repoId, groupId, artifactId ) )
138 if ( repoId != null )
140 // new artifacts in repo feed request
142 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
143 RssFeedProcessor.class.getName(),
145 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
147 else if ( ( groupId != null ) && ( artifactId != null ) )
149 // new versions of artifact feed request
151 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
152 RssFeedProcessor.class.getName(),
154 map.put( RssFeedProcessor.KEY_GROUP_ID, groupId );
155 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId );
160 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
164 feed = processor.process( map );
167 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
171 res.setContentType( MIME_TYPE );
173 if ( repoId != null )
175 feed.setLink( req.getRequestURL().toString() );
177 else if ( ( groupId != null ) && ( artifactId != null ) )
179 feed.setLink( req.getRequestURL().toString() );
182 SyndFeedOutput output = new SyndFeedOutput();
183 output.output( feed, res.getWriter() );
185 catch ( ArchivaDatabaseException e )
187 log.debug( COULD_NOT_GENERATE_FEED_ERROR, e );
188 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
190 catch ( UserNotFoundException unfe )
192 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
193 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
195 catch ( AccountLockedException acce )
197 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
199 catch ( AuthenticationException authe )
201 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
202 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
204 catch ( FeedException ex )
206 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
207 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
209 catch ( MustChangePasswordException e )
211 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
213 catch ( UnauthorizedException e )
215 log.debug( e.getMessage() );
216 if ( repoId != null )
218 res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" );
222 res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId );
225 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
230 * Basic authentication.
233 * @param repositoryId TODO
234 * @param groupId TODO
235 * @param artifactId TODO
238 private boolean isAllowed( HttpServletRequest req, String repositoryId, String groupId, String artifactId )
239 throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException,
240 UnauthorizedException
242 String auth = req.getHeader( "Authorization" );
243 List<String> repoIds = new ArrayList<String>();
245 if ( repositoryId != null )
247 repoIds.add( repositoryId );
249 else if ( artifactId != null && groupId != null )
253 if ( !auth.toUpperCase().startsWith( "BASIC " ) )
258 Decoder dec = new Base64();
259 String usernamePassword = "";
263 usernamePassword = new String( (byte[]) dec.decode( auth.substring( 6 ).getBytes() ) );
265 catch ( DecoderException ie )
267 log.warn( "Error decoding username and password.", ie.getMessage() );
270 if ( usernamePassword == null || usernamePassword.trim().equals( "" ) )
272 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
276 String[] userCredentials = usernamePassword.split( ":" );
277 repoIds = getObservableRepos( userCredentials[0] );
282 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
290 for ( String repoId : repoIds )
294 AuthenticationResult result = httpAuth.getAuthenticationResult( req, null );
295 SecuritySession securitySession = httpAuth.getSecuritySession( req.getSession( true ) );
297 if ( servletAuth.isAuthenticated( req, result )
298 && servletAuth.isAuthorized( req, securitySession, repoId,
299 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) )
304 catch ( AuthorizationException e )
308 catch ( UnauthorizedException e )
314 throw new UnauthorizedException( "Access denied." );
317 private List<String> getObservableRepos( String principal )
321 return userRepositories.getObservableRepositoryIds( principal );
323 catch ( PrincipalNotFoundException e )
325 log.warn( e.getMessage(), e );
327 catch ( AccessDeniedException e )
329 log.warn( e.getMessage(), e );
331 catch ( ArchivaSecurityException e )
333 log.warn( e.getMessage(), e );
336 return Collections.emptyList();