@Test public void testUploadJar() throws Exception { final Path uploadedFile = Files.createFile(jarDir.resolve("Kafka010Example.jar")); final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request = createRequest(uploadedFile); final JarUploadResponseBody jarUploadResponseBody = jarUploadHandler.handleRequest(request, mockDispatcherGateway).get(); assertThat(jarUploadResponseBody.getStatus(), equalTo(JarUploadResponseBody.UploadStatus.success)); final String returnedFileNameWithUUID = jarUploadResponseBody.getFilename(); assertThat(returnedFileNameWithUUID, containsString("_")); final String returnedFileName = returnedFileNameWithUUID.substring(returnedFileNameWithUUID.lastIndexOf("_") + 1); assertThat(returnedFileName, equalTo(uploadedFile.getFileName().toString())); }
@Override protected CompletableFuture<JarUploadResponseBody> handleRequest( @Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException { Collection<File> uploadedFiles = request.getUploadedFiles(); if (uploadedFiles.size() != 1) { throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST); } final Path fileUpload = uploadedFiles.iterator().next().toPath(); return CompletableFuture.supplyAsync(() -> { if (!fileUpload.getFileName().toString().endsWith(".jar")) { throw new CompletionException(new RestHandlerException( "Only Jar files are allowed.", HttpResponseStatus.BAD_REQUEST)); } else { final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName()); try { Files.move(fileUpload, destination); } catch (IOException e) { throw new CompletionException(new RestHandlerException( String.format("Could not move uploaded jar file [%s] to [%s].", fileUpload, destination), HttpResponseStatus.INTERNAL_SERVER_ERROR, e)); } return new JarUploadResponseBody(destination .normalize() .toString()); } }, executor); }
JarUploadHandler extends
		AbstractRestHandler<RestfulGateway, EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> { @Override protected CompletableFuture<JarUploadResponseBody> handleRequest( @Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException { Collection<File> uploadedFiles = request.getUploadedFiles(); if (uploadedFiles.size() != 1) { throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST); } final Path fileUpload = uploadedFiles.iterator().next().toPath(); return CompletableFuture.supplyAsync(() -> { if (!fileUpload.getFileName().toString().endsWith(".jar")) { throw new CompletionException(new RestHandlerException( "Only Jar files are allowed.", HttpResponseStatus.BAD_REQUEST)); } else { final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName()); try { Files.move(fileUpload, destination); } catch (IOException e) { throw new CompletionException(new RestHandlerException( String.format("Could not move uploaded jar file [%s] to [%s].", fileUpload, destination), HttpResponseStatus.INTERNAL_SERVER_ERROR, e)); } return new JarUploadResponseBody(destination .normalize() .toString()); } }, executor); } }
JarUploadHandler extends
		AbstractRestHandler<RestfulGateway, EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> { @Override protected CompletableFuture<JarUploadResponseBody> handleRequest( @Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException { Collection<File> uploadedFiles = request.getUploadedFiles(); if (uploadedFiles.size() != 1) { throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST); } final Path fileUpload = uploadedFiles.iterator().next().toPath(); return CompletableFuture.supplyAsync(() -> { if (!fileUpload.getFileName().toString().endsWith(".jar")) { throw new CompletionException(new RestHandlerException( "Only Jar files are allowed.", HttpResponseStatus.BAD_REQUEST)); } else { final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName()); try { Files.move(fileUpload, destination); } catch (IOException e) { throw new CompletionException(new RestHandlerException( String.format("Could not move uploaded jar file [%s] to [%s].", fileUpload, destination), HttpResponseStatus.INTERNAL_SERVER_ERROR, e)); } return new JarUploadResponseBody(destination .normalize() .toString()); } }, executor); } JarUploadHandler(
			final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
			final Time timeout,
			final Map<String, String> responseHeaders,
			final MessageHeaders<EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> messageHeaders,
			final Path jarDir,
			final Executor executor); }
JarUploadHandler extends
		AbstractRestHandler<RestfulGateway, EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> { @Override protected CompletableFuture<JarUploadResponseBody> handleRequest( @Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException { Collection<File> uploadedFiles = request.getUploadedFiles(); if (uploadedFiles.size() != 1) { throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST); } final Path fileUpload = uploadedFiles.iterator().next().toPath(); return CompletableFuture.supplyAsync(() -> { if (!fileUpload.getFileName().toString().endsWith(".jar")) { throw new CompletionException(new RestHandlerException( "Only Jar files are allowed.", HttpResponseStatus.BAD_REQUEST)); } else { final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName()); try { Files.move(fileUpload, destination); } catch (IOException e) { throw new CompletionException(new RestHandlerException( String.format("Could not move uploaded jar file [%s] to [%s].", fileUpload, destination), HttpResponseStatus.INTERNAL_SERVER_ERROR, e)); } return new JarUploadResponseBody(destination .normalize() .toString()); } }, executor); } JarUploadHandler(
			final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
			final Time timeout,
			final Map<String, String> responseHeaders,
			final MessageHeaders<EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> messageHeaders,
			final Path jarDir,
			final Executor executor);  }
JarUploadHandler extends
		AbstractRestHandler<RestfulGateway, EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> { @Override protected CompletableFuture<JarUploadResponseBody> handleRequest( @Nonnull final HandlerRequest<EmptyRequestBody, EmptyMessageParameters> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException { Collection<File> uploadedFiles = request.getUploadedFiles(); if (uploadedFiles.size() != 1) { throw new RestHandlerException("Exactly 1 file must be sent, received " + uploadedFiles.size() + '.', HttpResponseStatus.BAD_REQUEST); } final Path fileUpload = uploadedFiles.iterator().next().toPath(); return CompletableFuture.supplyAsync(() -> { if (!fileUpload.getFileName().toString().endsWith(".jar")) { throw new CompletionException(new RestHandlerException( "Only Jar files are allowed.", HttpResponseStatus.BAD_REQUEST)); } else { final Path destination = jarDir.resolve(UUID.randomUUID() + "_" + fileUpload.getFileName()); try { Files.move(fileUpload, destination); } catch (IOException e) { throw new CompletionException(new RestHandlerException( String.format("Could not move uploaded jar file [%s] to [%s].", fileUpload, destination), HttpResponseStatus.INTERNAL_SERVER_ERROR, e)); } return new JarUploadResponseBody(destination .normalize() .toString()); } }, executor); } JarUploadHandler(
			final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
			final Time timeout,
			final Map<String, String> responseHeaders,
			final MessageHeaders<EmptyRequestBody, JarUploadResponseBody, EmptyMessageParameters> messageHeaders,
			final Path jarDir,
			final Executor executor);   }