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;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServlet;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import com.sun.syndication.feed.synd.SyndFeed;
34 import com.sun.syndication.io.FeedException;
35 import com.sun.syndication.io.SyndFeedOutput;
36 import org.apache.archiva.rss.processor.RssFeedProcessor;
37 import org.apache.commons.codec.Decoder;
38 import org.apache.commons.codec.DecoderException;
39 import org.apache.commons.codec.binary.Base64;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.maven.archiva.security.AccessDeniedException;
42 import org.apache.maven.archiva.security.ArchivaRoleConstants;
43 import org.apache.maven.archiva.security.ArchivaSecurityException;
44 import org.apache.maven.archiva.security.PrincipalNotFoundException;
45 import org.apache.maven.archiva.security.ServletAuthenticator;
46 import org.apache.maven.archiva.security.UserRepositories;
47 import org.codehaus.plexus.redback.authentication.AuthenticationException;
48 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
49 import org.codehaus.plexus.redback.authorization.AuthorizationException;
50 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
51 import org.codehaus.plexus.redback.policy.AccountLockedException;
52 import org.codehaus.plexus.redback.policy.MustChangePasswordException;
53 import org.codehaus.plexus.redback.system.SecuritySession;
54 import org.codehaus.plexus.redback.users.UserManager;
55 import org.codehaus.plexus.redback.users.UserNotFoundException;
56 import org.codehaus.plexus.spring.PlexusToSpringUtils;
57 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60 import org.springframework.web.context.WebApplicationContext;
61 import org.springframework.web.context.support.WebApplicationContextUtils;
64 * Servlet for handling rss feed requests.
68 public class RssFeedServlet
71 public static final String MIME_TYPE = "application/rss+xml; charset=UTF-8";
73 private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
75 private static final String COULD_NOT_AUTHENTICATE_USER = "Could not authenticate user";
77 private static final String USER_NOT_AUTHORIZED = "User not authorized to access feed.";
79 private Logger log = LoggerFactory.getLogger( RssFeedServlet.class );
81 private RssFeedProcessor processor;
83 private WebApplicationContext wac;
85 private UserRepositories userRepositories;
87 private ServletAuthenticator servletAuth;
89 private HttpAuthenticator httpAuth;
91 public void init( javax.servlet.ServletConfig servletConfig )
92 throws ServletException
94 super.init( servletConfig );
95 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
97 (UserRepositories) wac.getBean( PlexusToSpringUtils.buildSpringId( UserRepositories.class.getName() ) );
99 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
101 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
104 public void doGet( HttpServletRequest req, HttpServletResponse res )
105 throws ServletException, IOException
107 String repoId = null;
108 String groupId = null;
109 String artifactId = null;
111 String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" );
112 if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 )
114 artifactId = StringUtils.substringAfterLast( url, "/" );
115 groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/");
116 groupId = StringUtils.replaceChars( groupId, '/', '.' );
118 else if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 )
120 repoId = StringUtils.substringAfterLast( url, "/" );
124 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." );
130 Map<String, String> map = new HashMap<String, String>();
131 SyndFeed feed = null;
133 if ( isAllowed( req, repoId, groupId, artifactId ) )
135 if ( repoId != null )
137 // new artifacts in repo feed request
139 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
140 RssFeedProcessor.class.getName(),
142 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
144 else if ( ( groupId != null ) && ( artifactId != null ) )
146 // TODO: this only works for guest - we could pass in the list of repos
147 // new versions of artifact feed request
149 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
150 RssFeedProcessor.class.getName(),
152 map.put( RssFeedProcessor.KEY_GROUP_ID, groupId );
153 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId );
158 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
162 feed = processor.process( map );
165 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
169 res.setContentType( MIME_TYPE );
171 if ( repoId != null )
173 feed.setLink( req.getRequestURL().toString() );
175 else if ( ( groupId != null ) && ( artifactId != null ) )
177 feed.setLink( req.getRequestURL().toString() );
180 SyndFeedOutput output = new SyndFeedOutput();
181 output.output( feed, res.getWriter() );
183 catch ( UserNotFoundException unfe )
185 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
186 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
188 catch ( AccountLockedException acce )
190 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
192 catch ( AuthenticationException authe )
194 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
195 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
197 catch ( FeedException ex )
199 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
200 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
202 catch ( MustChangePasswordException e )
204 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
206 catch ( UnauthorizedException e )
208 log.debug( e.getMessage() );
209 if ( repoId != null )
211 res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" );
215 res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId );
218 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
223 * Basic authentication.
226 * @param repositoryId TODO
227 * @param groupId TODO
228 * @param artifactId TODO
231 private boolean isAllowed( HttpServletRequest req, String repositoryId, String groupId, String artifactId )
232 throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException,
233 UnauthorizedException
235 String auth = req.getHeader( "Authorization" );
236 List<String> repoIds = new ArrayList<String>();
238 if ( repositoryId != null )
240 repoIds.add( repositoryId );
242 else if ( artifactId != null && groupId != null )
246 if ( !auth.toUpperCase().startsWith( "BASIC " ) )
251 Decoder dec = new Base64();
252 String usernamePassword = "";
256 usernamePassword = new String( (byte[]) dec.decode( auth.substring( 6 ).getBytes() ) );
258 catch ( DecoderException ie )
260 log.warn( "Error decoding username and password.", ie.getMessage() );
263 if ( usernamePassword == null || usernamePassword.trim().equals( "" ) )
265 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
269 String[] userCredentials = usernamePassword.split( ":" );
270 repoIds = getObservableRepos( userCredentials[0] );
275 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
283 for ( String repoId : repoIds )
287 AuthenticationResult result = httpAuth.getAuthenticationResult( req, null );
288 SecuritySession securitySession = httpAuth.getSecuritySession( req.getSession( true ) );
290 if ( servletAuth.isAuthenticated( req, result )
291 && servletAuth.isAuthorized( req, securitySession, repoId,
292 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) )
297 catch ( AuthorizationException e )
301 catch ( UnauthorizedException e )
307 throw new UnauthorizedException( "Access denied." );
310 private List<String> getObservableRepos( String principal )
314 return userRepositories.getObservableRepositoryIds( principal );
316 catch ( PrincipalNotFoundException e )
318 log.warn( e.getMessage(), e );
320 catch ( AccessDeniedException e )
322 log.warn( e.getMessage(), e );
324 catch ( ArchivaSecurityException e )
326 log.warn( e.getMessage(), e );
329 return Collections.emptyList();