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.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.UserManager;
53 import org.codehaus.plexus.redback.users.UserNotFoundException;
54 import org.codehaus.plexus.spring.PlexusToSpringUtils;
55 import org.codehaus.redback.integration.filter.authentication.HttpAuthenticator;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.springframework.web.context.WebApplicationContext;
59 import org.springframework.web.context.support.WebApplicationContextUtils;
61 import com.sun.syndication.feed.synd.SyndFeed;
62 import com.sun.syndication.io.FeedException;
63 import com.sun.syndication.io.SyndFeedOutput;
66 * Servlet for handling rss feed requests.
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 = null;
110 String groupId = null;
111 String artifactId = null;
113 String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" );
114 if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 )
116 artifactId = StringUtils.substringAfterLast( url, "/" );
117 groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/");
118 groupId = StringUtils.replaceChars( groupId, '/', '.' );
120 else if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 )
122 repoId = StringUtils.substringAfterLast( url, "/" );
126 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." );
132 Map<String, String> map = new HashMap<String, String>();
133 SyndFeed feed = null;
135 if ( isAllowed( req, repoId, groupId, artifactId ) )
137 if ( repoId != null )
139 // new artifacts in repo feed request
141 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
142 RssFeedProcessor.class.getName(),
144 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
146 else if ( ( groupId != null ) && ( artifactId != null ) )
148 // new versions of artifact feed request
150 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
151 RssFeedProcessor.class.getName(),
153 map.put( RssFeedProcessor.KEY_GROUP_ID, groupId );
154 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId );
159 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
163 feed = processor.process( map );
166 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
170 res.setContentType( MIME_TYPE );
172 if ( repoId != null )
174 feed.setLink( req.getRequestURL().toString() );
176 else if ( ( groupId != null ) && ( artifactId != null ) )
178 feed.setLink( req.getRequestURL().toString() );
181 SyndFeedOutput output = new SyndFeedOutput();
182 output.output( feed, res.getWriter() );
184 catch ( ArchivaDatabaseException e )
186 log.debug( COULD_NOT_GENERATE_FEED_ERROR, e );
187 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
189 catch ( UserNotFoundException unfe )
191 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
192 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
194 catch ( AccountLockedException acce )
196 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
198 catch ( AuthenticationException authe )
200 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
201 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
203 catch ( FeedException ex )
205 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
206 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
208 catch ( MustChangePasswordException e )
210 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
212 catch ( UnauthorizedException e )
214 log.debug( e.getMessage() );
215 if ( repoId != null )
217 res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" );
221 res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId );
224 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
229 * Basic authentication.
232 * @param repositoryId TODO
233 * @param groupId TODO
234 * @param artifactId TODO
237 private boolean isAllowed( HttpServletRequest req, String repositoryId, String groupId, String artifactId )
238 throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException,
239 UnauthorizedException
241 String auth = req.getHeader( "Authorization" );
242 List<String> repoIds = new ArrayList<String>();
244 if ( repositoryId != null )
246 repoIds.add( repositoryId );
248 else if ( artifactId != null && groupId != null )
252 if ( !auth.toUpperCase().startsWith( "BASIC " ) )
257 Decoder dec = new Base64();
258 String usernamePassword = "";
262 usernamePassword = new String( (byte[]) dec.decode( auth.substring( 6 ).getBytes() ) );
264 catch ( DecoderException ie )
266 log.warn( "Error decoding username and password.", ie.getMessage() );
269 if ( usernamePassword == null || usernamePassword.trim().equals( "" ) )
271 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
275 String[] userCredentials = usernamePassword.split( ":" );
276 repoIds = getObservableRepos( userCredentials[0] );
281 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
289 for ( String repoId : repoIds )
293 AuthenticationResult result = httpAuth.getAuthenticationResult( req, null );
294 SecuritySession securitySession = httpAuth.getSecuritySession( req.getSession( true ) );
296 if ( servletAuth.isAuthenticated( req, result ) &&
297 servletAuth.isAuthorized( req, securitySession, repoId, false ) )
302 catch ( AuthorizationException e )
306 catch ( UnauthorizedException e )
312 throw new UnauthorizedException( "Access denied." );
315 private List<String> getObservableRepos( String principal )
319 return userRepositories.getObservableRepositoryIds( principal );
321 catch ( PrincipalNotFoundException e )
323 log.warn( e.getMessage(), e );
325 catch ( AccessDeniedException e )
327 log.warn( e.getMessage(), e );
329 catch ( ArchivaSecurityException e )
331 log.warn( e.getMessage(), e );
334 return Collections.emptyList();