public static Boolean storeURLToFileDocument(IContext context, String url, IMendixObject __document, String filename) throws Exception { if (__document == null || url == null || filename == null) throw new Exception("No document, filename or URL provided"); final int MAX_REMOTE_FILESIZE = 1024 * 1024 * 200; //maxium of 200 MB URL imageUrl = new URL(url); URLConnection connection = imageUrl.openConnection(); //we connect in 20 seconds or not at all connection.setConnectTimeout(20000); connection.setReadTimeout(20000); connection.connect(); //check on forehand the size of the remote file, we don't want to kill the server by providing a 3 terabyte image. if (connection.getContentLength() > MAX_REMOTE_FILESIZE) { //maximum of 200 mb throw new IllegalArgumentException("MxID: importing image, wrong filesize of remote url: " + connection.getContentLength()+ " (max: " + String.valueOf(MAX_REMOTE_FILESIZE)+ ")"); } else if (connection.getContentLength() < 0) { // connection has not specified content length, wrap stream in a LimitedInputStream LimitedInputStream limitStream = new LimitedInputStream(connection.getInputStream(), MAX_REMOTE_FILESIZE) { @Override protected void raiseError(long pSizeMax, long pCount) throws IOException { throw new IllegalArgumentException("MxID: importing image, wrong filesize of remote url (max: " + String.valueOf(MAX_REMOTE_FILESIZE)+ ")"); } }; Core.storeFileDocumentContent(context, __document, filename, limitStream); } else { // connection has specified correct content length, read the stream normally //NB; stream is closed by the core Core.storeFileDocumentContent(context, __document, filename, connection.getInputStream()); } return true; }
private LimitedInputStream limitInputStream(InputStream thesisContentInputStream, long maxSize) { LimitedInputStream inputStream = new LimitedInputStream(thesisContentInputStream, maxSize) { @Override protected void raiseError(long pSizeMax, long pCount) throws IOException { throw new ServiceException(ERROR_MAX_UPLOAD_SIZE_EXCEEDED, ""+pSizeMax); } }; return inputStream; }
@Test public void uploadEnrichmentTags() throws Exception { // given InputStream inputStream = mock(InputStream.class); EnrichmentTagItem enrichmentTagItem = mock(EnrichmentTagItem.class); when(jsonFactory.createParser(Mockito.any(InputStream.class))).thenReturn(jsonParser); when(jsonParser.nextToken()).thenReturn(JsonToken.START_ARRAY); when(jsonObjectIterator.nextJsonObject(jsonParser, EnrichmentTagItem.class)).thenReturn(enrichmentTagItem, (EnrichmentTagItem)null); // execute enrichmentTagUploadService.uploadEnrichmentTags(inputStream); // verify InOrder inOrder = Mockito.inOrder(uploadEnrichmentTagRepository, jsonFactory, jsonObjectIterator, enrichmentTagItemUploadProcessor); inOrder.verify(uploadEnrichmentTagRepository).truncate(); ArgumentCaptor<InputStream> inputStreamArg = ArgumentCaptor.forClass(InputStream.class); inOrder.verify(jsonFactory).createParser(inputStreamArg.capture()); assertTrue(inputStreamArg.getValue() instanceof LimitedInputStream); inOrder.verify(jsonObjectIterator).nextJsonObject(jsonParser, EnrichmentTagItem.class); ArgumentCaptor<EnrichmentTagItem> tagItemToProcessArg = ArgumentCaptor.forClass(EnrichmentTagItem.class); inOrder.verify(enrichmentTagItemUploadProcessor).processEnrichmentTagItem(tagItemToProcessArg.capture()); assertTrue(enrichmentTagItem == tagItemToProcessArg.getValue()); inOrder.verify(jsonObjectIterator).nextJsonObject(jsonParser, EnrichmentTagItem.class); verifyNoMoreInteractions(jsonFactory, jsonObjectIterator, enrichmentTagItemUploadProcessor); }