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 = (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 if( endDate.equals( startDate ) )
195 Calendar cal = Calendar.getInstance();
196 cal.setTime( endDateInDF );
197 cal.set( Calendar.HOUR, 23 );
198 cal.set( Calendar.MINUTE, 59 );
199 cal.set( Calendar.SECOND, 59 );
201 endDateInDF = cal.getTime();
205 range[0] = ( page - 1 ) * rowCount;
206 range[1] = ( page * rowCount ) + 1;
208 ArchivaAuditLogsConstraint constraint = null;
209 if ( !repository.equals( ALL_REPOSITORIES ) )
212 new ArchivaAuditLogsConstraint( range, artifact, repository, AuditEvent.UPLOAD_FILE, startDateInDF, endDateInDF );
217 new ArchivaAuditLogsConstraint( range, artifact, null, AuditEvent.UPLOAD_FILE, startDateInDF, endDateInDF );
222 auditLogs = auditLogsDao.queryAuditLogs( constraint );
223 if( auditLogs.isEmpty() )
225 addActionError( "No audit logs found." );
233 headerName = HEADER_RESULTS;
236 catch ( ObjectNotFoundException e )
238 addActionError( "No audit logs found." );
241 catch ( ArchivaDatabaseException e )
243 addActionError( "Error occurred while querying audit logs." );
250 private void paginate()
252 if ( auditLogs.size() <= rowCount )
259 auditLogs.remove( rowCount );
263 request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
264 "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
268 request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
269 "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
272 prev = StringUtils.replace( prev, " ", "%20" );
273 next = StringUtils.replace( next, " ", "%20" );
276 private List<String> getObservableRepositories()
280 return userRepositories.getObservableRepositoryIds( getPrincipal() );
282 catch ( PrincipalNotFoundException e )
284 log.warn( e.getMessage(), e );
286 catch ( AccessDeniedException e )
288 log.warn( e.getMessage(), e );
290 catch ( ArchivaSecurityException e )
292 log.warn( e.getMessage(), e );
294 return Collections.emptyList();
297 public String getRepository()
302 public void setRepository( String repository )
304 this.repository = repository;
307 public List<String> getRepositories()
312 public void setRepositories( List<String> repositories )
314 this.repositories = repositories;
317 public String getGroupId()
322 public void setGroupId( String groupId )
324 this.groupId = groupId;
327 public String getArtifactId()
332 public void setArtifactId( String artifactId )
334 this.artifactId = artifactId;
337 public List<ArchivaAuditLogs> getAuditLogs()
342 public void setAuditLogs( List<ArchivaAuditLogs> auditLogs )
344 this.auditLogs = auditLogs;
347 public int getRowCount()
352 public void setRowCount( int rowCount )
354 this.rowCount = rowCount;
357 public String getStartDate()
362 public void setStartDate( String startDate )
364 this.startDate = startDate;
367 public String getEndDate()
372 public void setEndDate( String endDate )
374 this.endDate = endDate;
382 public void setPage( int page )
387 public boolean getIsLastPage()
392 public void setIsLastPage( boolean isLastPage )
394 this.isLastPage = isLastPage;
397 public String getPrev()
402 public void setPrev( String prev )
407 public String getNext()
412 public void setNext( String next )
417 public String getInitial()
422 public void setInitial( String initial )
424 this.initial = initial;
427 public String getHeaderName()
432 public void setHeaderName( String headerName )
434 this.headerName = headerName;