1 package org.apache.archiva.redback.integration.reports;
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 org.apache.archiva.redback.users.User;
23 import org.apache.archiva.redback.users.UserManager;
24 import org.apache.commons.beanutils.PropertyUtils;
25 import org.apache.commons.lang.StringEscapeUtils;
26 import org.apache.archiva.redback.system.SecuritySystem;
27 import org.apache.archiva.redback.integration.util.UserComparator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Service;
32 import javax.inject.Inject;
33 import java.io.OutputStream;
34 import java.io.PrintWriter;
35 import java.lang.reflect.InvocationTargetException;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
45 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
48 @Service( "report#userlist-csv" )
49 public class CsvUserList
52 private Logger log = LoggerFactory.getLogger( CsvUserList.class );
55 private SecuritySystem securitySystem;
57 private Map<String, String> fields;
61 fields = new HashMap<String, String>();
62 fields.put( "username", "User Name" );
63 fields.put( "fullName", "Full Name" );
64 fields.put( "email", "Email Address" );
65 fields.put( "permanent", "Permanent User" );
66 fields.put( "locked", "Locked User" );
67 fields.put( "validated", "Validated User" );
68 fields.put( "passwordChangeRequired", "Must Change Password On Next Login" );
69 fields.put( "countFailedLoginAttempts", "Failed Login Attempts" );
70 fields.put( "lastPasswordChange", "Last Password Change" );
71 fields.put( "accountCreationDate", "Date Created" );
72 fields.put( "lastLoginDate", "Date Last Logged In" );
80 public String getMimeType()
85 public String getName()
90 public String getType()
95 public void writeReport( OutputStream os )
96 throws ReportException
98 UserManager userManager = securitySystem.getUserManager();
100 List<User> allUsers = userManager.getUsers();
102 Collections.sort( allUsers, new UserComparator( "username", true ) );
104 PrintWriter out = new PrintWriter( os );
106 writeCsvHeader( out );
108 Iterator<User> itUsers = allUsers.iterator();
109 while ( itUsers.hasNext() )
111 User user = (User) itUsers.next();
112 writeCsvRow( out, user );
118 private void writeCsvHeader( PrintWriter out )
120 boolean hasPreviousField = false;
121 for ( String heading : fields.values() )
123 if ( hasPreviousField )
127 out.print( escapeCell( heading ) );
128 hasPreviousField = true;
133 @SuppressWarnings( "unchecked" )
134 private void writeCsvRow( PrintWriter out, User user )
135 throws ReportException
139 boolean hasPreviousField = false;
140 Map<String, Object> propMap = PropertyUtils.describe( user );
141 for ( String propName : fields.keySet() )
143 Object propValue = propMap.get( propName );
145 if ( hasPreviousField )
150 if ( propValue != null )
152 out.print( escapeCell( propValue.toString() ) );
154 hasPreviousField = true;
158 catch ( IllegalAccessException e )
160 String emsg = "Unable to produce " + getName() + " report.";
161 log.error( emsg, e );
162 throw new ReportException( emsg, e );
164 catch ( InvocationTargetException e )
166 String emsg = "Unable to produce " + getName() + " report.";
167 log.error( emsg, e );
168 throw new ReportException( emsg, e );
170 catch ( NoSuchMethodException e )
172 String emsg = "Unable to produce " + getName() + " report.";
173 log.error( emsg, e );
174 throw new ReportException( emsg, e );
178 private String escapeCell( String cell )
180 return "\"" + StringEscapeUtils.escapeJava( cell ) + "\"";