Java tutorial
/** * Copyright 2010-2015 Complex and Distributed IT Systems, TU Berlin * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.tu_berlin.cit.rwx4j.component; import java.util.concurrent.Exchanger; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import de.tu_berlin.cit.rwx4j.rest.RestDocument; import de.tu_berlin.cit.rwx4j.xmpp.packet.IQ; import de.tu_berlin.cit.rwx4j.xmpp.packet.Message; import de.tu_berlin.cit.rwx4j.xmpp.packet.IQ.Type; import de.tu_berlin.cit.rwx4j.xwadl.XwadlDocument; /** * TODO * * @author Alexander Stanik <alexander.stanik@tu-berlin.de> */ public class ResourceContainerSocket { private final ResourceContainerSocketManager socketManager; private final String jid; protected final Exchanger<XwadlDocument> xwadlExchanger = new Exchanger<XwadlDocument>(); protected final Exchanger<RestDocument> restXmlExchanger = new Exchanger<RestDocument>(); /** * This constructor should only be used by the ResourceContainerSocketManager. * * @param socketManager The ResourceContainerSocketManager instance. */ protected ResourceContainerSocket(ResourceContainerSocketManager socketManager, String jid) { this.socketManager = socketManager; this.jid = jid; } public String getJid() { return jid; } public void sendMessage(String message) { Message m = new Message(); m.setTo(jid); m.setBody(message); this.socketManager.sendMessage(m); } public XwadlDocument requestXWADL(String path) throws InterruptedException { IQ iq = new IQ(Type.get); iq.setTo(jid); // create request XwadlDocument request = XwadlDocument.Factory.newInstance(); request.addNewXwadl().setPath(path); try { Document doc; doc = DocumentHelper.parseText(request.toString()); // set request iq.setChildElement(doc.getRootElement()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } this.socketManager.sendIQ(iq, this); // wait for response XwadlDocument response; response = this.xwadlExchanger.exchange(request); return response; } public RestDocument invokeRestXML(RestDocument request) throws InterruptedException { IQ iq = new IQ(Type.set); iq.setTo(jid); // create request try { Document doc; doc = DocumentHelper.parseText(request.toString()); // set request iq.setChildElement(doc.getRootElement()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } this.socketManager.sendIQ(iq, this); // wait for response RestDocument response; response = this.restXmlExchanger.exchange(request); return response; } public void invokeAsyncRestXML(RestDocument request, AsynchronousResultListener listener) { IQ iq = new IQ(Type.set); iq.setTo(jid); // create request try { Document doc; doc = DocumentHelper.parseText(request.toString()); // set request iq.setChildElement(doc.getRootElement()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.socketManager.sendIQ(iq, this); // process the result in a separate thread new Thread(new Runnable() { @Override public void run() { // wait for response RestDocument response; try { response = restXmlExchanger.exchange(request); listener.processResult(response); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } }