use of org.json.simple.JSONObject in project jaggery by wso2.
the class WebAppManager method exec.
private static void exec(HttpServletRequest request, HttpServletResponse response) throws IOException {
InputStream sourceIn;
Context cx;
JaggeryContext context;
RhinoEngine engine = null;
ServletContext servletContext = request.getServletContext();
try {
engine = CommonManager.getInstance().getEngine();
cx = engine.enterContext();
String scriptPath = getScriptPath(request);
OutputStream out = response.getOutputStream();
context = createJaggeryContext(cx, out, scriptPath, request, response);
context.addProperty(FileHostObject.JAVASCRIPT_FILE_MANAGER, new WebAppFileManager(request.getServletContext()));
if (ModuleManager.isModuleRefreshEnabled()) {
//reload init scripts
refreshServletContext(servletContext);
InputStream jaggeryConf = servletContext.getResourceAsStream(JaggeryCoreConstants.JAGGERY_CONF_FILE);
if (jaggeryConf != null) {
StringWriter writer = new StringWriter();
IOUtils.copy(jaggeryConf, writer, null);
String jsonString = writer.toString();
JSONObject conf = (JSONObject) JSONValue.parse(jsonString);
JSONArray initScripts = (JSONArray) conf.get(JaggeryCoreConstants.JaggeryConfigParams.INIT_SCRIPTS);
if (initScripts != null) {
JaggeryContext sharedContext = WebAppManager.sharedJaggeryContext(servletContext);
ScriptableObject sharedScope = sharedContext.getScope();
Object[] scripts = initScripts.toArray();
for (Object script : scripts) {
if (!(script instanceof String)) {
log.error("Invalid value for initScripts in jaggery.conf : " + script);
continue;
}
String path = (String) script;
path = path.startsWith("/") ? path : "/" + path;
Stack<String> callstack = CommonManager.getCallstack(sharedContext);
callstack.push(path);
engine.exec(new ScriptReader(servletContext.getResourceAsStream(path)) {
@Override
protected void build() throws IOException {
try {
sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
} catch (ScriptException e) {
throw new IOException(e);
}
}
}, sharedScope, null);
callstack.pop();
}
}
}
}
Object serveFunction = request.getServletContext().getAttribute(SERVE_FUNCTION_JAGGERY);
if (serveFunction != null) {
Function function = (Function) serveFunction;
ScriptableObject scope = context.getScope();
function.call(cx, scope, scope, new Object[] { scope.get("request", scope), scope.get("response", scope), scope.get("session", scope) });
} else {
//resource rendering model proceeding
sourceIn = request.getServletContext().getResourceAsStream(scriptPath);
if (sourceIn == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, request.getRequestURI());
return;
}
CommonManager.getInstance().getEngine().exec(new ScriptReader(sourceIn), context.getScope(), getScriptCachingContext(request, scriptPath));
}
} catch (ScriptException e) {
String msg = e.getMessage();
log.error(msg, e);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
} catch (Error e) {
//Rhino doesn't catch Error instances and it is been used to stop the script execution
//from any specific place. Hence, Error exception propagates up to Java and we silently
//ignore it, assuming it was initiated by an exit() method call.
log.debug("Script has called exit() method", e);
} finally {
// Exiting from the context
if (engine != null) {
RhinoEngine.exitContext();
}
}
}
use of org.json.simple.JSONObject in project opennms by OpenNMS.
the class MattermostNotificationStrategyTestServlet method squawk.
@SuppressWarnings("unchecked")
private void squawk(final HttpServletResponse resp, String reason) throws IOException {
JSONObject errorJson = new JSONObject();
errorJson.put("message", reason);
errorJson.put("detailed_error", "");
errorJson.put("request_id", "deadbeefcafebabe");
errorJson.put("status_code", 500);
errorJson.put("isoauth", false);
final String responseText = errorJson.toJSONString();
final ServletOutputStream os = resp.getOutputStream();
os.print(responseText);
os.close();
resp.setContentType("application/json");
resp.setContentLength(responseText.length());
resp.setStatus(500);
}
use of org.json.simple.JSONObject in project tdi-studio-se by Talend.
the class OAuthClient method refreshToken.
public Token refreshToken(String refreshToken) throws Exception {
Token newToken = new Token();
newToken.setRefresh_token(refreshToken);
// get token using refresh token
String token_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_TOKEN : baseOAuthURL + "/" + OAUTH2_TOKEN;
URLConnection conn = new URL(token_url).openConnection();
// post
conn.setDoOutput(true);
String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&refresh_token=%s&format=%s", "refresh_token", clientID, clientSecret, refreshToken, "json");
OutputStream output = null;
try {
output = conn.getOutputStream();
output.write(query.getBytes(UTF8));
} finally {
if (output != null) {
try {
output.close();
} catch (IOException ignore) {
}
}
}
InputStream input = conn.getInputStream();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(input));
JSONParser jsonParser = new JSONParser();
JSONObject json = (JSONObject) jsonParser.parse(reader);
if (json.get("error") == null) {
if (json.get("access_token") != null) {
newToken.setAccess_token(json.get("access_token").toString());
}
if (json.get("instance_url") != null) {
newToken.setInstance_url(json.get("instance_url").toString());
}
if (json.get("id") != null) {
newToken.setId(json.get("id").toString());
}
if (json.get("issued_at") != null) {
newToken.setIssued_at(Long.parseLong(json.get("issued_at").toString()));
}
if (json.get("signature") != null) {
newToken.setSignature(json.get("signature").toString());
}
} else {
throw new Exception(json.toString());
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignore) {
}
}
}
return newToken;
}
use of org.json.simple.JSONObject in project tdi-studio-se by Talend.
the class OAuthClient method getToken.
public Token getToken() throws Exception {
Token token = new Token();
URL callback_url = new URL("https", callbackHost, callbackPort, "");
String oauth2_authorize_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_AUTHORIZE : baseOAuthURL + "/" + OAUTH2_AUTHORIZE;
String authorize_url = // &scope=%s
String.format(// &scope=%s
"%s?response_type=%s&client_id=%s&redirect_uri=%s", // &scope=%s
oauth2_authorize_url, "code", clientID, // , "full%20refresh_token"
URLEncoder.encode(callback_url.toString(), UTF8));
System.out.println("Paste this URL into a web browser to authorize Salesforce Access:");
System.out.println(authorize_url);
// start a service to get Authorization code
HttpsService service = new HttpsService(callbackPort);
String code = null;
while (service.getCode() == null) {
Thread.sleep(2 * 1000);
}
code = service.getCode();
// stop service
service.stop();
// get token using Authorization code
String token_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_TOKEN : baseOAuthURL + "/" + OAUTH2_TOKEN;
URLConnection conn = new URL(token_url).openConnection();
// post
conn.setDoOutput(true);
String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&redirect_uri=%s&code=%s", "authorization_code", clientID, clientSecret, URLEncoder.encode(callback_url.toString(), UTF8), code);
OutputStream output = null;
try {
output = conn.getOutputStream();
output.write(query.getBytes(UTF8));
} finally {
if (output != null) {
try {
output.close();
} catch (IOException ignore) {
}
}
}
InputStream input = conn.getInputStream();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(input));
JSONParser jsonParser = new JSONParser();
JSONObject json = (JSONObject) jsonParser.parse(reader);
if (json.get("access_token") != null) {
token.setAccess_token(json.get("access_token").toString());
}
if (json.get("refresh_token") != null) {
token.setRefresh_token(json.get("refresh_token").toString());
}
if (json.get("instance_url") != null) {
token.setInstance_url(json.get("instance_url").toString());
}
if (json.get("id") != null) {
token.setId(json.get("id").toString());
}
if (json.get("issued_at") != null) {
token.setIssued_at(Long.parseLong(json.get("issued_at").toString()));
}
if (json.get("signature") != null) {
token.setSignature(json.get("signature").toString());
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignore) {
}
}
}
return token;
}
use of org.json.simple.JSONObject in project tdi-studio-se by Talend.
the class OAuthClient method getTokenForWizard.
public Token getTokenForWizard(String code) throws Exception {
Token token = new Token();
URL callback_url = new URL("https", callbackHost, callbackPort, "");
String oauth2_authorize_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_AUTHORIZE : baseOAuthURL + "/" + OAUTH2_AUTHORIZE;
String authorize_url = // &scope=%s
String.format(// &scope=%s
"%s?response_type=%s&client_id=%s&redirect_uri=%s", // &scope=%s
oauth2_authorize_url, "code", clientID, // , "full%20refresh_token"
URLEncoder.encode(callback_url.toString(), UTF8));
String token_url = baseOAuthURL.endsWith("/") ? baseOAuthURL + OAUTH2_TOKEN : baseOAuthURL + "/" + OAUTH2_TOKEN;
URLConnection conn = new URL(token_url).openConnection();
// post
conn.setDoOutput(true);
String query = String.format("grant_type=%s&client_id=%s&client_secret=%s&redirect_uri=%s&code=%s", "authorization_code", clientID, clientSecret, URLEncoder.encode(callback_url.toString(), UTF8), code);
OutputStream output = null;
try {
output = conn.getOutputStream();
output.write(query.getBytes(UTF8));
} finally {
if (output != null) {
try {
output.close();
} catch (IOException ignore) {
}
}
}
InputStream input = conn.getInputStream();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(input));
JSONParser jsonParser = new JSONParser();
JSONObject json = (JSONObject) jsonParser.parse(reader);
if (json.get("access_token") != null) {
token.setAccess_token(json.get("access_token").toString());
}
if (json.get("refresh_token") != null) {
token.setRefresh_token(json.get("refresh_token").toString());
}
if (json.get("instance_url") != null) {
token.setInstance_url(json.get("instance_url").toString());
}
if (json.get("id") != null) {
token.setId(json.get("id").toString());
}
if (json.get("issued_at") != null) {
token.setIssued_at(Long.parseLong(json.get("issued_at").toString()));
}
if (json.get("signature") != null) {
token.setSignature(json.get("signature").toString());
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignore) {
}
}
}
return token;
}
Aggregations