1 package org.apache.archiva.rest.services;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.AuditInformation;
22 import org.apache.archiva.audit.AuditListener;
23 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
24 import org.apache.archiva.security.AccessDeniedException;
25 import org.apache.archiva.security.ArchivaSecurityException;
26 import org.apache.archiva.security.PrincipalNotFoundException;
27 import org.apache.archiva.security.UserRepositories;
28 import org.apache.commons.lang.StringUtils;
29 import org.codehaus.plexus.redback.users.User;
30 import org.codehaus.plexus.redback.users.UserManager;
31 import org.codehaus.redback.rest.services.RedbackAuthenticationThreadLocal;
32 import org.codehaus.redback.rest.services.RedbackRequestInformation;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.context.ApplicationContext;
37 import javax.inject.Inject;
38 import javax.inject.Named;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.core.Context;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.List;
48 * abstract class with common utilities methods
50 * @author Olivier Lamy
53 public abstract class AbstractRestService
56 protected Logger log = LoggerFactory.getLogger( getClass() );
59 private List<AuditListener> auditListeners = new ArrayList<AuditListener>();
62 private UserRepositories userRepositories;
66 @Named( value = "repositorySessionFactory" )
67 protected RepositorySessionFactory repositorySessionFactory;
70 protected HttpServletRequest httpServletRequest;
72 protected AuditInformation getAuditInformation()
74 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
75 User user = redbackRequestInformation == null ? null : redbackRequestInformation.getUser();
76 String remoteAddr = redbackRequestInformation == null ? null : redbackRequestInformation.getRemoteAddr();
77 return new AuditInformation( user, remoteAddr );
80 public List<AuditListener> getAuditListeners()
82 return auditListeners;
85 public void setAuditListeners( List<AuditListener> auditListeners )
87 this.auditListeners = auditListeners;
90 protected List<String> getObservableRepos()
94 List<String> ids = userRepositories.getObservableRepositoryIds( getPrincipal() );
95 return ids == null ? Collections.<String>emptyList() : ids;
97 catch ( PrincipalNotFoundException e )
99 log.warn( e.getMessage(), e );
101 catch ( AccessDeniedException e )
103 log.warn( e.getMessage(), e );
105 catch ( ArchivaSecurityException e )
107 log.warn( e.getMessage(), e );
109 return Collections.emptyList();
112 protected String getPrincipal()
114 RedbackRequestInformation redbackRequestInformation = RedbackAuthenticationThreadLocal.get();
116 return redbackRequestInformation == null
117 ? UserManager.GUEST_USERNAME
118 : ( redbackRequestInformation.getUser() == null
119 ? UserManager.GUEST_USERNAME
120 : redbackRequestInformation.getUser().getUsername() );
123 protected String getBaseUrl( HttpServletRequest req )
125 return req.getScheme() + "://" + req.getServerName() + ( req.getServerPort() == 80
127 : ":" + req.getServerPort() ) + req.getContextPath();
130 protected <T> Map<String, T> getBeansOfType( ApplicationContext applicationContext, Class<T> clazz )
132 //TODO do some caching here !!!
133 // olamy : with plexus we get only roleHint
134 // as per convention we named spring bean role#hint remove role# if exists
135 Map<String, T> springBeans = applicationContext.getBeansOfType( clazz );
137 Map<String, T> beans = new HashMap<String, T>( springBeans.size() );
139 for ( Map.Entry<String, T> entry : springBeans.entrySet() )
141 String key = StringUtils.substringAfterLast( entry.getKey(), "#" );
142 beans.put( key, entry.getValue() );