/** * Saves a payload which can't be send. * @param user The user to send the payload. * @param id The id of the payload which should be send. * @param spaceId The id of the node to send the payload to. * @param payload The payload to send. */ protected synchronized void savePayloadToSend(String user, String id, String spaceId, SimplePayload payload){ SQLiteDatabase db = scheme.getWritableDatabase(); String query = "INSERT INTO " + SendTable.TABLE_NAME + "(" + SendTable.SEND_ID+ ", " + SendTable.SEND_NAME + ", " + SendTable.SEND_NAMESPACE + ", " + SendTable.SEND_SPACE + ", " + SendTable.SEND_PAYLOAD + ", " + SpacesTable.USER + ") VALUES(?,?,?,?,?,?);"; SQLiteStatement stmt = db.compileStatement(query); stmt.bindString(1, id); stmt.bindString(2, payload.getElementName()); stmt.bindString(3, payload.getNamespace()); stmt.bindString(4, spaceId); stmt.bindString(5, payload.toXML()); stmt.bindString(6, user); try{ stmt.executeInsert(); }catch (Exception e){ Log.d("DataWrapper", "An Exception was thrown while saving a payload to send", e); }finally{ stmt.close(); db.close(); } }
/** * Gets all payloads to send for the given user. * @param user The user to get the payloads for. * @return A map consisting of the payloadIds and the corresponding simplepayloads. */ protected synchronized Map<String, SimplePayload> getPayloadsToSend(String user){ SQLiteDatabase db = scheme.getReadableDatabase(); String query = "Select * FROM " + SendTable.TABLE_NAME + " WHERE " + SendTable.USER + "='" + user + "';"; Cursor c = db.rawQuery(query, null); Map<String, SimplePayload> payloads = new HashMap<String, SimplePayload>(); while(c.moveToNext()){ SimplePayload payload = new SimplePayload(c.getString(c.getColumnIndex(SendTable.SEND_NAME)), c.getString(c.getColumnIndex(SendTable.SEND_NAMESPACE)), c.getString(c.getColumnIndex(SendTable.SEND_PAYLOAD))); payloads.put(c.getString(c.getColumnIndex(SendTable.SEND_ID)), payload); } c.close(); db.close(); return payloads; }
private void XMPPPublisher(String xmlString, String SCHEMA, String mtype, Node pubNode){ SimplePayload payload = new SimplePayload(mtype,SCHEMA, xmlString); @SuppressWarnings({ "unchecked", "rawtypes" }) PayloadItem payloadItem = new PayloadItem(null, payload); Log.i(appTAG, classTAG+": Message Published to:"+pubNode.getId()); try { ((LeafNode)pubNode).publish(payloadItem); } catch (NotConnectedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public Item parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { String id = parser.getAttributeValue(null, "id"); String node = parser.getAttributeValue(null, "node"); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(id, node); } else { String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); final ExtensionElementProvider<ExtensionElement> extensionProvider = ProviderManager.getExtensionProvider(payloadElemName, payloadNS); if (extensionProvider == null) { CharSequence payloadText = PacketParserUtils.parseElement(parser, true); return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText)); } else { return new PayloadItem<ExtensionElement>(id, node, extensionProvider.parse(parser)); } } }
public PacketExtension parseExtension(XmlPullParser parser) throws Exception { String id = parser.getAttributeValue(null, "id"); String elem = parser.getName(); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(id); } else { String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null) { boolean done = false; StringBuilder payloadText = new StringBuilder(); while (!done) { if (tag == XmlPullParser.END_TAG && parser.getName().equals(elem)) done = true; else if (!((tag == XmlPullParser.START_TAG) && parser.isEmptyElementTag())) payloadText.append(parser.getText()); if (!done) tag = parser.next(); } return new PayloadItem<SimplePayload>(id, new SimplePayload(payloadElemName, payloadNS, payloadText.toString())); } else { return new PayloadItem<PacketExtension>(id, PacketParserUtils.parsePacketExtension(payloadElemName, payloadNS, parser)); } } }
/** * Creates a new data handler. * @param connectionHandler The connection handler to be used for requests. * @param spaceHandler An instance of a space handler to be used for requesting space properties. */ public DataHandler(ConnectionHandler connectionHandler, SpaceHandler spaceHandler){ if (connectionHandler == null || spaceHandler == null){ throw new IllegalArgumentException("None of the Arguments may be null"); } this.connectionHandler = connectionHandler; this.isConnectionResetted = true; this.spaceHandler = spaceHandler; this.timeout = connectionHandler.getConfiguration().requestTimeout(); this.userWantedMode = Mode.OFFLINE; this.connection = this.connectionHandler.getXMPPConnection(); this.listeners = new ArrayList<DataObjectListener>(); this.handledSpaces = new ArrayList<Space>(); this.datawrapper = DataWrapper.getInstance(); this.pendingPayloadRequests = new HashMap<String, RequestFuture<List<PayloadItem<SimplePayload>>>>(); this.pendingPublishingRequests = new HashMap<String, RequestFuture<IQ>>(); this.pendingPersistenceServiceQueries = new HashMap<String, RequestFuture<IQ>>(); this.userInfo = this.connectionHandler.getCurrentUser(); this.dataObjectFilter = null; this.publishIdMap = new HashMap<String, String>(); this.pubsubServiceListeners = new HashMap<String, PacketListener>(); this.persistenceServiceListeners = new HashMap<String, PacketListener>(); this.pubsubManagers = new HashMap<String, PubSubManager>(); setPubSubPacketInterceptor(); setConnectionStatusListener(); }
/** * Item handler of this data handler implementation. Checks if received items are already cached, * if not, it caches them. Ultimately it sends the items to the subscribed listeners. */ @Override public void handlePublishedItems(ItemPublishEvent<PayloadItem<SimplePayload>> items){ String spaceId = getSpaceId(items.getNodeId()); if (spaceId == null) return; List<String> toRequest = new ArrayList<String>(); for (PayloadItem<SimplePayload> item:items.getItems()){ if (item.getPayload() == null && !pendingPayloadRequests.containsKey(item.getId())){ toRequest.add(item.getId()); continue; } DataObject obj = parseItemToDataObject(item); if (dataObjectFilter == null || dataObjectFilter.isDataObjectValid(obj)) { if (!datawrapper.isDataObjectAlreadyCached(item.getId())){ datawrapper.saveDataObject(items.getNodeId(), obj, item.getId()); } for (DataObjectListener listener:listeners){ listener.handleDataObject(obj, spaceId); } } } if (!toRequest.isEmpty()){ for (String id:toRequest){ try { retrievePayload(items.getNodeId(), id); } catch (Exception e) { //Ignore continue; } } } }
/** * Sends the payloads of the user, which are in the sendcache. * @throws UnknownEntityException Thrown when no pubsub node could be retrieved. */ private void sendSavedPayloads() throws UnknownEntityException, InvalidDataException { if (datawrapper == null) return; Map<String, SimplePayload> payloads = datawrapper.getPayloadsToSend(userInfo.getBareJID()); for (String id:payloads.keySet()){ String spaceId = datawrapper.getSpaceForPayload(id); if (spaceId == null) continue; publish(spaceId, payloads.get(id)); } datawrapper.clearSendCache(userInfo.getBareJID()); }
public PacketExtension parseExtension(XmlPullParser parser) throws Exception { String id = parser.getAttributeValue(null, "id"); String node = parser.getAttributeValue(null, "node"); String elem = parser.getName(); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(id, node); } else { String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null) { boolean done = false; StringBuilder payloadText = new StringBuilder(); while (!done) { if (tag == XmlPullParser.END_TAG && parser.getName().equals(elem)) done = true; else if (!((tag == XmlPullParser.START_TAG) && parser.isEmptyElementTag())) payloadText.append(parser.getText()); if (!done) tag = parser.next(); } return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText.toString())); } else { return new PayloadItem<PacketExtension>(id, node, PacketParserUtils.parsePacketExtension(payloadElemName, payloadNS, parser)); } } }
public PacketExtension parseExtension(XmlPullParser parser) throws Exception { String id = parser.getAttributeValue(null, "id"); String node = parser.getAttributeValue(null, "node"); String elem = parser.getName(); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(id, node); } else { String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null) { boolean done = false; boolean isEmptyElement = false; StringBuilder payloadText = new StringBuilder(); while (!done) { if (tag == XmlPullParser.END_TAG && parser.getName().equals(elem)) { done = true; continue; } else if (parser.getEventType() == XmlPullParser.START_TAG) { payloadText.append("<").append(parser.getName()); if (parser.getName().equals(payloadElemName) && (payloadNS.length() > 0)) payloadText.append(" xmlns=\"").append(payloadNS).append("\""); int n = parser.getAttributeCount(); for (int i = 0; i < n; i++) payloadText.append(" ").append(parser.getAttributeName(i)).append("=\"") .append(parser.getAttributeValue(i)).append("\""); if (parser.isEmptyElementTag()) { payloadText.append("/>"); isEmptyElement = true; } else { payloadText.append(">"); } } else if (parser.getEventType() == XmlPullParser.END_TAG) { if (isEmptyElement) { isEmptyElement = false; } else { payloadText.append("</").append(parser.getName()).append(">"); } } else if (parser.getEventType() == XmlPullParser.TEXT) { payloadText.append(parser.getText()); } tag = parser.next(); } return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText.toString())); } else { return new PayloadItem<PacketExtension>(id, node, PacketParserUtils.parsePacketExtension(payloadElemName, payloadNS, parser)); } } }
public PacketExtension parseExtension(XmlPullParser parser) throws Exception { String id = parser.getAttributeValue(null, "id"); String node = parser.getAttributeValue(null, "node"); String elem = parser.getName(); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(id, node); } else { while (tag != XmlPullParser.START_TAG) tag = parser.next(); String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); if (ProviderManager.getInstance().getExtensionProvider( payloadElemName, payloadNS) == null) { boolean done = false; StringBuilder payloadText = new StringBuilder(); while (!done) { if (tag == XmlPullParser.END_TAG && parser.getName().equals(elem)) done = true; else if (!((tag == XmlPullParser.START_TAG) && parser .isEmptyElementTag())) payloadText.append(parser.getText()); if (!done) tag = parser.next(); } return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText.toString())); } else { return new PayloadItem<PacketExtension>(id, node, PacketParserUtils.parsePacketExtension(payloadElemName, payloadNS, parser)); } } }
public PacketExtension parseExtension(XmlPullParser parser) throws Exception { String id = parser.getAttributeValue(null, "id"); String node = parser.getAttributeValue(null, "node"); String elem = parser.getName(); int tag = parser.next(); if (tag == XmlPullParser.END_TAG) { return new Item(id, node); } else { String payloadElemName = parser.getName(); String payloadNS = parser.getNamespace(); if (ProviderManager.getInstance().getExtensionProvider(payloadElemName, payloadNS) == null) { boolean done = false; boolean isEmptyElement = false; StringBuilder payloadText = new StringBuilder(); while (!done) { if (tag == XmlPullParser.END_TAG && parser.getName().equals(elem)) { done = true; } else if (parser.getEventType() == XmlPullParser.START_TAG) { payloadText.append("<").append(parser.getName()); if (parser.getName().equals(payloadElemName) && (payloadNS.length() > 0)) payloadText.append(" xmlns=\"").append(payloadNS).append("\""); int n = parser.getAttributeCount(); for (int i = 0; i < n; i++) payloadText.append(" ").append(parser.getAttributeName(i)).append("=\"") .append(parser.getAttributeValue(i)).append("\""); if (parser.isEmptyElementTag()) { payloadText.append("/>"); isEmptyElement = true; } else { payloadText.append(">"); } } else if (parser.getEventType() == XmlPullParser.END_TAG) { if (isEmptyElement) isEmptyElement = false; else payloadText.append("</").append(parser.getName()).append(">"); } else if (parser.getEventType() == XmlPullParser.TEXT) { payloadText.append(parser.getText()); } tag = parser.next(); } return new PayloadItem<SimplePayload>(id, node, new SimplePayload(payloadElemName, payloadNS, payloadText.toString())); } else { return new PayloadItem<PacketExtension>(id, node, PacketParserUtils.parsePacketExtension( payloadElemName, payloadNS, parser)); } } }
/** * Publishes a data object on the space with the given id. * ONLINE mode: The object is directly published on the related pubsub node. * OFFLINE mode: The object is stored locally and published when the connection is establised again. * @param object Data object to publish. * @param spaceId Identifier if the space to publish. * @throws UnknownEntityException A space with the given id is not known to the space handler. * @throws InvalidDataException The data was rejected by the spaces service. */ @Override public void publishDataObject(DataObject object, String spaceId) throws UnknownEntityException, InvalidDataException { de.imc.mirror.sdk.android.DataObject obj = (de.imc.mirror.sdk.android.DataObject) object; SimplePayload payload = new SimplePayload(obj.getElementName(), obj.getNamespaceURI(), obj.toString()); publish(spaceId, payload); }
/** * Publishes a data objects and returns the object sent over the space. * Use this method to access fields set server-side like the data object identifier. * @param dataObject Data object to publish. * @param spaceId Identifier if the space to publish. * @return Data object as published on the space. * @throws UnknownEntityException A space with the given id is not known to the space handler. * @throws InvalidDataException The data was rejected by the spaces service. * @throws ConnectionStatusException The data handler has to be online in order to make a synchronous call. */ public DataObject publishAndRetrieveDataObject(DataObject dataObject, String spaceId) throws UnknownEntityException, InvalidDataException, ConnectionStatusException { SimplePayload payload = new SimplePayload(dataObject.getElement().getName(), dataObject.getNamespaceURI(), dataObject.toString()); return publishAndRetrieve(spaceId, payload); }