Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.downloader / org.gvsig.downloader.swing / org.gvsig.downloader.swing.scribejava / src / main / java / org / gvsig / downloader / swing / scribejava / keycloak / DownloaderAuthenticationKeycloakFactory.java @ 47828

History | View | Annotate | Download (2.22 KB)

1
package org.gvsig.downloader.swing.scribejava.keycloak;
2

    
3
import com.sun.net.httpserver.HttpHandler;
4
import com.sun.net.httpserver.HttpServer;
5
import java.io.IOException;
6
import java.net.InetSocketAddress;
7
import org.gvsig.downloader.DownloaderAuthenticationFactory;
8
import org.gvsig.downloader.DownloaderLocator;
9
import org.gvsig.downloader.DownloaderManager;
10
import org.gvsig.downloader.spi.AbstractDownloaderAuthenticationFactory;
11

    
12
/**
13
 *
14
 * @author jjdelcerro
15
 */
16
public class DownloaderAuthenticationKeycloakFactory
17
        extends AbstractDownloaderAuthenticationFactory
18
        implements DownloaderAuthenticationFactory 
19
    {
20

    
21
    public static final String AUTH_MODE_KEYCLOAK = "Keycloak";
22
    
23
    private HttpServer httpserver;
24
    
25
    public DownloaderAuthenticationKeycloakFactory() {
26
        super(AUTH_MODE_KEYCLOAK);
27
    }
28
    
29
    @Override
30
    public DownloaderAuthenticationKeycloakConfig create(Object... parameters) {
31
        return new DownloaderAuthenticationKeycloakConfig(this, (String) parameters[0]);
32
    }
33
    
34
    public HttpServer getHttpServer(DownloaderAuthenticationKeycloakConfig config) throws IOException {
35
        if( this.httpserver == null ) {
36
            this.httpserver = HttpServer.create(new InetSocketAddress(config.getLocalPort()), 0);
37
            this.httpserver.setExecutor(null); // creates a default executor
38
            this.httpserver.start();
39
        }
40
        return this.httpserver;
41
    }
42
    
43
    public void stopHttpServer(int delay) {
44
        if( this.httpserver!=null ) {
45
            this.httpserver.stop(0);
46
        }
47
    }
48

    
49
    public void addCallback(DownloaderAuthenticationKeycloakConfig config, String contextPath, HttpHandler handler ) throws IOException {
50
        this.getHttpServer(config).createContext(contextPath, handler);
51
    }
52

    
53
    public void removeCallback(DownloaderAuthenticationKeycloakConfig config, String contextPath) throws IOException {
54
        this.getHttpServer(config).removeContext(contextPath);
55
    }
56

    
57
    public static void selfRegister() {
58
        DownloaderAuthenticationKeycloakConfig.selfRegister();
59

    
60
        DownloaderManager manager = DownloaderLocator.getDownloaderManager();
61
        manager.registerAuthenticationType(new DownloaderAuthenticationKeycloakFactory());
62
    }
63
}