Java Code Examples for org.springframework.http.MediaType#getSubtype()

The following examples show how to use org.springframework.http.MediaType#getSubtype() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceWebScriptPut.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
    * 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());
}
 
Example 2
Source File: ServerInterceptor.java    From careconnect-reference-implementation with Apache License 2.0 6 votes vote down vote up
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);
        }
    }
}
 
Example 3
Source File: LoggingClientHttpRequestInterceptor.java    From GreenSummer with GNU Lesser General Public License v2.1 5 votes vote down vote up
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;
}
 
Example 4
Source File: RestTemplateConfig.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
@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;
    }

}
 
Example 5
Source File: ServletServerHttpRequest.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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;
}
 
Example 6
Source File: ServletServerHttpRequest.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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;
}
 
Example 7
Source File: ServletServerHttpRequest.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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;
}
 
Example 8
Source File: ServletServerHttpRequest.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 9
Source File: TextHttpMessageConverter.java    From convergent-ui with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canRead(Class<?> type, MediaType mt) {
    return mt != null && ("text".equalsIgnoreCase(mt.getType()) || (mt.getSubtype() != null && mt.getSubtype().contains("javascript")));
}