]> source.dussan.org Git - archiva.git/blob
dc86e8e89c6fc42e1eacd0d34cd4aead16193e19
[archiva.git] /
1 package org.apache.archiva.redback.struts2.action.admin;
2
3 /*
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.util.Arrays;
25
26 import javax.inject.Inject;
27 import javax.servlet.http.HttpServletResponse;
28
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;
38
39 import com.opensymphony.module.sitemesh.filter.PageResponseWrapper;
40 import org.springframework.context.annotation.Scope;
41 import org.springframework.stereotype.Controller;
42
43 /**
44  * ReportAction
45  *
46  * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
47  *
48  */
49 @Controller("redback-report")
50 @Scope("prototype")
51 public class ReportAction
52     extends AbstractSecurityAction
53 {
54     /**
55      *
56      */
57     @Inject
58     private ReportManager reportManager;
59
60     private String reportId;
61
62     private String reportType;
63
64     public String generate()
65     {
66         Report report;
67         try
68         {
69             report = reportManager.findReport( reportId, reportType );
70         }
71         catch ( ReportException e )
72         {
73             addActionError( getText( "cannot.get.report", Arrays.asList( ( Object ) e.getMessage() ) ) );
74             return ERROR;
75         }
76
77         HttpServletResponse response = ServletActionContext.getResponse();
78
79         // HACK: Unwrap sitemesh response. (effectively disables sitemesh)
80         if ( response instanceof PageResponseWrapper )
81         {
82             response = (HttpServletResponse) ( (PageResponseWrapper) response ).getResponse();
83         }
84
85         try
86         {
87             ByteArrayOutputStream os = new ByteArrayOutputStream();
88             report.writeReport( os );
89
90             response.reset();
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();
99
100             // Don't return a result.
101             return null;
102         }
103         catch ( ReportException e )
104         {
105             String emsg = getText( "cannot.generate.report" );
106             addActionError( emsg );
107             log.error( emsg, e );
108             return ERROR;
109         }
110         catch ( IOException e )
111         {
112             String emsg = getText( "cannot.generate.report" );
113             addActionError( emsg );
114             log.error( emsg, e );
115             return ERROR;
116         }
117     }
118
119     public SecureActionBundle initSecureActionBundle()
120         throws SecureActionException
121     {
122         SecureActionBundle bundle = new SecureActionBundle();
123         bundle.setRequiresAuthentication( true );
124         bundle.addRequiredAuthorization( RoleConstants.USER_MANAGEMENT_USER_LIST_OPERATION, Resource.GLOBAL );
125         return bundle;
126     }
127
128     public String getReportId()
129     {
130         return reportId;
131     }
132
133     public void setReportId( String reportId )
134     {
135         this.reportId = reportId;
136     }
137
138     public String getReportType()
139     {
140         return reportType;
141     }
142
143     public void setReportType( String reportType )
144     {
145         this.reportType = reportType;
146     }
147 }