src/org/ohmage/cache/KeycloakCache.java
private static Boolean validKeycloakServer(String cachedKey){
		// Null check.
		if(cachedKey == null) {
			throw new IllegalArgumentException("The cachedKey is null.");
		}
		
		String url = getRealmEndpoint();
		
		try {
		    URL myURL = new URL(url);
		    HttpURLConnection urlConnection = (HttpURLConnection) myURL.openConnection();
		    
			// If a non-200 response was returned, get the text from the 
			// response.
			if(urlConnection.getResponseCode() != 200) {
				// Get the error text.
				ByteArrayOutputStream errorByteStream = new ByteArrayOutputStream();
				InputStream errorStream = urlConnection.getErrorStream();
				byte[] chunk = new byte[4096];
				int amountRead;
				while((amountRead = errorStream.read(chunk)) != -1) {
					errorByteStream.write(chunk, 0, amountRead);
				}
				errorStream.close();
				LOGGER.warn("Error from keycloak server: "
						+ urlConnection.getResponseCode()
						+ ", with message: "
						+ errorByteStream.toString());
				return false;
			}
			else {
				// Build the response.
				InputStream reader = urlConnection.getInputStream();

				// Generate the byte array.
				ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
				byte[] chunk = new byte[4096];
				int amountRead = 0;
				while((amountRead = reader.read(chunk)) != -1) {
					byteArrayStream.write(chunk, 0, amountRead);
				}
				reader.close();
				
				Map<String,String> realmMap;
				try {
					ObjectMapper mapper = new ObjectMapper();
					TypeReference<Map<String, String>> ref = new TypeReference<Map<String, String>>() { };
					realmMap = mapper.readValue(byteArrayStream.toString(), ref);
				}
				catch (JsonParseException e){
					LOGGER.warn("Error while parsing json response from keycloak server.", e);
					return false;
				}
				
				if (cachedKey.equals(realmMap.get(KEY_KEYCLOAK_PUBLIC_KEY_FROM_SERVER))) {
					// server was successfully contacted,
					// public key matches our cached copy.
					// all is well.
					return true;
				} else {
					LOGGER.warn("Keycloak server broadcasting a public key "
							+ "which does not match our stored key. Stored Key: "
							+ cachedKey
							+ ", Keycloak Key: "
							+ realmMap.get(KEY_KEYCLOAK_PUBLIC_KEY_FROM_SERVER));
					return false;
				}
			}
		} 
		catch (MalformedURLException e) {
			LOGGER.warn("Built a Malformed URL:"
					+ url,
					e);
			return false;
		} 
		catch (IOException e) {   
			LOGGER.warn("Error communicating with keycloak server at:"
					+ url,
					e);
			return false;
		}
	}
/**
	 * Attempts to determine if keycloak is setup properly.
	 * 
	 * Note that method intentionally does not throw errors as
	 * the server will operate as normal, just without keycloak support.
	 * 
	 * @param String representation of existing cached keycloak public key.
	 * 
	 * @return A Boolean of true if setup is valid, false otherwise.
	 */
Attempts to determine if keycloak is setup properly.

Note that method intentionally does not throw errors as
the server will operate as normal, just without keycloak support.

@param String representation of existing cached keycloak public key.

@return A Boolean of true if setup is valid, false otherwise.
Attempts to determine if keycloak is setup properly . Note that method intentionally does not throw errors as the server will operate as normal just without keycloak support . @param String representation of existing cached keycloak public key . @return A Boolean of true if setup is valid false otherwise .
private static Boolean validKeycloakServer(String cachedKey){
		if(cachedKey == null) {
			throw new IllegalArgumentException("The cachedKey is null.");
		}
		String url = getRealmEndpoint();
		try {
		    URL myURL = new URL(url);
		    HttpURLConnection urlConnection = (HttpURLConnection) myURL.openConnection();
			if(urlConnection.getResponseCode() != 200) {
				ByteArrayOutputStream errorByteStream = new ByteArrayOutputStream();
				InputStream errorStream = urlConnection.getErrorStream();
				byte[] chunk = new byte[4096];
				int amountRead;
				while((amountRead = errorStream.read(chunk)) != -1) {
					errorByteStream.write(chunk, 0, amountRead);
				}
				errorStream.close();
				LOGGER.warn("Error from keycloak server: "
						+ urlConnection.getResponseCode()
						+ ", with message: "
						+ errorByteStream.toString());
				return false;
			}
			else {
				InputStream reader = urlConnection.getInputStream();
				ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
				byte[] chunk = new byte[4096];
				int amountRead = 0;
				while((amountRead = reader.read(chunk)) != -1) {
					byteArrayStream.write(chunk, 0, amountRead);
				}
				reader.close();
				Map<String,String> realmMap;
				try {
					ObjectMapper mapper = new ObjectMapper();
					TypeReference<Map<String, String>> ref = new TypeReference<Map<String, String>>() { };
					realmMap = mapper.readValue(byteArrayStream.toString(), ref);
				}
				catch (JsonParseException e){
					LOGGER.warn("Error while parsing json response from keycloak server.", e);
					return false;
				}
				if (cachedKey.equals(realmMap.get(KEY_KEYCLOAK_PUBLIC_KEY_FROM_SERVER))) {
					return true;
				} else {
					LOGGER.warn("Keycloak server broadcasting a public key "
							+ "which does not match our stored key. Stored Key: "
							+ cachedKey
							+ ", Keycloak Key: "
							+ realmMap.get(KEY_KEYCLOAK_PUBLIC_KEY_FROM_SERVER));
					return false;
				}
			}
		} 
		catch (MalformedURLException e) {
			LOGGER.warn("Built a Malformed URL:"
					+ url,
					e);
			return false;
		} 
		catch (IOException e) {   
			LOGGER.warn("Error communicating with keycloak server at:"
					+ url,
					e);
			return false;
		}
	}
private static Boolean validKeycloakServer ( String cachedKey ) { if ( cachedKey == null ) { throw new IllegalArgumentException ( "The cachedKey is null." ) ; } String url = getRealmEndpoint ( ) ; try { URL myURL = new URL ( url ) ; HttpURLConnection urlConnection = ( HttpURLConnection ) myURL . openConnection ( ) ; if ( urlConnection . getResponseCode ( ) != 200 ) { ByteArrayOutputStream errorByteStream = new ByteArrayOutputStream ( ) ; InputStream errorStream = urlConnection . getErrorStream ( ) ; byte [ ] chunk = new byte [ 4096 ] ; int amountRead ; while ( ( amountRead = errorStream . read ( chunk ) ) != - 1 ) { errorByteStream . write ( chunk , 0 , amountRead ) ; } errorStream . close ( ) ; LOGGER . warn ( "Error from keycloak server: " + urlConnection . getResponseCode ( ) + ", with message: " + errorByteStream . toString ( ) ) ; return false ; } else { InputStream reader = urlConnection . getInputStream ( ) ; ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream ( ) ; byte [ ] chunk = new byte [ 4096 ] ; int amountRead = 0 ; while ( ( amountRead = reader . read ( chunk ) ) != - 1 ) { byteArrayStream . write ( chunk , 0 , amountRead ) ; } reader . close ( ) ; Map < String , String > realmMap ; try { ObjectMapper mapper = new ObjectMapper ( ) ; TypeReference < Map < String , String > > ref = new TypeReference < Map < String , String > > ( ) { } ; realmMap = mapper . readValue ( byteArrayStream . toString ( ) , ref ) ; } catch ( JsonParseException e ) { LOGGER . warn ( "Error while parsing json response from keycloak server." , e ) ; return false ; } if ( cachedKey . equals ( realmMap . get ( KEY_KEYCLOAK_PUBLIC_KEY_FROM_SERVER ) ) ) { return true ; } else { LOGGER . warn ( "Keycloak server broadcasting a public key " + "which does not match our stored key. Stored Key: " + cachedKey + ", Keycloak Key: " + realmMap . get ( KEY_KEYCLOAK_PUBLIC_KEY_FROM_SERVER ) ) ; return false ; } } } catch ( MalformedURLException e ) { LOGGER . warn ( "Built a Malformed URL:" + url , e ) ; return false ; } catch ( IOException e ) { LOGGER . warn ( "Error communicating with keycloak server at:" + url , e ) ; return false ; } }
Attempts to determine if keycloak is setup properly.
Attempts to determine if keycloak is setup properly .
// Null check. // If a non-200 response was returned, get the text from the  // response. // Get the error text. // Build the response. // Generate the byte array. // server was successfully contacted, // public key matches our cached copy. // all is well.