001/**
002 *
003 * Copyright 2020 Aditya Borikar, 2020-2021 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.websocket;
018
019import java.net.URI;
020import java.net.URISyntaxException;
021import java.util.HashSet;
022import java.util.Set;
023
024import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
025import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection;
026import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionConfiguration;
027import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModule;
028import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnectionModuleDescriptor;
029import org.jivesoftware.smack.c2s.internal.ModularXmppClientToServerConnectionInternal;
030import org.jivesoftware.smack.fsm.StateDescriptor;
031import org.jivesoftware.smack.util.Objects;
032import org.jivesoftware.smack.websocket.XmppWebSocketTransportModule.EstablishingWebSocketConnectionStateDescriptor;
033import org.jivesoftware.smack.websocket.rce.WebSocketRemoteConnectionEndpoint;
034
035/**
036 * The descriptor class for {@link XmppWebSocketTransportModule}.
037 * <br>
038 * To add {@link XmppWebSocketTransportModule} to {@link ModularXmppClientToServerConnection},
039 * use {@link ModularXmppClientToServerConnectionConfiguration.Builder#addModule(ModularXmppClientToServerConnectionModuleDescriptor)}.
040 */
041public final class XmppWebSocketTransportModuleDescriptor extends ModularXmppClientToServerConnectionModuleDescriptor {
042    private final boolean performWebSocketEndpointDiscovery;
043    private final boolean implicitWebSocketEndpoint;
044    private final URI uri;
045    private final WebSocketRemoteConnectionEndpoint wsRce;
046
047    public XmppWebSocketTransportModuleDescriptor(Builder builder) {
048        this.performWebSocketEndpointDiscovery = builder.performWebSocketEndpointDiscovery;
049        this.implicitWebSocketEndpoint = builder.implicitWebSocketEndpoint;
050
051        this.uri = builder.uri;
052        if (uri != null) {
053            wsRce = WebSocketRemoteConnectionEndpoint.from(uri);
054        } else {
055            wsRce = null;
056        }
057    }
058
059    @Override
060    @SuppressWarnings({"incomplete-switch", "MissingCasesInEnumSwitch"})
061    protected void validateConfiguration(ModularXmppClientToServerConnectionConfiguration configuration) {
062        if (wsRce == null) {
063            return;
064        }
065
066        SecurityMode securityMode = configuration.getSecurityMode();
067        switch (securityMode) {
068        case required:
069            if (!wsRce.isSecureEndpoint()) {
070                throw new IllegalArgumentException("The provided WebSocket endpoint " + wsRce + " is not a secure endpoint, but the connection configuration requires secure endpoints");
071            }
072            break;
073        case disabled:
074            if (wsRce.isSecureEndpoint()) {
075                throw new IllegalArgumentException("The provided WebSocket endpoint " + wsRce + " is a secure endpoint, but the connection configuration has security disabled");
076            }
077            break;
078        }
079    }
080
081    /**
082     * Returns true if websocket endpoint discovery is true, returns false otherwise.
083     * @return boolean
084     */
085    public boolean isWebSocketEndpointDiscoveryEnabled() {
086        return performWebSocketEndpointDiscovery;
087    }
088
089    public boolean isImplicitWebSocketEndpointEnabled() {
090        return implicitWebSocketEndpoint;
091    }
092
093    /**
094     * Returns explicitly configured websocket endpoint uri.
095     * @return uri
096     */
097    public URI getExplicitlyProvidedUri() {
098        return uri;
099    }
100
101    WebSocketRemoteConnectionEndpoint getExplicitlyProvidedEndpoint() {
102        return wsRce;
103    }
104
105    @Override
106    protected Set<Class<? extends StateDescriptor>> getStateDescriptors() {
107        Set<Class<? extends StateDescriptor>> res = new HashSet<>();
108        res.add(EstablishingWebSocketConnectionStateDescriptor.class);
109        return res;
110    }
111
112    @Override
113    protected ModularXmppClientToServerConnectionModule<? extends ModularXmppClientToServerConnectionModuleDescriptor> constructXmppConnectionModule(
114            ModularXmppClientToServerConnectionInternal connectionInternal) {
115        return new XmppWebSocketTransportModule(this, connectionInternal);
116    }
117
118    /**
119     * Returns a new instance of {@link Builder}.
120     * <br>
121     * @return Builder
122     * @param connectionConfigurationBuilder {@link ModularXmppClientToServerConnectionConfiguration.Builder}.
123     */
124    public static Builder getBuilder(
125            ModularXmppClientToServerConnectionConfiguration.Builder connectionConfigurationBuilder) {
126        return new Builder(connectionConfigurationBuilder);
127    }
128
129    /**
130     * Builder class for {@link XmppWebSocketTransportModuleDescriptor}.
131     * <br>
132     * To obtain an instance of {@link XmppWebSocketTransportModuleDescriptor.Builder}, use {@link XmppWebSocketTransportModuleDescriptor#getBuilder(ModularXmppClientToServerConnectionConfiguration.Builder)} method.
133     * <br>
134     * 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.
135     * <br>
136     * Use {@link Builder#explicitlySetWebSocketEndpointAndDiscovery(URI, boolean)} to configure endpoint and disallow websocket endpoint discovery through http lookup.
137     * By default, {@link Builder#performWebSocketEndpointDiscovery} is set to true.
138     * <br>
139     * Use {@link Builder#build()} to obtain {@link XmppWebSocketTransportModuleDescriptor}.
140     */
141    public static final class Builder extends ModularXmppClientToServerConnectionModuleDescriptor.Builder {
142        private boolean performWebSocketEndpointDiscovery = true;
143        private boolean implicitWebSocketEndpoint = true;
144        private URI uri;
145
146        private Builder(
147                ModularXmppClientToServerConnectionConfiguration.Builder connectionConfigurationBuilder) {
148            super(connectionConfigurationBuilder);
149        }
150
151        public Builder explicitlySetWebSocketEndpoint(URI endpoint) {
152            return explicitlySetWebSocketEndpointAndDiscovery(endpoint, true);
153        }
154
155        public Builder explicitlySetWebSocketEndpointAndDiscovery(URI endpoint, boolean performWebSocketEndpointDiscovery) {
156            Objects.requireNonNull(endpoint, "Provided endpoint URI must not be null");
157            this.uri = endpoint;
158            this.performWebSocketEndpointDiscovery = performWebSocketEndpointDiscovery;
159            return this;
160        }
161
162        public Builder explicitlySetWebSocketEndpoint(CharSequence endpoint) throws URISyntaxException {
163            URI endpointUri = new URI(endpoint.toString());
164            return explicitlySetWebSocketEndpoint(endpointUri);
165        }
166
167        public Builder explicitlySetWebSocketEndpointAndDiscovery(CharSequence endpoint, boolean performWebSocketEndpointDiscovery)
168                throws URISyntaxException {
169            URI endpointUri = new URI(endpoint.toString());
170            return explicitlySetWebSocketEndpointAndDiscovery(endpointUri, performWebSocketEndpointDiscovery);
171        }
172
173        public Builder disableImplicitWebsocketEndpoint() {
174            implicitWebSocketEndpoint = false;
175            return this;
176        }
177
178        @Override
179        public ModularXmppClientToServerConnectionModuleDescriptor build() {
180            return new XmppWebSocketTransportModuleDescriptor(this);
181        }
182    }
183}