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.ArchivaSecurityException;
42 import org.apache.maven.archiva.security.ArchivaXworkUser;
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 private ArchivaXworkUser archivaXworkUser;
96 public void init( javax.servlet.ServletConfig servletConfig )
97 throws ServletException
99 super.init( servletConfig );
100 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
102 (UserRepositories) wac.getBean( PlexusToSpringUtils.buildSpringId( UserRepositories.class.getName() ) );
104 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
106 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
107 archivaXworkUser = (ArchivaXworkUser) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaXworkUser.class ) );
110 public void doGet( HttpServletRequest req, HttpServletResponse res )
111 throws ServletException, IOException
113 String repoId = null;
114 String groupId = null;
115 String artifactId = null;
117 String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" );
118 if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 )
120 artifactId = StringUtils.substringAfterLast( url, "/" );
121 groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/");
122 groupId = StringUtils.replaceChars( groupId, '/', '.' );
124 else if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 )
126 repoId = StringUtils.substringAfterLast( url, "/" );
130 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." );
136 Map<String, String> map = new HashMap<String, String>();
137 SyndFeed feed = null;
139 if ( isAllowed( req, repoId, groupId, artifactId ) )
141 if ( repoId != null )
143 // new artifacts in repo feed request
145 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
146 RssFeedProcessor.class.getName(),
148 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
150 else if ( ( groupId != null ) && ( artifactId != null ) )
152 // new versions of artifact feed request
154 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
155 RssFeedProcessor.class.getName(),
157 map.put( RssFeedProcessor.KEY_GROUP_ID, groupId );
158 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId );
163 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
167 feed = processor.process( map );
170 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
174 res.setContentType( MIME_TYPE );
176 if ( repoId != null )
178 feed.setLink( req.getRequestURL().toString() );
180 else if ( ( groupId != null ) && ( artifactId != null ) )
182 feed.setLink( req.getRequestURL().toString() );
185 SyndFeedOutput output = new SyndFeedOutput();
186 output.output( feed, res.getWriter() );
188 catch ( ArchivaDatabaseException e )
190 log.debug( COULD_NOT_GENERATE_FEED_ERROR, e );
191 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
193 catch ( UserNotFoundException unfe )
195 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
196 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
198 catch ( AccountLockedException acce )
200 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
202 catch ( AuthenticationException authe )
204 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
205 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
207 catch ( FeedException ex )
209 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
210 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
212 catch ( MustChangePasswordException e )
214 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
216 catch ( UnauthorizedException e )
218 log.debug( e.getMessage() );
219 if ( repoId != null )
221 res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" );
225 res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId );
228 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
233 * Basic authentication.
236 * @param repositoryId TODO
237 * @param groupId TODO
238 * @param artifactId TODO
241 private boolean isAllowed( HttpServletRequest req, String repositoryId, String groupId, String artifactId )
242 throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException,
243 UnauthorizedException
245 String auth = req.getHeader( "Authorization" );
246 List<String> repoIds = new ArrayList<String>();
248 if ( repositoryId != null )
250 repoIds.add( repositoryId );
252 else if ( artifactId != null && groupId != null )
256 if ( !auth.toUpperCase().startsWith( "BASIC " ) )
261 Decoder dec = new Base64();
262 String usernamePassword = "";
266 usernamePassword = new String( (byte[]) dec.decode( auth.substring( 6 ).getBytes() ) );
268 catch ( DecoderException ie )
270 log.warn( "Error decoding username and password.", ie.getMessage() );
273 if ( usernamePassword == null || usernamePassword.trim().equals( "" ) )
275 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
279 String[] userCredentials = usernamePassword.split( ":" );
280 repoIds = getObservableRepos( userCredentials[0] );
285 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
293 for ( String repoId : repoIds )
297 AuthenticationResult result = httpAuth.getAuthenticationResult( req, null );
298 SecuritySession securitySession = httpAuth.getSecuritySession( req.getSession( true ) );
300 if ( servletAuth.isAuthenticated( req, result ) &&
301 servletAuth.isAuthorized( req, securitySession, repoId, false ) )
306 catch ( AuthorizationException e )
310 catch ( UnauthorizedException e )
316 throw new UnauthorizedException( "Access denied." );
319 private List<String> getObservableRepos( String principal )
323 return userRepositories.getObservableRepositoryIds( principal );
325 catch ( PrincipalNotFoundException e )
327 log.warn( e.getMessage(), e );
329 catch ( AccessDeniedException e )
331 log.warn( e.getMessage(), e );
333 catch ( ArchivaSecurityException e )
335 log.warn( e.getMessage(), e );
338 return Collections.emptyList();