From e9e1fd85687ecf6ff39f59051631991a09650538 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 28 Mar 2012 11:16:43 +0000 Subject: [PATCH] [MRM-1586] rewrite upload artifact page add REST service to upload files. git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1306256 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/archiva/i18n/default.properties | 13 +++ .../api/DefaultFileUploadService.java | 64 +++++++++++ .../ui/services/api/FileUploadService.java | 44 +++++++ .../ui/services/model/FileMetadata.java | 107 ++++++++++++++++++ .../resources/META-INF/spring-context.xml | 1 + .../src/main/webapp/js/archiva/main.js | 7 +- .../archiva/artifacts-management.html | 12 +- .../webapp/js/templates/archiva/menu.html | 4 +- 8 files changed, 244 insertions(+), 8 deletions(-) create mode 100644 archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/DefaultFileUploadService.java create mode 100644 archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/FileUploadService.java create mode 100644 archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/model/FileMetadata.java diff --git a/archiva-modules/archiva-web/archiva-web-common/src/main/resources/org/apache/archiva/i18n/default.properties b/archiva-modules/archiva-web/archiva-web-common/src/main/resources/org/apache/archiva/i18n/default.properties index 00070192a..9f7d49b34 100644 --- a/archiva-modules/archiva-web/archiva-web-common/src/main/resources/org/apache/archiva/i18n/default.properties +++ b/archiva-modules/archiva-web/archiva-web-common/src/main/resources/org/apache/archiva/i18n/default.properties @@ -376,4 +376,17 @@ appearance-configuration.logoLocation-label=Logo Location appearance-configuration.updated=Appearance has been updated appearance-configuration.updating-error=Error during appearance setting +#file upload +menu.artifacts.upload=Upload Artifact +fileupload.cancel=Cancel Upload +fileupload.start=Start Upload +fileupload.error=Error Upload +fileupload.destroy=Delete Upload +fileupload.errors.maxFileSize=File is too big +fileupload.errors.minFileSize=File is too small +fileupload.errors.acceptFileTypes=Filetype not allowed +fileupload.errors.maxNumberOfFiles=Max number of files exceeded +fileupload.errors.uploadedBytes=Uploaded bytes exceed file size +fileupload.errors.emptyResult=Empty file upload result + diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/DefaultFileUploadService.java b/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/DefaultFileUploadService.java new file mode 100644 index 000000000..49f0a5350 --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/DefaultFileUploadService.java @@ -0,0 +1,64 @@ +package org.apache.archiva.webapp.ui.services.api; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.rest.api.services.ArchivaRestServiceException; +import org.apache.archiva.webapp.ui.services.model.FileMetadata; +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import java.io.IOException; + +/** + * @author Olivier Lamy + */ +@Service( "fileUploadService#rest" ) +public class DefaultFileUploadService + implements FileUploadService +{ + private Logger log = LoggerFactory.getLogger( getClass() ); + + @Context + private HttpServletRequest httpServletRequest; + + @Context + private HttpServletResponse httpServletResponse; + + public FileMetadata post() + throws ArchivaRestServiceException + { + log.info( "uploading file" ); + try + { + byte[] bytes = IOUtils.toByteArray( httpServletRequest.getInputStream() ); + return new FileMetadata( "thefile", bytes.length, "theurl" ); + } + catch ( IOException e ) + { + throw new ArchivaRestServiceException( e.getMessage(), + Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() ); + } + } +} diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/FileUploadService.java b/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/FileUploadService.java new file mode 100644 index 000000000..1c8cc3d0a --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/api/FileUploadService.java @@ -0,0 +1,44 @@ +package org.apache.archiva.webapp.ui.services.api; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.archiva.rest.api.services.ArchivaRestServiceException; +import org.apache.archiva.webapp.ui.services.model.FileMetadata; +import org.codehaus.plexus.redback.authorization.RedbackAuthorization; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +/** + * @author Olivier Lamy + * @since 1.4-M3 + */ +@Path( "/fileUploadService/" ) +public interface FileUploadService +{ + + @Path( "upload" ) + @POST + @Consumes( MediaType.MULTIPART_FORM_DATA ) + @RedbackAuthorization( noRestriction = true ) + FileMetadata post() + throws ArchivaRestServiceException; +} diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/model/FileMetadata.java b/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/model/FileMetadata.java new file mode 100644 index 000000000..5a717431c --- /dev/null +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/java/org/apache/archiva/webapp/ui/services/model/FileMetadata.java @@ -0,0 +1,107 @@ +package org.apache.archiva.webapp.ui.services.model; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * @author Olivier Lamy + * @since 1.4-M3 + */ +@XmlRootElement( name = "fileMetadata" ) +public class FileMetadata +{ + private String name; + + private long size; + + private String url; + + private String deleteUrl; + + private String deleteType; + + public FileMetadata() + { + // no op + } + + public FileMetadata( String filename, long size, String url ) + { + this.name = filename; + this.size = size; + this.url = url; + this.deleteUrl = url; + this.deleteType = "DELETE"; + } + + public String getName() + { + return name; + } + + public void setName( String name ) + { + this.name = name; + } + + public long getSize() + { + return size; + } + + public void setSize( long size ) + { + this.size = size; + } + + public String getUrl() + { + return url; + } + + public void setUrl( String url ) + { + this.url = url; + } + + @XmlElement( name = "delete_url" ) + public String getDeleteUrl() + { + return deleteUrl; + } + + + public void setDeleteUrl( String deleteUrl ) + { + this.deleteUrl = deleteUrl; + } + + @XmlElement( name = "delete_type" ) + public String getDeleteType() + { + return deleteType; + } + + public void setDeleteType( String deleteType ) + { + this.deleteType = deleteType; + } +} diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/resources/META-INF/spring-context.xml b/archiva-modules/archiva-web/archiva-webapp-js/src/main/resources/META-INF/spring-context.xml index c367a9e92..2cce5e81d 100755 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/resources/META-INF/spring-context.xml +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/resources/META-INF/spring-context.xml @@ -66,6 +66,7 @@ + diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/main.js b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/main.js index fe4a32163..c8962a432 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/main.js +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/archiva/main.js @@ -131,7 +131,12 @@ function() { } if (screen=='appearance-configuration'&& hasKarma('archiva-manage-configuration')){ displayAppearanceConfiguration(); - return + return; + } + + if (screen=='artifact-upload' && hasKarma('archiva-upload-repository')){ + displayUploadArtifact(); + return; } } // by default display search screen diff --git a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/templates/archiva/artifacts-management.html b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/templates/archiva/artifacts-management.html index ec1d55c0b..1063fa2e8 100644 --- a/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/templates/archiva/artifacts-management.html +++ b/archiva-modules/archiva-web/archiva-webapp-js/src/main/webapp/js/templates/archiva/artifacts-management.html @@ -1,5 +1,5 @@