From ae3019cb446e628f4b6da8da63a8d746e03b9d6d Mon Sep 17 00:00:00 2001 From: Ethan Kromhout Date: Fri, 29 Mar 2019 12:42:20 -0400 Subject: [PATCH] Handle AlreadyExistsException correctly --- connector-rest-wordpress/README | 10 +- .../samples/connector-rest-wordpress.xml | 175 +++++++++++++----- .../wordpress/WordpressRestConnector.java | 134 +++++++++----- 3 files changed, 222 insertions(+), 97 deletions(-) diff --git a/connector-rest-wordpress/README b/connector-rest-wordpress/README index d13a0fc..62d9be8 100644 --- a/connector-rest-wordpress/README +++ b/connector-rest-wordpress/README @@ -2,14 +2,14 @@ # Supports create, update, schema, test # search and delete operations for users. -# search has not yet been tested, and -# several create and update error -# handling are a work in progress +# Search has not been tested, and +# some create and update error +# handlings are a work in progress # Project is currently appropriate for # POC and testing, but not production -# use. It assumes Wordpress to use the +# use. It assumes Wordpress has the # JWT authentication plugin for the # REST API https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api -# That plugin requires that the REST API +# Note that plugin requires that the REST API # is at /wp-json diff --git a/connector-rest-wordpress/samples/connector-rest-wordpress.xml b/connector-rest-wordpress/samples/connector-rest-wordpress.xml index 606bd97..5e83b15 100644 --- a/connector-rest-wordpress/samples/connector-rest-wordpress.xml +++ b/connector-rest-wordpress/samples/connector-rest-wordpress.xml @@ -1,12 +1,12 @@ - - wordpress15 + + wordpress - 2019-03-19T13:02:44.112Z + 2019-03-29T16:22:22.388Z http://midpoint.evolveum.com/xml/ns/public/gui/channels-3#user - - 2019-03-19T13:02:35.004Z + + 2019-03-19T13:02:41.338Z modify @@ -15,16 +15,16 @@ com.evolveum.midpoint.model.impl.lens.ChangeExecutor.executeDelta success - 1000000000000000168 + 1000000000000000273 - wordpress15 + wordpress success http://midpoint.evolveum.com/xml/ns/public/gui/channels-3#user - - 2019-03-19T13:02:39.309Z + + 2019-03-19T13:02:44.233Z modify @@ -33,16 +33,16 @@ com.evolveum.midpoint.model.impl.lens.ChangeExecutor.executeDelta success - 1000000000000000232 + 1000000000000000314 - wordpress15 + wordpress success http://midpoint.evolveum.com/xml/ns/public/gui/channels-3#user - - 2019-03-19T13:02:41.338Z + + 2019-03-28T13:53:37.825Z modify @@ -51,16 +51,16 @@ com.evolveum.midpoint.model.impl.lens.ChangeExecutor.executeDelta success - 1000000000000000273 + 1000000000000002054 - wordpress15 + wordpress success http://midpoint.evolveum.com/xml/ns/public/gui/channels-3#user - - 2019-03-19T13:02:44.233Z + + 2019-03-29T14:12:12.351Z modify @@ -69,9 +69,27 @@ com.evolveum.midpoint.model.impl.lens.ChangeExecutor.executeDelta success - 1000000000000000314 + 1000000000000002055 + + wordpress + + success + + http://midpoint.evolveum.com/xml/ns/public/gui/channels-3#user + + + 2019-03-29T16:22:22.515Z + + + modify + c:ResourceType + + + com.evolveum.midpoint.model.impl.lens.ChangeExecutor.executeDelta + success + 1000000000000002018 - wordpress15 + wordpress success @@ -80,32 +98,31 @@ up - - - - c:connectorType - edu.unc.polygon.connector.wordpress.WordpressRestConnector - - - - + + + + c:connectorType + edu.unc.polygon.connector.wordpress.WordpressRestConnector + + + - - 54y6RxN7GfC7aes3 - true - id - http://wordpress_server/wp-json - password - username - admin - 1 - true + + 54y6RxN7GfC7aes3 + true + id + http://wordpress_server/wp-json + password + username + admin + 1 + true - 2019-03-19T13:02:35.235Z - db5b306ee09fcbbd-63313e1ea25f76e9 + 2019-03-29T16:22:22.861Z + 318be2268b91039f-c8d8c12096d6f0bf @@ -216,18 +233,29 @@ account default true - ri:MESSAGE_OBJECT_CLASS___ACCOUNT__ + MESSAGE_OBJECT_CLASS___ACCOUNT__ ri:username + + 0 + + mr:stringIgnoreCase + strong $user/name + + + $user/name + + ri:email + strong $user/emailAddress @@ -240,6 +268,11 @@ $user/fullName + + + $user/fullName + + ri:first_name @@ -259,11 +292,21 @@ icfs:name + + 0 + + mr:stringIgnoreCase + strong $user/name + + + $user/name + + @@ -274,8 +317,8 @@ - 2019-03-19T13:02:35.229Z - 2859b764880a952d-7fb0628b3129443b + 2019-03-29T16:22:22.854Z + 5b1c4a484a81371-d0dffd979b34680e @@ -321,6 +364,50 @@ - + + + Account sync policy + MESSAGE_OBJECT_CLASS___ACCOUNT__ + account + default + c:UserType + true + + + polyStringNorm + c:name + + Matches using Name + declare namespace ri="http://midpoint.evolveum.com/xml/ns/public/resource/instance-3"; $account/attributes/icfs:name + + + + + linked + true + + + deleted + true + + http://midpoint.evolveum.com/xml/ns/public/model/action-3#deleteFocus + + + + unlinked + true + + http://midpoint.evolveum.com/xml/ns/public/model/action-3#link + + + + unmatched + true + + http://midpoint.evolveum.com/xml/ns/public/model/action-3#addFocus + + + + diff --git a/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java b/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java index af30e53..0313341 100644 --- a/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java +++ b/connector-rest-wordpress/src/main/java/edu/unc/polygon/connector/wordpress/WordpressRestConnector.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2016 Evolveum * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -214,36 +214,12 @@ public FilterTranslator createFilterTranslator(ObjectClass oc, LOG.info("inside createFilterTranslator"); return new WordpressFilterTranslator(); } - - - @Override - public void executeQuery(ObjectClass oc, WordpressFilter filter, ResultsHandler handler, OperationOptions oo) { - //Integer searchId = (Integer) uid; - LOG.info(">>> executeQuery "); - if ( oc.is(ObjectClass.ACCOUNT_NAME)) { - JSONObject uidSearchResponseJson = new JSONObject(); - JSONObject jsonObject = new JSONObject(); - HttpEntityEnclosingRequestBase request; - if (filter != null && filter.byUid != null) { - Integer searchId = new Integer(filter.byUid); - request = new HttpGetWithBody(configuration.getWordpressRestUrl() + "/wp/v2/users/" + searchId); - } - else { - LOG.error("No attribute for search passed for an ObjectClass.ACCOUNT_NAME query"); - //this really shouldn't be possible - Integer searchId = new Integer(filter.byUid); - request = new HttpGetWithBody(configuration.getWordpressRestUrl() + "/wp/v2/users/" + searchId); - } - try { - uidSearchResponseJson = callRequest(request, jsonObject); - FileWriter file8 = new FileWriter("/tmp/file8.txt"); - file8.write(uidSearchResponseJson.toString()); - System.out.println("Successfully Copied JSON Object to File..."); - System.out.println("\nJSON Object: " + uidSearchResponseJson); - file8.flush(); - file8.close(); + + private void handleUserId ( ResultsHandler handler, JSONObject uidSearchResponseJson ) { + LOG.info("inside handleUserId " + String.valueOf(uidSearchResponseJson.getInt(ATTR_ID))); ConnectorObjectBuilder builder = new ConnectorObjectBuilder(); builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(ATTR_ID)))); + //builder.setUid(new Uid(String.valueOf(id))); String [] potentialAttributes = { ATTR_NAME, ATTR_MAIL, ATTR_FIRST_NAME, ATTR_LAST_NAME, ATTR_FULL_NAME}; for ( String potentialAttribute: potentialAttributes ) { LOG.info(">>> checking for attribute " + potentialAttribute ); @@ -259,13 +235,76 @@ public void executeQuery(ObjectClass oc, WordpressFilter filter, ResultsHandler } ConnectorObject connectorObject = builder.build(); handler.handle(connectorObject); - } catch (IOException IOE) { - throw new ConnectorIOException(IOE.getMessage(), IOE); + } + + @Override + public void executeQuery(ObjectClass oc, WordpressFilter filter, ResultsHandler handler, OperationOptions oo) { + //Integer searchId = (Integer) uid; + LOG.info(">>> executeQuery "); + if ( oc.is(ObjectClass.ACCOUNT_NAME)) { + JSONObject uidSearchResponseJson = new JSONObject(); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("context","edit"); + HttpEntityEnclosingRequestBase request; + if (filter != null && filter.byUid != null) { + Integer searchId = new Integer(filter.byUid); + request = new HttpGetWithBody(configuration.getWordpressRestUrl() + "/wp/v2/users/" + searchId); + try { + uidSearchResponseJson = callRequest(request, jsonObject); + FileWriter file8 = new FileWriter("/tmp/file8.txt"); + file8.write(uidSearchResponseJson.toString()); + System.out.println("Successfully Copied JSON Object to File..."); + System.out.println("\nJSON Object: " + uidSearchResponseJson); + file8.flush(); + file8.close(); + //builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(ATTR_ID)))); + handleUserId( handler, uidSearchResponseJson ); + } catch (IOException IOE) { + throw new ConnectorIOException(IOE.getMessage(), IOE); + } + } + else if (filter != null && filter.byName != null) { + String searchName = filter.byName; + JSONObject nameSearchJson = new JSONObject(); + nameSearchJson.put("search",searchName); + nameSearchJson.put("context","edit"); + request = new HttpGetWithBody(configuration.getWordpressRestUrl() + "/wp/v2/users/"); + try { + JSONArray nameSearchResponseJson = callRequestArray(request, nameSearchJson); + //builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(ATTR_ID)))); + for ( int i = 0; i < nameSearchResponseJson.length(); i++ ) { + JSONObject userJson = nameSearchResponseJson.getJSONObject(i); + if ( userJson.get("username").equals(searchName) ) { + handleUserId( handler, userJson ); + } + } + } catch (IOException IOE) { + throw new ConnectorIOException(IOE.getMessage(), IOE); + } + } + else if (filter != null && filter.byEmailAddress != null) { + String searchEmail = filter.byEmailAddress; + JSONObject emailSearchJson = new JSONObject(); + emailSearchJson.put("search",searchEmail); + emailSearchJson.put("context","edit"); + request = new HttpGetWithBody(configuration.getWordpressRestUrl() + "/wp/v2/users/"); + try { + JSONArray emailSearchResponseJson = callRequestArray(request, emailSearchJson); + for ( int i = 0; i < emailSearchResponseJson.length(); i++ ) { + JSONObject userJson = emailSearchResponseJson.getJSONObject(i); + if ( userJson.get("email").equals(searchEmail) ) { + handleUserId( handler, userJson ); + } + } + } catch (IOException IOE) { + throw new ConnectorIOException(IOE.getMessage(), IOE); + } } } else { JSONArray searchResponseJson = new JSONArray(); JSONObject jsonObject = new JSONObject(); + jsonObject.put("context","edit"); try { HttpEntityEnclosingRequestBase request; request = new HttpGetWithBody(configuration.getWordpressRestUrl() + "/wp/v2/users"); @@ -279,23 +318,7 @@ public void executeQuery(ObjectClass oc, WordpressFilter filter, ResultsHandler for ( int i = 0; i < searchResponseJson.length(); i++ ) { JSONObject uidSearchResponseJson = searchResponseJson.getJSONObject(i); LOG.info(">>> processing ID " + i); - ConnectorObjectBuilder builder = new ConnectorObjectBuilder(); - builder.setUid(new Uid(String.valueOf(uidSearchResponseJson.getInt(ATTR_ID)))); - String [] potentialAttributes = { ATTR_NAME, ATTR_MAIL, ATTR_FIRST_NAME, ATTR_LAST_NAME, ATTR_FULL_NAME}; - for ( String potentialAttribute: potentialAttributes ) { - LOG.info(">>> checking for attribute " + potentialAttribute ); - if ( uidSearchResponseJson.has(potentialAttribute) && uidSearchResponseJson.get(potentialAttribute) != null && !JSONObject.NULL.equals(uidSearchResponseJson.get(potentialAttribute)) ) { - LOG.info(">>> found attribute " + potentialAttribute ); - if ( potentialAttribute.equals(ATTR_NAME) ) { - builder.setName(uidSearchResponseJson.getString(potentialAttribute)); - } - else { - addAttr(builder, potentialAttribute, uidSearchResponseJson.getString(potentialAttribute)); - } - } - } - ConnectorObject connectorObject = builder.build(); - handler.handle(connectorObject); + handleUserId( handler, uidSearchResponseJson ); } } catch (IOException IOE) { throw new ConnectorIOException(IOE.getMessage(), IOE); @@ -303,6 +326,8 @@ public void executeQuery(ObjectClass oc, WordpressFilter filter, ResultsHandler } } + + @NotThreadSafe class HttpGetWithBody extends HttpEntityEnclosingRequestBase { public static final String METHOD_NAME = "GET"; @@ -605,6 +630,19 @@ protected JSONObject callRequest(HttpEntityEnclosingRequestBase request, JSONObj System.out.println("\nJSON Object: " + responseText); file.flush(); file.close(); + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode == 500) { + JSONObject errorResult = new JSONObject(responseText); + //JSONObject errorData = errorResult.getJSONObject("data"); + if (errorResult.getString("code").equals("existing_user_login")) { + closeResponse(response); + throw new AlreadyExistsException(jo.getString(ATTR_NAME)); + } + if (errorResult.getString("code").equals("existing_user_email")) { + closeResponse(response); + throw new AlreadyExistsException(jo.getString(ATTR_MAIL)); + } + } processResponseErrors(response); JSONObject result = new JSONObject (responseText); LOG.info("response body: ");