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.ArchivaXworkUser;
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.UserNotFoundException;
55 import org.codehaus.plexus.redback.xwork.filter.authentication.HttpAuthenticator;
56 import org.codehaus.plexus.spring.PlexusToSpringUtils;
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.
69 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
72 public class RssFeedServlet
75 public static final String MIME_TYPE = "application/rss+xml; charset=UTF-8";
77 private static final String COULD_NOT_GENERATE_FEED_ERROR = "Could not generate feed";
79 private static final String COULD_NOT_AUTHENTICATE_USER = "Could not authenticate user";
81 private static final String USER_NOT_AUTHORIZED = "User not authorized to access feed.";
83 private Logger log = LoggerFactory.getLogger( RssFeedServlet.class );
85 private RssFeedProcessor processor;
87 private WebApplicationContext wac;
89 private UserRepositories userRepositories;
91 private ServletAuthenticator servletAuth;
93 private HttpAuthenticator httpAuth;
95 private ArchivaXworkUser archivaXworkUser;
97 public void init( javax.servlet.ServletConfig servletConfig )
98 throws ServletException
100 super.init( servletConfig );
101 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
103 (UserRepositories) wac.getBean( PlexusToSpringUtils.buildSpringId( UserRepositories.class.getName() ) );
105 (ServletAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( ServletAuthenticator.class.getName() ) );
107 (HttpAuthenticator) wac.getBean( PlexusToSpringUtils.buildSpringId( HttpAuthenticator.ROLE, "basic" ) );
108 archivaXworkUser = (ArchivaXworkUser) wac.getBean( PlexusToSpringUtils.buildSpringId( ArchivaXworkUser.class ) );
111 public void doGet( HttpServletRequest req, HttpServletResponse res )
112 throws ServletException, IOException
114 String repoId = null;
115 String groupId = null;
116 String artifactId = null;
118 String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" );
119 if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 )
121 artifactId = StringUtils.substringAfterLast( url, "/" );
122 groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/");
123 groupId = StringUtils.replaceChars( groupId, '/', '.' );
125 else if( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 )
127 repoId = StringUtils.substringAfterLast( url, "/" );
131 res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." );
137 Map<String, String> map = new HashMap<String, String>();
138 SyndFeed feed = null;
140 if ( isAllowed( req, repoId, groupId, artifactId ) )
142 if ( repoId != null )
144 // new artifacts in repo feed request
146 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
147 RssFeedProcessor.class.getName(),
149 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
151 else if ( ( groupId != null ) && ( artifactId != null ) )
153 // new versions of artifact feed request
155 (RssFeedProcessor) wac.getBean( PlexusToSpringUtils.buildSpringId(
156 RssFeedProcessor.class.getName(),
158 map.put( RssFeedProcessor.KEY_GROUP_ID, groupId );
159 map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId );
164 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
168 feed = processor.process( map );
171 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
175 res.setContentType( MIME_TYPE );
177 if ( repoId != null )
179 feed.setLink( req.getRequestURL().toString() );
181 else if ( ( groupId != null ) && ( artifactId != null ) )
183 feed.setLink( req.getRequestURL().toString() );
186 SyndFeedOutput output = new SyndFeedOutput();
187 output.output( feed, res.getWriter() );
189 catch ( ArchivaDatabaseException e )
191 log.debug( COULD_NOT_GENERATE_FEED_ERROR, e );
192 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
194 catch ( UserNotFoundException unfe )
196 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
197 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
199 catch ( AccountLockedException acce )
201 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
203 catch ( AuthenticationException authe )
205 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
206 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
208 catch ( FeedException ex )
210 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
211 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
213 catch ( MustChangePasswordException e )
215 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
217 catch ( UnauthorizedException e )
219 log.debug( e.getMessage() );
220 if ( repoId != null )
222 res.setHeader("WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" );
226 res.setHeader("WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId );
229 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED );
234 * Basic authentication.
237 * @param repositoryId TODO
238 * @param groupId TODO
239 * @param artifactId TODO
242 private boolean isAllowed( HttpServletRequest req, String repositoryId, String groupId, String artifactId )
243 throws UserNotFoundException, AccountLockedException, AuthenticationException, MustChangePasswordException,
244 UnauthorizedException
246 String auth = req.getHeader( "Authorization" );
247 List<String> repoIds = new ArrayList<String>();
249 if ( repositoryId != null )
251 repoIds.add( repositoryId );
253 else if ( artifactId != null && groupId != null )
257 if ( !auth.toUpperCase().startsWith( "BASIC " ) )
262 Decoder dec = new Base64();
263 String usernamePassword = "";
267 usernamePassword = new String( (byte[]) dec.decode( auth.substring( 6 ).getBytes() ) );
269 catch ( DecoderException ie )
271 log.warn( "Error decoding username and password.", ie.getMessage() );
274 if ( usernamePassword == null || usernamePassword.trim().equals( "" ) )
276 repoIds = getObservableRepos( archivaXworkUser.getGuest() );
280 String[] userCredentials = usernamePassword.split( ":" );
281 repoIds = getObservableRepos( userCredentials[0] );
286 repoIds = getObservableRepos( archivaXworkUser.getGuest() );
294 for ( String repoId : repoIds )
298 AuthenticationResult result = httpAuth.getAuthenticationResult( req, null );
299 SecuritySession securitySession = httpAuth.getSecuritySession();
301 if ( servletAuth.isAuthenticated( req, result ) &&
302 servletAuth.isAuthorized( req, securitySession, repoId, false ) )
307 catch ( AuthorizationException e )
311 catch ( UnauthorizedException e )
317 throw new UnauthorizedException( "Access denied." );
320 private List<String> getObservableRepos( String principal )
324 return userRepositories.getObservableRepositoryIds( principal );
326 catch ( PrincipalNotFoundException e )
328 log.warn( e.getMessage(), e );
330 catch ( AccessDeniedException e )
332 log.warn( e.getMessage(), e );
334 catch ( ArchivaSecurityException e )
336 log.warn( e.getMessage(), e );
339 return Collections.emptyList();