Skip to content

Commit

Permalink
Merge pull request #138 from ligangty/import
Browse files Browse the repository at this point in the history
Fix a bug of importing the repo zip file
  • Loading branch information
ligangty authored Feb 26, 2024
2 parents 0930579 + 28c45ff commit 44c74fd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
15 changes: 10 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
Expand All @@ -289,11 +299,6 @@
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-interpolation</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -96,35 +100,40 @@ public File getRepoBundle()
public Map<String, List<String>> 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<String> skipped = new ArrayList<>();
final List<String> failed = new ArrayList<>();
final Map<String, String> 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<ZipArchiveEntry> 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<String, String> entry : payload.entrySet() )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<String, List<String>> results = maintController.importRepoBundle( request.getInputStream() );
Map<String, List<String>> results = maintController.importRepoBundle( input );
return ok( results ).build();
}
catch ( IOException e )
Expand Down

0 comments on commit 44c74fd

Please sign in to comment.