All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.huaweicloud.dis.http.converter.StringHttpMessageConverter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huaweicloud.dis.http.converter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;

import com.huaweicloud.dis.util.StreamUtils;


/**
 * Implementation of {@link HttpMessageConverter} that can read strings.
 *
 */
public class StringHttpMessageConverter extends AbstractHttpMessageConverter {

	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");


	private final List availableCharsets;


	/**
	 * A default constructor that uses {@code "UTF-8"} as the default charset.
	 * @see #StringHttpMessageConverter(Charset)
	 */
	public StringHttpMessageConverter() {
		this(DEFAULT_CHARSET);
	}

	/**
	 * A constructor accepting a default charset to use if the requested content
	 * type does not specify one.
	 * 
	 * @param defaultCharset Default charset.
	 */
	public StringHttpMessageConverter(Charset defaultCharset) {
		super(defaultCharset, ContentType.TEXT_PLAIN);
		this.availableCharsets = new ArrayList(Charset.availableCharsets().values());
	}


	@Override
	public boolean supports(Class clazz) {
		return String.class == clazz;
	}

	@Override
	protected String readInternal(Class clazz, HttpEntity entity) throws IOException {
		Charset charset = getContentTypeCharset(ContentType.getOrDefault(entity));
		return StreamUtils.copyToString(entity.getContent(), charset);
	}

	@Override
	protected Long getContentLength(String str, ContentType contentType) {
		Charset charset = getContentTypeCharset(contentType);
		try {
			return (long) str.getBytes(charset.name()).length;
		}
		catch (UnsupportedEncodingException ex) {
			// should not occur
			throw new IllegalStateException(ex);
		}
	}


	/**
	 * Return the list of supported {@link Charset}s.
	 * 

By default, returns {@link Charset#availableCharsets()}. * Can be overridden in subclasses. * @return the list of accepted charsets */ protected List getAcceptedCharsets() { return this.availableCharsets; } private Charset getContentTypeCharset(ContentType contentType) { if (contentType != null && contentType.getCharset() != null) { return contentType.getCharset(); } else { return getDefaultCharset(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy