| /******************************************************************************** |
| * Copyright (c) 2015-2019 Contributors to the Eclipse Foundation |
| * |
| * See the NOTICE file(s) distributed with this work for additional |
| * information regarding copyright ownership. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| ********************************************************************************/ |
| package org.eclipse.mdm.apicopy.boundary; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.OutputStream; |
| import java.nio.file.Files; |
| import java.nio.file.Path; |
| import java.util.Comparator; |
| import java.util.List; |
| import java.util.stream.Collectors; |
| import java.util.zip.ZipEntry; |
| import java.util.zip.ZipOutputStream; |
| |
| import javax.ejb.EJB; |
| import javax.ws.rs.Consumes; |
| import javax.ws.rs.POST; |
| import javax.ws.rs.Produces; |
| import javax.ws.rs.WebApplicationException; |
| import javax.ws.rs.core.MediaType; |
| import javax.ws.rs.core.Response; |
| import javax.ws.rs.core.StreamingOutput; |
| |
| import org.eclipse.mdm.shoppingbasket.entity.ShoppingBasket; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import io.swagger.v3.oas.annotations.tags.Tag; |
| |
| @Tag(name = "Export") |
| @javax.ws.rs.Path("/export") |
| public class ExportResource { |
| private static final Logger LOG = LoggerFactory.getLogger(ExportResource.class); |
| |
| @EJB |
| private ExportService exportService; |
| |
| /** |
| * Default public constructor for JAX-RS |
| */ |
| public ExportResource() { |
| } |
| |
| /** |
| * Used to provide service for unit tests |
| * |
| * @param exportService |
| */ |
| protected ExportResource(ExportService exportService) { |
| this.exportService = exportService; |
| } |
| |
| @POST |
| @Consumes(MediaType.APPLICATION_XML) |
| @Produces(MediaType.APPLICATION_XML) |
| public Response exportAtfx(ShoppingBasket basket) throws IOException { |
| |
| final Path exportAtfx = exportService.exportAtfx(basket); |
| |
| List<File> files = Files.list(exportAtfx).map(p -> p.toFile()).collect(Collectors.toList()); |
| if (files.size() == 1) { |
| StreamingOutput output = new StreamingOutput() { |
| @Override |
| public void write(OutputStream out) throws IOException { |
| try (final InputStream responseStream = new FileInputStream(files.get(0))) { |
| int length; |
| byte[] buffer = new byte[1024]; |
| while ((length = responseStream.read(buffer)) != -1) { |
| out.write(buffer, 0, length); |
| } |
| out.flush(); |
| } finally { |
| if (exportAtfx != null) { |
| try { |
| deleteDirectory(exportAtfx); |
| } catch (IOException e) { |
| LOG.warn("Could not delete temporary directory " |
| + exportAtfx.getParent().toFile().getAbsolutePath(), e); |
| } |
| } |
| } |
| } |
| }; |
| |
| return Response.ok(output, MediaType.APPLICATION_XML).build(); |
| } else { |
| StreamingOutput out = new StreamingOutput() { |
| @Override |
| public void write(OutputStream output) throws IOException, WebApplicationException { |
| try (ZipOutputStream out = new ZipOutputStream(output)) { |
| List<File> files = Files.list(exportAtfx).map(p -> p.toFile()).collect(Collectors.toList()); |
| for (File f : files) { |
| out.putNextEntry(new ZipEntry(f.getName())); |
| try (InputStream in = new FileInputStream(f)) { |
| int read = 0; |
| byte[] bytes = new byte[1024]; |
| |
| while ((read = in.read(bytes)) != -1) { |
| out.write(bytes, 0, read); |
| } |
| } |
| } |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| if (exportAtfx != null) { |
| try { |
| deleteDirectory(exportAtfx); |
| } catch (IOException e) { |
| LOG.warn( |
| "Could not delete temporary directory " + exportAtfx.toFile().getAbsolutePath(), |
| e); |
| } |
| } |
| } |
| } |
| }; |
| return Response.ok(out, "application/zip").build(); |
| } |
| } |
| |
| /** |
| * Deletes a directory. |
| * |
| * @param pathToBeDeleted directory to delete |
| * @throws IOException |
| */ |
| private void deleteDirectory(java.nio.file.Path pathToBeDeleted) throws IOException { |
| Files.walk(pathToBeDeleted).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); |
| } |
| } |