Skip to content

Commit

Permalink
#5 proper invocation over http via json
Browse files Browse the repository at this point in the history
  • Loading branch information
marvec committed May 5, 2015
1 parent 7a99d5b commit 9d35461
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,4 @@ public Object lookupMicroservice(final MicroserviceMetaData metaData) {
return null;
}

@Override
public Object lookupLocalMicroservice(final MicroserviceMetaData metaData) {
return lookupMicroservice(metaData);
}
}
4 changes: 2 additions & 2 deletions http-invoker-microservice-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<groupId>com.cedarsoftware</groupId>
<artifactId>json-io</artifactId>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
package org.silverware.microservices.providers.http.invoker.internal;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
import org.silverware.microservices.Context;
import org.silverware.microservices.MicroserviceMetaData;
import org.silverware.microservices.silver.cluster.Invocation;
Expand All @@ -37,7 +38,6 @@
*/
public class HttpInvokerServlet extends HttpServlet {

private ObjectMapper mapper = new ObjectMapper();
private static Context context;

public static void setContext(final Context ctx) {
Expand All @@ -53,13 +53,17 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
if (req.getRequestURI().endsWith("query")) {
final MicroserviceMetaData metaData = mapper.readValue(req.getInputStream(), MicroserviceMetaData.class);
final JsonReader jsonReader = new JsonReader(req.getInputStream());
final MicroserviceMetaData metaData = (MicroserviceMetaData) jsonReader.readObject();
final List<ServiceHandle> handles = context.assureHandles(metaData);
mapper.writeValue(resp.getWriter(), handles);
final JsonWriter jsonWriter = new JsonWriter(resp.getOutputStream());
jsonWriter.write(handles);
} else if (req.getRequestURI().endsWith("invoke")) {
final Invocation invocation = mapper.readValue(req.getInputStream(), Invocation.class);
final JsonReader jsonReader = new JsonReader(req.getInputStream());
final Invocation invocation = (Invocation) jsonReader.readObject();
try {
mapper.writeValue(resp.getWriter(), invocation.invoke(context));
final JsonWriter jsonWriter = new JsonWriter(resp.getOutputStream());
jsonWriter.write(invocation.invoke(context));
} catch (Exception e) {
throw new IOException(String.format("Unable to invoke Microservice using invocation %s: ", invocation.toString()), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.silverware.microservices.providers.http.invoker;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.cedarsoftware.util.io.JsonReader;
import com.cedarsoftware.util.io.JsonWriter;
import org.silverware.microservices.MicroserviceMetaData;
import org.silverware.microservices.annotations.Microservice;
import org.silverware.microservices.providers.cdi.CdiMicroserviceProvider;
Expand All @@ -15,8 +16,7 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.Inet4Address;
import java.net.InetAddress;
Expand All @@ -33,7 +33,6 @@
public class HttpInvokerMicroserviceProviderTest {

private HttpInvokerSilverService httpInvokerSilverService = null;
private ObjectMapper mapper = new ObjectMapper();

@Test
public void testHttpInvoker() throws Exception {
Expand Down Expand Up @@ -64,10 +63,12 @@ public void testHttpInvoker() throws Exception {
con.connect();

final MicroserviceMetaData metaData = new MicroserviceMetaData("sumService", SumService.class, Collections.emptySet());
mapper.writeValue(con.getOutputStream(), metaData);
JsonWriter jsonWriter = new JsonWriter(con.getOutputStream());
jsonWriter.write(metaData);

Assert.assertEquals(con.getResponseMessage(), "OK");
final List<ServiceHandle> handles = mapper.readValue(con.getInputStream(), mapper.getTypeFactory().constructCollectionType(List.class, ServiceHandle.class));
JsonReader jsonReader = new JsonReader(con.getInputStream());
final List<ServiceHandle> handles = (List<ServiceHandle>) jsonReader.readObject();
Assert.assertEquals(handles.size(), 1);

con.disconnect();
Expand All @@ -78,12 +79,75 @@ public void testHttpInvoker() throws Exception {
con.setDoOutput(true);
con.connect();

final Invocation invocation = new Invocation(handles.get(0).getHandle(), "sum", new Class[] { short.class, int.class }, new Object[] { (short) 3, 4 });
mapper.writeValue(con.getOutputStream(), invocation);
final Object response = mapper.readValue(con.getInputStream(), Long.class);
Invocation invocation = new Invocation(handles.get(0).getHandle(), "sum", new Class[] { short.class, int.class }, new Object[] { (short) 3, 4 });
jsonWriter = new JsonWriter(con.getOutputStream());
jsonWriter.write(invocation);
jsonReader = new JsonReader(con.getInputStream());
Object response = jsonReader.readObject();

Assert.assertEquals(response, 7L);

con.disconnect();

con = (HttpURLConnection) new URL(urlBase + "invoke").openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();

invocation = new Invocation(handles.get(0).getHandle(), "allTypes",
new Class[] { byte.class, short.class, int.class, long.class, float.class, double.class, boolean.class, char.class },
new Object[] { Byte.MAX_VALUE, Short.MAX_VALUE, Integer.MAX_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Double.MIN_VALUE, true, 'c' });

jsonWriter = new JsonWriter(con.getOutputStream());
jsonWriter.write(invocation);
jsonReader = new JsonReader(con.getInputStream());
response = jsonReader.readObject();

Assert.assertEquals(response, "9.223372036854776E18truec");

con.disconnect();


con = (HttpURLConnection) new URL(urlBase + "invoke").openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();

invocation = new Invocation(handles.get(0).getHandle(), "allTypes2",
new Class[] { Byte.class, Short.class, Integer.class, Long.class, Float.class, Double.class, Boolean.class, Character.class },
new Object[] { Byte.MAX_VALUE, Short.MAX_VALUE, Integer.MAX_VALUE, Long.MAX_VALUE, Float.MIN_VALUE, Double.MIN_VALUE, true, 'c' });

jsonWriter = new JsonWriter(con.getOutputStream());
jsonWriter.write(invocation);
jsonReader = new JsonReader(con.getInputStream());
response = jsonReader.readObject();

Assert.assertEquals(response, "9.223372036854776E18truec");

con.disconnect();

con = (HttpURLConnection) new URL(urlBase + "invoke").openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();

MagicBox box = new MagicBox();

invocation = new Invocation(handles.get(0).getHandle(), "doMagic", new Class[] { MagicBox.class }, new Object[] { box });

jsonWriter = new JsonWriter(con.getOutputStream());
jsonWriter.write(invocation);
jsonReader = new JsonReader(con.getInputStream());
response = jsonReader.readObject();

//Assert.assertEquals(response, "9.223372036854776E18truec");
System.out.println(response);

con.disconnect();

Thread.sleep(10000);

platform.interrupt();
Expand All @@ -105,12 +169,53 @@ public void testIpAddresses() throws Exception {
}
}

public static class MagicBox implements Serializable {
private Short s = Short.MAX_VALUE;
private Float f = Float.MAX_VALUE;

public Short getS() {
return s;
}

public void setS(final Short s) {
this.s = s;
}

public Float getF() {
return f;
}

public void setF(final Float f) {
this.f = f;
}

@Override
public String toString() {
return "MagicBox{" +
"s=" + s +
", f=" + f +
'}';
}
}

@Microservice
public static class SumService {

public long sum(short a, int b) {
return a + b;
}

public String allTypes(final byte b, short s, int i, long l, float f, double d, boolean o, char c) {
return (b + s + i + l + f + d) + String.valueOf(o) + c;
}

public String allTypes2(Byte b, Short s, Integer i, Long l, Float f, Double d, Boolean o, Character c) {
return (b + s + i + l + f + d) + String.valueOf(o) + c;
}

public MagicBox doMagic(final MagicBox box) {
return box;
}
}

}
6 changes: 6 additions & 0 deletions microservices-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<version.undertow>1.2.0.Beta10</version.undertow>
<version.jolokia>1.2.3</version.jolokia>
<version.jackson>2.5.3</version.jackson>
<version.jsonio>3.0.2</version.jsonio>
<java.level>1.8</java.level>
</properties>
<dependencyManagement>
Expand Down Expand Up @@ -154,6 +155,11 @@
<artifactId>jackson-annotations</artifactId>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>json-io</artifactId>
<version>${version.jsonio}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
public interface ProvidingSilverService extends SilverService {

Object lookupMicroservice(final MicroserviceMetaData metaData);
Object lookupLocalMicroservice(final MicroserviceMetaData metaData);

default Object lookupLocalMicroservice(final MicroserviceMetaData metaData) {
return lookupMicroservice(metaData);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package org.silverware.microservices.silver.cluster;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.silverware.microservices.Context;
Expand All @@ -43,7 +42,7 @@ public class Invocation {

private final Object[] params;

public Invocation(@JsonProperty("handle") final int handle, @JsonProperty("method") final String method, @JsonProperty("paramTypes") final Class[] paramTypes, @JsonProperty("params") final Object[] params) {
public Invocation(final int handle, final String method, final Class[] paramTypes, final Object[] params) {
this.handle = handle;
this.method = method;
this.paramTypes = paramTypes;
Expand Down Expand Up @@ -122,27 +121,8 @@ public Object invoke(final Context context) throws Exception {
throw new SilverWareException(String.format("Handle no. %d. No such handle found.", getHandle()));
}

final Object[] realParams = new Object[params.length];
for (int i = 0; i < realParams.length; i++) {
if (paramTypes[i].getName().equals("short")) {
realParams[i] = (short) ((Integer) params[i]).intValue();
} else if (paramTypes[i].getName().equals("long")) {
realParams[i] = (long) ((Integer) params[i]).longValue();
} else if (paramTypes[i].getName().equals("byte")) {
realParams[i] = (byte) ((Integer) params[i]).intValue();
} else if (paramTypes[i].getName().equals("float")) {
realParams[i] = (float) ((Double) params[i]).doubleValue();
} else if (paramTypes[i].getName().equals("int")) {
realParams[i] = (Integer) params[i];
} else if (paramTypes[i].getName().equals("double")) {
realParams[i] = (Double) params[i];
} else {
realParams[i] = paramTypes[i].cast(params[i]);
}
}

final Method method = serviceHandle.getService().getClass().getDeclaredMethod(getMethod(), paramTypes);
return method.invoke(serviceHandle.getService(), realParams);
return method.invoke(serviceHandle.getService(), params);
}


Expand Down

0 comments on commit 9d35461

Please sign in to comment.