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.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 private ArchivaXworkUser archivaXworkUser;
95 public void init( javax.servlet.ServletConfig servletConfig )
96 throws ServletException
98 super.init( servletConfig );
99 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
101 (UserRepositories) wac.getBean( PlexusToSpringUtils.buildSpringId( UserRepositories.class.getName() ) );
103 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
105 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
106 archivaXworkUser = (ArchivaXworkUser) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaXworkUser.class ) );
109 public void doGet( HttpServletRequest req, HttpServletResponse res )
110 throws ServletException, IOException
112 String repoId = null;
113 String groupId = null;
114 String artifactId = null;
116 String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" );
117 if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 )
119 artifactId = StringUtils.substringAfterLast( url, "/" );
120 groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/");
121 groupId = StringUtils.replaceChars( groupId, '/', '.' );
123 else if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 )
125 repoId = StringUtils.substringAfterLast( url, "/" );
129 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." );
135 Map<String, String> map = new HashMap<String, String>();
136 SyndFeed feed = null;
138 if ( isAllowed( req, repoId, groupId, artifactId ) )
140 if ( repoId != null )
142 // new artifacts in repo feed request
144 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
145 RssFeedProcessor.class.getName(),
147 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
149 else if ( ( groupId != null ) && ( artifactId != null ) )
151 // new versions of artifact feed request
153 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
154 RssFeedProcessor.class.getName(),
156 map.put( RssFeedProcessor.KEY_GROUP_ID, groupId );
157 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId );
162 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
166 feed = processor.process( map );
169 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
173 res.setContentType( MIME_TYPE );
175 if ( repoId != null )
177 feed.setLink( req.getRequestURL().toString() );
179 else if ( ( groupId != null ) && ( artifactId != null ) )
181 feed.setLink( req.getRequestURL().toString() );
184 SyndFeedOutput output = new SyndFeedOutput();
185 output.output( feed, res.getWriter() );
187 catch ( ArchivaDatabaseException e )
189 log.debug( COULD_NOT_GENERATE_FEED_ERROR, e );
190 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
192 catch ( UserNotFoundException unfe )
194 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
195 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
197 catch ( AccountLockedException acce )
199 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
201 catch ( AuthenticationException authe )
203 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
204 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
206 catch ( FeedException ex )
208 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
209 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
211 catch ( MustChangePasswordException e )
213 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
215 catch ( UnauthorizedException e )
217 log.debug( e.getMessage() );
218 if ( repoId != null )
220 res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" );
224 res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId );
227 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
232 * Basic authentication.
235 * @param repositoryId TODO
236 * @param groupId TODO
237 * @param artifactId TODO
240 private boolean isAllowed( HttpServletRequest req, String repositoryId, String groupId, String artifactId )
241 throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException,
242 UnauthorizedException
244 String auth = req.getHeader( "Authorization" );
245 List<String> repoIds = new ArrayList<String>();
247 if ( repositoryId != null )
249 repoIds.add( repositoryId );
251 else if ( artifactId != null && groupId != null )
255 if ( !auth.toUpperCase().startsWith( "BASIC " ) )
260 Decoder dec = new Base64();
261 String usernamePassword = "";
265 usernamePassword = new String( (byte[]) dec.decode( auth.substring( 6 ).getBytes() ) );
267 catch ( DecoderException ie )
269 log.warn( "Error decoding username and password.", ie.getMessage() );
272 if ( usernamePassword == null || usernamePassword.trim().equals( "" ) )
274 repoIds = getObservableRepos( archivaXworkUser.getGuest() );
278 String[] userCredentials = usernamePassword.split( ":" );
279 repoIds = getObservableRepos( userCredentials[0] );
284 repoIds = getObservableRepos( archivaXworkUser.getGuest() );
292 for ( String repoId : repoIds )
296 AuthenticationResult result = httpAuth.getAuthenticationResult( req, null );
297 SecuritySession securitySession = httpAuth.getSecuritySession( req.getSession( true ) );
299 if ( servletAuth.isAuthenticated( req, result ) &&
300 servletAuth.isAuthorized( req, securitySession, repoId, false ) )
305 catch ( AuthorizationException e )
309 catch ( UnauthorizedException e )
315 throw new UnauthorizedException( "Access denied." );
318 private List<String> getObservableRepos( String principal )
322 return userRepositories.getObservableRepositoryIds( principal );
324 catch ( PrincipalNotFoundException e )
326 log.warn( e.getMessage(), e );
328 catch ( AccessDeniedException e )
330 log.warn( e.getMessage(), e );
332 catch ( ArchivaSecurityException e )
334 log.warn( e.getMessage(), e );
337 return Collections.emptyList();