This uses Java's Service Provider Interface (SPI) to abstract different WebSocket implementations. SMACK-835listEqualsHashcode
parent
525f27abf1
commit
6533cb7ed1
@ -0,0 +1,10 @@
|
||||
description = """\
|
||||
Smack for XMPP connections over WebSocket (RFC 7395) using OkHttp."""
|
||||
|
||||
dependencies {
|
||||
api project(':smack-websocket')
|
||||
|
||||
testFixturesApi(testFixtures(project(':smack-websocket')))
|
||||
|
||||
implementation("com.squareup.okhttp3:okhttp:4.6.0")
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus.
|
||||
*
|
||||
* 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 org.jivesoftware.smack.websocket.okhttp;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.websocket.impl.AbstractWebsocket;
|
||||
import org.jivesoftware.smack.websocket.impl.WebsocketFactory;
|
||||
|
||||
public class OkHttpWebsocketFactory implements WebsocketFactory {
|
||||
|
||||
@Override
|
||||
public AbstractWebsocket create(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
return new OkHttpWebsocket(connectionInternal);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.jivesoftware.smack.websocket.okhttp.OkHttpWebsocketFactory
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus.
|
||||
*
|
||||
* 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 org.jivesoftware.smack.websocket.okhttp;
|
||||
|
||||
import org.jivesoftware.smack.websocket.test.WebsocketFactoryServiceTestUtil;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class OkHttpWebsocketFactoryServiceTest {
|
||||
|
||||
@Test
|
||||
public void createWebsocketTest() {
|
||||
WebsocketFactoryServiceTestUtil.createWebsocketTest(OkHttpWebsocket.class);
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,8 @@
|
||||
description = """\
|
||||
Smack for standard XMPP connections over Websockets."""
|
||||
Smack for XMPP connections over WebSocket (RFC 7395)."""
|
||||
|
||||
dependencies {
|
||||
compile project(':smack-core')
|
||||
api project(':smack-core')
|
||||
|
||||
testFixturesApi(testFixtures(project(":smack-core")))
|
||||
|
||||
implementation("com.squareup.okhttp3:okhttp:4.6.0")
|
||||
testFixturesApi(testFixtures(project(':smack-core')))
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus.
|
||||
*
|
||||
* 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 org.jivesoftware.smack.websocket.impl;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
|
||||
public interface WebsocketFactory {
|
||||
|
||||
AbstractWebsocket create(ModularXmppClientToServerConnectionInternal connectionInternal);
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus.
|
||||
*
|
||||
* 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 org.jivesoftware.smack.websocket.impl;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
|
||||
public final class WebsocketFactoryService {
|
||||
|
||||
private static final ServiceLoader<WebsocketFactory> SERVICE_LOADER = ServiceLoader.load(WebsocketFactory.class);
|
||||
|
||||
public static AbstractWebsocket createWebsocket(ModularXmppClientToServerConnectionInternal connectionInternal) {
|
||||
assert connectionInternal != null;
|
||||
|
||||
Iterator<WebsocketFactory> websocketFactories = SERVICE_LOADER.iterator();
|
||||
if (!websocketFactories.hasNext()) {
|
||||
throw new IllegalStateException("No smack websocket service configured");
|
||||
}
|
||||
|
||||
WebsocketFactory websocketFactory = websocketFactories.next();
|
||||
return websocketFactory.create(connectionInternal);
|
||||
}
|
||||
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2020 Aditya Borikar.
|
||||
*
|
||||
* 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 org.jivesoftware.smack.websocket.implementations;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.util.Objects;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints;
|
||||
|
||||
public final class WebsocketImplProvider {
|
||||
|
||||
public static AbstractWebsocket getWebsocketImpl(Class<? extends AbstractWebsocket> websocketImpl, ModularXmppClientToServerConnectionInternal connectionInternal, DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||
Objects.requireNonNull(connectionInternal, "ConnectionInternal cannot be null");
|
||||
|
||||
// Creates an instance of the constructor for the desired websocket implementation.
|
||||
Constructor<? extends AbstractWebsocket> constructor = websocketImpl.getConstructor(ModularXmppClientToServerConnectionInternal.class, DiscoveredWebsocketEndpoints.class);
|
||||
return (AbstractWebsocket) constructor.newInstance(connectionInternal, discoveredWebsocketEndpoints);
|
||||
}
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2020 Aditya Borikar.
|
||||
*
|
||||
* 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 org.jivesoftware.smack.websocket.implementations;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.websocket.XmppWebsocketTransportModule.XmppWebsocketTransport.DiscoveredWebsocketEndpoints;
|
||||
|
||||
import org.jivesoftware.smack.websocket.implementations.okhttp.OkHttpWebsocket;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpoint;
|
||||
import org.jivesoftware.smack.websocket.rce.WebsocketRemoteConnectionEndpointLookup.Result;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ProviderTest {
|
||||
@Test
|
||||
public void providerTest() {
|
||||
assertThrows(IllegalArgumentException.class, () -> WebsocketImplProvider.getWebsocketImpl(OkHttpWebsocket.class, null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getImplTest() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, URISyntaxException {
|
||||
WebsocketRemoteConnectionEndpoint endpoint = new WebsocketRemoteConnectionEndpoint("wss://localhost.org:7443/ws/");
|
||||
|
||||
List<WebsocketRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints = new ArrayList<>();
|
||||
discoveredRemoteConnectionEndpoints.add(endpoint);
|
||||
|
||||
Result result = new Result(discoveredRemoteConnectionEndpoints, null);
|
||||
|
||||
DiscoveredWebsocketEndpoints discoveredWebsocketEndpoints = mock(DiscoveredWebsocketEndpoints.class);
|
||||
when(discoveredWebsocketEndpoints.getResult()).thenReturn(result);
|
||||
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal = mock(ModularXmppClientToServerConnectionInternal.class);
|
||||
|
||||
assertNotNull(WebsocketImplProvider.getWebsocketImpl(OkHttpWebsocket.class, connectionInternal, discoveredWebsocketEndpoints));
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/**
|
||||
*
|
||||
* Copyright 2020 Florian Schmaus.
|
||||
*
|
||||
* 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 org.jivesoftware.smack.websocket.test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
|
||||
import org.jivesoftware.smack.websocket.impl.AbstractWebsocket;
|
||||
import org.jivesoftware.smack.websocket.impl.WebsocketFactoryService;
|
||||
|
||||
public class WebsocketFactoryServiceTestUtil {
|
||||
|
||||
public static void createWebsocketTest(Class<? extends AbstractWebsocket> expected) {
|
||||
ModularXmppClientToServerConnectionInternal connectionInternal = mock(ModularXmppClientToServerConnectionInternal.class);
|
||||
|
||||
AbstractWebsocket websocket = WebsocketFactoryService.createWebsocket(connectionInternal);
|
||||
assertEquals(expected, websocket.getClass());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue