1 package org.apache.archiva.redback.struts2.action.admin;
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.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.util.Arrays;
26 import javax.inject.Inject;
27 import javax.servlet.http.HttpServletResponse;
29 import org.apache.struts2.ServletActionContext;
30 import org.apache.archiva.redback.rbac.Resource;
31 import org.apache.archiva.redback.struts2.action.AbstractSecurityAction;
32 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
33 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
34 import org.apache.archiva.redback.integration.reports.Report;
35 import org.apache.archiva.redback.integration.reports.ReportException;
36 import org.apache.archiva.redback.integration.reports.ReportManager;
37 import org.apache.archiva.redback.integration.role.RoleConstants;
39 import com.opensymphony.module.sitemesh.filter.PageResponseWrapper;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.stereotype.Controller;
46 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
49 @Controller("redback-report")
51 public class ReportAction
52 extends AbstractSecurityAction
58 private ReportManager reportManager;
60 private String reportId;
62 private String reportType;
64 public String generate()
69 report = reportManager.findReport( reportId, reportType );
71 catch ( ReportException e )
73 addActionError( getText( "cannot.get.report", Arrays.asList( ( Object ) e.getMessage() ) ) );
77 HttpServletResponse response = ServletActionContext.getResponse();
79 // HACK: Unwrap sitemesh response. (effectively disables sitemesh)
80 if ( response instanceof PageResponseWrapper )
82 response = (HttpServletResponse) ( (PageResponseWrapper) response ).getResponse();
87 ByteArrayOutputStream os = new ByteArrayOutputStream();
88 report.writeReport( os );
91 response.setContentType( report.getMimeType() );
92 response.addHeader( "Content-Disposition",
93 "attachment; filename=" + report.getId() + "." + report.getType() );
94 byte bytes[] = os.toByteArray();
95 response.setContentLength( bytes.length );
96 response.getOutputStream().write( bytes, 0, bytes.length );
97 response.getOutputStream().flush();
98 response.getOutputStream().close();
100 // Don't return a result.
103 catch ( ReportException e )
105 String emsg = getText( "cannot.generate.report" );
106 addActionError( emsg );
107 log.error( emsg, e );
110 catch ( IOException e )
112 String emsg = getText( "cannot.generate.report" );
113 addActionError( emsg );
114 log.error( emsg, e );
119 public SecureActionBundle initSecureActionBundle()
120 throws SecureActionException
122 SecureActionBundle bundle = new SecureActionBundle();
123 bundle.setRequiresAuthentication( true );
124 bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_USER_LIST_OPERATION, Resource.GLOBAL );
128 public String getReportId()
133 public void setReportId( String reportId )
135 this.reportId = reportId;
138 public String getReportType()
143 public void setReportType( String reportType )
145 this.reportType = reportType;