@Test public void testHandleRequest() throws UnsupportedOperationException, IOException { APIGatewayProxyRequestEvent input = new APIGatewayProxyRequestEvent(); HttpEntity entity = MultipartEntityBuilder.create().addBinaryBody("not-jar", Paths.get(getClass().getResource("/push-handler-test.jar").getPath()).toFile()).build(); Map<String, String> headers = new HashMap<>(); Header header = entity.getContentType(); headers.put(header.getName(), header.getValue()); input.setBody(Base64.getEncoder().encodeToString(IOUtils.toString(entity.getContent()).getBytes())); input.setHeaders(headers); PushHandler handler = new PushHandler(); APIGatewayProxyResponseEvent output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(400)); assertThat(output.getBody(), is("Parameter 'jar' is required")); input = new APIGatewayProxyRequestEvent(); entity = MultipartEntityBuilder.create().addBinaryBody("jar", Paths.get(getClass().getResource("/push-handler-test.zip").getPath()).toFile()).build(); headers = new HashMap<>(); header = entity.getContentType(); headers.put(header.getName(), header.getValue()); input.setBody(Base64.getEncoder().encodeToString(IOUtils.toString(entity.getContent()).getBytes())); input.setHeaders(headers); handler = new PushHandler(); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(400)); assertThat(output.getBody(), is("Parameter 'name' must be *.jar")); input = new APIGatewayProxyRequestEvent(); entity = MultipartEntityBuilder.create().addBinaryBody("jar", Paths.get(getClass().getResource("/push-handler-test.jar").getPath()).toFile()).build(); headers = new HashMap<>(); header = entity.getContentType(); headers.put(header.getName(), header.getValue()); input.setBody(Base64.getEncoder().encodeToString(IOUtils.toString(entity.getContent()).getBytes())); input.setHeaders(headers); handler = new PushHandler(); Repository repository = mock(Repository.class); doThrow(new IllegalStateException("hello")).when(repository).save(anyString(), ArgumentMatchers.any(InputStream.class)); Prodigy.container(new Container(mock(Scheduler.class), repository)); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(500)); assertThat(output.getBody(), is("hello")); input = new APIGatewayProxyRequestEvent(); entity = MultipartEntityBuilder.create().addBinaryBody("jar", Paths.get(getClass().getResource("/push-handler-test.jar").getPath()).toFile()).build(); headers = new HashMap<>(); header = entity.getContentType(); headers.put(header.getName(), header.getValue()); input.setBody(Base64.getEncoder().encodeToString(IOUtils.toString(entity.getContent()).getBytes())); input.setHeaders(headers); handler = new PushHandler(); repository = mock(Repository.class); doNothing().when(repository).save(anyString(), ArgumentMatchers.any(InputStream.class)); Prodigy.container(new Container(mock(Scheduler.class), repository)); output = handler.handleRequest(input, null); assertThat(output.getStatusCode(), is(200)); assertThat(output.getBody(), is("{}")); }
@Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { log.debug("'input' is [" + input + "]"); APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); byte[] body = Base64.getDecoder().decode(input.getBody()); FileUpload upload = new FileUpload(new DiskFileItemFactory()); try { FileItemIterator iterator = upload.getItemIterator(new UploadContext() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(body); } @Override public String getContentType() { return input.getHeaders().get("Content-Type"); } @Override public int getContentLength() { return body.length; } @Override public String getCharacterEncoding() { return StandardCharsets.UTF_8.name(); } @Override public long contentLength() { return body.length; } }); while (iterator.hasNext()) { FileItemStream item = iterator.next(); if (!item.isFormField() && item.getFieldName().equals("jar")) { String name = item.getName(); Prodigy.push(name, item.openStream()); return output.withStatusCode(200).withBody("{}"); } } throw new IllegalArgumentException("Parameter 'jar' is required"); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }
PushHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { log.debug("'input' is [" + input + "]"); APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); byte[] body = Base64.getDecoder().decode(input.getBody()); FileUpload upload = new FileUpload(new DiskFileItemFactory()); try { FileItemIterator iterator = upload.getItemIterator(new UploadContext() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(body); } @Override public String getContentType() { return input.getHeaders().get("Content-Type"); } @Override public int getContentLength() { return body.length; } @Override public String getCharacterEncoding() { return StandardCharsets.UTF_8.name(); } @Override public long contentLength() { return body.length; } }); while (iterator.hasNext()) { FileItemStream item = iterator.next(); if (!item.isFormField() && item.getFieldName().equals("jar")) { String name = item.getName(); Prodigy.push(name, item.openStream()); return output.withStatusCode(200).withBody("{}"); } } throw new IllegalArgumentException("Parameter 'jar' is required"); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } } }
PushHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { log.debug("'input' is [" + input + "]"); APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); byte[] body = Base64.getDecoder().decode(input.getBody()); FileUpload upload = new FileUpload(new DiskFileItemFactory()); try { FileItemIterator iterator = upload.getItemIterator(new UploadContext() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(body); } @Override public String getContentType() { return input.getHeaders().get("Content-Type"); } @Override public int getContentLength() { return body.length; } @Override public String getCharacterEncoding() { return StandardCharsets.UTF_8.name(); } @Override public long contentLength() { return body.length; } }); while (iterator.hasNext()) { FileItemStream item = iterator.next(); if (!item.isFormField() && item.getFieldName().equals("jar")) { String name = item.getName(); Prodigy.push(name, item.openStream()); return output.withStatusCode(200).withBody("{}"); } } throw new IllegalArgumentException("Parameter 'jar' is required"); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }  }
PushHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { log.debug("'input' is [" + input + "]"); APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); byte[] body = Base64.getDecoder().decode(input.getBody()); FileUpload upload = new FileUpload(new DiskFileItemFactory()); try { FileItemIterator iterator = upload.getItemIterator(new UploadContext() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(body); } @Override public String getContentType() { return input.getHeaders().get("Content-Type"); } @Override public int getContentLength() { return body.length; } @Override public String getCharacterEncoding() { return StandardCharsets.UTF_8.name(); } @Override public long contentLength() { return body.length; } }); while (iterator.hasNext()) { FileItemStream item = iterator.next(); if (!item.isFormField() && item.getFieldName().equals("jar")) { String name = item.getName(); Prodigy.push(name, item.openStream()); return output.withStatusCode(200).withBody("{}"); } } throw new IllegalArgumentException("Parameter 'jar' is required"); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }  @Override APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context); }
PushHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { @Override public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { log.debug("'input' is [" + input + "]"); APIGatewayProxyResponseEvent output = new APIGatewayProxyResponseEvent(); byte[] body = Base64.getDecoder().decode(input.getBody()); FileUpload upload = new FileUpload(new DiskFileItemFactory()); try { FileItemIterator iterator = upload.getItemIterator(new UploadContext() { @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(body); } @Override public String getContentType() { return input.getHeaders().get("Content-Type"); } @Override public int getContentLength() { return body.length; } @Override public String getCharacterEncoding() { return StandardCharsets.UTF_8.name(); } @Override public long contentLength() { return body.length; } }); while (iterator.hasNext()) { FileItemStream item = iterator.next(); if (!item.isFormField() && item.getFieldName().equals("jar")) { String name = item.getName(); Prodigy.push(name, item.openStream()); return output.withStatusCode(200).withBody("{}"); } } throw new IllegalArgumentException("Parameter 'jar' is required"); } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); return output.withStatusCode(400).withBody(e.getMessage()); } catch (Exception e) { log.error(e.getMessage(), e); return output.withStatusCode(500).withBody(e.getMessage()); } }  @Override APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context);  }