StreamReceiverHandler implements Serializable { public void handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response, StreamReceiver streamReceiver, String uiId, String securityKey) throws IOException { StateNode source; session.lock(); try { String secKey = streamReceiver.getId(); if (secKey == null || !secKey.equals(securityKey)) { getLogger().warn( "Received incoming stream with faulty security key."); return; } UI ui = session.getUIById(Integer.parseInt(uiId)); UI.setCurrent(ui); source = streamReceiver.getNode(); } finally { session.unlock(); } try { if (isMultipartUpload(request)) { doHandleMultipartFileUpload(session, request, response, streamReceiver, source); } else { doHandleXhrFilePost(session, request, response, streamReceiver, source, getContentLength(request)); } } finally { UI.setCurrent(null); } }  void handleRequest(VaadinSession session, VaadinRequest request,
            VaadinResponse response, StreamReceiver streamReceiver, String uiId,
            String securityKey); static final int DEFAULT_STREAMING_PROGRESS_EVENT_INTERVAL_MS; }
@Test public void responseIsSentOnCorrectSecurityKey() throws IOException { handler.handleRequest(session, request, response, streamReceiver, String.valueOf(uiId), expectedSecurityKey); verify(responseOutput).close(); }
@Test public void responseIsNotSentOnIncorrectSecurityKey() throws IOException { when(streamReceiver.getId()).thenReturn("another key expected"); handler.handleRequest(session, request, response, streamReceiver, String.valueOf(uiId), expectedSecurityKey); verifyZeroInteractions(responseOutput); }
@Test public void responseIsNotSentOnMissingSecurityKey() throws IOException { when(streamReceiver.getId()).thenReturn(null); handler.handleRequest(session, request, response, streamReceiver, String.valueOf(uiId), expectedSecurityKey); verifyZeroInteractions(responseOutput); }
@Test public void partsAreUsedDirectlyIfPresentWithoutParsingInput() throws IOException { contentType = "multipart/form-data; boundary=----WebKitFormBoundary7NsWHeCJVZNwi6ll"; inputStream = createInputStream( "------WebKitFormBoundary7NsWHeCJVZNwi6ll\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"EBookJP.txt\"\n" + "Content-Type: text/plain\n" + "\n" + "\n" + "------WebKitFormBoundary7NsWHeCJVZNwi6ll--"); outputStream = new ByteArrayOutputStream(); contentLength = "99"; final String fileName = "EBookJP.txt"; final String contentType = "text/plain"; parts = new ArrayList<>(); parts.add(createPart(createInputStream("foobar"), contentType, fileName, 6)); handler.handleRequest(session, request, response, streamReceiver, String.valueOf(uiId), expectedSecurityKey); verify(responseOutput).close(); ArgumentCaptor<StreamVariable.StreamingEndEvent> endEventArgumentCaptor = ArgumentCaptor .forClass(StreamVariable.StreamingEndEvent.class); verify(streamVariable) .streamingFinished(endEventArgumentCaptor.capture()); Assert.assertEquals("foobar", new String( ((ByteArrayOutputStream) outputStream).toByteArray())); Assert.assertEquals(fileName, endEventArgumentCaptor.getValue().getFileName()); Assert.assertEquals(contentType, endEventArgumentCaptor.getValue().getMimeType()); Mockito.verify(response).setContentType( ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8); Mockito.verify(response, Mockito.times(0)).setStatus(Mockito.anyInt()); }