ResourceController { @SuppressWarnings("unchecked") private void putContentDisposition(RequestMessage requestMessage, ResponseMessage responseMessage) { Object downloadRequestObj = requestMessage.get(messageMetadata.REQUEST_FILE_DOWNLOAD); if (downloadRequestObj == null) { return; } Map<String, Object> headers; Object headersObj = responseMessage.get(messageMetadata.RESPONSE_HEADER); if (headersObj == null) { headers = new HashMap<>(); responseMessage.put(messageMetadata.RESPONSE_HEADER, headers); } else { headers = (Map<String, Object>) headersObj; } try { if (Boolean.valueOf((String) downloadRequestObj)) { String fileName = (String) responseMessage.get(messageMetadata.RESPONSE_DOWNLOAD_FILE_NAME); if (fileName == null || fileName.isEmpty()) { throw new GenericResourceException( "When resource class returns file data, response message need to include RESPONSE_DOWNLOAD_FILE_NAME metadata."); } String userAgent = (String) requestMessage.get(messageMetadata.HTTP_HEADER_USER_AGENT); String fileNameStr = null; if (userAgent.contains(IE_UA_CONTAINS)) { fileNameStr = "filename=\"" + URLEncoder.encode(fileName, "UTF8").replace("+", "%20") + "\""; } else { fileNameStr = "filename*=utf-8'ja'" + URLEncoder.encode(fileName, "UTF8").replace("+", "%20"); } String contentDispositionValue = "attachment;" + fileNameStr; headers.put(messageMetadata.HTTP_HEADER_CONTENT_DISPOSITION, contentDispositionValue); } } catch (UnsupportedEncodingException e) { throw new GenericResourceException("bad charactor encoding on response file name."); } }  @ResourceHandler ResponseMessageContainer handle(WebRequest webRequest, RequestMessageContainer requestMessages); void setResourceProcessor(ResourceProcessor resourceProcessor); void setMessageMetadata(MessageMetadata messageMetadata); void setResourceConfigurationParameter(ResourceConfigurationParameter resourceConfigurationParameter);  }
@Test public void testPutContentDisposition(final @Mocked WebRequest webRequest) throws Exception { final ResourceController target = new ResourceController(); target.setMessageMetadata(messageMetadata); target.setResourceConfigurationParameter(resourceConfigurationParameter); final RequestMessage reqMsg = new RequestMessage(messageMetadata) { { put(messageMetadata.ACTION, "findById"); put(messageMetadata.HTTP_METHOD, "GET"); put(messageMetadata.REQUEST_FILE_DOWNLOAD, "true"); } }; final RequestMessageContainer reqContainer = new RequestMessageContainer(false) { { addMessage(reqMsg); } }; final ResponseMessage resMsg1 = new ResponseMessage(reqMsg) { { put(messageMetadata.RESPONSE_DOWNLOAD_FILE_NAME, "日本語 ファイル名"); } }; final ResponseMessageContainer expected = new ResponseMessageContainer(false) { { addMessage(resMsg1); } }; new Expectations() { { setField(target, resourceProcessor); webRequest.getUserPrincipal(); result = principal; requestMessageContext.put(messageMetadata.USER_PRINCIPAL, principal, MessageSource.CONTROLLER); resourceProcessor.process(reqContainer); result = expected; responseMessageContext.get(messageMetadata.RESPONSE_HEADER); result = null; responseMessageContext.get(messageMetadata.HTTP_HEADER_USER_AGENT); result = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7"; } }; ResponseMessageContainer actual = target.handle(webRequest, reqContainer); ResponseMessage responseMessage = actual.getMessages().get(0); assertThat(responseMessage.get(messageMetadata.RESPONSE_HEADER), is(not(nullValue()))); @SuppressWarnings("unchecked") Map<String, Object> headers = (Map<String, Object>) responseMessage.get(messageMetadata.RESPONSE_HEADER); assertThat(headers.get(messageMetadata.HTTP_HEADER_CONTENT_DISPOSITION), is(not(nullValue()))); String urlEncodedFileName = URLEncoder.encode("日本語 ファイル名", "UTF8").replace("+", "%20"); String contentDisposition = (String) headers.get(messageMetadata.HTTP_HEADER_CONTENT_DISPOSITION); assertThat(contentDisposition, is(equalTo("attachment;filename*=utf-8'ja'" + urlEncodedFileName))); }