DropboxAuthServlet.java
2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* Copyright (c) 2006-2019, JGraph Ltd
*/
package com.mxgraph.online;
import java.io.IOException;
@SuppressWarnings("serial")
public class DropboxAuthServlet extends AbsAuthServlet
{
public static String CLIENT_SECRET_FILE_PATH = "dropbox_client_secret";
public static String CLIENT_ID_FILE_PATH = "dropbox_client_id";
private static Config CONFIG = null;
protected Config getConfig()
{
if (CONFIG == null)
{
String clientSerets, clientIds;
try
{
clientSerets = Utils
.readInputStream(getServletContext()
.getResourceAsStream(getSecretPath()))
.replaceAll("\n", "");
}
catch (IOException e)
{
throw new RuntimeException("Client secrets path invalid");
}
try
{
clientIds = Utils
.readInputStream(getServletContext()
.getResourceAsStream(getIdPath()))
.replaceAll("\n", "");
}
catch (IOException e)
{
throw new RuntimeException("Client IDs path invalid");
}
CONFIG = new Config(clientIds, clientSerets);
CONFIG.AUTH_SERVICE_URL = "https://api.dropboxapi.com/oauth2/token";
CONFIG.REDIRECT_PATH = "/dropbox";
}
return CONFIG;
}
protected String getSecretPath()
{
return AbsAuthServlet.SECRETS_DIR_PATH + CLIENT_SECRET_FILE_PATH;
}
protected String getIdPath()
{
return AbsAuthServlet.SECRETS_DIR_PATH + CLIENT_ID_FILE_PATH;
}
public DropboxAuthServlet()
{
super();
cookiePath = "/dropbox";
withRedirectUrlInRefresh = false;
}
protected String processAuthResponse(String authRes, boolean jsonResponse)
{
StringBuffer res = new StringBuffer();
if (!jsonResponse)
{
res.append("<!DOCTYPE html><html><head><script type=\"text/javascript\">");
res.append("(function() { var authInfo = "); //The following is a json containing access_token
}
res.append(authRes);
if (!jsonResponse)
{
res.append(";");
res.append("if (window.opener != null && window.opener.onDropboxCallback != null)");
res.append("{");
res.append(" window.opener.onDropboxCallback(authInfo, window);");
res.append("} else {");
res.append(" onDropboxCallback(authInfo);");
res.append("}");
res.append("})();</script>");
res.append("</head><body></body></html>");
}
return res.toString();
}
}