use of org.springframework.http.MediaType in project spring-cloud-function by spring-cloud.
the class FluxReturnValueHandler method handleReturnValue.
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
if (returnValue == null) {
mavContainer.setRequestHandled(true);
return;
}
Object adaptFrom = returnValue;
if (returnValue instanceof ResponseEntity) {
ResponseEntity<?> value = (ResponseEntity<?>) returnValue;
adaptFrom = value.getBody();
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
response.setStatus(value.getStatusCodeValue());
HttpHeaders headers = value.getHeaders();
for (String name : headers.keySet()) {
List<String> list = headers.get(name);
for (String header : list) {
response.addHeader(name, header);
}
}
}
Publisher<?> flux = (Publisher<?>) adaptFrom;
Object handler = webRequest.getAttribute(WebRequestConstants.HANDLER, NativeWebRequest.SCOPE_REQUEST);
Class<?> type = inspector.getOutputType(handler);
if (isOutputSingle(webRequest, handler, type)) {
Object result = Flux.from(flux).blockFirst();
if (result instanceof Message) {
Message<?> message = (Message<?>) result;
result = message.getPayload();
addHeaders(webRequest, message);
}
single.handleReturnValue(result, singleReturnType, mavContainer, webRequest);
return;
}
MediaType mediaType = null;
if (isPlainText(webRequest) && (CharSequence.class.isAssignableFrom(type) || Void.class.isAssignableFrom(type))) {
mediaType = MediaType.TEXT_PLAIN;
} else {
mediaType = findMediaType(webRequest);
}
if (logger.isDebugEnabled()) {
logger.debug("Handling return value " + type + " with media type: " + mediaType);
}
ServletServerHttpRequest request = new ServletServerHttpRequest(webRequest.getNativeRequest(HttpServletRequest.class));
delegate.handleReturnValue(getEmitter(timeout, flux, mediaType, request.getHeaders()), returnType, mavContainer, webRequest);
}
use of org.springframework.http.MediaType in project irida by phac-nml.
the class SequenceFileMessageConverterTest method canConvertFastqToString.
@Test
public void canConvertFastqToString() {
Class<String> clazz = String.class;
MediaType mediaType = new MediaType("application", "fastq");
assertFalse(converter.canRead(clazz, mediaType));
}
use of org.springframework.http.MediaType in project ma-core-public by infiniteautomation.
the class JsonUsernamePasswordAuthenticationFilter method requiresAuthentication.
@Override
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
String contentType = request.getContentType();
if (contentType == null || contentType.isEmpty())
return false;
boolean pathMatches = super.requiresAuthentication(request, response);
if (!pathMatches) {
return false;
}
MediaType requestMediaType;
try {
requestMediaType = MediaType.valueOf(contentType);
} catch (Exception e) {
return false;
}
return requestMediaType.isCompatibleWith(MediaType.APPLICATION_JSON);
}
use of org.springframework.http.MediaType in project ma-core-public by infiniteautomation.
the class ExceptionCsvMessageConverter method writeInternal.
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
JsonEncoding encoding = getJsonEncoding(contentType);
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
try {
CsvSchema schema;
if (object instanceof AbstractRestV2Exception) {
schema = this.restExceptionSchema;
object = new CsvRestException((AbstractRestV2Exception) object);
} else {
schema = this.exceptionSchema;
}
writePrefix(generator, object);
ObjectWriter objectWriter = this.objectMapper.writer().with(schema);
objectWriter.writeValue(generator, object);
writeSuffix(generator, object);
generator.flush();
} catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
}
}
use of org.springframework.http.MediaType in project TranslateIt-api by zunpiau.
the class WebConfig method configureMessageConverters.
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ArrayList<MediaType> mediaTypes = new ArrayList<>(2);
mediaTypes.add(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8));
mediaTypes.add(new MediaType(MediaType.TEXT_HTML, StandardCharsets.UTF_8));
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
stringHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
jackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(stringHttpMessageConverter);
converters.add(jackson2HttpMessageConverter);
}
Aggregations