/*
 * Copyright 2014 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 org.springframework.social.twitter.api.impl;

import java.io.IOException;
import java.io.OutputStream;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.MultiValueMap;

/**
 * Custom FormHttpMessageConverter that meets Twitter's non-RFC1738 escaping of asterisks ('*').
 * @author Craig Walls
 */
class TwitterEscapingFormHttpMessageConverter extends FormHttpMessageConverter {

	@Override
	public void write(MultiValueMap<String, ?> map, MediaType contentType, HttpOutputMessage outputMessage) 
			throws IOException, HttpMessageNotWritableException {
		// only do special escaping if this is a non-multipart request
		if (!isMultipart(map, contentType)) {
			super.write(map, contentType, new TwitterEscapingHttpOutputMessage(outputMessage));
		} else {
			super.write(map, contentType, outputMessage);
		}
	}
	
	private static class TwitterEscapingHttpOutputMessage implements HttpOutputMessage {
		private HttpOutputMessage target;

		public TwitterEscapingHttpOutputMessage(HttpOutputMessage target) {
			this.target = target;
		}
		
		public HttpHeaders getHeaders() {
			return target.getHeaders();
		}

		public OutputStream getBody() throws IOException {
			return new TwitterEscapingOutputStream(target.getBody());
		}
	}

	private static class TwitterEscapingOutputStream extends OutputStream {
		private OutputStream target;

		public TwitterEscapingOutputStream(OutputStream target) {
			this.target = target;
		}
		
		@Override
		public void write(int b) throws IOException {
			// If more exceptions to URL encoding are found, this can be made to be mode clever than a bunch of
			// else-if blocks. But until then, this is sufficient.
			if (b == '*') {
				target.write('%');
				target.write('2');
				target.write('A');
			} else {
				target.write(b);
			}
		}
	}

	// "borrowed" from FormHttpMessageConverter
	private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType) {
		if (contentType != null) {
			return MediaType.MULTIPART_FORM_DATA.equals(contentType);
		}
		for (String name : map.keySet()) {
			for (Object value : map.get(name)) {
				if (value != null && !(value instanceof String)) {
					return true;
				}
			}
		}
		return false;
	}

}


@@ -719,16 +719,100 @@
 tStream;
+%0Aimport java.nio.charset.Charset;%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -944,16 +944,89 @@
 iaType;%0A
+import org.springframework.http.converter.ByteArrayHttpMessageConverter;%0A
 import o
@@ -1077,32 +1077,96 @@
 ssageConverter;%0A
+import org.springframework.http.converter.HttpMessageConverter;%0A
 import org.sprin
@@ -1220,24 +1220,166 @@
 eException;%0A
+import org.springframework.http.converter.ResourceHttpMessageConverter;%0Aimport org.springframework.http.converter.StringHttpMessageConverter;%0A
 import org.s
@@ -1624,16 +1624,578 @@
 rter %7B%0A%0A
+%09TwitterEscapingFormHttpMessageConverter() %7B%0A%09%09setCharset(Charset.forName(%22UTF-8%22));%0A%09%09List%3CHttpMessageConverter%3C?%3E%3E partConverters = new ArrayList%3CHttpMessageConverter%3C?%3E%3E();%0A%09%09partConverters.add(new ByteArrayHttpMessageConverter());%0A%09%09StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName(%22UTF-8%22));%0A%09%09stringHttpMessageConverter.setWriteAcceptCharset(false);%0A%09%09partConverters.add(stringHttpMessageConverter);%0A%09%09partConverters.add(new ResourceHttpMessageConverter());%0A%09%09setPartConverters(partConverters);%0A%09%7D%0A%09%0A%09%0A
 %09@Overri

Set UTF-8 in TwitterEscapingFormHttpMessageConverter to fix encoding problem. SOCIALTW-37