1 package org.apache.maven.archiva.web.action.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 com.opensymphony.xwork2.Preparable;
23 import org.apache.archiva.audit.AuditEvent;
24 import org.apache.archiva.audit.AuditManager;
25 import org.apache.archiva.metadata.repository.RepositorySession;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.lang.time.DateUtils;
28 import org.apache.archiva.security.AccessDeniedException;
29 import org.apache.archiva.security.ArchivaSecurityException;
30 import org.apache.archiva.security.PrincipalNotFoundException;
31 import org.apache.archiva.security.UserRepositories;
32 import org.apache.maven.archiva.web.action.AbstractActionSupport;
33 import org.apache.struts2.interceptor.ServletRequestAware;
34 import org.codehaus.redback.integration.interceptor.SecureAction;
35 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
36 import org.codehaus.redback.integration.interceptor.SecureActionException;
37 import org.springframework.context.annotation.Scope;
38 import org.springframework.stereotype.Controller;
40 import javax.inject.Inject;
41 import javax.servlet.http.HttpServletRequest;
42 import java.util.ArrayList;
43 import java.util.Calendar;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.Date;
47 import java.util.List;
50 * plexus.component role="com.opensymphony.xwork2.Action" role-hint="viewAuditLogReport"
51 * instantiation-strategy="per-lookup"
53 @Controller( "viewAuditLogReport" )
55 public class ViewAuditLogReportAction
56 extends AbstractActionSupport
57 implements SecureAction, ServletRequestAware, Preparable
59 protected HttpServletRequest request;
65 private UserRepositories userRepositories;
67 private String repository;
69 private List<String> repositories;
71 private String groupId;
73 private String artifactId;
75 private String startDate;
77 private String endDate;
79 private int rowCount = 30;
83 protected boolean isLastPage = true;
85 private List<AuditEvent> auditLogs;
87 private static final String ALL_REPOSITORIES = "all";
89 private String initial = "true";
91 private String headerName;
93 private static final String HEADER_LATEST_EVENTS = "Latest Events";
95 private static final String HEADER_RESULTS = "Results";
97 private String[] datePatterns =
98 new String[]{ "MM/dd/yy", "MM/dd/yyyy", "MMMMM/dd/yyyy", "MMMMM/dd/yy", "dd MMMMM yyyy", "dd/MM/yy",
99 "dd/MM/yyyy", "yyyy/MM/dd", "yyyy-MM-dd", "yyyy-dd-MM", "MM-dd-yyyy", "MM-dd-yy" };
105 private AuditManager auditManager;
107 public SecureActionBundle getSecureActionBundle()
108 throws SecureActionException
110 SecureActionBundle bundle = new SecureActionBundle();
112 // TODO: should require this, but for now we trust in the list of repositories
113 // bundle.setRequiresAuthentication( true );
114 // bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_VIEW_AUDIT_LOG );
119 public void setServletRequest( HttpServletRequest request )
121 this.request = request;
124 @SuppressWarnings( "unchecked" )
125 public void prepare()
128 repositories = new ArrayList<String>();
129 repositories.add( ALL_REPOSITORIES );
130 List<String> repos = getManagableRepositories();
131 repositories.addAll( repos );
138 if ( Boolean.parseBoolean( initial ) )
140 headerName = HEADER_LATEST_EVENTS;
144 headerName = HEADER_RESULTS;
147 RepositorySession repositorySession = repositorySessionFactory.createSession();
150 auditLogs = auditManager.getMostRecentAuditEvents( repositorySession.getRepository(), repos );
154 repositorySession.close();
158 public String execute()
161 Date startDateInDF = null;
162 Date endDateInDF = null;
163 if ( !StringUtils.isEmpty( startDate ) )
165 startDateInDF = DateUtils.parseDate( startDate, datePatterns );
168 if ( !StringUtils.isEmpty( endDate ) )
170 endDateInDF = DateUtils.parseDate( endDate, datePatterns );
171 Calendar cal = Calendar.getInstance();
172 cal.setTime( endDateInDF );
173 cal.set( Calendar.HOUR, 23 );
174 cal.set( Calendar.MINUTE, 59 );
175 cal.set( Calendar.SECOND, 59 );
177 endDateInDF = cal.getTime();
180 Collection<String> repos = getManagableRepositories();
181 if ( !repository.equals( ALL_REPOSITORIES ) )
183 if ( repos.contains( repository ) )
185 repos = Collections.singletonList( repository );
189 repos = Collections.emptyList();
193 if ( StringUtils.isEmpty( groupId ) && !StringUtils.isEmpty( artifactId ) )
195 // Until we store the full artifact metadata in the audit event, we can't query by these individually
196 addActionError( "If you specify an artifact ID, you must specify a group ID" );
201 String resource = null;
202 if ( !StringUtils.isEmpty( groupId ) )
204 String groupIdAsPath = groupId.replace( '.', '/' );
205 if ( StringUtils.isEmpty( artifactId ) )
207 resource = groupIdAsPath;
211 resource = groupIdAsPath + "/" + artifactId;
215 RepositorySession repositorySession = repositorySessionFactory.createSession();
219 auditManager.getAuditEventsInRange( repositorySession.getRepository(), repos, resource, startDateInDF,
224 repositorySession.close();
227 headerName = HEADER_RESULTS;
229 if ( auditLogs.isEmpty() )
231 addActionError( "No audit logs found." );
242 private String paginate()
244 int rowCount = getRowCount();
245 int extraPage = ( auditLogs.size() % rowCount ) != 0 ? 1 : 0;
246 int totalPages = ( auditLogs.size() / rowCount ) + extraPage;
248 int currentPage = getPage();
249 if ( currentPage > totalPages )
252 "Error encountered while generating report :: The requested page exceeds the total number of pages." );
256 if ( currentPage == totalPages )
265 int start = rowCount * ( currentPage - 1 );
266 int end = ( start + rowCount ) - 1;
268 if ( end >= auditLogs.size() )
270 end = auditLogs.size() - 1;
273 auditLogs = auditLogs.subList( start, end + 1 );
278 private List<String> getManagableRepositories()
282 return userRepositories.getManagableRepositoryIds( getPrincipal() );
284 catch ( PrincipalNotFoundException e )
286 log.warn( e.getMessage(), e );
288 catch ( AccessDeniedException e )
290 log.warn( e.getMessage(), e );
292 catch ( ArchivaSecurityException e )
294 log.warn( e.getMessage(), e );
296 return Collections.emptyList();
299 public String getRepository()
304 public void setRepository( String repository )
306 this.repository = repository;
309 public List<String> getRepositories()
314 public void setRepositories( List<String> repositories )
316 this.repositories = repositories;
319 public String getGroupId()
324 public void setGroupId( String groupId )
326 this.groupId = groupId;
329 public String getArtifactId()
334 public void setArtifactId( String artifactId )
336 this.artifactId = artifactId;
339 public List<AuditEvent> getAuditLogs()
344 public int getRowCount()
349 public void setRowCount( int rowCount )
351 this.rowCount = rowCount;
354 public String getStartDate()
359 public void setStartDate( String startDate )
361 this.startDate = startDate;
364 public String getEndDate()
369 public void setEndDate( String endDate )
371 this.endDate = endDate;
379 public void setPage( int page )
384 public boolean getIsLastPage()
389 public void setIsLastPage( boolean isLastPage )
391 this.isLastPage = isLastPage;
394 public String getInitial()
399 public void setInitial( String initial )
401 this.initial = initial;
404 public String getHeaderName()
409 public void setHeaderName( String headerName )
411 this.headerName = headerName;