org.springframework.http.MediaType#getSubtype ( )源码实例Demo

下面列出了org.springframework.http.MediaType#getSubtype ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
    * Returns the basic content info from the request.
    * @param req WebScriptRequest
    * @return BasicContentInfo
    */
   private BasicContentInfo getContentInfo(WebScriptRequest req) {
   	
	String encoding = "UTF-8";
	String contentType = MimetypeMap.MIMETYPE_BINARY;
	
	if (StringUtils.isNotEmpty(req.getContentType()))
	{
		MediaType media = MediaType.parseMediaType(req.getContentType());
		contentType = media.getType()+'/'+media.getSubtype();
		if (media.getCharset() != null)
		{
			encoding = media.getCharset().toString();
		}			
	}

       return new ContentInfoImpl(contentType, encoding, -1, Locale.getDefault());
}
 
public void checkContentType(String contentType) {
    try {
            MediaType media = MediaType.parseMediaType(contentType);
            // TODO improve the logic here
            if (media.getSubtype() != null && !media.getSubtype().contains("xml") && !media.getSubtype().contains("fhir") && !media.getSubtype().contains("json") && !media.getSubtype().contains("plain")) {
                log.debug("Unsupported media type: " + contentType);
                throw new InvalidRequestException("Unsupported media type: sub " + contentType);
            } else {
                if (!contentType.contains("xml") && !contentType.contains("json")) {
                    log.debug("Unsupported media type: " + contentType);
                    throw new InvalidRequestException("Unsupported media type: content " + contentType);
                }
            }

    } catch (InvalidMediaTypeException e) {
        log.debug("Unsupported media type: " + contentType);
        // KGM 26/02/2018 Don't throw error for xml and json
        if (!contentType.contains("xml") && !contentType.contains("json")) {
            log.debug("Unsupported media type: " + contentType);
            throw new InvalidRequestException("Unsupported media type: content " + contentType);
        }
    }
}
 
private boolean hasTextBody(HttpHeaders headers) {
    long contentLength = headers.getContentLength();
    if (contentLength != 0) {
        MediaType contentType = headers.getContentType();
        if (contentType != null) {
            String subtype = contentType.getSubtype();
            return "text".equals(contentType.getType()) || "xml".equals(subtype) || "json".equals(subtype);
        }
    }
    return false;
}
 
源代码4 项目: spring-boot-cookbook   文件: RestTemplateConfig.java
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
    long begin = System.currentTimeMillis();
    String httpMethodName = UNKNOWN_HTTP_METHOD;
    HttpMethod method = request.getMethod();
    if (method != null) {
        httpMethodName = method.name();
    }
    String requestBody = IOUtils.toString(body, "utf-8");
    try {
        ClientHttpResponse response = execution.execute(request, body);
        long cost = System.currentTimeMillis() - begin;

        MediaType contentType = response.getHeaders().getContentType();
        String responseContent;
        if (contentType != null) {
            String subtype = contentType.getSubtype();
            if (subtype.contains("json") || contentType.getType().contains("text") || subtype.contains("xml")) {
                responseContent = IOUtils.toString(response.getBody(), "utf-8");
            } else {
                responseContent = "neither text,nor xml,nor json";
            }
        } else {
            responseContent = HAS_NO_CONTENT_TYPE;
        }

        if (cost > 3000) {
            log.info("【特殊标识】慢接口 【 TODOLIST 】 大于3秒 cost:[{}]ms,{} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
        } else if (cost > 2000) {
            log.info("【特殊标识】慢接口 【 TODOLIST 】 大于2秒 cost:[{}]ms,{} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
        } else {
            log.info("cost:[{}]ms,on {} {}. request:{},response:{},", cost, httpMethodName, request.getURI().toString(), requestBody, responseContent);
        }
        return response;
    } catch (IOException e) {
        log.error("【特殊标识】【 TODOLIST 】 接口 cost:[{}]ms,I/O error on {} {}. request:{},response:{},", (System.currentTimeMillis() - begin), httpMethodName, request.getURI().toString(), requestBody, e.getMessage());
        throw e;
    }

}
 
@Override
public HttpHeaders getHeaders() {
	if (this.headers == null) {
		this.headers = new HttpHeaders();

		for (Enumeration<?> names = this.servletRequest.getHeaderNames(); names.hasMoreElements();) {
			String headerName = (String) names.nextElement();
			for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName);
					headerValues.hasMoreElements();) {
				String headerValue = (String) headerValues.nextElement();
				this.headers.add(headerName, headerValue);
			}
		}

		// HttpServletRequest exposes some headers as properties:
		// we should include those if not already present
		try {
			MediaType contentType = this.headers.getContentType();
			if (contentType == null) {
				String requestContentType = this.servletRequest.getContentType();
				if (StringUtils.hasLength(requestContentType)) {
					contentType = MediaType.parseMediaType(requestContentType);
					this.headers.setContentType(contentType);
				}
			}
			if (contentType != null && contentType.getCharset() == null) {
				String requestEncoding = this.servletRequest.getCharacterEncoding();
				if (StringUtils.hasLength(requestEncoding)) {
					Charset charSet = Charset.forName(requestEncoding);
					Map<String, String> params = new LinkedCaseInsensitiveMap<>();
					params.putAll(contentType.getParameters());
					params.put("charset", charSet.toString());
					MediaType mediaType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
					this.headers.setContentType(mediaType);
				}
			}
		}
		catch (InvalidMediaTypeException ex) {
			// Ignore: simply not exposing an invalid content type in HttpHeaders...
		}

		if (this.headers.getContentLength() < 0) {
			int requestContentLength = this.servletRequest.getContentLength();
			if (requestContentLength != -1) {
				this.headers.setContentLength(requestContentLength);
			}
		}
	}

	return this.headers;
}
 
@Override
public HttpHeaders getHeaders() {
	if (this.headers == null) {
		this.headers = new HttpHeaders();

		for (Enumeration<?> names = this.servletRequest.getHeaderNames(); names.hasMoreElements();) {
			String headerName = (String) names.nextElement();
			for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName);
					headerValues.hasMoreElements();) {
				String headerValue = (String) headerValues.nextElement();
				this.headers.add(headerName, headerValue);
			}
		}

		// HttpServletRequest exposes some headers as properties:
		// we should include those if not already present
		try {
			MediaType contentType = this.headers.getContentType();
			if (contentType == null) {
				String requestContentType = this.servletRequest.getContentType();
				if (StringUtils.hasLength(requestContentType)) {
					contentType = MediaType.parseMediaType(requestContentType);
					this.headers.setContentType(contentType);
				}
			}
			if (contentType != null && contentType.getCharset() == null) {
				String requestEncoding = this.servletRequest.getCharacterEncoding();
				if (StringUtils.hasLength(requestEncoding)) {
					Charset charSet = Charset.forName(requestEncoding);
					Map<String, String> params = new LinkedCaseInsensitiveMap<>();
					params.putAll(contentType.getParameters());
					params.put("charset", charSet.toString());
					MediaType mediaType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
					this.headers.setContentType(mediaType);
				}
			}
		}
		catch (InvalidMediaTypeException ex) {
			// Ignore: simply not exposing an invalid content type in HttpHeaders...
		}

		if (this.headers.getContentLength() < 0) {
			int requestContentLength = this.servletRequest.getContentLength();
			if (requestContentLength != -1) {
				this.headers.setContentLength(requestContentLength);
			}
		}
	}

	return this.headers;
}
 
源代码7 项目: lams   文件: ServletServerHttpRequest.java
@Override
public HttpHeaders getHeaders() {
	if (this.headers == null) {
		this.headers = new HttpHeaders();

		for (Enumeration<?> headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements();) {
			String headerName = (String) headerNames.nextElement();
			for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName);
					headerValues.hasMoreElements();) {
				String headerValue = (String) headerValues.nextElement();
				this.headers.add(headerName, headerValue);
			}
		}

		// HttpServletRequest exposes some headers as properties: we should include those if not already present
		try {
			MediaType contentType = this.headers.getContentType();
			if (contentType == null) {
				String requestContentType = this.servletRequest.getContentType();
				if (StringUtils.hasLength(requestContentType)) {
					contentType = MediaType.parseMediaType(requestContentType);
					this.headers.setContentType(contentType);
				}
			}
			if (contentType != null && contentType.getCharset() == null) {
				String requestEncoding = this.servletRequest.getCharacterEncoding();
				if (StringUtils.hasLength(requestEncoding)) {
					Charset charSet = Charset.forName(requestEncoding);
					Map<String, String> params = new LinkedCaseInsensitiveMap<String>();
					params.putAll(contentType.getParameters());
					params.put("charset", charSet.toString());
					MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
					this.headers.setContentType(newContentType);
				}
			}
		}
		catch (InvalidMediaTypeException ex) {
			// Ignore: simply not exposing an invalid content type in HttpHeaders...
		}

		if (this.headers.getContentLength() < 0) {
			int requestContentLength = this.servletRequest.getContentLength();
			if (requestContentLength != -1) {
				this.headers.setContentLength(requestContentLength);
			}
		}
	}

	return this.headers;
}
 
@Override
public HttpHeaders getHeaders() {
	if (this.headers == null) {
		this.headers = new HttpHeaders();
		for (Enumeration<?> headerNames = this.servletRequest.getHeaderNames(); headerNames.hasMoreElements();) {
			String headerName = (String) headerNames.nextElement();
			for (Enumeration<?> headerValues = this.servletRequest.getHeaders(headerName);
					headerValues.hasMoreElements();) {
				String headerValue = (String) headerValues.nextElement();
				this.headers.add(headerName, headerValue);
			}
		}
		// HttpServletRequest exposes some headers as properties: we should include those if not already present
		MediaType contentType = this.headers.getContentType();
		if (contentType == null) {
			String requestContentType = this.servletRequest.getContentType();
			if (StringUtils.hasLength(requestContentType)) {
				contentType = MediaType.parseMediaType(requestContentType);
				this.headers.setContentType(contentType);
			}
		}
		if (contentType != null && contentType.getCharSet() == null) {
			String requestEncoding = this.servletRequest.getCharacterEncoding();
			if (StringUtils.hasLength(requestEncoding)) {
				Charset charSet = Charset.forName(requestEncoding);
				Map<String, String> params = new LinkedCaseInsensitiveMap<String>();
				params.putAll(contentType.getParameters());
				params.put("charset", charSet.toString());
				MediaType newContentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
				this.headers.setContentType(newContentType);
			}
		}
		if (this.headers.getContentLength() == -1) {
			int requestContentLength = this.servletRequest.getContentLength();
			if (requestContentLength != -1) {
				this.headers.setContentLength(requestContentLength);
			}
		}
	}
	return this.headers;
}
 
源代码9 项目: convergent-ui   文件: TextHttpMessageConverter.java
@Override
public boolean canRead(Class<?> type, MediaType mt) {
    return mt != null && ("text".equalsIgnoreCase(mt.getType()) || (mt.getSubtype() != null && mt.getSubtype().contains("javascript")));
}