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

The following examples show how to use org.springframework.http.MediaType#getType() . 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: 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 3
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 4
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 5
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;
}