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.commons.lang.StringUtils;
26 import org.apache.commons.lang.time.DateUtils;
27 import org.apache.maven.archiva.security.AccessDeniedException;
28 import org.apache.maven.archiva.security.ArchivaSecurityException;
29 import org.apache.maven.archiva.security.PrincipalNotFoundException;
30 import org.apache.maven.archiva.security.UserRepositories;
31 import org.apache.maven.archiva.web.action.PlexusActionSupport;
32 import org.apache.struts2.interceptor.ServletRequestAware;
33 import org.codehaus.redback.integration.interceptor.SecureAction;
34 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
35 import org.codehaus.redback.integration.interceptor.SecureActionException;
37 import java.util.ArrayList;
38 import java.util.Calendar;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.Date;
42 import java.util.List;
43 import javax.servlet.http.HttpServletRequest;
46 * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="viewAuditLogReport"
47 * instantiation-strategy="per-lookup"
49 public class ViewAuditLogReportAction
50 extends PlexusActionSupport
51 implements SecureAction, ServletRequestAware, Preparable
53 protected HttpServletRequest request;
58 private UserRepositories userRepositories;
60 private String repository;
62 private List<String> repositories;
64 private String groupId;
66 private String artifactId;
68 private String startDate;
70 private String endDate;
72 private int rowCount = 30;
80 protected boolean isLastPage = true;
82 private List<AuditEvent> auditLogs;
84 private static final String ALL_REPOSITORIES = "all";
86 protected int[] range = new int[2];
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"};
101 * @plexus.requirement
103 private AuditManager auditManager;
105 public SecureActionBundle getSecureActionBundle()
106 throws SecureActionException
108 SecureActionBundle bundle = new SecureActionBundle();
110 // TODO: should require this, but for now we trust in the list of repositories
111 // bundle.setRequiresAuthentication( true );
112 // bundle.addRequiredAuthorization( ArchivaRoleConstants.OPERATION_VIEW_AUDIT_LOG );
117 public void setServletRequest( HttpServletRequest request )
119 this.request = request;
122 @SuppressWarnings("unchecked")
123 public void prepare()
126 repositories = new ArrayList<String>();
127 repositories.add( ALL_REPOSITORIES );
128 List<String> repos = getManagableRepositories();
129 repositories.addAll( repos );
136 if ( Boolean.parseBoolean( initial ) )
138 headerName = HEADER_LATEST_EVENTS;
142 headerName = HEADER_RESULTS;
145 auditLogs = auditManager.getMostRecentAuditEvents( repos );
148 public String execute()
151 Date startDateInDF = null;
152 Date endDateInDF = null;
153 if ( !StringUtils.isEmpty( startDate ) )
155 startDateInDF = DateUtils.parseDate( startDate, datePatterns );
158 if ( !StringUtils.isEmpty( endDate ) )
160 endDateInDF = DateUtils.parseDate( endDate, datePatterns );
161 Calendar cal = Calendar.getInstance();
162 cal.setTime( endDateInDF );
163 cal.set( Calendar.HOUR, 23 );
164 cal.set( Calendar.MINUTE, 59 );
165 cal.set( Calendar.SECOND, 59 );
167 endDateInDF = cal.getTime();
170 range[0] = ( page - 1 ) * rowCount;
171 range[1] = ( page * rowCount ) + 1;
173 Collection<String> repos = getManagableRepositories();
174 if ( !repository.equals( ALL_REPOSITORIES ) )
176 if ( repos.contains( repository ) )
178 repos = Collections.singletonList( repository );
182 repos = Collections.emptyList();
186 if ( StringUtils.isEmpty( groupId ) && !StringUtils.isEmpty( artifactId ) )
188 // Until we store the full artifact metadata in the audit event, we can't query by these individually
189 addActionError( "If you specify an artifact ID, you must specify a group ID" );
194 String resource = null;
195 if ( !StringUtils.isEmpty( groupId ) )
197 String groupIdAsPath = groupId.replace( '.', '/' );
198 if ( StringUtils.isEmpty( artifactId ) )
200 resource = groupIdAsPath;
204 resource = groupIdAsPath + "/" + artifactId;
208 auditLogs = auditManager.getAuditEventsInRange( repos, resource, startDateInDF, endDateInDF );
210 if ( auditLogs.isEmpty() )
212 addActionError( "No audit logs found." );
220 headerName = HEADER_RESULTS;
226 private void paginate()
228 if ( auditLogs.size() <= rowCount )
235 auditLogs.remove( rowCount );
238 prev = request.getRequestURL() + "?page=" + ( page - 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
239 "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
242 next = request.getRequestURL() + "?page=" + ( page + 1 ) + "&rowCount=" + rowCount + "&groupId=" + groupId +
243 "&artifactId=" + artifactId + "&repository=" + repository + "&startDate=" + startDate + "&endDate=" +
246 prev = StringUtils.replace( prev, " ", "%20" );
247 next = StringUtils.replace( next, " ", "%20" );
250 private List<String> getManagableRepositories()
254 return userRepositories.getManagableRepositoryIds( getPrincipal() );
256 catch ( PrincipalNotFoundException e )
258 log.warn( e.getMessage(), e );
260 catch ( AccessDeniedException e )
262 log.warn( e.getMessage(), e );
264 catch ( ArchivaSecurityException e )
266 log.warn( e.getMessage(), e );
268 return Collections.emptyList();
271 public String getRepository()
276 public void setRepository( String repository )
278 this.repository = repository;
281 public List<String> getRepositories()
286 public void setRepositories( List<String> repositories )
288 this.repositories = repositories;
291 public String getGroupId()
296 public void setGroupId( String groupId )
298 this.groupId = groupId;
301 public String getArtifactId()
306 public void setArtifactId( String artifactId )
308 this.artifactId = artifactId;
311 public List<AuditEvent> getAuditLogs()
316 public int getRowCount()
321 public void setRowCount( int rowCount )
323 this.rowCount = rowCount;
326 public String getStartDate()
331 public void setStartDate( String startDate )
333 this.startDate = startDate;
336 public String getEndDate()
341 public void setEndDate( String endDate )
343 this.endDate = endDate;
351 public void setPage( int page )
356 public boolean getIsLastPage()
361 public void setIsLastPage( boolean isLastPage )
363 this.isLastPage = isLastPage;
366 public String getPrev()
371 public void setPrev( String prev )
376 public String getNext()
381 public void setNext( String next )
386 public String getInitial()
391 public void setInitial( String initial )
393 this.initial = initial;
396 public String getHeaderName()
401 public void setHeaderName( String headerName )
403 this.headerName = headerName;