diff options
Diffstat (limited to 'eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils')
-rw-r--r-- | eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/SL20JSONExtractorUtils.java | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/SL20JSONExtractorUtils.java b/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/SL20JSONExtractorUtils.java index 827b5970..524c9e80 100644 --- a/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/SL20JSONExtractorUtils.java +++ b/eaaf_modules/eaaf_module_auth_sl20/src/main/java/at/gv/egiz/eaaf/modules/auth/sl20/utils/SL20JSONExtractorUtils.java @@ -1,6 +1,5 @@ package at.gv.egiz.eaaf.modules.auth.sl20.utils; -import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; @@ -13,6 +12,7 @@ import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; import org.jose4j.base64url.Base64Url; @@ -39,17 +39,17 @@ public class SL20JSONExtractorUtils { */ public static String getStringValue(JsonNode input, String keyID, boolean isRequired) throws SLCommandoParserException { try { - JsonNode internal = getAndCheck(input, keyID, isRequired); + final JsonNode internal = getAndCheck(input, keyID, isRequired); if (internal != null) return internal.asText(); else return null; - } catch (SLCommandoParserException e) { + } catch (final SLCommandoParserException e) { throw e; - } catch (Exception e) { + } catch (final Exception e) { throw new SLCommandoParserException("Can not extract String value with keyId: " + keyID, e); } @@ -66,17 +66,17 @@ public class SL20JSONExtractorUtils { */ public static boolean getBooleanValue(ObjectNode input, String keyID, boolean isRequired, boolean defaultValue) throws SLCommandoParserException { try { - JsonNode internal = getAndCheck(input, keyID, isRequired); + final JsonNode internal = getAndCheck(input, keyID, isRequired); if (internal != null) return internal.asBoolean(); else return defaultValue; - } catch (SLCommandoParserException e) { + } catch (final SLCommandoParserException e) { throw e; - } catch (Exception e) { + } catch (final Exception e) { throw new SLCommandoParserException("Can not extract Boolean value with keyId: " + keyID, e); } @@ -93,17 +93,17 @@ public class SL20JSONExtractorUtils { */ public static JsonNode getJSONObjectValue(JsonNode input, String keyID, boolean isRequired) throws SLCommandoParserException { try { - JsonNode internal = getAndCheck(input, keyID, isRequired); + final JsonNode internal = getAndCheck(input, keyID, isRequired); if (internal != null) return internal; else return null; - } catch (SLCommandoParserException e) { + } catch (final SLCommandoParserException e) { throw e; - } catch (Exception e) { + } catch (final Exception e) { throw new SLCommandoParserException("Can not extract Boolean value with keyId: " + keyID, e); } @@ -117,12 +117,12 @@ public class SL20JSONExtractorUtils { * @throws SLCommandoParserException */ public static List<String> getListOfStringElements(JsonNode input) throws SLCommandoParserException { - List<String> result = new ArrayList<String>(); + final List<String> result = new ArrayList<String>(); if (input != null) { if (input.isArray()) { - Iterator<JsonNode> arrayIterator = input.iterator(); + final Iterator<JsonNode> arrayIterator = input.iterator(); while(arrayIterator.hasNext()) { - JsonNode next = arrayIterator.next(); + final JsonNode next = arrayIterator.next(); if (next.isTextual()) result.add(next.asText()); } @@ -150,7 +150,7 @@ public class SL20JSONExtractorUtils { * @throws SLCommandoParserException */ public static Map<String, String> getMapOfStringElements(JsonNode input, String keyID, boolean isRequired) throws SLCommandoParserException { - JsonNode internal = getAndCheck(input, keyID, isRequired); + final JsonNode internal = getAndCheck(input, keyID, isRequired); return getMapOfStringElements(internal); } @@ -163,20 +163,20 @@ public class SL20JSONExtractorUtils { * @throws SLCommandoParserException */ public static Map<String, String> getMapOfStringElements(JsonNode input) throws SLCommandoParserException { - Map<String, String> result = new HashMap<String, String>(); + final Map<String, String> result = new HashMap<String, String>(); if (input != null) { if (input.isArray()) { - Iterator<JsonNode> arrayIterator = input.iterator(); + final Iterator<JsonNode> arrayIterator = input.iterator(); while(arrayIterator.hasNext()) { - JsonNode next = arrayIterator.next(); - Iterator<Entry<String, JsonNode>> entry = next.fields(); + final JsonNode next = arrayIterator.next(); + final Iterator<Entry<String, JsonNode>> entry = next.fields(); entitySetToMap(result, entry); } } else if (input.isObject()) { - Iterator<Entry<String, JsonNode>> objectKeys = input.fields(); + final Iterator<Entry<String, JsonNode>> objectKeys = input.fields(); entitySetToMap(result, objectKeys); } else @@ -189,7 +189,7 @@ public class SL20JSONExtractorUtils { private static void entitySetToMap(Map<String, String> result, Iterator<Entry<String, JsonNode>> entry) { while (entry.hasNext()) { - Entry<String, JsonNode> el = entry.next(); + final Entry<String, JsonNode> el = entry.next(); if (result.containsKey(el.getKey())) log.info("Attr. Map already contains Element with Key: " + el.getKey() + ". Overwrite element ... "); @@ -201,8 +201,8 @@ public class SL20JSONExtractorUtils { public static JsonNode extractSL20Result(JsonNode command, IJOSETools decrypter, boolean mustBeEncrypted) throws SL20Exception { - JsonNode result = command.get(SL20Constants.SL20_COMMAND_CONTAINER_RESULT); - JsonNode encryptedResult = command.get(SL20Constants.SL20_COMMAND_CONTAINER_ENCRYPTEDRESULT); + final JsonNode result = command.get(SL20Constants.SL20_COMMAND_CONTAINER_RESULT); + final JsonNode encryptedResult = command.get(SL20Constants.SL20_COMMAND_CONTAINER_ENCRYPTEDRESULT); if (result == null && encryptedResult == null) throw new SLCommandoParserException("NO result OR encryptedResult FOUND."); @@ -214,18 +214,18 @@ public class SL20JSONExtractorUtils { try { return decrypter.decryptPayload(encryptedResult.asText()); - } catch (Exception e) { + } catch (final Exception e) { log.info("Can NOT decrypt SL20 result. Reason:" + e.getMessage()); if (!mustBeEncrypted) { log.warn("Decrypted results are disabled by configuration. Parse result in plain if it is possible"); //dummy code try { - String[] signedPayload = encryptedResult.toString().split("\\."); - JsonNode payLoad = mapper.getMapper().readTree(new String(Base64.getUrlDecoder().decode(signedPayload[1]))); + final String[] signedPayload = encryptedResult.toString().split("\\."); + final JsonNode payLoad = mapper.getMapper().readTree(new String(Base64.getUrlDecoder().decode(signedPayload[1]))); return payLoad; - } catch (Exception e1) { + } catch (final Exception e1) { log.debug("DummyCode FAILED, Reason: " + e1.getMessage() + " Ignore it ..."); throw new SL20Exception(e.getMessage(), null, e); @@ -255,8 +255,8 @@ public class SL20JSONExtractorUtils { */ public static VerificationResult extractSL20PayLoad(JsonNode container, IJOSETools joseTools, boolean mustBeSigned) throws SL20Exception { - JsonNode sl20Payload = container.get(SL20Constants.SL20_PAYLOAD); - JsonNode sl20SignedPayload = container.get(SL20Constants.SL20_SIGNEDPAYLOAD); + final JsonNode sl20Payload = container.get(SL20Constants.SL20_PAYLOAD); + final JsonNode sl20SignedPayload = container.get(SL20Constants.SL20_SIGNEDPAYLOAD); if (mustBeSigned && joseTools == null) throw new SLCommandoParserException("'joseTools' MUST be set if 'mustBeSigned' is 'true'"); @@ -291,11 +291,11 @@ public class SL20JSONExtractorUtils { try { JsonNode sl20Resp = null; if (httpResp.getStatusLine().getStatusCode() == 307) { - Header[] locationHeader = httpResp.getHeaders("Location"); + final Header[] locationHeader = httpResp.getHeaders("Location"); if (locationHeader == null) throw new SLCommandoParserException("Find Redirect statuscode but not Location header"); - String sl20RespString = new URIBuilder(locationHeader[0].getValue()).getQueryParams().get(0).getValue(); + final String sl20RespString = new URIBuilder(locationHeader[0].getValue()).getQueryParams().get(0).getValue(); sl20Resp = mapper.getMapper().readTree(Base64Url.encode((sl20RespString.getBytes()))); } else if (httpResp.getStatusLine().getStatusCode() == 200) { @@ -317,7 +317,7 @@ public class SL20JSONExtractorUtils { log.info("Find JSON object in http response"); return sl20Resp; - } catch (Exception e) { + } catch (final Exception e) { throw new SLCommandoParserException("SL20 response parsing FAILED! Reason: " + e.getMessage(), e); } @@ -325,7 +325,8 @@ public class SL20JSONExtractorUtils { private static JsonNode parseSL20ResultFromResponse(HttpEntity resp) throws Exception { if (resp != null && resp.getContent() != null) { - JsonNode sl20Resp = mapper.getMapper().readTree(new InputStreamReader(resp.getContent())); + final String rawSL20Resp = EntityUtils.toString(resp); + final JsonNode sl20Resp = mapper.getMapper().readTree(rawSL20Resp); //TODO: check sl20Resp type like && sl20Resp.isJsonObject() if (sl20Resp != null) { @@ -342,7 +343,7 @@ public class SL20JSONExtractorUtils { private static JsonNode getAndCheck(JsonNode input, String keyID, boolean isRequired) throws SLCommandoParserException { - JsonNode internal = input.get(keyID); + final JsonNode internal = input.get(keyID); if (internal == null && isRequired) throw new SLCommandoParserException("REQUIRED Element with keyId: " + keyID + " does not exist"); |