View Javadoc

1   package org.apache.maven.continuum.web.action;
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 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   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
43   * @version $Id: WorkingCopyAction.java 1372260 2012-08-13 04:29:09Z brett $
44   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="workingCopy"
45   */
46  public class WorkingCopyAction
47      extends ContinuumActionSupport
48  {
49      /**
50       * @plexus.requirement
51       */
52      private WorkingCopyContentGenerator generator;
53  
54      /**
55       * @plexus.requirement
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                 //TODO: maybe create a plexus component for this so that additional mimetypes can be easily added
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 }