/*******************************************************************************
 * Copyright (C) 2016 Push Technology Ltd.
 *
 * 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 com.pushtechnology.adapters.rest.adapter;

import static com.pushtechnology.diffusion.client.session.SessionAttributes.Transport.WEBSOCKET;

import javax.net.ssl.SSLContext;

import org.picocontainer.annotations.Nullable;
import org.picocontainer.injectors.ProviderAdapter;

import com.pushtechnology.adapters.rest.model.latest.DiffusionConfig;
import com.pushtechnology.adapters.rest.model.latest.Model;
import com.pushtechnology.diffusion.client.Diffusion;
import com.pushtechnology.diffusion.client.session.Session;

/**
 * Factory for session.
 *
 * @author Push Technology Limited
 */
public final class SessionFactory extends ProviderAdapter {
    /**
     * @return an open session
     */
    public Session provide(Model model, SessionLostListener listener, @Nullable SSLContext sslContext) {
        final DiffusionConfig diffusionConfig = model.getDiffusion();

        com.pushtechnology.diffusion.client.session.SessionFactory sessionFactory = Diffusion
            .sessions()
            .serverHost(diffusionConfig.getHost())
            .serverPort(diffusionConfig.getPort())
            .secureTransport(diffusionConfig.isSecure())
            .transports(WEBSOCKET)
            .reconnectionTimeout(5000)
            .listener(listener);

        if (sslContext != null) {
            sessionFactory = sessionFactory.sslContext(sslContext);
        }

        if (diffusionConfig.getPrincipal() != null && diffusionConfig.getPassword() != null) {
            sessionFactory = sessionFactory
                .principal(diffusionConfig.getPrincipal())
                .password(diffusionConfig.getPassword());
        }

        return sessionFactory.open();
    }
}

@@ -2179,49 +2179,8 @@
 ) !=
- null && diffusionConfig.getPassword() !=
  nul
@@ -2227,33 +2227,16 @@
 nFactory
-%0A                
 .princip
@@ -2269,16 +2269,18 @@
 cipal())
+;%0A
 %0A       
@@ -2284,20 +2284,108 @@
         
-    
+if (diffusionConfig.getPassword() != null) %7B%0A                sessionFactory = sessionFactory
 .passwor
@@ -2418,16 +2418,30 @@
 ord());%0A
+            %7D%0A
         

Allow the session principal to be set without a credential.