1 package org.apache.maven.continuum.web.action;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.continuum.builder.distributed.manager.DistributedBuildManager;
23 import org.apache.maven.continuum.ContinuumException;
24 import org.apache.maven.continuum.model.project.Project;
25 import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
26 import org.apache.maven.continuum.web.util.WorkingCopyContentGenerator;
27 import org.apache.struts2.ServletActionContext;
28 import org.apache.struts2.views.util.UrlHelper;
29 import org.codehaus.plexus.util.StringUtils;
30
31 import java.io.ByteArrayInputStream;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.io.InputStream;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import javax.activation.MimetypesFileTypeMap;
40
41
42
43
44
45
46 public class WorkingCopyAction
47 extends ContinuumActionSupport
48 {
49
50
51
52 private WorkingCopyContentGenerator generator;
53
54
55
56
57 private DistributedBuildManager distributedBuildManager;
58
59 private Project project;
60
61 private int projectId;
62
63 private String userDirectory;
64
65 private String currentFile;
66
67 private String currentFileContent;
68
69 private String output;
70
71 private String projectName;
72
73 private File downloadFile;
74
75 private String mimeType = "application/octet-stream";
76
77 private static final String FILE_SEPARATOR = System.getProperty( "file.separator" );
78
79 private String projectGroupName = "";
80
81 private String downloadFileName = "";
82
83 private String downloadFileLength = "";
84
85 private InputStream downloadFileInputStream;
86
87 public String execute()
88 throws ContinuumException
89 {
90 try
91 {
92 checkViewProjectGroupAuthorization( getProjectGroupName() );
93 }
94 catch ( AuthorizationRequiredException e )
95 {
96 return REQUIRES_AUTHORIZATION;
97 }
98
99 if ( "release.properties".equals( currentFile ) )
100 {
101 throw new ContinuumException( "release.properties is not accessible." );
102 }
103
104 project = getContinuum().getProject( projectId );
105
106 projectName = project.getName();
107
108 HashMap<String, Object> params = new HashMap<String, Object>();
109
110 params.put( "projectId", projectId );
111
112 params.put( "projectName", projectName );
113
114 String baseUrl = UrlHelper.buildUrl( "/workingCopy.action", ServletActionContext.getRequest(),
115 ServletActionContext.getResponse(), params );
116
117 String imagesBaseUrl = UrlHelper.buildUrl( "/images/", ServletActionContext.getRequest(),
118 ServletActionContext.getResponse(), params );
119
120 imagesBaseUrl = imagesBaseUrl.substring( 0, imagesBaseUrl.indexOf( "/images/" ) + "/images/".length() );
121
122 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
123 {
124 output = distributedBuildManager.generateWorkingCopyContent( projectId, userDirectory, baseUrl,
125 imagesBaseUrl );
126
127 if ( currentFile != null && !currentFile.equals( "" ) )
128 {
129 Map<String, Object> projectFile = distributedBuildManager.getFileContent( projectId, userDirectory,
130 currentFile );
131
132 if ( projectFile == null )
133 {
134 currentFileContent = "";
135 }
136 else
137 {
138 downloadFileInputStream = new ByteArrayInputStream( (byte[]) projectFile.get( "downloadFile" ) );
139 downloadFileLength = (String) projectFile.get( "downloadFileLength" );
140 downloadFileName = (String) projectFile.get( "downloadFileName" );
141 currentFileContent = (String) projectFile.get( "fileContent" );
142 mimeType = (String) projectFile.get( "mimeType" );
143
144 if ( (Boolean) projectFile.get( "isStream" ) )
145 {
146 return "stream";
147 }
148 }
149 }
150 else
151 {
152 currentFileContent = "";
153 }
154 }
155 else
156 {
157 List<File> files = getContinuum().getFiles( projectId, userDirectory );
158
159 output = generator.generate( files, baseUrl, imagesBaseUrl, getContinuum().getWorkingDirectory(
160 projectId ) );
161
162 if ( currentFile != null && !currentFile.equals( "" ) )
163 {
164 String dir;
165
166
167 MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
168 mimeTypesMap.addMimeTypes( "application/java-archive jar war ear" );
169 mimeTypesMap.addMimeTypes( "application/java-class class" );
170 mimeTypesMap.addMimeTypes( "image/png png" );
171
172 if ( FILE_SEPARATOR.equals( userDirectory ) )
173 {
174 dir = userDirectory;
175 }
176 else
177 {
178 dir = FILE_SEPARATOR + userDirectory + FILE_SEPARATOR;
179 }
180
181 downloadFile = new File( getContinuum().getWorkingDirectory( projectId ) + dir + currentFile );
182 mimeType = mimeTypesMap.getContentType( downloadFile );
183 downloadFileLength = Long.toString( downloadFile.length() );
184 downloadFileName = downloadFile.getName();
185
186 if ( ( mimeType.indexOf( "image" ) >= 0 ) || ( mimeType.indexOf( "java-archive" ) >= 0 ) ||
187 ( mimeType.indexOf( "java-class" ) >= 0 ) || ( downloadFile.length() > 100000 ) )
188 {
189 return "stream";
190 }
191
192 currentFileContent = getContinuum().getFileContent( projectId, userDirectory, currentFile );
193 }
194 else
195 {
196 currentFileContent = "";
197 }
198 }
199
200 return SUCCESS;
201 }
202
203 public int getProjectId()
204 {
205 return projectId;
206 }
207
208 public void setProjectId( int projectId )
209 {
210 this.projectId = projectId;
211 }
212
213 public String getProjectName()
214 {
215 return projectName;
216 }
217
218 public String getUserDirectory()
219 {
220 return userDirectory;
221 }
222
223 public void setUserDirectory( String userDirectory )
224 {
225 this.userDirectory = userDirectory;
226 }
227
228 public void setFile( String currentFile )
229 {
230 this.currentFile = currentFile;
231 }
232
233 public String getFile()
234 {
235 return currentFile;
236 }
237
238 public String getOutput()
239 {
240 return output;
241 }
242
243 public String getFileContent()
244 {
245 return currentFileContent;
246 }
247
248
249 public InputStream getInputStream()
250 throws ContinuumException
251 {
252 if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
253 {
254 return downloadFileInputStream;
255 }
256 else
257 {
258 FileInputStream fis;
259 try
260 {
261 fis = new FileInputStream( downloadFile );
262 }
263 catch ( FileNotFoundException fne )
264 {
265 throw new ContinuumException( "Error accessing file.", fne );
266 }
267
268 return fis;
269 }
270 }
271
272 public String getFileLength()
273 {
274 return downloadFileLength;
275 }
276
277 public String getDownloadFilename()
278 {
279 return downloadFileName;
280 }
281
282 public String getMimeType()
283 {
284 return this.mimeType;
285 }
286
287 public Project getProject()
288 {
289 return project;
290 }
291
292 public String getProjectGroupName()
293 throws ContinuumException
294 {
295 if ( StringUtils.isEmpty( projectGroupName ) )
296 {
297 projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
298 }
299
300 return projectGroupName;
301 }
302 }