gvsig-projects-pool / org.gvsig.online / trunk / org.gvsig.online / org.gvsig.online.swing / org.gvsig.online.swing.scribejava / src / main / java / org / gvsig / online / swing / scribejava / keycloak / callbacks / AbstractCallback.java @ 9514
History | View | Annotate | Download (3.19 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.online.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.online.swing.scribejava.keycloak.UserIdentificationKeycloak; |
14 |
import org.gvsig.online.swing.scribejava.keycloak.UserIdentificationKeycloakConfig; |
15 |
import org.gvsig.online.swing.scribejava.keycloak.UserIdentificationKeycloakFactory; |
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 UserIdentificationKeycloak identificationRequester; |
30 |
protected final String contextPath; |
31 |
protected boolean aborted; |
32 |
|
33 |
public AbstractCallback(UserIdentificationKeycloak 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 void response(HttpExchange t, int code, String body) throws IOException { |
41 |
t.sendResponseHeaders(code, body.length()); |
42 |
try (final OutputStream os = t.getResponseBody()) { |
43 |
os.write(body.getBytes()); |
44 |
} |
45 |
} |
46 |
|
47 |
protected void responseQuietly(HttpExchange t, int code, String body) { |
48 |
try {
|
49 |
response(t, code, body); |
50 |
} catch (Exception e) { |
51 |
// Do nothing
|
52 |
LOGGER.warn("Can't generate response.", e);
|
53 |
} |
54 |
} |
55 |
|
56 |
public void remove() throws IOException { |
57 |
UserIdentificationKeycloakConfig config = identificationRequester.getConfig(); |
58 |
UserIdentificationKeycloakFactory factory = config.getFactory(); |
59 |
factory.removeCallback(config, contextPath); |
60 |
this.aborted = true; |
61 |
} |
62 |
|
63 |
public void stopWaitingForResponse() { |
64 |
if (aborted) {
|
65 |
return;
|
66 |
} |
67 |
this.identificationRequester.stopWaitingForResponse();
|
68 |
} |
69 |
|
70 |
public String message_and_close(String s) { |
71 |
StringBuilder builder = new StringBuilder(); |
72 |
builder.append("<html>\n");
|
73 |
builder.append("<body >\n");
|
74 |
builder.append(StringUtils.replace(s, "\n", "<br>\n")); |
75 |
builder.append("<script type=\"text/javascript\">");
|
76 |
builder.append("window.close();");
|
77 |
builder.append("</script>\n");
|
78 |
builder.append("</body>\n");
|
79 |
builder.append("</html>\n");
|
80 |
return builder.toString();
|
81 |
} |
82 |
|
83 |
public String message(String s) { |
84 |
StringBuilder builder = new StringBuilder(); |
85 |
builder.append("<html>\n");
|
86 |
builder.append("<body >\n");
|
87 |
builder.append(StringUtils.replace(s, "\n", "<br>\n")); |
88 |
builder.append("</body>\n");
|
89 |
builder.append("</html>\n");
|
90 |
return builder.toString();
|
91 |
} |
92 |
} |