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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.InputStream;
26  import java.util.HashMap;
27  import java.util.List;
28  
29  import javax.activation.MimetypesFileTypeMap;
30  
31  import org.apache.continuum.builder.distributed.manager.DistributedBuildManager;
32  import org.apache.maven.continuum.ContinuumException;
33  import org.apache.maven.continuum.model.project.Project;
34  import org.apache.maven.continuum.web.exception.AuthorizationRequiredException;
35  import org.apache.maven.continuum.web.util.WorkingCopyContentGenerator;
36  import org.apache.struts2.ServletActionContext;
37  import org.apache.struts2.views.util.UrlHelper;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  /**
41   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
42   * @version $Id: WorkingCopyAction.java 765340 2009-04-15 20:22:00Z evenisse $
43   * @plexus.component role="com.opensymphony.xwork2.Action" role-hint="workingCopy"
44   */
45  public class WorkingCopyAction
46      extends ContinuumActionSupport
47  {
48      /**
49       * @plexus.requirement
50       */
51      private WorkingCopyContentGenerator generator;
52  
53      /**
54       * @plexus.requirement
55       */
56      private DistributedBuildManager distributedBuildManager;
57  
58      private Project project;
59  
60      private int projectId;
61  
62      private String userDirectory;
63  
64      private String currentFile;
65  
66      private String currentFileContent;
67  
68      private String output;
69  
70      private String projectName;
71  
72      private File downloadFile;
73  
74      private String mimeType = "application/octet-stream";
75  
76      private static final String FILE_SEPARATOR = System.getProperty( "file.separator" );
77  
78      private String projectGroupName = "";
79  
80      public String execute()
81          throws ContinuumException
82      {
83          try
84          {
85              checkViewProjectGroupAuthorization( getProjectGroupName() );
86          }
87          catch ( AuthorizationRequiredException e )
88          {
89              return REQUIRES_AUTHORIZATION;
90          }
91  
92          if ( "release.properties".equals( currentFile ) )
93          {
94              throw new ContinuumException( "release.properties is not accessible." );
95          }
96  
97          project = getContinuum().getProject( projectId );
98  
99          projectName = project.getName();
100 
101         HashMap<String, Object> params = new HashMap<String, Object>();
102 
103         params.put( "projectId", projectId );
104 
105         params.put( "projectName", projectName );
106 
107         String baseUrl = UrlHelper.buildUrl( "/workingCopy.action", ServletActionContext.getRequest(),
108                                              ServletActionContext.getResponse(), params );
109 
110         String imagesBaseUrl =
111             UrlHelper.buildUrl( "/images/", ServletActionContext.getRequest(), ServletActionContext.getResponse(),
112                                 params );
113 
114         imagesBaseUrl = imagesBaseUrl.substring( 0, imagesBaseUrl.indexOf( "/images/" ) + "/images/".length() );
115 
116         if ( getContinuum().getConfiguration().isDistributedBuildEnabled() )
117         {
118             output =
119                 distributedBuildManager.generateWorkingCopyContent( projectId, userDirectory, baseUrl, imagesBaseUrl );
120 
121             if ( currentFile != null && !currentFile.equals( "" ) )
122             {
123                 currentFileContent = distributedBuildManager.getFileContent( projectId, userDirectory, currentFile );
124             }
125             else
126             {
127                 currentFileContent = "";
128             }
129         }
130         else
131         {
132             List<File> files = getContinuum().getFiles( projectId, userDirectory );
133 
134             output =
135                 generator.generate( files, baseUrl, imagesBaseUrl, getContinuum().getWorkingDirectory( projectId ) );
136 
137             if ( currentFile != null && !currentFile.equals( "" ) )
138             {
139                 String dir;
140 
141                 //TODO: maybe create a plexus component for this so that additional mimetypes can be easily added
142                 MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
143                 mimeTypesMap.addMimeTypes( "application/java-archive jar war ear" );
144                 mimeTypesMap.addMimeTypes( "application/java-class class" );
145                 mimeTypesMap.addMimeTypes( "image/png png" );
146 
147                 if ( FILE_SEPARATOR.equals( userDirectory ) )
148                 {
149                     dir = userDirectory;
150                 }
151                 else
152                 {
153                     dir = FILE_SEPARATOR + userDirectory + FILE_SEPARATOR;
154                 }
155 
156                 downloadFile = new File( getContinuum().getWorkingDirectory( projectId ) + dir + currentFile );
157                 mimeType = mimeTypesMap.getContentType( downloadFile );
158 
159                 if ( ( mimeType.indexOf( "image" ) >= 0 ) || ( mimeType.indexOf( "java-archive" ) >= 0 ) ||
160                     ( mimeType.indexOf( "java-class" ) >= 0 ) || ( downloadFile.length() > 100000 ) )
161                 {
162                     return "stream";
163                 }
164 
165                 currentFileContent = getContinuum().getFileContent( projectId, userDirectory, currentFile );
166             }
167             else
168             {
169                 currentFileContent = "";
170             }
171         }
172 
173         return SUCCESS;
174     }
175 
176     public int getProjectId()
177     {
178         return projectId;
179     }
180 
181     public void setProjectId( int projectId )
182     {
183         this.projectId = projectId;
184     }
185 
186     public String getProjectName()
187     {
188         return projectName;
189     }
190 
191     public String getUserDirectory()
192     {
193         return userDirectory;
194     }
195 
196     public void setUserDirectory( String userDirectory )
197     {
198         this.userDirectory = userDirectory;
199     }
200 
201     public void setFile( String currentFile )
202     {
203         this.currentFile = currentFile;
204     }
205 
206     public String getFile()
207     {
208         return currentFile;
209     }
210 
211     public String getOutput()
212     {
213         return output;
214     }
215 
216     public String getFileContent()
217     {
218         return currentFileContent;
219     }
220 
221 
222     public InputStream getInputStream()
223         throws ContinuumException
224     {
225         FileInputStream fis;
226         try
227         {
228             fis = new FileInputStream( downloadFile );
229         }
230         catch ( FileNotFoundException fne )
231         {
232             throw new ContinuumException( "Error accessing file.", fne );
233         }
234 
235         return fis;
236     }
237 
238     public String getFileLength()
239     {
240         return Long.toString( downloadFile.length() );
241     }
242 
243     public String getDownloadFilename()
244     {
245         return downloadFile.getName();
246     }
247 
248     public String getMimeType()
249     {
250         return this.mimeType;
251     }
252 
253     public Project getProject()
254     {
255         return project;
256     }
257 
258     public String getProjectGroupName()
259         throws ContinuumException
260     {
261         if ( StringUtils.isEmpty( projectGroupName ) )
262         {
263             projectGroupName = getContinuum().getProjectGroupByProjectId( projectId ).getName();
264         }
265 
266         return projectGroupName;
267     }
268 }