List of usage examples for org.springframework.http MediaType getParameter
@Nullable
public String getParameter(String name)
From source file:org.terasoluna.gfw.functionaltest.app.download.DownloadTest.java
@Test public void test01_02_fileDownload() { ResponseEntity<String> response = restTemplate.getForEntity(applicationContextUrl + "/download/1_2", String.class); HttpHeaders headers = response.getHeaders(); System.out.println("test01_02_fileDownload: X-Track=" + headers.getFirst("X-Track")); assertThat(headers.getFirst("Content-Disposition"), is("attachment; filename=framework.txt")); MediaType contentType = headers.getContentType(); assertThat(contentType.getType(), is("text")); assertThat(contentType.getSubtype(), is("plain")); assertThat(contentType.getParameter("charset"), equalToIgnoringCase("UTF-8")); assertThat(response.getBody(), is("Spring Framework")); }
From source file:org.springframework.http.codec.multipart.DefaultMultipartMessageReader.java
@Nullable private static byte[] boundary(HttpMessage message) { MediaType contentType = message.getHeaders().getContentType(); if (contentType != null) { String boundary = contentType.getParameter("boundary"); if (boundary != null) { return boundary.getBytes(StandardCharsets.ISO_8859_1); }//w w w . ja v a 2s . c om } return null; }
From source file:org.springframework.http.converter.FormHttpMessageConverterTests.java
@Test public void writeMultipart() throws Exception { MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("name 1", "value 1"); parts.add("name 2", "value 2+1"); parts.add("name 2", "value 2+2"); parts.add("name 3", null); Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); parts.add("logo", logo); // SPR-12108/* w ww .j av a2s .c o m*/ Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") { @Override public String getFilename() { return "Hall\u00F6le.jpg"; } }; parts.add("utf8", utf8); Source xml = new StreamSource(new StringReader("<root><child/></root>")); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(MediaType.TEXT_XML); HttpEntity<Source> entity = new HttpEntity<>(xml, entityHeaders); parts.add("xml", entity); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage); final MediaType contentType = outputMessage.getHeaders().getContentType(); assertNotNull("No boundary found", contentType.getParameter("boundary")); // see if Commons FileUpload can read what we wrote FileItemFactory fileItemFactory = new DiskFileItemFactory(); FileUpload fileUpload = new FileUpload(fileItemFactory); RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage); List<FileItem> items = fileUpload.parseRequest(requestContext); assertEquals(6, items.size()); FileItem item = items.get(0); assertTrue(item.isFormField()); assertEquals("name 1", item.getFieldName()); assertEquals("value 1", item.getString()); item = items.get(1); assertTrue(item.isFormField()); assertEquals("name 2", item.getFieldName()); assertEquals("value 2+1", item.getString()); item = items.get(2); assertTrue(item.isFormField()); assertEquals("name 2", item.getFieldName()); assertEquals("value 2+2", item.getString()); item = items.get(3); assertFalse(item.isFormField()); assertEquals("logo", item.getFieldName()); assertEquals("logo.jpg", item.getName()); assertEquals("image/jpeg", item.getContentType()); assertEquals(logo.getFile().length(), item.getSize()); item = items.get(4); assertFalse(item.isFormField()); assertEquals("utf8", item.getFieldName()); assertEquals("Hall\u00F6le.jpg", item.getName()); assertEquals("image/jpeg", item.getContentType()); assertEquals(logo.getFile().length(), item.getSize()); item = items.get(5); assertEquals("xml", item.getFieldName()); assertEquals("text/xml", item.getContentType()); verify(outputMessage.getBody(), never()).close(); }
From source file:org.springframework.http.converter.FormHttpMessageConverterTests.java
@Test public void writeMultipartOrder() throws Exception { MyBean myBean = new MyBean(); myBean.setString("foo"); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("part1", myBean); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(MediaType.TEXT_XML); HttpEntity<MyBean> entity = new HttpEntity<>(myBean, entityHeaders); parts.add("part2", entity); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.converter.setMultipartCharset(StandardCharsets.UTF_8); this.converter.write(parts, new MediaType("multipart", "form-data", StandardCharsets.UTF_8), outputMessage); final MediaType contentType = outputMessage.getHeaders().getContentType(); assertNotNull("No boundary found", contentType.getParameter("boundary")); // see if Commons FileUpload can read what we wrote FileItemFactory fileItemFactory = new DiskFileItemFactory(); FileUpload fileUpload = new FileUpload(fileItemFactory); RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage); List<FileItem> items = fileUpload.parseRequest(requestContext); assertEquals(2, items.size());// ww w.j a v a 2 s. co m FileItem item = items.get(0); assertTrue(item.isFormField()); assertEquals("part1", item.getFieldName()); assertEquals("{\"string\":\"foo\"}", item.getString()); item = items.get(1); assertTrue(item.isFormField()); assertEquals("part2", item.getFieldName()); // With developer builds we get: <MyBean><string>foo</string></MyBean> // But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean> // So... we make a compromise: assertThat(item.getString(), allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>"))); }
From source file:org.springframework.integration.x.channel.registry.ChannelRegistrySupport.java
private Object transformPayloadForInputChannel(Object payload, String contentType, Collection<MediaType> to) { if (payload instanceof byte[]) { Object result = null;/*from w ww.ja va2s. c o m*/ // Get the preferred java type, if any, and first try to decode directly from JSON. MediaType toObjectType = findJavaObjectType(to); if (XD_JSON_OCTET_STREAM_VALUE.equals(contentType)) { if (toObjectType == null || toObjectType.getParameter("type") == null) { try { result = this.jsonMapper.fromBytes((byte[]) payload); } catch (ConversionException e) { if (logger.isDebugEnabled()) { logger.debug("JSON decode failed, raw byte[]?"); } } } else { String preferredClass = toObjectType.getParameter("type"); try { // If this fails, fall back to generic decoding and delegate object conversion to the // conversionService result = this.jsonMapper.fromBytes((byte[]) payload, preferredClass); } catch (ConversionException e) { try { if (logger.isDebugEnabled()) { logger.debug("JSON decode failed to convert to requested type: " + preferredClass + " - will try to decode to original type"); } result = this.jsonMapper.fromBytes((byte[]) payload); } catch (ConversionException ce) { if (logger.isDebugEnabled()) { logger.debug("JSON decode failed, raw byte[]?"); } } } } if (result != null) { if (to.contains(MediaType.ALL)) { return result; } // TODO: currently only tries the first application/x-java-object;type=foo.Foo toObjectType = findJavaObjectType(to); if (toObjectType != null) { if (toObjectType.getParameter("type") == null) { return result; } String resultClass = result.getClass().getName(); if (resultClass.equals(toObjectType.getParameter("type"))) { return result; } // recursive call return transformPayloadForInputChannel(result, contentType, Collections.singletonList(toObjectType)); } } } else if (XD_TEXT_PLAIN_UTF8_VALUE.equals(contentType)) { try { return new String((byte[]) payload, "UTF-8"); } catch (UnsupportedEncodingException e) { logger.error("Could not convert bytes to String", e); } } else if (XD_OCTET_STREAM_VALUE.equals(contentType)) { return payload; } } if (to.contains(MediaType.ALL)) { return payload; } return convert(payload, to); }
From source file:org.springframework.integration.x.channel.registry.ChannelRegistrySupport.java
private Object convert(Object payload, Collection<MediaType> to) { if (this.conversionService != null) { MediaType requiredMediaType = findJavaObjectType(to); if (requiredMediaType != null) { String requiredType = requiredMediaType.getParameter("type"); if (requiredType == null) { return payload; }/*from w w w . ja v a 2 s . c o m*/ Class<?> clazz = null; try { clazz = this.beanClassloader.loadClass(requiredType); } catch (ClassNotFoundException e) { if (logger.isDebugEnabled()) { logger.debug("Class not found", e); } } if (clazz != null) { if (this.conversionService.canConvert(payload.getClass(), clazz)) { return this.conversionService.convert(payload, clazz); } } } else { if (this.acceptsString(to)) { if (this.conversionService.canConvert(payload.getClass(), String.class)) { return this.conversionService.convert(payload, String.class); } } } } return null; }