From 28c45ffde5a20c344879b38c42f1d59ecd402e20 Mon Sep 17 00:00:00 2001 From: Gang Li Date: Mon, 26 Feb 2024 21:34:20 +0800 Subject: [PATCH] Fix a bug of importing the repo zip file --- pom.xml | 15 ++++-- .../controller/MaintenanceController.java | 47 +++++++++++-------- .../jaxrs/RepositoryMaintenanceResources.java | 11 ++--- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 811a0d2..f7f2b39 100644 --- a/pom.xml +++ b/pom.xml @@ -274,6 +274,16 @@ commons-lang3 3.12.0 + + org.apache.commons + commons-compress + 1.26.0 + + + commons-io + commons-io + 2.15.1 + com.datastax.cassandra cassandra-driver-core @@ -289,11 +299,6 @@ httpclient 4.5.13 - - commons-io - commons-io - 2.11.0 - org.codehaus.plexus plexus-interpolation diff --git a/src/main/java/org/commonjava/indy/service/repository/controller/MaintenanceController.java b/src/main/java/org/commonjava/indy/service/repository/controller/MaintenanceController.java index fba5c44..9cbc6d3 100644 --- a/src/main/java/org/commonjava/indy/service/repository/controller/MaintenanceController.java +++ b/src/main/java/org/commonjava/indy/service/repository/controller/MaintenanceController.java @@ -17,6 +17,9 @@ import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; +import org.apache.commons.compress.archivers.zip.ZipFile; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.commonjava.event.common.EventMetadata; import org.commonjava.indy.service.repository.audit.ChangeSummary; @@ -32,17 +35,18 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import static java.util.Map.of; @@ -96,35 +100,40 @@ public File getRepoBundle() public Map> importRepoBundle( final InputStream zipStream ) throws IOException { + File tempRepoZip = createTempFile(); + logger.info( "Saving repo file to {}", tempRepoZip.getPath() ); + try (zipStream) + { + try (OutputStream out = new FileOutputStream( tempRepoZip )) + { + IOUtils.copy( zipStream, out ); + } + } + final List skipped = new ArrayList<>(); final List failed = new ArrayList<>(); final Map payload = new HashMap<>(); - logger.info( "Start extracting repos definitions from bundle!" ); - try (ZipInputStream zip = new ZipInputStream( zipStream )) + + try (ZipFile zipFile = ZipFile.builder().setFile( tempRepoZip ).get()) { - if ( zip.available() > 0 ) + Enumeration entries = zipFile.getEntries(); + while ( entries.hasMoreElements() ) { - ZipEntry entry = zip.getNextEntry(); - while ( entry != null ) + ZipArchiveEntry entry = entries.nextElement(); + if ( !entry.isDirectory() ) { - if ( !entry.isDirectory() ) + try (InputStream in = zipFile.getInputStream( entry )) { - logger.debug( "Processing {}", entry.getName() ); - byte[] buffer = new byte[2048]; - final StringBuilder builder = new StringBuilder(); - while ( zip.read( buffer ) > 0 ) - { - builder.append( new String( buffer, Charset.defaultCharset() ) ); - buffer = new byte[2048]; - } - - payload.put( entry.getName(), builder.toString().trim() ); - + payload.put( entry.getName(), IOUtils.toString( in, Charset.defaultCharset() ) ); } - entry = zip.getNextEntry(); } } } + finally + { + FileUtils.deleteQuietly( tempRepoZip ); + } + logger.info( "Repos definitions extraction from bundle finished.\n\n" ); logger.info( "Start importing repos definitions to data store." ); for ( Map.Entry entry : payload.entrySet() ) diff --git a/src/main/java/org/commonjava/indy/service/repository/jaxrs/RepositoryMaintenanceResources.java b/src/main/java/org/commonjava/indy/service/repository/jaxrs/RepositoryMaintenanceResources.java index f41f6c0..d9fd172 100644 --- a/src/main/java/org/commonjava/indy/service/repository/jaxrs/RepositoryMaintenanceResources.java +++ b/src/main/java/org/commonjava/indy/service/repository/jaxrs/RepositoryMaintenanceResources.java @@ -23,7 +23,6 @@ import org.eclipse.microprofile.openapi.annotations.Operation; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.tags.Tag; -import org.jboss.resteasy.spi.HttpRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,10 +36,11 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.util.List; import java.util.Map; @@ -49,7 +49,6 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static javax.ws.rs.core.Response.Status.BAD_REQUEST; import static javax.ws.rs.core.Response.Status.FORBIDDEN; -import static javax.ws.rs.core.Response.Status.METHOD_NOT_ALLOWED; import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.ok; import static org.apache.commons.lang3.StringUtils.isBlank; @@ -100,13 +99,13 @@ public Response getRepoBundle() @APIResponse( responseCode = "200", description = "All repository definitions which are imported successfully." ) @POST @Path( "/import" ) - @Consumes( MEDIATYPE_APPLICATION_ZIP ) + @Consumes( MediaType.MULTIPART_FORM_DATA ) @Produces( APPLICATION_JSON ) - public Response importRepoBundle( @Context final HttpRequest request ) + public Response importRepoBundle( InputStream input ) { try { - Map> results = maintController.importRepoBundle( request.getInputStream() ); + Map> results = maintController.importRepoBundle( input ); return ok( results ).build(); } catch ( IOException e )