use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.
the class RecordReaderHttpMessageConverter method read.
@Override
public RecordReader read(final Class<? extends RecordReader> clazz, final HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
try {
final HttpHeaders headers = inputMessage.getHeaders();
final MediaType mediaType = headers.getContentType();
Charset charset = mediaType.getCharSet();
if (charset == null) {
charset = StandardCharsets.UTF_8;
}
final InputStream body = inputMessage.getBody();
final String mediaTypeString = mediaType.getType() + "/" + mediaType.getSubtype();
final RecordReaderFactory readerFactory = IoFactory.factoryByMediaType(RecordReaderFactory.class, mediaTypeString);
if (readerFactory == null) {
throw new HttpMessageNotReadableException("Cannot read data in format" + mediaType);
} else {
final Reader<Record> reader = readerFactory.newRecordReader(new InputStreamResource("recordInput", body));
GeometryFactory factory = this.geometryFactory;
final ServletWebRequest requestAttributes = (ServletWebRequest) RequestContextHolder.getRequestAttributes();
final String srid = requestAttributes.getParameter("srid");
if (srid != null && srid.trim().length() > 0) {
factory = GeometryFactory.floating3d(Integer.parseInt(srid));
}
reader.setProperty(IoConstants.GEOMETRY_FACTORY, factory);
return (RecordReader) reader;
}
} catch (final IOException e) {
throw new HttpMessageNotReadableException("Error reading data", e);
}
}
use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.
the class MediaTypeUtil method isHtmlPage.
public static boolean isHtmlPage() {
final HttpServletRequest request = HttpServletUtils.getRequest();
final String format = request.getParameter("format");
if (Property.hasValue(format)) {
return false;
}
final String requestUri = URL_PATH_HELPER.getRequestUri(request);
final String filename = WebUtils.extractFullFilenameFromUrlPath(requestUri);
final String extension = StringUtils.getFilenameExtension(filename);
if (Property.hasValue(extension)) {
return false;
}
final String acceptHeader = request.getHeader(ACCEPT_HEADER);
if (Property.hasValue(acceptHeader)) {
for (final MediaType mediaType : MediaType.parseMediaTypes(acceptHeader)) {
if (mediaType.includes(MediaType.TEXT_HTML)) {
return true;
}
}
return false;
} else {
return true;
}
}
use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.
the class MediaTypeUtil method getAcceptedFileNameExtension.
public static String getAcceptedFileNameExtension() {
final HttpServletRequest request = HttpServletUtils.getRequest();
final List<MediaType> mediaTypes = getAcceptedMediaTypes(request);
for (final MediaType mediaType : mediaTypes) {
final String extension = mediaTypeToExtensionMap.get(mediaType);
if (Property.hasValue(extension)) {
return extension;
}
}
return "html";
}
use of org.springframework.http.MediaType in project com.revolsys.open by revolsys.
the class WebAnnotationMethodHandlerAdapter method handleResponseBody.
private View handleResponseBody(final Object returnValue, final ServletWebRequest webRequest) throws ServletException, IOException {
final HttpServletRequest request = webRequest.getRequest();
String jsonp = request.getParameter("jsonp");
if (jsonp == null) {
jsonp = request.getParameter("callback");
}
request.setAttribute(IoConstants.JSONP_PROPERTY, jsonp);
List<MediaType> acceptedMediaTypes = MediaTypeUtil.getAcceptedMediaTypes(request, WebAnnotationMethodHandlerAdapter.this.mediaTypes, WebAnnotationMethodHandlerAdapter.this.mediaTypeOrder, WebAnnotationMethodHandlerAdapter.this.urlPathHelper, WebAnnotationMethodHandlerAdapter.this.parameterName, WebAnnotationMethodHandlerAdapter.this.defaultMediaType);
if (acceptedMediaTypes.isEmpty()) {
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
}
final Class<?> returnValueType = returnValue.getClass();
final Set<MediaType> allSupportedMediaTypes = new LinkedHashSet<>();
if (WebAnnotationMethodHandlerAdapter.this.messageConverters != null) {
for (final MediaType acceptedMediaType : acceptedMediaTypes) {
for (final HttpMessageConverter<?> messageConverter : WebAnnotationMethodHandlerAdapter.this.messageConverters) {
allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
if (messageConverter.canWrite(returnValueType, acceptedMediaType)) {
final MediaType mediaType = getMediaType(messageConverter.getSupportedMediaTypes(), acceptedMediaType);
return new HttpMessageConverterView(messageConverter, mediaType, returnValue);
}
}
}
}
throw new HttpMediaTypeNotAcceptableException(new ArrayList<>(allSupportedMediaTypes));
}
use of org.springframework.http.MediaType in project rest-assured by rest-assured.
the class MockMvcRequestSenderImpl method setContentTypeToApplicationFormUrlEncoded.
private void setContentTypeToApplicationFormUrlEncoded(MockHttpServletRequestBuilder request) {
String contentType = APPLICATION_FORM_URLENCODED_VALUE;
EncoderConfig encoderConfig = config.getEncoderConfig();
if (encoderConfig.shouldAppendDefaultContentCharsetToContentTypeIfUndefined()) {
contentType += "; charset=";
if (encoderConfig.hasDefaultCharsetForContentType(contentType)) {
contentType += encoderConfig.defaultCharsetForContentType(contentType);
} else {
contentType += encoderConfig.defaultContentCharset();
}
}
MediaType mediaType = MediaType.parseMediaType(contentType);
request.contentType(mediaType);
List<Header> newHeaders = new ArrayList<Header>(headers.asList());
newHeaders.add(new Header(CONTENT_TYPE, mediaType.toString()));
headers = new Headers(newHeaders);
}
Aggregations