/*
 * Copyright 2002-2013 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.web.socket.sockjs.transport.handler;

import java.io.IOException;
import java.util.Arrays;

import com.fasterxml.jackson.databind.JsonMappingException;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.Assert;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.sockjs.SockJsException;
import org.springframework.web.socket.sockjs.transport.SockJsSession;
import org.springframework.web.socket.sockjs.transport.session.AbstractHttpSockJsSession;

/**
 * Base class for HTTP transport handlers that receive messages via HTTP POST.
 *
 * @author Rossen Stoyanchev
 * @since 4.0
 */
public abstract class AbstractHttpReceivingTransportHandler extends AbstractTransportHandler {

	@Override
	public final void handleRequest(ServerHttpRequest request, ServerHttpResponse response,
			WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException {

		Assert.notNull(wsSession, "No session");
		AbstractHttpSockJsSession sockJsSession = (AbstractHttpSockJsSession) wsSession;

		handleRequestInternal(request, response, wsHandler, sockJsSession);
	}

	protected void handleRequestInternal(ServerHttpRequest request, ServerHttpResponse response,
			WebSocketHandler wsHandler, AbstractHttpSockJsSession sockJsSession) throws SockJsException {

		String[] messages;
		try {
			messages = readMessages(request);
		}
		catch (JsonMappingException ex) {
			logger.error("Failed to read message", ex);
			handleReadError(response, "Payload expected.", sockJsSession.getId());
			return;
		}
		catch (IOException ex) {
			logger.error("Failed to read message", ex);
			handleReadError(response, "Broken JSON encoding.", sockJsSession.getId());
			return;
		}
		catch (Throwable ex) {
			logger.error("Failed to read message", ex);
			handleReadError(response, "Failed to read message(s)", sockJsSession.getId());
			return;
		}

		if (messages == null) {
			handleReadError(response, "Payload expected.", sockJsSession.getId());
			return;
		}

		if (logger.isTraceEnabled()) {
			logger.trace("Received message(s): " + Arrays.asList(messages));
		}

		response.setStatusCode(getResponseStatus());
		response.getHeaders().setContentType(new MediaType("text", "plain", UTF8_CHARSET));

		sockJsSession.delegateMessages(messages);
	}

	private void handleReadError(ServerHttpResponse resp, String error, String sessionId) {
		try {
			resp.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
			resp.getBody().write(error.getBytes("UTF-8"));
		}
		catch (IOException ex) {
			throw new SockJsException("Failed to send error: " + error, sessionId, ex);
		}
	}

	protected abstract String[] readMessages(ServerHttpRequest request) throws IOException;

	protected abstract HttpStatus getResponseStatus();

}

@@ -739,69 +739,8 @@
 s;%0A%0A
-import com.fasterxml.jackson.databind.JsonMappingException;%0A%0A
 impo
@@ -2145,19 +2145,10 @@
 ch (
-JsonMapping
+IO
 Exce
@@ -2151,32 +2151,86 @@
 Exception ex) %7B%0A
+%09%09%09if(ex.getClass().getName().contains(%22Mapping%22)) %7B%0A%09
 %09%09%09logger.error(
@@ -2252,32 +2252,33 @@
  message%22, ex);%0A
+%09
 %09%09%09handleReadErr
@@ -2342,47 +2342,21 @@
 %0A%09%09%09
-return;%0A%09%09%7D%0A%09%09catch (IOException ex)
+%7D%0A%09%09%09else
  %7B%0A
+%09
 %09%09%09l
@@ -2390,32 +2390,33 @@
  message%22, ex);%0A
+%09
 %09%09%09handleReadErr
@@ -2469,32 +2469,37 @@
 ssion.getId());%0A
+%09%09%09%7D%0A
 %09%09%09return;%0A%09%09%7D%0A%09

Remove Jackson dependency from TransportHandler hierarchy