1 package org.apache.archiva.webapp.ui.services.api;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
41 * @author Olivier Lamy
43 @Service( "fileUploadService#rest" )
44 public class DefaultFileUploadService
45 implements FileUploadService
47 private Logger log = LoggerFactory.getLogger( getClass() );
50 private HttpServletRequest httpServletRequest;
53 private HttpServletResponse httpServletResponse;
55 public FileMetadata post( String groupId, String artifactId, String version, String packaging, String classifier,
56 String repositoryId, String generatePom )
57 throws ArchivaRestServiceException
59 log.info( "uploading file:" + groupId + ":" + artifactId + ":" + version );
62 File file = File.createTempFile( "upload-artifact", "tmp" );
64 IOUtils.copy( httpServletRequest.getInputStream(), new FileOutputStream( file ) );
65 FileMetadata fileMetadata = new FileMetadata( "thefile", file.length(), "theurl" );
66 fileMetadata.setDeleteUrl( file.getName() );
69 catch ( IOException e )
71 throw new ArchivaRestServiceException( e.getMessage(),
72 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
76 public FileMetadata post( MultipartBody multipartBody )
77 throws ArchivaRestServiceException
83 IOUtils.toString( multipartBody.getAttachment( "groupId" ).getDataHandler().getInputStream() );
85 IOUtils.toString( multipartBody.getAttachment( "artifactId" ).getDataHandler().getInputStream() );
87 IOUtils.toString( multipartBody.getAttachment( "version" ).getDataHandler().getInputStream() );
89 IOUtils.toString( multipartBody.getAttachment( "packaging" ).getDataHandler().getInputStream() );
91 boolean generatePom = BooleanUtils.toBoolean(
92 IOUtils.toString( multipartBody.getAttachment( "generatePom" ).getDataHandler().getInputStream() ) );
95 IOUtils.toString( multipartBody.getAttachment( "classifier" ).getDataHandler().getInputStream() );
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() );
106 catch ( IOException e )
108 throw new ArchivaRestServiceException( e.getMessage(),
109 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
114 public Boolean deleteFile( String fileName )
115 throws ArchivaRestServiceException
117 File file = new File( SystemUtils.getJavaIoTmpDir(), fileName );
118 log.debug( "delete file:{},exists:{}", file.getPath(), file.exists() );
121 return file.delete();
123 return Boolean.FALSE;