private boolean isStreamingEnabled(HttpServletRequest pReq) { String streamingFromReq = pReq.getParameter(ConfigKey.STREAMING.getKeyValue()); if (streamingFromReq != null) { return Boolean.parseBoolean(streamingFromReq); } return streamingEnabled; }
private void addJolokiaId(Map<String, String> pFinalCfg) { if (!pFinalCfg.containsKey(ConfigKey.AGENT_ID.getKeyValue())) { pFinalCfg.put(ConfigKey.AGENT_ID.getKeyValue(), NetworkUtil.getAgentId(hashCode(),"jvm")); } pFinalCfg.put(ConfigKey.AGENT_TYPE.getKeyValue(), "jvm"); }
protected void prepareDetectorOptions(Map<String, String> pConfig) { StringBuffer detectorOpts = new StringBuffer("{"); if (pConfig.containsKey("bootAmx") && Boolean.parseBoolean(pConfig.get("bootAmx"))) { detectorOpts.append("\"glassfish\" : { \"bootAmx\" : true }"); } if (detectorOpts.length() > 1) { detectorOpts.append("}"); pConfig.put(ConfigKey.DETECTOR_OPTIONS.getKeyValue(),detectorOpts.toString()); } }
private void validateCallbackIfGiven(HttpServletRequest pReq) { String callback = pReq.getParameter(ConfigKey.CALLBACK.getKeyValue()); if (callback != null && !MimeTypeUtil.isValidCallback(callback)) { throw new IllegalArgumentException("Invalid callback name given, which must be a valid javascript function name"); } } private void sendStreamingResponse(HttpServletResponse pResp, String pCallback, JSONStreamAware pJson) throws IOException {
private boolean listenForDiscoveryMcRequests(Configuration pConfig) { // Check for system props, system env and agent config boolean sysProp = System.getProperty("jolokia." + ConfigKey.DISCOVERY_ENABLED.getKeyValue()) != null; boolean env = System.getenv("JOLOKIA_DISCOVERY") != null; boolean config = pConfig.getAsBoolean(ConfigKey.DISCOVERY_ENABLED); return sysProp || env || config; } /**
private Dictionary<String,String> getConfiguration() { Dictionary<String,String> config = new Hashtable<String,String>(); for (ConfigKey key : ConfigKey.values()) { String value = getConfiguration(key); if (value != null) { config.put(key.getKeyValue(),value); } } String jolokiaId = NetworkUtil.replaceExpression(config.get(ConfigKey.AGENT_ID.getKeyValue())); if (jolokiaId == null) { config.put(ConfigKey.AGENT_ID.getKeyValue(), NetworkUtil.getAgentId(hashCode(),"osgi")); } config.put(ConfigKey.AGENT_TYPE.getKeyValue(),"osgi"); return config; }
private String getConfigurationFromConfigAdmin(ConfigKey pkey) { ConfigurationAdmin configAdmin = (ConfigurationAdmin) configAdminTracker.getService(); if (configAdmin == null) { return null; } try { Configuration config = configAdmin.getConfiguration(CONFIG_ADMIN_PID); if (config == null) { return null; } Dictionary<?, ?> props = config.getProperties(); if (props == null) { return null; } return (String) props.get(CONFIG_PREFIX + "." + pkey.getKeyValue()); } catch (IOException e) { return null; } }
private String getConfiguration(ConfigKey pKey) { // TODO: Use fragments if available. String value = getConfigurationFromConfigAdmin(pKey); if (value == null) { value = bundleContext.getProperty(CONFIG_PREFIX + "." + pKey.getKeyValue()); } if (value == null) { value = pKey.getDefaultValue(); } return value; }
private String findAgentUrl(Configuration pConfig) { // System property has precedence String url = System.getProperty("jolokia." + ConfigKey.DISCOVERY_AGENT_URL.getKeyValue()); if (url == null) { url = System.getenv("JOLOKIA_DISCOVERY_AGENT_URL"); if (url == null) { url = pConfig.get(ConfigKey.DISCOVERY_AGENT_URL); } } return NetworkUtil.replaceExpression(url); }
private void validateCallbackIfGiven(ParsedUri pUri) { String callback = pUri.getParameter(ConfigKey.CALLBACK.getKeyValue()); if (callback != null && !MimeTypeUtil.isValidCallback(callback)) { throw new IllegalArgumentException("Invalid callback name given, which must be a valid javascript function name"); } }
/** * Update this global configuration from a string-string. Only the known keys are taken * from this map * * @param pConfig config map from where to take the configuration */ public void updateGlobalConfiguration(Map<String, String> pConfig) { for (ConfigKey c : ConfigKey.values()) { if (c.isGlobalConfig()) { String value = pConfig.get(c.getKeyValue()); if (value != null) { globalConfig.put(c,value); } } } }
private String getMimeType(ParsedUri pParsedUri) { return MimeTypeUtil.getResponseMimeType( pParsedUri.getParameter(ConfigKey.MIME_TYPE.getKeyValue()), configuration.get(ConfigKey.MIME_TYPE), pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue())); }
private JSONObject configToJSONObject() { JSONObject info = new JSONObject(); if (config != null) { for (ConfigKey key : ConfigKey.values()) { if (key.isGlobalConfig()) { String value = config.get(key); if (value != null) { info.put(key.getKeyValue(), value); } } } } return info; }
ConfigKey.DISPATCHER_CLASSES.getKeyValue(), (dispatchers != null ? dispatchers + "," : "") + jsr160DispatcherClass)); ConfigKey.DISPATCHER_CLASSES.getKeyValue(), Jsr160ProxyNotEnabledByDefaultAnymoreDispatcher.class.getCanonicalName()));
private void sendResponse(HttpServletResponse pResp, HttpServletRequest pReq, JSONAware pJson) throws IOException { String callback = pReq.getParameter(ConfigKey.CALLBACK.getKeyValue()); setContentType(pResp, MimeTypeUtil.getResponseMimeType( pReq.getParameter(ConfigKey.MIME_TYPE.getKeyValue()), configMimeType, callback)); pResp.setStatus(HttpServletResponse.SC_OK); setNoCacheHeaders(pResp); if (pJson == null) { pResp.setContentLength(-1); } else { if (isStreamingEnabled(pReq)) { sendStreamingResponse(pResp, callback, (JSONStreamAware) pJson); } else { // Fallback, send as one object // TODO: Remove for 2.0 where should support only streaming sendAllJSON(pResp, callback, pJson); } } }
Configuration initConfig(ServletConfig pConfig) { Configuration config = new Configuration( ConfigKey.AGENT_ID, NetworkUtil.getAgentId(hashCode(),"servlet")); // From ServletContext .... config.updateGlobalConfiguration(new ServletConfigFacade(pConfig)); // ... and ServletConfig config.updateGlobalConfiguration(new ServletContextFacade(getServletContext())); // Set type last and overwrite anything written config.updateGlobalConfiguration(Collections.singletonMap(ConfigKey.AGENT_TYPE.getKeyValue(),"servlet")); return config; }
private void sendAllJSON(HttpExchange pExchange, ParsedUri pParsedUri, JSONAware pJson) throws IOException { OutputStream out = null; try { Headers headers = pExchange.getResponseHeaders(); if (pJson != null) { headers.set("Content-Type", getMimeType(pParsedUri) + "; charset=utf-8"); String json = pJson.toJSONString(); String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue()); String content = callback != null && MimeTypeUtil.isValidCallback(callback) ? callback + "(" + json + ");" : json; byte[] response = content.getBytes("UTF8"); pExchange.sendResponseHeaders(200,response.length); out = pExchange.getResponseBody(); out.write(response); } else { headers.set("Content-Type", "text/plain"); pExchange.sendResponseHeaders(200,-1); } } finally { if (out != null) { // Always close in order to finish the request. // Otherwise the thread blocks. out.close(); } } }
private void sendStreamingResponse(HttpExchange pExchange, ParsedUri pParsedUri, JSONStreamAware pJson) throws IOException { Headers headers = pExchange.getResponseHeaders(); if (pJson != null) { headers.set("Content-Type", getMimeType(pParsedUri) + "; charset=utf-8"); pExchange.sendResponseHeaders(200, 0); Writer writer = new OutputStreamWriter(pExchange.getResponseBody(), "UTF-8"); String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue()); IoUtil.streamResponseAndClose(writer, pJson, callback != null && MimeTypeUtil.isValidCallback(callback) ? callback : null); } else { headers.set("Content-Type", "text/plain"); pExchange.sendResponseHeaders(200,-1); } }
private boolean isStreamingEnabled(HttpServletRequest pReq) { String streamingFromReq = pReq.getParameter(ConfigKey.STREAMING.getKeyValue()); if (streamingFromReq != null) { return Boolean.parseBoolean(streamingFromReq); } return streamingEnabled; }
private boolean isStreamingEnabled(HttpServletRequest pReq) { String streamingFromReq = pReq.getParameter(ConfigKey.STREAMING.getKeyValue()); if (streamingFromReq != null) { return Boolean.parseBoolean(streamingFromReq); } return streamingEnabled; }