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 java.util.ArrayList;
23 import java.util.Calendar;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.List;
28 import javax.servlet.http.HttpServletRequest;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.commons.lang.time.DateUtils;
32 import org.apache.maven.archiva.database.ArchivaAuditLogsDao;
33 import org.apache.maven.archiva.database.ArchivaDAO;
34 import org.apache.maven.archiva.database.ArchivaDatabaseException;
35 import org.apache.maven.archiva.database.ObjectNotFoundException;
36 import org.apache.maven.archiva.database.SimpleConstraint;
37 import org.apache.maven.archiva.database.constraints.ArchivaAuditLogsConstraint;
38 import org.apache.maven.archiva.database.constraints.MostRecentArchivaAuditLogsConstraint;
39 import org.apache.maven.archiva.model.ArchivaAuditLogs;
40 import org.apache.maven.archiva.repository.audit.AuditEvent;
41 import org.apache.maven.archiva.security.AccessDeniedException;
42 import org.apache.maven.archiva.security.ArchivaSecurityException;
43 import org.apache.maven.archiva.security.PrincipalNotFoundException;
44 import org.apache.maven.archiva.security.UserRepositories;
45 import org.apache.maven.archiva.web.action.PlexusActionSupport;
46 import org.apache.struts2.interceptor.ServletRequestAware;
47 import org.codehaus.redback.integration.interceptor.SecureAction;
48 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
49 import org.codehaus.redback.integration.interceptor.SecureActionException;
51 import com.opensymphony.xwork2.Preparable;
54 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="viewAuditLogReport"
55 * instantiation-strategy="per-lookup"
57 public class ViewAuditLogReportAction
58 extends PlexusActionSupport
59 implements SecureAction, ServletRequestAware, Preparable
61 protected HttpServletRequest request;
66 private UserRepositories userRepositories;
69 * @plexus.requirement role-hint="jdo"
71 private ArchivaAuditLogsDao auditLogsDao;
74 * @plexus.requirement role-hint="jdo"
76 private ArchivaDAO dao;
78 private String repository;
80 private List<String> repositories;
82 private String groupId;
84 private String artifactId;
86 private String startDate;
88 private String endDate;
90 private int rowCount = 30;
98 protected boolean isLastPage = true;
100 private List<ArchivaAuditLogs> auditLogs;
102 private static final String ALL_REPOSITORIES = "all";
104 protected int[] range = new int[2];
106 private String initial = "true";
108 private String headerName;
110 private static final String HEADER_LATEST_EVENTS = "Latest Events";
112 private static final String HEADER_RESULTS = "Results";
114 private String[] datePatterns = new String[] { "MM/dd/yy", "MM/dd/yyyy", "MMMMM/dd/yyyy", "MMMMM/dd/yy",
115 "dd MMMMM yyyy", "dd/MM/yy", "dd/MM/yyyy", "yyyy/MM/dd", "yyyy-MM-dd", "yyyy-dd-MM", "MM-dd-yyyy",
118 public SecureActionBundle getSecureActionBundle()
119 throws SecureActionException
124 public void setServletRequest( HttpServletRequest request )
126 this.request = request;
129 @SuppressWarnings( "unchecked" )
130 public void prepare()
133 repositories = new ArrayList<String>();
134 repositories.add( ALL_REPOSITORIES );
135 repositories.addAll( getObservableRepositories() );
142 if( Boolean.parseBoolean( initial ) )
144 headerName = HEADER_LATEST_EVENTS;
148 headerName = HEADER_RESULTS;
151 SimpleConstraint constraint = new MostRecentArchivaAuditLogsConstraint();
152 auditLogs = filterLogs( (List<ArchivaAuditLogs>) dao.query( constraint ) );
155 public String execute()
159 String artifact = "";
161 if ( groupId != null && !"".equals( groupId.trim() ) )
163 artifact = groupId + ( ( artifactId != null && !"".equals( artifactId.trim() ) ) ? ( "/" + artifactId + "/%" ) : "%" );
167 artifact = ( artifactId != null && !"".equals( artifactId.trim() ) ) ? ( "%" + artifactId + "%" ) : "";
170 Date startDateInDF = null;
171 Date endDateInDF = null;
172 if ( startDate == null || "".equals( startDate ) )
174 Calendar cal = Calendar.getInstance();
175 cal.set( Calendar.HOUR, 0 );
176 cal.set( Calendar.MINUTE, 0 );
177 cal.set( Calendar.SECOND, 0 );
179 startDateInDF = cal.getTime();
183 startDateInDF = DateUtils.parseDate( startDate, datePatterns );
186 if ( endDate == null || "".equals( endDate ) )
188 endDateInDF = Calendar.getInstance().getTime();
192 endDateInDF = DateUtils.parseDate( endDate, datePatterns );
193 Calendar cal = Calendar.getInstance();
194 cal.setTime( endDateInDF );
195 cal.set( Calendar.HOUR, 23 );
196 cal.set( Calendar.MINUTE, 59 );
197 cal.set( Calendar.SECOND, 59 );
199 endDateInDF = cal.getTime();
202 range[0] = ( page - 1 ) * rowCount;
203 range[1] = ( page * rowCount ) + 1;
205 ArchivaAuditLogsConstraint constraint = null;
206 if ( !repository.equals( ALL_REPOSITORIES ) )
209 new ArchivaAuditLogsConstraint( range, artifact, repository, AuditEvent.UPLOAD_FILE, startDateInDF, endDateInDF );
214 new ArchivaAuditLogsConstraint( range, artifact, null, AuditEvent.UPLOAD_FILE, startDateInDF, endDateInDF );
219 auditLogs = filterLogs( auditLogsDao.queryAuditLogs( constraint ) );
221 if( auditLogs.isEmpty() )
223 addActionError( "No audit logs found." );
231 headerName = HEADER_RESULTS;
234 catch ( ObjectNotFoundException e )
236 addActionError( "No audit logs found." );
239 catch ( ArchivaDatabaseException e )
241 addActionError( "Error occurred while querying audit logs." );
248 private List<ArchivaAuditLogs> filterLogs( List<ArchivaAuditLogs> auditLogs )
250 List<String> observableRepos = getManageableRepositories();
251 List<ArchivaAuditLogs> filteredAuditLogs = new ArrayList<ArchivaAuditLogs>();
253 if( auditLogs != null )
255 for( ArchivaAuditLogs auditLog : auditLogs )
257 if( observableRepos.contains( auditLog.getRepositoryId() ) )
259 filteredAuditLogs.add( auditLog );
264 return filteredAuditLogs;
267 private void paginate()
269 if ( auditLogs.size() <= rowCount )
276 auditLogs.remove( rowCount );
280 request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
281 "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
285 request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
286 "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
289 prev = StringUtils.replace( prev, " ", "%20" );
290 next = StringUtils.replace( next, " ", "%20" );
293 private List<String> getManageableRepositories()
297 return userRepositories.getManagableRepositoryIds( getPrincipal() );
299 catch ( PrincipalNotFoundException e )
301 log.warn( e.getMessage(), e );
303 catch ( AccessDeniedException e )
305 log.warn( e.getMessage(), e );
307 catch ( ArchivaSecurityException e )
309 log.warn( e.getMessage(), e );
311 return Collections.emptyList();
314 private List<String> getObservableRepositories()
318 return userRepositories.getObservableRepositoryIds( getPrincipal() );
320 catch ( PrincipalNotFoundException e )
322 log.warn( e.getMessage(), e );
324 catch ( AccessDeniedException e )
326 log.warn( e.getMessage(), e );
328 catch ( ArchivaSecurityException e )
330 log.warn( e.getMessage(), e );
332 return Collections.emptyList();
335 public String getRepository()
340 public void setRepository( String repository )
342 this.repository = repository;
345 public List<String> getRepositories()
350 public void setRepositories( List<String> repositories )
352 this.repositories = repositories;
355 public String getGroupId()
360 public void setGroupId( String groupId )
362 this.groupId = groupId;
365 public String getArtifactId()
370 public void setArtifactId( String artifactId )
372 this.artifactId = artifactId;
375 public List<ArchivaAuditLogs> getAuditLogs()
380 public void setAuditLogs( List<ArchivaAuditLogs> auditLogs )
382 this.auditLogs = auditLogs;
385 public int getRowCount()
390 public void setRowCount( int rowCount )
392 this.rowCount = rowCount;
395 public String getStartDate()
400 public void setStartDate( String startDate )
402 this.startDate = startDate;
405 public String getEndDate()
410 public void setEndDate( String endDate )
412 this.endDate = endDate;
420 public void setPage( int page )
425 public boolean getIsLastPage()
430 public void setIsLastPage( boolean isLastPage )
432 this.isLastPage = isLastPage;
435 public String getPrev()
440 public void setPrev( String prev )
445 public String getNext()
450 public void setNext( String next )
455 public String getInitial()
460 public void setInitial( String initial )
462 this.initial = initial;
465 public String getHeaderName()
470 public void setHeaderName( String headerName )
472 this.headerName = headerName;