From 50c46ab0ff03557c7d78daa85995b10d0bc0127e Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Tue, 26 Mar 2019 23:25:59 +0100 Subject: [PATCH] Fix logging code --- .../grouper/rest/AccountProcessor.java | 2 +- .../grouper/rest/GrouperConfiguration.java | 2 +- .../grouper/rest/GrouperConnector.java | 2 +- .../connector/grouper/rest/Processor.java | 98 ++++--------------- .../grouper/test/GrouperTestHelper.java | 2 - 5 files changed, 21 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/evolveum/polygon/connector/grouper/rest/AccountProcessor.java b/src/main/java/com/evolveum/polygon/connector/grouper/rest/AccountProcessor.java index 670b9e9..4c6254f 100644 --- a/src/main/java/com/evolveum/polygon/connector/grouper/rest/AccountProcessor.java +++ b/src/main/java/com/evolveum/polygon/connector/grouper/rest/AccountProcessor.java @@ -117,7 +117,7 @@ private List selectSubjectIds(JSONArray subjects) { rv.add(processor.getString(subjObject, "id")); } } else { - LOG.warn("Skipping not-success subject from response: {}", subject); + LOG.warn("Skipping not-success subject from response: {0}", subject); } } else { throw new IllegalStateException("Expected subject as JSONObject, got " + subject); diff --git a/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConfiguration.java b/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConfiguration.java index 040ffa1..8caf6d9 100644 --- a/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConfiguration.java +++ b/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConfiguration.java @@ -160,7 +160,7 @@ public void validate() { } else { return; } - LOG.error(exceptionMsg); + LOG.error("{0}", exceptionMsg); throw new ConfigurationException(exceptionMsg); } diff --git a/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConnector.java b/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConnector.java index 8ca1194..f77c00a 100644 --- a/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConnector.java +++ b/src/main/java/com/evolveum/polygon/connector/grouper/rest/GrouperConnector.java @@ -113,7 +113,7 @@ public void executeQuery(ObjectClass objClass, Filter filter, ResultsHandler han LOG.error("Get operation failed: Attribute Object Class is not provided."); throw new InvalidAttributeValueException("Attribute Object Class is not provided."); } else - LOG.info("ObjectClasss: {0}", objClass.toString()); + LOG.info("ObjectClass: {0}", objClass.toString()); if (handler == null) { LOG.error("Get operation failed: Attribute Result Handler is not provided."); diff --git a/src/main/java/com/evolveum/polygon/connector/grouper/rest/Processor.java b/src/main/java/com/evolveum/polygon/connector/grouper/rest/Processor.java index a5f8fa4..ed64b8a 100644 --- a/src/main/java/com/evolveum/polygon/connector/grouper/rest/Processor.java +++ b/src/main/java/com/evolveum/polygon/connector/grouper/rest/Processor.java @@ -92,8 +92,8 @@ JSONObject callRequest(HttpEntityEnclosingRequestBase request, JSONObject jo, Bo try { entity = new ByteArrayEntity(jo.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException e1) { - String exceptionMsg = "Creating request entity failed: problem occured during entity encoding."; - LOG.error(exceptionMsg); + String exceptionMsg = "Creating request entity failed: problem occurred during entity encoding."; + LOG.error("{0}", exceptionMsg); throw new ConnectorIOException(exceptionMsg); } request.setEntity(entity); @@ -118,7 +118,7 @@ JSONObject callRequest(HttpEntityEnclosingRequestBase request, JSONObject jo, Bo StringBuilder exceptionMsg = new StringBuilder(); exceptionMsg.append("Request failed: problem occured during execute request with uri: ") .append(request.getURI()).append(": \n\t").append(e.getLocalizedMessage()); - LOG.error(exceptionMsg.toString()); + LOG.error("{0}", exceptionMsg.toString()); throw new ConnectorIOException(exceptionMsg.toString(), e); } } @@ -148,7 +148,7 @@ JSONObject callRequest(HttpRequestBase request, Boolean parseResult, String cont exceptionMsg.append("Request failed: problem occured during execute request with uri: ") .append(request.getURI()).append(": \n\t").append(e.getLocalizedMessage()); //closeResponse(response); - LOG.error(exceptionMsg.toString()); + LOG.error("{0}", exceptionMsg.toString()); throw new ConnectorIOException(exceptionMsg.toString(), e); } } @@ -196,7 +196,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) { StringBuilder exceptionMsg = new StringBuilder(); exceptionMsg.append("Execution of the request failed: problem occurred during HTTP client execution: \n\t") .append(e.getLocalizedMessage()); - LOG.error(exceptionMsg.toString(), e); + LOG.error("{0}", exceptionMsg.toString(), e); e.printStackTrace(); throw new ConnectorIOException(exceptionMsg.toString()); } @@ -216,7 +216,7 @@ void processResponseErrors(CloseableHttpResponse response) { try { responseBody = EntityUtils.toString(response.getEntity()); } catch (IOException e) { - LOG.warn("cannot read response body: " + e, e); + LOG.warn("cannot read response body: {0}", e, e); } StringBuilder message = new StringBuilder(); @@ -242,37 +242,37 @@ void processResponseErrors(CloseableHttpResponse response) { LOG.error("{0}", message.toString()); if ((statusCode == 400 || statusCode == 404) && message.toString().contains("already")) { closeResponse(response); - LOG.error(message.toString()); + LOG.error("{0}", message.toString()); throw new AlreadyExistsException(message.toString()); } if (statusCode == 400 || statusCode == 405 || statusCode == 406) { closeResponse(response); - LOG.error(message.toString()); + LOG.error("{0}", message.toString()); throw new ConnectorIOException(message.toString()); } if (statusCode == 402 || statusCode == 407) { closeResponse(response); - LOG.error(message.toString()); + LOG.error("{0}", message.toString()); throw new PermissionDeniedException(message.toString()); } if (statusCode == 404 || statusCode == 410) { closeResponse(response); - LOG.error(message.toString()); + LOG.error("{0}", message.toString()); throw new UnknownUidException(message.toString()); } if (statusCode == 408) { closeResponse(response); - LOG.error(message.toString()); + LOG.error("{0}", message.toString()); throw new OperationTimeoutException(message.toString()); } if (statusCode == 412) { closeResponse(response); - LOG.error(message.toString()); + LOG.error("{0}", message.toString()); throw new PreconditionFailedException(message.toString()); } if (statusCode == 418) { closeResponse(response); - LOG.error(message.toString()); + LOG.error("{0}", message.toString()); throw new UnsupportedOperationException("Sorry, no coffee: " + message.toString()); } @@ -288,7 +288,7 @@ void closeResponse(CloseableHttpResponse response) { try { response.close(); } catch (IOException e) { - LOG.warn(e, "Failed to close response: " + response); + LOG.warn("Failed to close response: {0}", response, e); } } @@ -321,7 +321,7 @@ void throwNullAttrException(Filter query) { exceptionMsg .append("Get operation failed: problem occurred because of not provided attribute of query filter: ") .append(query); - LOG.error(exceptionMsg.toString()); + LOG.error("{0}", exceptionMsg.toString()); throw new InvalidAttributeValueException(exceptionMsg.toString()); } @@ -377,13 +377,13 @@ private T getAttr(Set attributes, String attrName, Class type, StringBuilder exceptionMsg = new StringBuilder(); exceptionMsg.append("Unsupported type ").append(val.getClass()).append(" for attribute ") .append(attrName).append(", value: ").append(val); - LOG.error(exceptionMsg.toString()); + LOG.error("{0}", exceptionMsg.toString()); throw new InvalidAttributeValueException(exceptionMsg.toString()); } StringBuilder exceptionMsg = new StringBuilder(); exceptionMsg.append("More than one value for attribute ").append(attrName).append(", values: ") .append(vals); - LOG.error(exceptionMsg.toString()); + LOG.error("{0}", exceptionMsg.toString()); throw new InvalidAttributeValueException(exceptionMsg.toString()); } } @@ -408,68 +408,6 @@ void getIfExists(JSONObject jsonObj, String attr, ConnectorObjectBuilder builder } } - Uid getUidIfExists(JSONObject jsonObj, String attr, ConnectorObjectBuilder builder) { - if (jsonObj.has(attr) && jsonObj.get(attr) != null && !JSONObject.NULL.equals(jsonObj.get(attr))) { - Uid uid = new Uid(jsonObj.getString(attr)); - builder.setUid(uid); - return uid; - } else { - StringBuilder exceptionMsg = new StringBuilder(); - exceptionMsg.append("Primary identifier '").append(attr).append("' is missing."); - LOG.error(exceptionMsg.toString()); - throw new ConfigurationException(exceptionMsg.toString()); - } - } - - void getNameIfExists(JSONObject jsonObj, String attr, ConnectorObjectBuilder builder) { - if (jsonObj.has(attr) && jsonObj.get(attr) != null && !JSONObject.NULL.equals(jsonObj.get(attr))) { - builder.setName(new Name(jsonObj.getString(attr))); - } else { - StringBuilder exceptionMsg = new StringBuilder(); - exceptionMsg.append("Secondary identifier '").append(attr).append("' is missing."); - LOG.error(exceptionMsg.toString()); - throw new ConfigurationException(exceptionMsg.toString()); - } - } - - // processing of multivalue attribute containing JSONArray attribute - // (applicable for e.g. extended attributes of users/groups/projects) - // get string array of attributes 'name' from multivalue attribute 'item': - ArrayList getMultiAttrItems(JSONObject object, String attrName, String subAttrName) { - - JSONObject itemJSONObject = null; - JSONArray itemsJSONArray = (JSONArray) object.get(attrName); - int size = itemsJSONArray.length(); - if (size > 0) { - ArrayList itemsArray = new ArrayList(); - // String[] itemsArray = new String[size]; - // JSONArray itemsJSONArray = roles.keySet().toArray(new - // String[roles.keySet().size()]); - // LOG.info("\n\tItems Count: {0} of atribute {1}: ' {2} '", - // size, attrName, itemsJSONArray); - - for (int i = 0; i < itemsJSONArray.length(); i++) { - // LOG.ok("\n\tProcessing {0}/{1} {2}: {3}", i + 1, - // itemsJSONArray.length(), attrName, - // itemsJSONArray.getJSONObject(i).get(subAttrName)); - - itemJSONObject = itemsJSONArray.getJSONObject(i); - itemsArray.add(itemJSONObject.getString(subAttrName)); - // LOG.info("\n\tGroup {0}: {1}", i+1, itemsArray[i]); - } - return itemsArray; - } - - return null; - } - - void putFieldIfExists(Set attributes, String fieldName, JSONObject jo) { - String fieldValue = getStringAttr(attributes, fieldName); - if (fieldValue != null) { - jo.put(fieldName, fieldValue); - } - } - public void checkSuccess(JSONObject response, String rootName) { Object success = get(response, rootName, RESULT_METADATA, SUCCESS); if (!"T".equals(success)) { @@ -518,7 +456,7 @@ public ConnectorException processException(Exception e, URIBuilder uriBuilder, f StringBuilder exceptionMsg = new StringBuilder(); exceptionMsg.append(operationName).append(" failed: problem occurred during executing URI: ").append(uriBuilder) .append("\n\t").append(e.getLocalizedMessage()); - LOG.error(exceptionMsg.toString()); + LOG.error("{0}", exceptionMsg.toString()); return new ConnectorException(exceptionMsg.toString(), e); } diff --git a/src/test/java/com/evolveum/polygon/connector/grouper/test/GrouperTestHelper.java b/src/test/java/com/evolveum/polygon/connector/grouper/test/GrouperTestHelper.java index 45c3d82..c0df248 100644 --- a/src/test/java/com/evolveum/polygon/connector/grouper/test/GrouperTestHelper.java +++ b/src/test/java/com/evolveum/polygon/connector/grouper/test/GrouperTestHelper.java @@ -41,10 +41,8 @@ public class GrouperTestHelper { protected final GrouperConnector grouperConnector = new GrouperConnector(); protected final OperationOptions options = new OperationOptions(new HashMap()); - protected static final Log LOG = Log.getLog(GrouperConnector.class); protected final ObjectClass accountObjectClass = ObjectClass.ACCOUNT; - protected GuardedString password = new GuardedString("secret".toCharArray()); protected final ArrayList results = new ArrayList<>(); protected SearchResultsHandler handler = new SearchResultsHandler() { @Override