Name: processfetch/Server.java
| 1: | import java.util.ArrayList; |
| 2: | import java.util.Date; |
| 3: | import java.util.List; |
| 4: | import java.io.*; |
| 5: | |
| 6: | import javax.xml.bind.JAXBException; |
| 7: | |
| 8: | import org.jivesoftware.smack.XMPPException; |
| 9: | |
| 10: | import adventure.messagerouting.AdventureMessage; |
| 11: | import adventure.messagerouting.C; |
| 12: | import adventure.messagerouting.MessageClient; |
| 13: | import adventure.messagerouting.LocalHttpRestExecutor; |
| 14: | import adventure.messagerouting.exceptions.MessageRoutingException; |
| 15: | import adventure.messagerouting.interfaces.OnAuthErrorListener; |
| 16: | import adventure.messagerouting.interfaces.OnErrorListener; |
| 17: | import adventure.messagerouting.interfaces.OnHttpMessageListener; |
| 18: | import adventure.messagerouting.interfaces.OnMessageListener; |
| 19: | import adventure.messagerouting.tests.RestTestServer; |
| 20: | import adventure.messagerouting.XmppExtensions.HeaderExtension; |
| 21: | import adventure.messagerouting.XmppExtensions.OkExtension; |
| 22: | import adventure.messagerouting.XmppExtensions.OperationExtension; |
| 23: | import adventure.messagerouting.XmppExtensions.PartExtension; |
| 24: | |
| 25: | import org.jbpm.designer.repository.impl.AdventureAsset; |
| 26: | import org.jivesoftware.smack.packet.Message; |
| 27: | import org.jivesoftware.smack.packet.Packet; |
| 28: | import org.jivesoftware.smack.packet.PacketExtension; |
| 29: | import org.restlet.data.Protocol; |
| 30: | |
| 31: | import adventure.messagerouting.exceptions.MessageRoutingException; |
| 32: | |
| 33: | class ProcessFetch extends Thread { |
| 34: | private static MessageClient mc1; |
| 35: | private static MessageClient mc2; |
| 36: | |
| 37: | public static AdventureMessage mc1latestMessage; |
| 38: | public static AdventureMessage mc2latestMessage; |
| 39: | |
| 40: | public static final String BUCKET_PROCESS = "ds-bin"; |
| 41: | public static final String BUCKET_DATA = "ds-binaries"; |
| 42: | |
| 43: | |
| 44: | AdventureMessage sentMessage; |
| 45: | |
| 46: | public void run() { |
| 47: | try { |
| 48: | new org.restlet.Server(Protocol.HTTP, 9299, RestTestServer.class).start(); |
| 49: | } catch (Exception e1) { |
| 50: | e1.printStackTrace(); |
| 51: | } |
| 52: | |
| 53: | mc1 = new MessageClient("adventure_processfetch","adventure_processfetch"); |
| 54: | |
| 55: | mc1.AddOnAuthErrorEventListener(new OnAuthErrorListener() { |
| 56: | @Override |
| 57: | public void OnAuthError(Exception e) { |
| 58: | System.out.println("autherror"); |
| 59: | C.log(e.getStackTrace().toString()); |
| 60: | } |
| 61: | }); |
| 62: | mc1.AddOnErrorEventListener(new OnErrorListener() { |
| 63: | @Override |
| 64: | public void OnError(Exception exception) { |
| 65: | System.out.println("error..."); |
| 66: | C.log(exception.getStackTrace().toString()); |
| 67: | } |
| 68: | }); |
| 69: | |
| 70: | mc1.AddOnHttpMessageEventListener(new OnHttpMessageListener() { |
| 71: | @Override |
| 72: | public void OnHttpMessage(AdventureMessage message) { |
| 73: | System.out.println("http message incoming"); |
| 74: | System.out.println("[mc1] Http From: " + message.getXmppMessage().getFrom() + " To: " + message.getXmppMessage().getTo()); |
| 75: | if (message.getHeaders() != null ) { |
| 76: | |
| 77: | for (HeaderExtension header : message.getHeaders()) |
| 78: | System.out.println("[mc1] Header: " + header.toXML()); |
| 79: | if (message.getParts() != null) |
| 80: | System.out.println("got some parts"); |
| 81: | for (PartExtension part : message.getParts()) { |
| 82: | System.out.println("part: "+part); |
| 83: | System.out.println("[mc1] Part: " + part.toXML()); |
| 84: | if(part.getContent()!=null && !part.getContent().isEmpty()){ |
| 85: | String processId=part.getContent(); |
| 86: | System.out.println("processid: "+processId); |
| 87: | |
| 88: | AdventureCloudComm cs = AdventureCloudComm.getInstance(); |
| 89: | if(cs!=null) { |
| 90: | try { |
| 91: | AdventureAsset process=(AdventureAsset) cs.get(BUCKET_PROCESS, processId, AdventureAsset.class); |
| 92: | if(process!=null) { |
| 93: | System.out.println("process from id: "+process); |
| 94: | com.google.gson.internal.StringMap bpmn = (com.google.gson.internal.StringMap) cs.get(BUCKET_DATA, process.getBpmn2(), Object.class); |
| 95: | |
| 96: | if(bpmn!=null) { |
| 97: | List<PacketExtension> list = new ArrayList<PacketExtension>(); |
| 98: | |
| 99: | PartExtension p = new PartExtension(); |
| 100: | p.setContentTransferEncoding("binary"); |
| 101: | p.setContentType("text/xml"); |
| 102: | p.setContent(bpmn.get("content").toString()); |
| 103: | list.add(p); |
| 104: | |
| 105: | mc1.sendHttpResponse(message, list); |
| 106: | } else { |
| 107: | Exception e = new XMPPException("no process model could be retrieved"); |
| 108: | mc1.getWrapper().FireAuthErrorToSubscribers(e); |
| 109: | } |
| 110: | } else { |
| 111: | Exception e = new XMPPException("no process model found for posted id"); |
| 112: | mc1.getWrapper().FireAuthErrorToSubscribers(e); |
| 113: | } |
| 114: | } catch (JAXBException e) { |
| 115: | // TODO Auto-generated catch block |
| 116: | e.printStackTrace(); |
| 117: | } catch (MessageRoutingException e) { |
| 118: | // TODO Auto-generated catch block |
| 119: | e.printStackTrace(); |
| 120: | } |
| 121: | } else { |
| 122: | Exception e = new XMPPException("no connection to cloud storage could be established"); |
| 123: | mc1.getWrapper().FireAuthErrorToSubscribers(e); |
| 124: | } |
| 125: | } |
| 126: | else{ |
| 127: | Exception e = new XMPPException("no process id received"); |
| 128: | mc1.getWrapper().FireAuthErrorToSubscribers(e); |
| 129: | } |
| 130: | } |
| 131: | } |
| 132: | } |
| 133: | }); |
| 134: | |
| 135: | System.out.println("client: "+mc1); |
| 136: | mc1.SetLocalHttpEndpoint(new LocalHttpRestExecutor(mc1, "http://localhost:9299")); |
| 137: | System.out.println("trying to login"); |
| 138: | mc1.login(); |
| 139: | System.out.println("logged in"); |
| 140: | } |
| 141: | |
| 142: | } |
| 143: | |
| 144: | class ShutdownThread extends Thread {// {{{ |
| 145: | private ProcessFetch mt = null; |
| 146: | |
| 147: | public ShutdownThread(ProcessFetch mt) { |
| 148: | super(); |
| 149: | this.mt = mt; |
| 150: | mt.start(); |
| 151: | } |
| 152: | |
| 153: | public void run() { |
| 154: | mt.interrupt(); |
| 155: | System.out.println("Shutting down ..."); |
| 156: | } |
| 157: | }// }}} |
| 158: | |
| 159: | public class Server {// {{{ |
| 160: | public static void main(String [] args) { |
| 161: | try { |
| 162: | Runtime.getRuntime().addShutdownHook(new ShutdownThread(new ProcessFetch())); |
| 163: | } catch (Throwable t) { |
| 164: | System.out.println("Could not add Shutdown hook."); |
| 165: | System.out.println(t); |
| 166: | } |
| 167: | |
| 168: | try { |
| 169: | while (true) { Thread.currentThread().sleep(10000); } |
| 170: | } catch (InterruptedException ie) { } |
| 171: | System.exit(0); |
| 172: | } |
| 173: | }// }}} |
