From e2f24c6930603dbd016a6530f7d12b08c97ea900 Mon Sep 17 00:00:00 2001 From: Gurmeen Bindra Date: Thu, 19 Apr 2012 10:13:47 +0100 Subject: Port Classes for Storage/Private Storage This patch ports the classes for Storage, PrivateStorage and PrivateStorage requests from Swiften to Stroke. Test-information: junit test for GetPrivateStorageRequestTest is also ported and tested diff --git a/src/com/isode/stroke/elements/PrivateStorage.java b/src/com/isode/stroke/elements/PrivateStorage.java new file mode 100644 index 0000000..171e56e --- /dev/null +++ b/src/com/isode/stroke/elements/PrivateStorage.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tronçon. + * All rights reserved. + */ +package com.isode.stroke.elements; + +/** + * Class representing Private XML Storage for storing arbitrary private + * user data on User's server(such as XEP-0048's bookmarks) + * + */ +public class PrivateStorage extends Payload { + + /** + * Constructor + * @param p payload, not null + */ + public PrivateStorage(Payload p) { + this.payload = p; + } + + /** + * Get payload + * @return payload, not null + */ + public Payload getPayload() { + return payload; + } + + /** + * Set payload + * @param p payload, not null + */ + public void setPayload(Payload p) { + payload = p; + } + + private Payload payload; +} diff --git a/src/com/isode/stroke/elements/Storage.java b/src/com/isode/stroke/elements/Storage.java new file mode 100644 index 0000000..8597c27 --- /dev/null +++ b/src/com/isode/stroke/elements/Storage.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tronçon. + * All rights reserved. + */ + +package com.isode.stroke.elements; + +import java.util.Vector; +import com.isode.stroke.jid.JID; + +/** + * Class representing storage for storing payloads + * + */ +public class Storage extends Payload { + private Vector rooms = new Vector(); + private Vector urls = new Vector(); + + /** + * Class representing a chat room + * + */ + public static class Room { + public Room() { + autoJoin = false; + } + + public String name = ""; + public JID jid = JID.fromString(""); + public boolean autoJoin = false; + public String nick = ""; + public String password; + } + + /** + * Class for bookmarking web pages, i.e., HTTP or HTTPS URLs. + * + */ + public class URL { + public URL() { + + } + public String name = ""; + public String url = ""; + } + + /** + * Constructor + */ + public Storage() { + } + + /** + * Clear the list of rooms + */ + public void clearRooms() { + rooms.clear(); + } + + /** + * Get the list of rooms + * @return room list, can be empty but not null + */ + public Vector getRooms() { + return rooms; + } + + /** + * Add a room to the list + * @param room room, not null + */ + public void addRoom(Room room) { + rooms.add(room); + } + + /** + * Get a list of URLs + * @return URL list, can be empty but not null + */ + public Vector getURLs() { + return urls; + } + + /** + * Add a URL + * @param url rul, not null + */ + public void addURL(URL url) { + urls.add(url); + } +} diff --git a/src/com/isode/stroke/queries/DummyIQChannel.java b/src/com/isode/stroke/queries/DummyIQChannel.java new file mode 100644 index 0000000..fbd69b9 --- /dev/null +++ b/src/com/isode/stroke/queries/DummyIQChannel.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tronçon + * All rights reserved. + */ +package com.isode.stroke.queries; + +import java.util.Vector; + +import com.isode.stroke.elements.IQ; + +/** + * Dummy IQ Channel + * + */ +public class DummyIQChannel extends IQChannel { + + /** + * Constructor + */ + public DummyIQChannel() {} + + @Override + public void sendIQ(IQ iq) { + iqs_.add(iq); + } + + @Override + public String getNewIQID() { + return "test-id"; + } + + @Override + public boolean isAvailable() { + return true; + } + + public Vector iqs_ = new Vector(); +} diff --git a/src/com/isode/stroke/queries/requests/GetPrivateStorageRequest.java b/src/com/isode/stroke/queries/requests/GetPrivateStorageRequest.java new file mode 100644 index 0000000..a453d62 --- /dev/null +++ b/src/com/isode/stroke/queries/requests/GetPrivateStorageRequest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tronçon + * All rights reserved. + */ +package com.isode.stroke.queries.requests; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.PrivateStorage; +import com.isode.stroke.jid.JID; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.queries.Request; +import com.isode.stroke.signals.Signal2; + +/** + * Class representing a request to get a private storage + * + * @param type extending {@link Payload} + */ +public class GetPrivateStorageRequest extends Request { + + /** + * Create the request + * @param Payload Type + * @param payload object of type payload + * @param router IQ router + * @return request to get Private storage + */ + public static GetPrivateStorageRequest create(T payload,IQRouter router) { + return new GetPrivateStorageRequest(payload,router); + } + + private GetPrivateStorageRequest(T payload,IQRouter router) { + super(IQ.Type.Get,new JID(),new PrivateStorage(payload),router); + } + + @Override + public void handleResponse(Payload payload, ErrorPayload error) { + PrivateStorage storage = null; + if(payload instanceof PrivateStorage) { + storage = (PrivateStorage)payload; + } + if (storage != null) { + onResponse.emit((T) storage.getPayload(), error); + } + else { + onResponse.emit(null, error); + } + } + + /** + * Siganl to be notified on receiving the response for the request sent + */ + public Signal2 onResponse = new Signal2(); +} diff --git a/src/com/isode/stroke/queries/requests/SetPrivateStorageRequest.java b/src/com/isode/stroke/queries/requests/SetPrivateStorageRequest.java new file mode 100644 index 0000000..21d753f --- /dev/null +++ b/src/com/isode/stroke/queries/requests/SetPrivateStorageRequest.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tronçon. + * All rights reserved. + */ +package com.isode.stroke.queries.requests; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.PrivateStorage; +import com.isode.stroke.jid.JID; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.queries.Request; +import com.isode.stroke.signals.Signal1; + +/** + * Class representing a request to set a private storage + * + * @param type extending {@link Payload} + */ +public class SetPrivateStorageRequest extends Request { + + /** + * Create the request + * @param Payload Type + * @param payload object of type payload + * @param router IQ router + * @return request to set Private storage + */ + public static SetPrivateStorageRequest create(T payload, IQRouter router) { + return new SetPrivateStorageRequest(payload, router); + } + + private SetPrivateStorageRequest(T payload, IQRouter router) { + super(IQ.Type.Set, new JID(), new PrivateStorage(payload), router); + } + + @Override + public void handleResponse(Payload p, ErrorPayload error) { + onResponse.emit(error); + } + + public Signal1 onResponse = new Signal1(); +} diff --git a/test/com/isode/stroke/queries/requests/GetPrivateStorageRequestTest.java b/test/com/isode/stroke/queries/requests/GetPrivateStorageRequestTest.java new file mode 100644 index 0000000..b310b6c --- /dev/null +++ b/test/com/isode/stroke/queries/requests/GetPrivateStorageRequestTest.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2012, Isode Limited, London, England. + * All rights reserved. + */ +/* + * Copyright (c) 2010, Remko Tronçon + * All rights reserved. + */ +package com.isode.stroke.queries.requests; + +import java.util.Vector; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.Payload; +import com.isode.stroke.elements.PrivateStorage; +import com.isode.stroke.jid.JID; +import com.isode.stroke.queries.DummyIQChannel; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.signals.Slot2; + +public class GetPrivateStorageRequestTest { + private IQRouter router; + private DummyIQChannel channel; + private Vector errors = new Vector(); + private Vector responses = new Vector(); + + public class MyPayload extends Payload { + public MyPayload() { + this.text = ""; + } + public MyPayload(String text) { + this.text = text; + } + public String text; + } + + @Before + public void setUp() { + channel = new DummyIQChannel(); + router = new IQRouter(channel); + } + + @Test + public void testSend() throws Exception { + MyPayload mpl = new MyPayload(); + PrivateStorage privStType = new PrivateStorage(mpl); + GetPrivateStorageRequest request = GetPrivateStorageRequest.create(mpl,router); + request.send(); + + assertEquals(1, channel.iqs_.size()); + assertEquals(new JID(), channel.iqs_.get(0).getTo()); + assertEquals(IQ.Type.Get, channel.iqs_.get(0).getType()); + + PrivateStorage storage = channel.iqs_.get(0).getPayload(privStType); + assertTrue(storage != null); + MyPayload payload = (MyPayload)storage.getPayload(); + assertTrue(payload != null); + } + + @Test + public void testHandleResponse() { + MyPayload mpl = new MyPayload(); + GetPrivateStorageRequest testling = GetPrivateStorageRequest.create(mpl,router); + testling.onResponse.connect(new Slot2() { + @Override + public void call(MyPayload p1, ErrorPayload p2) { + handleResponse(p1,p2); + } + }); + testling.send(); + channel.onIQReceived.emit(createResponse("test-id", "foo")); + + assertEquals(1, responses.size()); + assertEquals("foo", ((MyPayload)responses.get(0)).text); + } + + @Test + public void testHandleResponse_Error() { + MyPayload mpl = new MyPayload(); + GetPrivateStorageRequest testling = GetPrivateStorageRequest.create(mpl,router); + testling.onResponse.connect(new Slot2() { + @Override + public void call(MyPayload p1, ErrorPayload ep) { + handleResponse(p1, ep); + } + }); + testling.send(); + channel.onIQReceived.emit(createError("test-id")); + + assertEquals(0, responses.size()); + assertEquals(1, errors.size()); + } + + private void handleResponse(Payload p, ErrorPayload e) { + if (e != null) { + errors.add(e); + } else { + responses.add(p); + } + } + + private IQ createResponse(String id, String text) { + IQ iq = new IQ(IQ.Type.Result); + MyPayload mPl = new MyPayload(text); + PrivateStorage storage = new PrivateStorage(mPl); + storage.setPayload(mPl); + iq.addPayload(storage); + iq.setID(id); + return iq; + } + + private IQ createError(String id) { + IQ iq = new IQ(IQ.Type.Error); + iq.setID(id); + return iq; + } +} -- cgit v0.10.2-6-g49f6