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 / callbacks / AbstractCallback.java @ 47828

History | View | Annotate | Download (3.29 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.downloader.swing.scribejava.keycloak.callbacks;
7

    
8
import com.github.scribejava.core.oauth.OAuth20Service;
9
import com.sun.net.httpserver.HttpExchange;
10
import java.io.IOException;
11
import java.io.OutputStream;
12
import org.apache.commons.lang3.StringUtils;
13
import org.gvsig.downloader.swing.scribejava.keycloak.DownloaderAuthenticationKeycloakConfig;
14
import org.gvsig.downloader.swing.scribejava.keycloak.DownloaderAuthenticationKeycloakFactory;
15
import org.gvsig.downloader.swing.scribejava.keycloak.DownloaderAuthenticationKeycloakRequester;
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
18

    
19
/**
20
 *
21
 * @author jjdelcerro
22
 */
23
@SuppressWarnings("UseSpecificCatch")
24
public class AbstractCallback {
25

    
26
    protected static final Logger LOGGER = LoggerFactory.getLogger(CallbackAuthorizationHandler.class);
27

    
28
    protected final OAuth20Service service;
29
    protected final DownloaderAuthenticationKeycloakRequester identificationRequester;
30
    protected final String contextPath;
31
    protected boolean aborted;
32

    
33
    public AbstractCallback(DownloaderAuthenticationKeycloakRequester identificationRequester, OAuth20Service service, String contextPath) {
34
        this.contextPath = contextPath;
35
        this.service = service;
36
        this.identificationRequester = identificationRequester;
37
        this.aborted = false;
38
    }
39
    
40
    protected DownloaderAuthenticationKeycloakConfig getConfig() {
41
        return this.identificationRequester.getConfig();
42
    }
43

    
44
    protected void response(HttpExchange t, int code, String body) throws IOException {
45
        t.sendResponseHeaders(code, body.length());
46
        try (final OutputStream os = t.getResponseBody()) {
47
            os.write(body.getBytes());
48
        }
49
    }
50

    
51
    protected void responseQuietly(HttpExchange t, int code, String body) {
52
        try {
53
            response(t, code, body);
54
        } catch (Exception e) {
55
            // Do nothing
56
            LOGGER.warn("Can't generate response.", e);
57
        }
58
    }
59

    
60
    public void remove() throws IOException {
61
        DownloaderAuthenticationKeycloakConfig config = identificationRequester.getConfig();
62
        DownloaderAuthenticationKeycloakFactory factory = config.getFactory();
63
        factory.removeCallback(config, contextPath);
64
        this.aborted = true;
65
    }
66

    
67
    public void stopWaitingForResponse() {
68
        if (aborted) {
69
            return;
70
        }
71
        this.identificationRequester.stopWaitingForResponse();
72
    }
73

    
74
    public String message_and_close(String s) {
75
        StringBuilder builder = new StringBuilder();
76
        builder.append("<html>\n");
77
        builder.append("<body  onload=\"window.close()\">\n");
78
        builder.append(StringUtils.replace(s, "\n", "<br>\n"));
79
        builder.append("</body>\n");
80
        builder.append("</html>\n");
81
        return builder.toString();
82
    }
83

    
84
    public String message(String s) {
85
        StringBuilder builder = new StringBuilder();
86
        builder.append("<html>\n");
87
        builder.append("<body >\n");
88
        builder.append(StringUtils.replace(s, "\n", "<br>\n"));
89
        builder.append("</body>\n");
90
        builder.append("</html>\n");
91
        return builder.toString();
92
    }
93
}