Skip to content
Snippets Groups Projects
Commit 910146f9 authored by Patient NTUMBA's avatar Patient NTUMBA
Browse files

rest bundle

parent d1cec192
No related branches found
No related tags found
No related merge requests found
Showing
with 396 additions and 6 deletions
package eu.chorevolution.vsb.bc.manager; package eu.chorevolution.vsb.bc.manager;
public class VsbOutput { import java.io.Serializable;
public class VsbOutput implements Serializable {
public byte[] war; public byte[] war;
public byte[] wsdl; public byte[] wsdl;
......
...@@ -409,7 +409,8 @@ public class PathResolver { ...@@ -409,7 +409,8 @@ public class PathResolver {
DOMSource contextFileDocUpdated = new DOMSource(doc); DOMSource contextFileDocUpdated = new DOMSource(doc);
StreamResult contextFile = new StreamResult(new File(contextFilePath)); StreamResult contextFile = new StreamResult(new File(contextFilePath));
try { try{
transformer = transformerFactory.newTransformer(); transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {e.printStackTrace();} } catch (TransformerConfigurationException e) {e.printStackTrace();}
...@@ -420,7 +421,7 @@ public class PathResolver { ...@@ -420,7 +421,7 @@ public class PathResolver {
} catch (TransformerException e) {e.printStackTrace();} } catch (TransformerException e) {e.printStackTrace();}
System.out.println(address+" update to http://localhost:"+port+"/BaseService/setinvaddr"); // System.out.println(address+" update to http://localhost:"+port+"/BaseService/setinvaddr");
} }
......
...@@ -23,11 +23,10 @@ public class RestClientExample { ...@@ -23,11 +23,10 @@ public class RestClientExample {
String content = obj.toJSONString(); String content = obj.toJSONString();
String url = "http://jinx.viktoria.chalmers.se:3002/getWeather"; String url = "http://127.0.0.1:8891/interface";
Response resp = RestClientExample.post(url, content); Response resp = RestClientExample.post(url, content);
System.out.println(resp.getEntityAsText()); System.out.println(resp.getEntityAsText());
} }
......
...@@ -79,6 +79,7 @@ ...@@ -79,6 +79,7 @@
<module>binding-components</module> <module>binding-components</module>
<module>playgrounds</module> <module>playgrounds</module>
<module>gmdl-tools</module> <module>gmdl-tools</module>
<module>vsb-manager-service-bundle</module>
</modules> </modules>
<build> <build>
...@@ -110,4 +111,4 @@ ...@@ -110,4 +111,4 @@
<version>4.9</version> <version>4.9</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>eu.chorevolution.vsb</groupId>
<artifactId>vsb</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>eu.chorevolution.vsb</groupId>
<artifactId>vsb-manager-service-bundle</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>vsb-manager-service-bundle Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<contextpath>vsb-manager-service-bundle</contextpath>
</properties>
<repositories>
<repository>
<id>repository.jboss.org</id>
<name>JBoss Repository</name>
<url>http://mvnrepository.com/artifact/org.jboss.shrinkwrap</url>
</repository>
<repository>
<id>maven-restlet</id>
<name>Public online Restlet repository</name>
<url>http://maven.restlet.org</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>eu.chorevolution.vsb</groupId>
<artifactId>vsb-manager</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package eu.chorevolution.vsb.manager; package eu.chorevolution.vsb.managerservice;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.plaf.basic.BasicScrollPaneUI.VSBChangeListener;
import java.io.IOException;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import org.restlet.Component; import org.restlet.Component;
import org.restlet.Server; import org.restlet.Server;
import org.restlet.data.Protocol; import org.restlet.data.Protocol;
import org.restlet.representation.ObjectRepresentation;
import org.restlet.representation.Representation; import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ResourceException; import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource; import org.restlet.resource.ServerResource;
import eu.chorevolution.vsb.gmdl.utils.enums.ProtocolType; import eu.chorevolution.vsb.gmdl.utils.enums.ProtocolType;
import eu.chorevolution.vsb.gm.protocols.Manageable; import eu.chorevolution.vsb.bc.manager.VsbOutput;
import eu.chorevolution.vsb.gm.protocols.soap.BcSoapSubcomponent;
import eu.chorevolution.vsb.gm.protocols.primitives.BcGmSubcomponent;
import eu.chorevolution.vsb.gm.protocols.rest.BcRestSubcomponent;
import eu.chorevolution.vsb.gmdl.utils.BcConfiguration;
import eu.chorevolution.vsb.manager.VsbManager; import eu.chorevolution.vsb.manager.VsbManager;
public class VsbManagerRestService { public class VsbManagerServer {
private Component component; private Component component;
private Server server; private Server server;
private Boolean serverOnline = false; private Boolean serverOnline = false;
public VsbManagerRestService(final int port) { public VsbManagerServer(final int port) {
this.server = new Server(Protocol.HTTP, port); this.server = new Server(Protocol.HTTP, port);
this.component = new Component(); this.component = new Component();
this.component.getServers().add(server); this.component.getServers().add(server);
this.component.getDefaultHost().attach("/interface", InterfaceDescPost.class); this.component.getDefaultHost().attach("/bcgenerator", InterfaceDescPost.class);
this.component.getDefaultHost().attach("/{interface}/{protocol}", InterfaceDescGet.class); try{
try {
this.component.start(); this.component.start();
} catch (Exception e) {
e.printStackTrace();
}
}
public static class InterfaceDescGet extends ServerResource {
@Override
protected Representation get() throws ResourceException {
String interfacePath = (String) this.getRequestAttributes().get("interface");
String protocol = (String) this.getRequestAttributes().get("protocol");
String decodedURL = java.net.URLDecoder.decode(interfacePath);
System.out.println(decodedURL + " " + protocol + " " + interfacePath);
ProtocolType busProtocol = null;
switch(protocol.toUpperCase()) {
case "REST":
busProtocol = ProtocolType.REST;
break;
case "SOAP":
busProtocol = ProtocolType.SOAP;
break;
case "MQTT":
busProtocol = ProtocolType.MQTT;
break;
case "WEBSOCKETS":
busProtocol = ProtocolType.WEB_SOCKETS;
break;
case "SEMI_SPACE":
busProtocol = ProtocolType.SEMI_SPACE;
break;
case "JMS":
busProtocol = ProtocolType.JMS;
break;
case "PUBNUB":
busProtocol = ProtocolType.PUB_NUB;
break;
case "COAP":
busProtocol = ProtocolType.COAP;
break;
case "ZERO_MQ":
busProtocol = ProtocolType.ZERO_MQ;
break;
case "DPWS":
busProtocol = ProtocolType.DPWS;
break;
}
VsbManager vsbm = new VsbManager(); }catch (Exception e){e.printStackTrace();}
vsbm.generateWar(decodedURL, busProtocol);
String returnMessage = "Request forwarded!";
return new StringRepresentation(returnMessage);
}
} }
public static class InterfaceDescPost extends ServerResource { public static class InterfaceDescPost extends ServerResource {
@Override @Override
protected Representation post(Representation entity) throws ResourceException { protected Representation post(Representation entity) throws ResourceException {
String receivedText = null; String receivedText = null;
try { try {
receivedText = entity.getText();
} catch (IOException e1) { receivedText = entity.getText();
e1.printStackTrace();
} } catch (IOException e1){ e1.printStackTrace(); }
System.out.println("rec: " + receivedText); System.out.println("received : " + receivedText);
JSONParser parser = new JSONParser();
String[] arguments = receivedText.split(","); JSONObject jsonObject = null;
System.out.println(arguments[0]); String protocol = null;
System.out.println(arguments[1]); String interfaceService = null;
try {
jsonObject = (JSONObject)parser.parse(receivedText);
protocol = (String)jsonObject.get("protocol");
interfaceService = (String)jsonObject.get("interface");
} catch (ParseException e){e.printStackTrace();}
byte[] byteArray = stringToByteArray(interfaceService);
ProtocolType busProtocol = null; ProtocolType busProtocol = null;
switch(arguments[1].toUpperCase()) { switch(protocol.toUpperCase()){
case "REST": case "REST":
busProtocol = ProtocolType.REST; busProtocol = ProtocolType.REST;
break; break;
...@@ -139,22 +93,29 @@ public class VsbManagerRestService { ...@@ -139,22 +93,29 @@ public class VsbManagerRestService {
busProtocol = ProtocolType.ZERO_MQ; busProtocol = ProtocolType.ZERO_MQ;
break; break;
case "DPWS": case "DPWS":
busProtocol = ProtocolType.DPWS; busProtocol = ProtocolType.DPWS;
break; break;
} }
VsbManager vsbm = new VsbManager(); VsbManager vsbm = new VsbManager();
// vsbm.generateWar(arguments[0], busProtocol); VsbOutput vsbOutput = vsbm.generateWar(byteArray, busProtocol);
return new ObjectRepresentation<VsbOutput>(vsbOutput);
String returnMessage = ""; }
returnMessage = "Received!";
return new StringRepresentation(returnMessage); private byte[] stringToByteArray(String string){
byte[] array = new byte[string.length()];
for (int i = 0; i < string.length(); i++) {
array[i] = (byte) string.charAt(i);
}
return array;
} }
} }
public static void main(String[] args) { public static void main(String[] args){
VsbManagerRestService bcmanager = new VsbManagerRestService(1111); VsbManagerServer server = new VsbManagerServer(8891);
} }
} }
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint name="VsbManagerService"
implementor="eu.chorevolution.vsb.managerservice.VsbManagerServer"
address="http://localhost:8891/bcgenerator">
</jaxws:endpoint>
</beans>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Binding Component Application</display-name>
<description>
This is a simple web application with a source code organization.
</description>
<!-- Configuration of set invocation address Begin -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/bcgenerator</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Configuration of set invocation address End -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
package eu.chorevolution.vsb.manager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.json.simple.JSONObject;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.representation.ObjectRepresentation;
import eu.chorevolution.vsb.bc.manager.BcManagerRestService;
import eu.chorevolution.vsb.bc.manager.VsbOutput;
import eu.chorevolution.vsb.gmdl.utils.PathResolver;
public class VsbManagerClient{
private String interfaceDescServiceContent = null;
private String protocol = null;
private String ipVsbManagerService = null;
private int portVsbManagerService = 0;
private String urlVsbManagerService = null;
public VsbManagerClient(String ipVsbManagerService, int portVsbManagerService, byte[] interfaceDescServiceByteArray, String protocol){
this.interfaceDescServiceContent = byteArrayToString(interfaceDescServiceByteArray);
this.protocol = protocol;
this.ipVsbManagerService = ipVsbManagerService;
this.portVsbManagerService = portVsbManagerService;
urlVsbManagerService();
}
VsbOutput generateBC(){
VsbOutput vsbOutput = null;
JSONObject obj = new JSONObject();
obj.put("interface", interfaceDescServiceContent);
obj.put("protocol", protocol);
String data = obj.toJSONString();
Response response = post(urlVsbManagerService, data);
ObjectRepresentation<VsbOutput> vsbOutputObj;
try {
vsbOutputObj = new ObjectRepresentation<VsbOutput>(response.getEntity(),null,true,true);
vsbOutput = vsbOutputObj.getObject();
}catch (IllegalArgumentException | IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return vsbOutput;
}
private void urlVsbManagerService(){
urlVsbManagerService = "http://"+ipVsbManagerService+":"+portVsbManagerService+"/bcgenerator";
}
private static String byteArrayToString(byte[] array){
String string = null;
try{
string = new String(array, "UTF-8");
}
catch (UnsupportedEncodingException e){ e.printStackTrace();}
return string;
}
private static Response post(String url, String content) {
Request request = new Request();
request.setResourceRef(url);
request.setMethod(Method.POST);
request.setEntity(content, MediaType.APPLICATION_JSON);
Context ctx = new Context();
Client client = new Client(ctx, Protocol.HTTP);
return client.handle(request);
}
}
package eu.chorevolution.vsb.manager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import eu.chorevolution.vsb.bc.manager.BcManagerRestService;
import eu.chorevolution.vsb.bc.manager.VsbOutput;
import eu.chorevolution.vsb.gmdl.utils.PathResolver;
public class VsbManagerRestServiceClient{
public static void main(String[] args) {
// TODO Auto-generated method stub
String file_path = PathResolver.myFilePath(BcManagerRestService.class,"bridge.gidl");
byte[] byteArray = readBytesFromFile(file_path);
VsbManagerClient client = new VsbManagerClient("127.0.0.1", 8891, byteArray, "SOAP");
VsbOutput vsbOutput = client.generateBC();
System.out.println("vsbOutput.war.length "+vsbOutput.war.length);
System.out.println("vsbOutput.wsdl.length "+vsbOutput.wsdl.length);
}
private static byte[] readBytesFromFile(String filePath){
FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {
File file = new File(filePath);
bytesArray = new byte[(int) file.length()];
//read file into bytes[]
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment