Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.utilities.remoting; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.StreamCorruptedException; import java.util.ArrayList; import java.util.List; import org.springframework.remoting.httpinvoker.SimpleHttpInvokerServiceExporter; import org.springframework.remoting.support.RemoteInvocation; import org.springframework.remoting.support.RemoteInvocationResult; import com.sun.net.httpserver.HttpExchange; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; import org.codehaus.jackson.ObjectCodec; import org.codehaus.jackson.map.ObjectMapper; /** * Transports arguments as JSON. @author Pascal S. de Kloe */ public class JsonInvokerServiceExporter extends SimpleHttpInvokerServiceExporter { private static final JsonFactory factory = new JsonFactory(); private static final ObjectCodec codec = new ObjectMapper(); @Override protected RemoteInvocation readRemoteInvocation(HttpExchange exchange, InputStream is) throws IOException { JsonParser parser = factory.createJsonParser(is); parser.setCodec(codec); System.err.println(parser.nextToken()); openField("method", parser); String method = parser.getText(); List<Class<?>> types = new ArrayList<Class<?>>(); for (JsonToken next = openField("types", parser); next != JsonToken.END_ARRAY; next = parser.nextToken()) { try { types.add(Class.forName(parser.getText())); } catch (ClassNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } List<Object> arguments = new ArrayList<Object>(); openField("arguments", parser); for (Class<?> type : types) { parser.nextToken(); arguments.add(parser.readValueAs(type)); } return new RemoteInvocation(method, types.toArray(new Class[types.size()]), arguments.toArray()); } @Override protected void writeRemoteInvocationResult(HttpExchange exchange, RemoteInvocationResult result, OutputStream os) throws IOException { Object content = result.getException(); if (content == null) content = result.getValue(); String type = content == null ? null : content.getClass().getName(); JsonGenerator generator = factory.createJsonGenerator(os); generator.setCodec(codec); generator.writeStartObject(); generator.writeFieldName("type"); generator.writeString(type); generator.writeFieldName("content"); generator.writeObject(content); generator.writeEndObject(); generator.close(); } private static final JsonToken openField(String name, JsonParser parser) throws IOException { System.err.println(parser.nextToken()); if (!name.equals(parser.getCurrentName())) { String msg = String.format("Expected field %s instead of %s", name, parser.getCurrentName()); throw new StreamCorruptedException(msg); } return parser.nextToken(); } }