2020-05-14 14:35:37 +02:00
/ * *
*
2021-01-25 19:51:45 +01:00
* Copyright 2020 Aditya Borikar , 2020 - 2021 Florian Schmaus
2020-05-14 14:35:37 +02:00
*
* 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 ;
import java.net.URI ;
import java.net.URISyntaxException ;
import java.util.HashSet ;
import java.util.Set ;
2021-01-25 19:51:45 +01:00
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode ;
2020-05-14 14:35:37 +02:00
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection ;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration ;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModule ;
import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor ;
import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal ;
import org.jivesoftware.smack.fsm.StateDescriptor ;
import org.jivesoftware.smack.util.Objects ;
2020-09-01 21:30:14 +02:00
import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.EstablishingWebSocketConnectionStateDescriptor ;
2021-02-14 19:42:27 +01:00
import org.jivesoftware.smack.websocket.impl.WebSocketFactory ;
2021-01-25 19:51:45 +01:00
import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint ;
2020-05-14 14:35:37 +02:00
/ * *
2020-09-01 21:30:14 +02:00
* The descriptor class for { @link XmppWebSocketTransportModule } .
2020-05-14 14:35:37 +02:00
* < br >
2020-09-01 21:30:14 +02:00
* To add { @link XmppWebSocketTransportModule } to { @link ModularXmppClientToServerConnection } ,
2020-05-14 14:35:37 +02:00
* use { @link ModularXmppClientToServerConnectionConfiguration . Builder # addModule ( ModularXmppClientToServerConnectionModuleDescriptor ) } .
* /
2020-09-01 21:30:14 +02:00
public final class XmppWebSocketTransportModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {
2021-01-25 19:51:45 +01:00
private final boolean performWebSocketEndpointDiscovery ;
private final boolean implicitWebSocketEndpoint ;
private final WebSocketRemoteConnectionEndpoint wsRce ;
2020-05-14 14:35:37 +02:00
2021-02-14 19:42:27 +01:00
final WebSocketFactory webSocketFactory ;
2020-09-01 21:30:14 +02:00
public XmppWebSocketTransportModuleDescriptor ( Builder builder ) {
this . performWebSocketEndpointDiscovery = builder . performWebSocketEndpointDiscovery ;
2021-01-25 19:51:45 +01:00
this . implicitWebSocketEndpoint = builder . implicitWebSocketEndpoint ;
2021-02-14 19:42:27 +01:00
this . webSocketFactory = builder . webSocketFactory ;
2021-01-25 19:51:45 +01:00
2021-02-14 19:42:27 +01:00
URI uri = builder . uri ;
2021-01-25 19:51:45 +01:00
if ( uri ! = null ) {
wsRce = WebSocketRemoteConnectionEndpoint . from ( uri ) ;
} else {
wsRce = null ;
}
}
@Override
@SuppressWarnings ( { " incomplete-switch " , " MissingCasesInEnumSwitch " } )
protected void validateConfiguration ( ModularXmppClientToServerConnectionConfiguration configuration ) {
if ( wsRce = = null ) {
return ;
}
SecurityMode securityMode = configuration . getSecurityMode ( ) ;
switch ( securityMode ) {
case required :
if ( ! wsRce . isSecureEndpoint ( ) ) {
throw new IllegalArgumentException ( " The provided WebSocket endpoint " + wsRce + " is not a secure endpoint, but the connection configuration requires secure endpoints " ) ;
}
break ;
case disabled :
if ( wsRce . isSecureEndpoint ( ) ) {
throw new IllegalArgumentException ( " The provided WebSocket endpoint " + wsRce + " is a secure endpoint, but the connection configuration has security disabled " ) ;
}
break ;
}
2020-05-14 14:35:37 +02:00
}
/ * *
* Returns true if websocket endpoint discovery is true , returns false otherwise .
* @return boolean
* /
2020-09-01 21:30:14 +02:00
public boolean isWebSocketEndpointDiscoveryEnabled ( ) {
return performWebSocketEndpointDiscovery ;
2020-05-14 14:35:37 +02:00
}
2021-01-25 19:51:45 +01:00
public boolean isImplicitWebSocketEndpointEnabled ( ) {
return implicitWebSocketEndpoint ;
}
2020-05-14 14:35:37 +02:00
/ * *
* Returns explicitly configured websocket endpoint uri .
* @return uri
* /
public URI getExplicitlyProvidedUri ( ) {
2021-02-14 19:42:27 +01:00
return wsRce . getUri ( ) ;
2020-05-14 14:35:37 +02:00
}
2021-01-25 19:51:45 +01:00
WebSocketRemoteConnectionEndpoint getExplicitlyProvidedEndpoint ( ) {
return wsRce ;
}
2020-05-14 14:35:37 +02:00
@Override
protected Set < Class < ? extends StateDescriptor > > getStateDescriptors ( ) {
Set < Class < ? extends StateDescriptor > > res = new HashSet < > ( ) ;
2020-09-01 21:30:14 +02:00
res . add ( EstablishingWebSocketConnectionStateDescriptor . class ) ;
2020-05-14 14:35:37 +02:00
return res ;
}
@Override
protected ModularXmppClientToServerConnectionModule < ? extends ModularXmppClientToServerConnectionModuleDescriptor > constructXmppConnectionModule (
ModularXmppClientToServerConnectionInternal connectionInternal ) {
2020-09-01 21:30:14 +02:00
return new XmppWebSocketTransportModule ( this , connectionInternal ) ;
2020-05-14 14:35:37 +02:00
}
/ * *
* Returns a new instance of { @link Builder } .
* < br >
* @return Builder
* @param connectionConfigurationBuilder { @link ModularXmppClientToServerConnectionConfiguration . Builder } .
* /
public static Builder getBuilder (
ModularXmppClientToServerConnectionConfiguration . Builder connectionConfigurationBuilder ) {
return new Builder ( connectionConfigurationBuilder ) ;
}
/ * *
2020-09-01 21:30:14 +02:00
* Builder class for { @link XmppWebSocketTransportModuleDescriptor } .
2020-05-14 14:35:37 +02:00
* < br >
2020-09-01 21:30:14 +02:00
* To obtain an instance of { @link XmppWebSocketTransportModuleDescriptor . Builder } , use { @link XmppWebSocketTransportModuleDescriptor # getBuilder ( ModularXmppClientToServerConnectionConfiguration . Builder ) } method .
2020-05-14 14:35:37 +02:00
* < br >
2020-09-01 21:30:14 +02:00
* Use { @link Builder # explicitlySetWebSocketEndpoint ( URI ) } to configure the URI of an endpoint as a backup in case connection couldn ' t be established with endpoints through http lookup .
2020-05-14 14:35:37 +02:00
* < br >
2020-09-01 21:30:14 +02:00
* Use { @link Builder # explicitlySetWebSocketEndpointAndDiscovery ( URI , boolean ) } to configure endpoint and disallow websocket endpoint discovery through http lookup .
* By default , { @link Builder # performWebSocketEndpointDiscovery } is set to true .
2020-05-14 14:35:37 +02:00
* < br >
2020-09-01 21:30:14 +02:00
* Use { @link Builder # build ( ) } to obtain { @link XmppWebSocketTransportModuleDescriptor } .
2020-05-14 14:35:37 +02:00
* /
public static final class Builder extends ModularXmppClientToServerConnectionModuleDescriptor . Builder {
2020-09-01 21:30:14 +02:00
private boolean performWebSocketEndpointDiscovery = true ;
2021-01-25 19:51:45 +01:00
private boolean implicitWebSocketEndpoint = true ;
2020-05-14 14:35:37 +02:00
private URI uri ;
2021-02-14 19:42:27 +01:00
private WebSocketFactory webSocketFactory ;
2020-05-14 14:35:37 +02:00
private Builder (
ModularXmppClientToServerConnectionConfiguration . Builder connectionConfigurationBuilder ) {
super ( connectionConfigurationBuilder ) ;
}
2020-09-01 21:30:14 +02:00
public Builder explicitlySetWebSocketEndpoint ( URI endpoint ) {
return explicitlySetWebSocketEndpointAndDiscovery ( endpoint , true ) ;
2020-05-14 14:35:37 +02:00
}
2020-09-01 21:30:14 +02:00
public Builder explicitlySetWebSocketEndpointAndDiscovery ( URI endpoint , boolean performWebSocketEndpointDiscovery ) {
2020-05-14 14:35:37 +02:00
Objects . requireNonNull ( endpoint , " Provided endpoint URI must not be null " ) ;
this . uri = endpoint ;
2020-09-01 21:30:14 +02:00
this . performWebSocketEndpointDiscovery = performWebSocketEndpointDiscovery ;
2020-05-14 14:35:37 +02:00
return this ;
}
2020-09-01 21:30:14 +02:00
public Builder explicitlySetWebSocketEndpoint ( CharSequence endpoint ) throws URISyntaxException {
2020-05-14 14:35:37 +02:00
URI endpointUri = new URI ( endpoint . toString ( ) ) ;
2021-01-25 19:51:45 +01:00
return explicitlySetWebSocketEndpoint ( endpointUri ) ;
2020-05-14 14:35:37 +02:00
}
2021-01-25 19:51:45 +01:00
public Builder explicitlySetWebSocketEndpointAndDiscovery ( CharSequence endpoint , boolean performWebSocketEndpointDiscovery )
2020-05-14 14:35:37 +02:00
throws URISyntaxException {
URI endpointUri = new URI ( endpoint . toString ( ) ) ;
2020-09-01 21:30:14 +02:00
return explicitlySetWebSocketEndpointAndDiscovery ( endpointUri , performWebSocketEndpointDiscovery ) ;
2020-05-14 14:35:37 +02:00
}
2021-01-25 19:51:45 +01:00
public Builder disableImplicitWebsocketEndpoint ( ) {
implicitWebSocketEndpoint = false ;
return this ;
}
2021-02-14 19:42:27 +01:00
public Builder setWebSocketFactory ( WebSocketFactory webSocketFactory ) {
Objects . requireNonNull ( webSocketFactory ) ;
this . webSocketFactory = webSocketFactory ;
return this ;
}
2020-05-14 14:35:37 +02:00
@Override
public ModularXmppClientToServerConnectionModuleDescriptor build ( ) {
2020-09-01 21:30:14 +02:00
return new XmppWebSocketTransportModuleDescriptor ( this ) ;
2020-05-14 14:35:37 +02:00
}
}
}