public Resource createTempFile(ModuleDetails details, ByteArrayOutputStream streamedArtifact) { return this.cachedJars.computeIfAbsent(getKey(details), key -> { Path path = null; try { path = Files.createTempFile(details.getName()+"-app", ".jar"); log.info("Dumping JAR contents to " + path); Files.write(path, streamedArtifact.toByteArray()); path.toFile().deleteOnExit(); } catch (IOException e) { throw new RuntimeException(e); } return new PathResource(path); }); }
@Nullable public static String extractPath(Resource resource, boolean fromMetaInf) { try { String resourcePath = null; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); } else if (resource instanceof FileSystemResource) { resourcePath = ((FileSystemResource) resource).getPath(); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); } else if (resource instanceof PathResource) { resourcePath = ((PathResource) resource).getPath(); } return (fromMetaInf ? StringUtils.substringAfter(resourcePath, META_INF_PATH_PREFIX) : resourcePath); } catch (IOException ignored) { } return null; }
@Nullable public static String extractPath(Resource resource) { try { if (resource instanceof UrlResource) { return resource.getURL().toString(); } else if (resource instanceof FileSystemResource) { return ((FileSystemResource) resource).getPath(); } else if (resource instanceof ClassPathResource) { return ((ClassPathResource) resource).getPath(); } else if (resource instanceof PathResource) { return ((PathResource) resource).getPath(); } } catch (IOException ignored) { } return null; }
private void testUpload(RestTemplate template, String cseUrlPrefix) throws IOException { String file1Content = "hello world"; File file1 = File.createTempFile("upload1", ".txt"); FileUtils.writeStringToFile(file1, file1Content); String file2Content = " bonjour"; File someFile = File.createTempFile("upload2", ".txt"); FileUtils.writeStringToFile(someFile, file2Content); String expect = String.format("%s:%s:%s\n" + "%s:%s:%s", file1.getName(), MediaType.TEXT_PLAIN_VALUE, file1Content, someFile.getName(), MediaType.TEXT_PLAIN_VALUE, file2Content); String result = testRestTemplateUpload(template, cseUrlPrefix, file1, someFile); TestMgr.check(expect, result); result = uploadPartAndFile.fileUpload(new FilePart(null, file1), someFile); TestMgr.check(expect, result); expect = String.format("null:%s:%s\n" + "%s:%s:%s", MediaType.APPLICATION_OCTET_STREAM_VALUE, file1Content, someFile.getName(), MediaType.TEXT_PLAIN_VALUE, file2Content); result = uploadStreamAndResource .fileUpload(new ByteArrayInputStream(file1Content.getBytes(StandardCharsets.UTF_8)), new PathResource(someFile.getAbsolutePath())); TestMgr.check(expect, result); }
@RequestMapping(value = "{cluster}/{appId}/initFile", method = GET, produces = APPLICATION_OCTET_STREAM_VALUE) public ResponseEntity<Resource> getInitFile(@PathVariable("cluster") String cluster, @PathVariable("appId") String appId) { File initComposeFile = applicationService.getInitComposeFile(cluster, appId); log.info("fetching config file for application: {}, cluster: {}, path {}", appId, cluster, initComposeFile); if (!initComposeFile.exists()) { throw new NotFoundException("file not found " + initComposeFile); } HttpHeaders respHeaders = new HttpHeaders(); respHeaders.set("Content-Disposition", "attachment; filename=" + initComposeFile.getName()); respHeaders.setContentLength(initComposeFile.length()); return new ResponseEntity<>(new PathResource(initComposeFile.toPath()), respHeaders, HttpStatus.OK); }
@SuppressWarnings("unchecked") @Override protected <T extends EntityModelDescriptor> void loadDataNodes(Map<String, T> targetMap, String loadDirectory, String loadExtension, Class<T> clazz) { try { if (fileExists(loadDirectory)) { Path entityDirectory = getEntityDirectory(loadDirectory); AbstractFileReader reader = getReaderHelper().getReader(entityDirectory); getLog().info("Loading entities from: " + entityDirectory.toString()); List<E> entities = new ArrayList<>(); List<Path> filesToLoad = reader.getListOfFilesByWildcard(entityDirectory, SetUtils.buildSet(loadExtension)); for (Path entry : filesToLoad) { getLog().info("Currently loading: "+entry); entities.addAll(loadInternal(new PathResource(entry))); } for (E entity : entities) { targetMap.put(entity.getId(), (T) entity); } getLog().info("Loaded " + entities.size() + " " + getEntityClass().getSimpleName() + " entities."); } } catch (Exception e) { throw new IllegalStateException("Error while loading: ", e); } }