MultipartUtils { public static String readFormParameterValue(final StreamStorage streamStorage, final Map<String, List<String>> headers){ try { return IOUtils.inputStreamAsString(streamStorage.getInputStream(), MultipartUtils.getCharEncoding(headers)); }catch (Exception e){ throw new IllegalStateException("Unable to read the form parameter value", e); } } private  MultipartUtils(); static boolean isMultipart(final String contentTypeHeaderValue); static boolean hasMultipartContentType(final Map<String, List<String>> headers); static String getContentType(final Map<String, List<String>> headers); static long getContentLength(final Map<String, List<String>> headers); static String getCharEncoding(final Map<String, List<String>> headers); static List<String> getHeaders(final String headerName, final Map<String, List<String>> headers); static String getHeader(final String headerName, final Map<String, List<String>> headers); static boolean isFormField(final Map<String, List<String>> headers, final MultipartContext context); static String readFormParameterValue(final StreamStorage streamStorage, final Map<String, List<String>> headers); static String getFileName(final Map<String, List<String>> headers); static String getFieldName(final Map<String, List<String>> headers); static boolean isContentTransferEncodingBase64Encoded(final Map<String, List<String>> partHeaders); static byte[] getBoundary(final String contentType); static final byte DASH; static final byte CR; static final byte LF; static final byte[] HEADER_DELIMITER; static final String MULTIPART; static final String CONTENT_DISPOSITION; static final String CONTENT_TRANSFER_ENCODING; static final String CONTENT_LENGTH; static final String CONTENT_TYPE; static final String FORM_DATA; static final String ATTACHMENT; static final String MULTIPART_FORM_DATA; static final String MULTIPART_MIXED; static final String TEXT_PLAIN; }
@Test public void testReadFormParameterValue() throws Exception { StreamStorage streamStorage = Mockito.mock(StreamStorage.class); Mockito.when(streamStorage.getInputStream()).thenReturn(new ByteArrayInputStream("Body data".getBytes())); String value = MultipartUtils.readFormParameterValue( streamStorage, new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain; charset=utf-8")); }} ); assertEquals("Body data", value); StreamStorage streamStorage1 = Mockito.mock(StreamStorage.class); Mockito.when(streamStorage1.getInputStream()).thenReturn(new ByteArrayInputStream("Body data".getBytes())); String value1 = MultipartUtils.readFormParameterValue( streamStorage1, new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain")); }} ); assertEquals("Body data", value1); }
@Test(expected = IllegalStateException.class) public void testReadFormParameterValue_error() throws Exception { StreamStorage streamStorage = Mockito.mock(StreamStorage.class); InputStream inputStream = Mockito.mock(InputStream.class); Mockito.when(inputStream.read()).thenThrow(new IOException()); Mockito.when(inputStream.read(Mockito.any(byte[].class))).thenThrow(new IOException()); Mockito.when(inputStream.read(Mockito.any(byte[].class), Mockito.anyInt(), Mockito.anyInt())).thenThrow(new IOException()); Mockito.when(streamStorage.getInputStream()).thenReturn(inputStream); String value = MultipartUtils.readFormParameterValue( streamStorage, new HashMap<String, List<String>>(){{ put(MultipartUtils.CONTENT_TYPE.toLowerCase(), Collections.singletonList("text/plain; charset=utf-8")); }} ); }