MultipartHeadersPartParser implements Parser<MultipartHeadersPart> { @Override public MultipartHeadersPart parse(final String headersString) throws MalformedInputException { MultipartHeadersPart part = new MultipartHeadersPart(); Headers headers = parser.parse(headersString, false); String contentDispositionHeaderValue = headers.getHeader(Headers.HEADER_CONTENT_DISPOSITION); if (contentDispositionHeaderValue != null) { String contentDispositionLower = contentDispositionHeaderValue.toLowerCase(); int nameStartPos = contentDispositionLower.indexOf(NAME_START); if (nameStartPos > -1) { String name = contentDispositionHeaderValue.substring(nameStartPos + NAME_START.length()); int quotationMarkPosition = name.indexOf("\""); if (quotationMarkPosition == -1) { throw new MalformedInputException("Malformed header, unable to detect value beginning"); } else { name = name.substring(0, quotationMarkPosition); } part.setName(name); } int fileNameStartPos = contentDispositionLower.indexOf(FILENAME_START); if (fileNameStartPos > -1) { String fileName = contentDispositionHeaderValue.substring(fileNameStartPos + FILENAME_START.length()); int quotationMark2Position = fileName.indexOf("\""); if (quotationMark2Position == -1) { throw new MalformedInputException("Malformed header, unable to detect value end"); } else { fileName = fileName.substring(0, quotationMark2Position); } part.setFileName(fileName); } } part.setContentType(headers.getHeader(Headers.HEADER_CONTENT_TYPE)); return part; } MultipartHeadersPartParser(final HeadersParser headersParser); @Override MultipartHeadersPart parse(final String headersString);  }
@Test public void shouldParseValidAttachmentHeader() throws MalformedInputException { MultipartHeadersPart headers = multipartHeadersPartParser .parse("Content-Disposition: attachment; name=\"FIELDNAME\"; filename=\"FILE.PDF\"\nContent-type: application/pdf"); assertThat(headers.getFileName(), is("FILE.PDF")); assertThat(headers.getName(), is("FIELDNAME")); assertThat(headers.getContentType(), is("application/pdf")); }
@Test public void shouldParseValidAttachmentHeaderCaseInsensitive() throws MalformedInputException { MultipartHeadersPart headers = multipartHeadersPartParser .parse("CONTENT-DISPOSITION: attachment; NAME=\"FIELDNAME\"; FILENAME=\"FILE.PDF\"\nContent-TYPE: application/pdf"); assertThat(headers.getFileName(), is("FILE.PDF")); assertThat(headers.getName(), is("FIELDNAME")); assertThat(headers.getContentType(), is("application/pdf")); }
@Test public void shouldParseFormDataText() throws MalformedInputException { MultipartHeadersPart headers = multipartHeadersPartParser.parse("Content-Disposition: form-data; name=\"text\""); assertThat(headers.getFileName(), is(nullValue())); assertThat(headers.getName(), is("text")); assertThat(headers.getContentType(), is(nullValue())); }
@Test public void shouldParseFormDataTextWhenThereIsNoName() throws MalformedInputException { MultipartHeadersPart headers = multipartHeadersPartParser.parse("Content-Disposition: form-data;"); assertThat(headers.getFileName(), is(nullValue())); assertThat(headers.getName(), is(nullValue())); assertThat(headers.getContentType(), is(nullValue())); }
@Test(expected = MalformedInputException.class) public void shouldThrowMalformedInputException() throws MalformedInputException { multipartHeadersPartParser.parse("Content-Disposition: form-data; name=\"text\" filename=\"text"); }
@Test(expected = MalformedInputException.class) public void shouldThrowMalformedInputExceptionForMissingClosing() throws MalformedInputException { multipartHeadersPartParser.parse("Content-Disposition: form-data; name=\"text"); }