1 package org.apache.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 com.sun.syndication.feed.synd.SyndFeed;
23 import com.sun.syndication.io.FeedException;
24 import com.sun.syndication.io.SyndFeedOutput;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
27 import org.apache.archiva.redback.authentication.AuthenticationException;
28 import org.apache.archiva.redback.policy.AccountLockedException;
29 import org.apache.archiva.redback.policy.MustChangePasswordException;
30 import org.apache.archiva.redback.users.UserManager;
31 import org.apache.archiva.redback.users.UserNotFoundException;
32 import org.apache.archiva.rss.processor.RssFeedProcessor;
33 import org.apache.archiva.security.AccessDeniedException;
34 import org.apache.archiva.security.ArchivaSecurityException;
35 import org.apache.archiva.security.PrincipalNotFoundException;
36 import org.apache.archiva.security.ServletAuthenticator;
37 import org.apache.archiva.security.UserRepositories;
38 import org.apache.archiva.security.common.ArchivaRoleConstants;
39 import org.apache.commons.codec.Decoder;
40 import org.apache.commons.codec.DecoderException;
41 import org.apache.commons.codec.binary.Base64;
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.archiva.redback.authentication.AuthenticationResult;
44 import org.apache.archiva.redback.authorization.AuthorizationException;
45 import org.apache.archiva.redback.authorization.UnauthorizedException;
46 import org.apache.archiva.redback.system.SecuritySession;
47 import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.web.context.WebApplicationContext;
51 import org.springframework.web.context.support.WebApplicationContextUtils;
53 import javax.servlet.ServletConfig;
54 import javax.servlet.ServletException;
55 import javax.servlet.http.HttpServlet;
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58 import java.io.IOException;
59 import java.util.ArrayList;
60 import java.util.Collections;
61 import java.util.HashMap;
62 import java.util.List;
66 * 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;
92 * FIXME: this could be multiple implementations and needs to be configured.
94 private RepositorySessionFactory repositorySessionFactory;
97 public void init( ServletConfig servletConfig )
98 throws ServletException
100 super.init( servletConfig );
101 wac = WebApplicationContextUtils.getRequiredWebApplicationContext( servletConfig.getServletContext() );
102 userRepositories = wac.getBean( UserRepositories.class );
103 servletAuth = wac.getBean( ServletAuthenticator.class );
104 httpAuth = wac.getBean( "httpAuthenticator#basic", HttpAuthenticator.class );
105 // TODO: what if there are other types?
106 repositorySessionFactory = wac.getBean( "repositorySessionFactory", RepositorySessionFactory.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<>();
137 SyndFeed feed = null;
139 if ( isAllowed( req, repoId, groupId, artifactId ) )
141 if ( repoId != null )
143 // new artifacts in repo feed request
144 processor = wac.getBean( "rssFeedProcessor#new-artifacts", RssFeedProcessor.class );
145 map.put( RssFeedProcessor.KEY_REPO_ID, repoId );
147 else if ( ( groupId != null ) && ( artifactId != null ) )
149 // TODO: this only works for guest - we could pass in the list of repos
150 // new versions of artifact feed request
151 processor = wac.getBean( "rssFeedProcessor#new-versions", RssFeedProcessor.class );
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 RepositorySession repositorySession = repositorySessionFactory.createSession();
165 feed = processor.process( map, repositorySession.getRepository() );
169 repositorySession.close();
173 res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." );
177 res.setContentType( MIME_TYPE );
179 if ( repoId != null )
181 feed.setLink( req.getRequestURL().toString() );
183 else if ( ( groupId != null ) && ( artifactId != null ) )
185 feed.setLink( req.getRequestURL().toString() );
188 SyndFeedOutput output = new SyndFeedOutput();
189 output.output( feed, res.getWriter() );
191 catch ( UserNotFoundException unfe )
193 log.debug( COULD_NOT_AUTHENTICATE_USER, unfe );
194 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
196 catch ( AccountLockedException acce )
198 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
200 catch ( AuthenticationException authe )
202 log.debug( COULD_NOT_AUTHENTICATE_USER, authe );
203 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
205 catch ( FeedException ex )
207 log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex );
208 res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR );
210 catch ( MustChangePasswordException e )
212 res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER );
214 catch ( UnauthorizedException e )
216 log.debug( e.getMessage() );
217 if ( repoId != null )
219 res.setHeader( "WWW-Authenticate",
220 "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<>();
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( UserManager.GUEST_USERNAME );
278 String[] userCredentials = usernamePassword.split( ":" );
279 repoIds = getObservableRepos( userCredentials[0] );
284 repoIds = getObservableRepos( UserManager.GUEST_USERNAME );
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 ) && servletAuth.isAuthorized( req, securitySession,
301 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) )
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();