]> source.dussan.org Git - archiva.git/blob
37c6dafeb41289676864cfc91fc34fd3d8562f60
[archiva.git] /
1 package org.apache.archiva.webapp.ui.services.api;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
22 import org.apache.archiva.webapp.ui.services.model.FileMetadata;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.BooleanUtils;
25 import org.apache.commons.lang.SystemUtils;
26 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
27 import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Service;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.ws.rs.core.Context;
35 import javax.ws.rs.core.Response;
36 import java.io.File;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39
40 /**
41  * @author Olivier Lamy
42  */
43 @Service( "fileUploadService#rest" )
44 public class DefaultFileUploadService
45     implements FileUploadService
46 {
47     private Logger log = LoggerFactory.getLogger( getClass() );
48
49     @Context
50     private HttpServletRequest httpServletRequest;
51
52     @Context
53     private HttpServletResponse httpServletResponse;
54
55     public FileMetadata post( String groupId, String artifactId, String version, String packaging, String classifier,
56                               String repositoryId, String generatePom )
57         throws ArchivaRestServiceException
58     {
59         log.info( "uploading file:" + groupId + ":" + artifactId + ":" + version );
60         try
61         {
62             File file = File.createTempFile( "upload-artifact", "tmp" );
63             file.deleteOnExit();
64             IOUtils.copy( httpServletRequest.getInputStream(), new FileOutputStream( file ) );
65             FileMetadata fileMetadata = new FileMetadata( "thefile", file.length(), "theurl" );
66             fileMetadata.setDeleteUrl( file.getName() );
67             return fileMetadata;
68         }
69         catch ( IOException e )
70         {
71             throw new ArchivaRestServiceException( e.getMessage(),
72                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
73         }
74     }
75
76     public FileMetadata post( MultipartBody multipartBody )
77         throws ArchivaRestServiceException
78     {
79
80         try
81         {
82             String groupId =
83                 IOUtils.toString( multipartBody.getAttachment( "groupId" ).getDataHandler().getInputStream() );
84             String artifactId =
85                 IOUtils.toString( multipartBody.getAttachment( "artifactId" ).getDataHandler().getInputStream() );
86             String version =
87                 IOUtils.toString( multipartBody.getAttachment( "version" ).getDataHandler().getInputStream() );
88             String packaging =
89                 IOUtils.toString( multipartBody.getAttachment( "packaging" ).getDataHandler().getInputStream() );
90
91             boolean generatePom = BooleanUtils.toBoolean(
92                 IOUtils.toString( multipartBody.getAttachment( "generatePom" ).getDataHandler().getInputStream() ) );
93
94             String classifier =
95                 IOUtils.toString( multipartBody.getAttachment( "classifier" ).getDataHandler().getInputStream() );
96
97             log.info( "uploading file:" + groupId + ":" + artifactId + ":" + version );
98             Attachment file = multipartBody.getAttachment( "files[]" );
99             File tmpFile = File.createTempFile( "upload-artifact", "tmp" );
100             tmpFile.deleteOnExit();
101             IOUtils.copy( file.getDataHandler().getInputStream(), new FileOutputStream( tmpFile ) );
102             FileMetadata fileMetadata = new FileMetadata( "thefile", tmpFile.length(), "theurl" );
103             fileMetadata.setDeleteUrl( tmpFile.getName() );
104             return fileMetadata;
105         }
106         catch ( IOException e )
107         {
108             throw new ArchivaRestServiceException( e.getMessage(),
109                                                    Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
110         }
111
112     }
113
114     public Boolean deleteFile( String fileName )
115         throws ArchivaRestServiceException
116     {
117         File file = new File( SystemUtils.getJavaIoTmpDir(), fileName );
118         log.debug( "delete file:{},exists:{}", file.getPath(), file.exists() );
119         if ( file.exists() )
120         {
121             return file.delete();
122         }
123         return Boolean.FALSE;
124     }
125 }