Java tutorial
/* * Copyright (c) 2011-2012 by 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 io.dyn.net.tcp.stomp; import io.dyn.core.sys.Sys; import io.dyn.net.nio.Buffer; import org.springframework.http.MediaType; import org.springframework.util.StringUtils; /** * @author Jon Brisbin <jon@jbrisbin.com> */ public abstract class StompMessages { private StompMessages() { } public static StompMessage connected(Object... headers) { StompMessage msg = new StompMessage(StompCommand.CONNECTED); addHeaders(msg, "server", StompServer.SERVER_NAME); addHeaders(msg, headers); return msg; } public static StompMessage error(String errorMsg, Object... headers) { StompMessage msg = new StompMessage(StompCommand.ERROR); addHeaders(msg, "server", StompServer.SERVER_NAME); addHeaders(msg, headers); msg.header("content-type", MediaType.TEXT_PLAIN_VALUE); msg.header("content-length", "" + errorMsg.length()); msg.content(Buffer.wrap(errorMsg)); return msg; } private static void addHeaders(StompMessage msg, Object... headers) { String name = ""; for (int i = 0; i < headers.length; i++) { String s; if (headers[i] instanceof String) { s = (String) headers[i]; } else { s = Sys.DEFAULT_CONVERSION_SERVICE.convert(headers[i], String.class); } if (i % 2 == 0) { name = s; } else { if (StringUtils.hasText(name)) { msg.header(name, s); } } } } }