1 package org.apache.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.archiva.web.action.AbstractActionSupport;
33 import org.apache.struts2.interceptor.ServletRequestAware;
34 import org.apache.archiva.redback.integration.interceptor.SecureAction;
35 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
36 import org.apache.archiva.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;
52 @Controller( "viewAuditLogReport" )
54 public class ViewAuditLogReportAction
55 extends AbstractActionSupport
56 implements SecureAction, ServletRequestAware, Preparable
58 protected HttpServletRequest request;
61 private UserRepositories userRepositories;
64 private AuditManager auditManager;
66 private String repository;
68 private List<String> repositories;
70 private String groupId;
72 private String artifactId;
74 private String startDate;
76 private String endDate;
78 private int rowCount = 30;
82 protected boolean isLastPage = true;
84 private List<AuditEvent> auditLogs;
86 private static final String ALL_REPOSITORIES = "all";
88 private String initial = "true";
90 private String headerName;
92 private static final String HEADER_LATEST_EVENTS = "Latest Events";
94 private static final String HEADER_RESULTS = "Results";
96 private String[] datePatterns =
97 new String[]{ "MM/dd/yy", "MM/dd/yyyy", "MMMMM/dd/yyyy", "MMMMM/dd/yy", "dd MMMMM yyyy", "dd/MM/yy",
98 "dd/MM/yyyy", "yyyy/MM/dd", "yyyy-MM-dd", "yyyy-dd-MM", "MM-dd-yyyy", "MM-dd-yy" };
102 public SecureActionBundle getSecureActionBundle()
103 throws SecureActionException
105 SecureActionBundle bundle = new SecureActionBundle();
107 // TODO: should require this, but for now we trust in the list of repositories
108 // bundle.setRequiresAuthentication( true );
109 // bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_VIEW_AUDIT_LOG );
114 public void setServletRequest( HttpServletRequest request )
116 this.request = request;
119 @SuppressWarnings( "unchecked" )
120 public void prepare()
123 repositories = new ArrayList<String>();
124 repositories.add( ALL_REPOSITORIES );
125 List<String> repos = getManagableRepositories();
126 repositories.addAll( repos );
133 if ( Boolean.parseBoolean( initial ) )
135 headerName = HEADER_LATEST_EVENTS;
139 headerName = HEADER_RESULTS;
142 RepositorySession repositorySession = repositorySessionFactory.createSession();
145 auditLogs = auditManager.getMostRecentAuditEvents( repositorySession.getRepository(), repos );
149 repositorySession.close();
153 public String execute()
156 Date startDateInDF = null;
157 Date endDateInDF = null;
158 if ( !StringUtils.isEmpty( startDate ) )
160 startDateInDF = DateUtils.parseDate( startDate, datePatterns );
163 if ( !StringUtils.isEmpty( endDate ) )
165 endDateInDF = DateUtils.parseDate( endDate, datePatterns );
166 Calendar cal = Calendar.getInstance();
167 cal.setTime( endDateInDF );
168 cal.set( Calendar.HOUR, 23 );
169 cal.set( Calendar.MINUTE, 59 );
170 cal.set( Calendar.SECOND, 59 );
172 endDateInDF = cal.getTime();
175 Collection<String> repos = getManagableRepositories();
176 if ( !repository.equals( ALL_REPOSITORIES ) )
178 if ( repos.contains( repository ) )
180 repos = Collections.singletonList( repository );
184 repos = Collections.emptyList();
188 if ( StringUtils.isEmpty( groupId ) && !StringUtils.isEmpty( artifactId ) )
190 // Until we store the full artifact metadata in the audit event, we can't query by these individually
191 addActionError( "If you specify an artifact ID, you must specify a group ID" );
196 String resource = null;
197 if ( !StringUtils.isEmpty( groupId ) )
199 String groupIdAsPath = groupId.replace( '.', '/' );
200 if ( StringUtils.isEmpty( artifactId ) )
202 resource = groupIdAsPath;
206 resource = groupIdAsPath + "/" + artifactId;
210 RepositorySession repositorySession = repositorySessionFactory.createSession();
214 auditManager.getAuditEventsInRange( repositorySession.getRepository(), repos, resource, startDateInDF,
219 repositorySession.close();
222 headerName = HEADER_RESULTS;
224 if ( auditLogs.isEmpty() )
226 addActionError( "No audit logs found." );
237 private String paginate()
239 int rowCount = getRowCount();
240 int extraPage = ( auditLogs.size() % rowCount ) != 0 ? 1 : 0;
241 int totalPages = ( auditLogs.size() / rowCount ) + extraPage;
243 int currentPage = getPage();
244 if ( currentPage > totalPages )
247 "Error encountered while generating report :: The requested page exceeds the total number of pages." );
251 if ( currentPage == totalPages )
260 int start = rowCount * ( currentPage - 1 );
261 int end = ( start + rowCount ) - 1;
263 if ( end >= auditLogs.size() )
265 end = auditLogs.size() - 1;
268 auditLogs = auditLogs.subList( start, end + 1 );
273 private List<String> getManagableRepositories()
277 return userRepositories.getManagableRepositoryIds( getPrincipal() );
279 catch ( PrincipalNotFoundException e )
281 log.warn( e.getMessage(), e );
283 catch ( AccessDeniedException e )
285 log.warn( e.getMessage(), e );
287 catch ( ArchivaSecurityException e )
289 log.warn( e.getMessage(), e );
291 return Collections.emptyList();
294 public String getRepository()
299 public void setRepository( String repository )
301 this.repository = repository;
304 public List<String> getRepositories()
309 public void setRepositories( List<String> repositories )
311 this.repositories = repositories;
314 public String getGroupId()
319 public void setGroupId( String groupId )
321 this.groupId = groupId;
324 public String getArtifactId()
329 public void setArtifactId( String artifactId )
331 this.artifactId = artifactId;
334 public List<AuditEvent> getAuditLogs()
339 public int getRowCount()
344 public void setRowCount( int rowCount )
346 this.rowCount = rowCount;
349 public String getStartDate()
354 public void setStartDate( String startDate )
356 this.startDate = startDate;
359 public String getEndDate()
364 public void setEndDate( String endDate )
366 this.endDate = endDate;
374 public void setPage( int page )
379 public boolean getIsLastPage()
384 public void setIsLastPage( boolean isLastPage )
386 this.isLastPage = isLastPage;
389 public String getInitial()
394 public void setInitial( String initial )
396 this.initial = initial;
399 public String getHeaderName()
404 public void setHeaderName( String headerName )
406 this.headerName = headerName;