|
| 1 | +/* |
| 2 | + * Knowage, Open Source Business Intelligence suite |
| 3 | + * Copyright (C) 2021 Engineering Ingegneria Informatica S.p.A. |
| 4 | + * |
| 5 | + * Knowage is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Knowage is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package it.eng.knowage.resourcemanager.resource; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | + |
| 24 | +import javax.activation.MimetypesFileTypeMap; |
| 25 | +import javax.ws.rs.GET; |
| 26 | +import javax.ws.rs.Path; |
| 27 | +import javax.ws.rs.Produces; |
| 28 | +import javax.ws.rs.QueryParam; |
| 29 | +import javax.ws.rs.core.MediaType; |
| 30 | +import javax.ws.rs.core.Response; |
| 31 | + |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.stereotype.Component; |
| 34 | + |
| 35 | +import it.eng.knowage.boot.context.BusinessRequestContext; |
| 36 | +import it.eng.knowage.boot.error.KnowageBusinessException; |
| 37 | +import it.eng.knowage.boot.error.KnowageRuntimeException; |
| 38 | +import it.eng.knowage.knowageapi.utils.PathTraversalChecker; |
| 39 | +import it.eng.knowage.resourcemanager.service.ResourceManagerAPI; |
| 40 | +import it.eng.spagobi.services.security.SpagoBIUserProfile; |
| 41 | + |
| 42 | +@Path("/2.0/resources/external-libraries") |
| 43 | +@Component |
| 44 | +public class ExternalLibrariesResource { |
| 45 | + |
| 46 | + @Autowired |
| 47 | + ResourceManagerAPI resourceManagerAPIservice; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + BusinessRequestContext businessContext; |
| 51 | + |
| 52 | + @GET |
| 53 | + @Produces(MediaType.APPLICATION_OCTET_STREAM) |
| 54 | + public Response downloadExternalLibrary(@QueryParam("libraryName") String libraryName) throws KnowageBusinessException { |
| 55 | + if (libraryName == null || libraryName.trim().isEmpty()) { |
| 56 | + return Response.status(Response.Status.BAD_REQUEST).entity("Missing libraryName").build(); |
| 57 | + } |
| 58 | + |
| 59 | + String requestedLibraryName = libraryName.trim(); |
| 60 | + try { |
| 61 | + PathTraversalChecker.isValidFileName(requestedLibraryName); |
| 62 | + } catch (KnowageRuntimeException e) { |
| 63 | + return Response.status(Response.Status.BAD_REQUEST).entity("Invalid libraryName").build(); |
| 64 | + } |
| 65 | + |
| 66 | + SpagoBIUserProfile profile = businessContext.getUserProfile(); |
| 67 | + try { |
| 68 | + java.nio.file.Path externalLibraryPath = resourceManagerAPIservice.getExternalLibraryPath(requestedLibraryName, profile); |
| 69 | + File externalLibrary = externalLibraryPath.toFile(); |
| 70 | + String mimeType = Files.probeContentType(externalLibraryPath); |
| 71 | + if (mimeType == null || mimeType.trim().isEmpty()) { |
| 72 | + mimeType = new MimetypesFileTypeMap().getContentType(externalLibrary.getName()); |
| 73 | + } |
| 74 | + if (mimeType == null || mimeType.trim().isEmpty()) { |
| 75 | + mimeType = MediaType.APPLICATION_OCTET_STREAM; |
| 76 | + } |
| 77 | + |
| 78 | + return Response.ok(externalLibrary, mimeType) |
| 79 | + .header("Content-length", String.valueOf(Files.size(externalLibraryPath))) |
| 80 | + .header("Content-Disposition", String.format("attachment; filename=\"%s\"", externalLibrary.getName())) |
| 81 | + .build(); |
| 82 | + } catch (KnowageBusinessException e) { |
| 83 | + throw new KnowageBusinessException(e, businessContext.getLocale()); |
| 84 | + } catch (IOException e) { |
| 85 | + throw new KnowageRuntimeException("Error calculating file size for " + requestedLibraryName, e); |
| 86 | + } catch (Exception e) { |
| 87 | + throw new KnowageRuntimeException(e); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments