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.ObjectInputStream; import java.io.ObjectStreamException; import java.io.OutputStream; import java.io.StreamCorruptedException; import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor; import org.springframework.remoting.support.RemoteInvocation; import org.springframework.remoting.support.RemoteInvocationResult; import com.cedarsoftware.util.io.JsonReader; import com.cedarsoftware.util.io.JsonWriter; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.ObjectCodec; import org.codehaus.jackson.map.ObjectMapper; /** * Provides arguments as JSON. @author Pascal S. de Kloe. */ public class JsonInvokerRequestExecutor extends SimpleHttpInvokerRequestExecutor { private static final JsonFactory factory = new JsonFactory(); private static final ObjectCodec codec = new ObjectMapper(); @Override protected void writeRemoteInvocation(RemoteInvocation invocation, OutputStream os) throws IOException { JsonGenerator generator = factory.createJsonGenerator(os); generator.writeStartObject(); generator.writeFieldName("method"); generator.writeString(invocation.getMethodName()); generator.writeArrayFieldStart("types"); for (Class type : invocation.getParameterTypes()) generator.writeString(type.getName()); generator.writeEndArray(); generator.writeArrayFieldStart("arguments"); for (Object argument : invocation.getArguments()) generator.writeObject(argument); generator.writeEndArray(); generator.writeEndObject(); generator.close(); } @Override protected RemoteInvocationResult readRemoteInvocationResult(InputStream is, String codebaseUrl) throws IOException { JsonParser parser = factory.createJsonParser(is); parser.setCodec(codec); System.err.println(parser.nextToken()); System.err.println(parser.nextToken()); if (!"type".equals(parser.getCurrentName())) throw new StreamCorruptedException("Expected type field instead of " + parser.getCurrentName()); System.err.println(parser.nextToken()); Class<?> type = null; try { type = Class.forName(parser.getText()); } catch (ClassNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } System.err.println(parser.nextToken()); if (!"content".equals(parser.getCurrentName())) throw new StreamCorruptedException("Expected content field instead of " + parser.getCurrentName()); System.err.println(parser.nextToken()); Object content = parser.readValueAs(type); if (content instanceof Throwable) return new RemoteInvocationResult((Throwable) content); return new RemoteInvocationResult(content); } }