Skip to content

Commit

Permalink
Fix for apigee#24
Browse files Browse the repository at this point in the history
  • Loading branch information
nandoan committed Mar 24, 2021
1 parent c0f2e74 commit a1fba50
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public abstract class GatewayAbstractMojo extends AbstractMojo {
*/
private String configFile;

/**
* Config File
*
* @parameter property="portal.api.id.field"
*/
private String apiIdField;

/**
* Portal User Name
*
Expand Down Expand Up @@ -181,6 +188,7 @@ public ServerProfile getProfile() {
this.buildProfile = new ServerProfile();
this.buildProfile.setOptions(this.options);
this.buildProfile.setConfigFile(this.configFile);
this.buildProfile.setApiIdField(this.apiIdField);
this.buildProfile.setPortalUserName(this.portalUserName);
this.buildProfile.setPortalPassword(this.portalPassword);
this.buildProfile.setPortalDirectory(this.portalDirectory);
Expand Down
107 changes: 66 additions & 41 deletions src/main/java/com/apigee/smartdocs/config/rest/PortalRestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,44 +357,40 @@ private static ByteArrayContent getAPIDocContent(ServerProfile profile, SpecObje
JsonObject relationships = new JsonObject();

//config
if(profile.getConfigFile()!=null && !profile.getConfigFile().equalsIgnoreCase("")) {
FileContent tempFileContent = new FileContent("application/json", new File(profile.getConfigFile()).getAbsoluteFile());
Reader reader = new InputStreamReader(tempFileContent.getInputStream());
Map<String, Object> result = new ObjectMapper().readValue(reader, HashMap.class);
if(result!=null && result.size()>0 && result.get("fields")!=null) {
//For Custom Fields
Map<String, Object> fieldsMap = (Map<String, Object>) result.get("fields");
if(fieldsMap!=null && fieldsMap.size()>0) {
for (String key : fieldsMap.keySet()) {
if(fieldsMap.get(key) instanceof List){
attributes.add(key, gson.toJsonTree(fieldsMap.get(key)));
}else
attributes.addProperty(key, (String)fieldsMap.get(key));
}
Map<String, Object> result = loadApiConfigFile(profile);
if(result!=null && result.size()>0 && result.get("fields")!=null) {
//For Custom Fields
Map<String, Object> fieldsMap = (Map<String, Object>) result.get("fields");
if(fieldsMap!=null && fieldsMap.size()>0) {
for (String key : fieldsMap.keySet()) {
if(fieldsMap.get(key) instanceof List){
attributes.add(key, gson.toJsonTree(fieldsMap.get(key)));
}else
attributes.addProperty(key, (String)fieldsMap.get(key));
}
//For Custom taxonomy_terms
List<Map<String, Object>> taxonomyTerm = (List<Map<String, Object>>) result.get("taxonomy_terms");
if(taxonomyTerm!=null && taxonomyTerm.size()>0) {
for (Map<String, Object> map : taxonomyTerm) {
Map<String, List<String>> taxonomyTermsIdMap = getTaxonomyTermId(profile, map);
if(taxonomyTermsIdMap!=null && taxonomyTermsIdMap.size()>0) {
JsonArray taxonomyData = new JsonArray();
for (String key : taxonomyTermsIdMap.keySet()) {
List<String> list = taxonomyTermsIdMap.get(key);
if(list!=null && list.size()>0) {
for (String id : taxonomyTermsIdMap.get(key)) {
JsonObject taxonomyId = new JsonObject();
taxonomyId.addProperty("type", "taxonomy_term--"+(String)map.get("vocabulary"));
taxonomyId.addProperty("id", id);
taxonomyData.add(taxonomyId);
}
}
//For Custom taxonomy_terms
List<Map<String, Object>> taxonomyTerm = (List<Map<String, Object>>) result.get("taxonomy_terms");
if(taxonomyTerm!=null && taxonomyTerm.size()>0) {
for (Map<String, Object> map : taxonomyTerm) {
Map<String, List<String>> taxonomyTermsIdMap = getTaxonomyTermId(profile, map);
if(taxonomyTermsIdMap!=null && taxonomyTermsIdMap.size()>0) {
JsonArray taxonomyData = new JsonArray();
for (String key : taxonomyTermsIdMap.keySet()) {
List<String> list = taxonomyTermsIdMap.get(key);
if(list!=null && list.size()>0) {
for (String id : taxonomyTermsIdMap.get(key)) {
JsonObject taxonomyId = new JsonObject();
taxonomyId.addProperty("type", "taxonomy_term--"+(String)map.get("vocabulary"));
taxonomyId.addProperty("id", id);
taxonomyData.add(taxonomyId);
}
JsonObject taxonomyDataObj = new JsonObject();
taxonomyDataObj.add("data", taxonomyData);
relationships.add(key, taxonomyDataObj);
}
}
}
JsonObject taxonomyDataObj = new JsonObject();
taxonomyDataObj.add("data", taxonomyData);
relationships.add(key, taxonomyDataObj);
}
}
}
}
Expand Down Expand Up @@ -426,6 +422,16 @@ private static ByteArrayContent getAPIDocContent(ServerProfile profile, SpecObje
ByteArrayContent content = new ByteArrayContent("application/vnd.api+json", payload.getBytes());
return content;
}

public static Map<String, Object> loadApiConfigFile(ServerProfile profile) throws IOException {
Map<String, Object> result = null;
if(profile.getConfigFile()!=null && !profile.getConfigFile().equalsIgnoreCase("")) {
FileContent tempFileContent = new FileContent("application/json", new File(profile.getConfigFile()).getAbsoluteFile());
Reader reader = new InputStreamReader(tempFileContent.getInputStream());
result = new ObjectMapper().readValue(reader, HashMap.class);
}
return result;
}

/**
* Posts the OpenAPI Spec to a APIDoc in Developer Portal.
Expand Down Expand Up @@ -504,14 +510,33 @@ public static Map<String, List<String>> getTaxonomyTermId(ServerProfile profile,
public static APIDocResponseObject getAPIDoc(ServerProfile profile, File file) throws IOException {
HttpResponse response = null;
try {
SpecObject spec = parseSpec(profile, file);
logger.info("Getting API doc for "+ spec.getTitle());
String apiIdField = null;
String apiIdValue = null;
if (profile.getApiIdField() != null) {
apiIdField = profile.getApiIdField();
Map<String, Object> result = loadApiConfigFile(profile);
if(result!=null && result.size()>0 && result.get("fields")!=null) {
Map<String, Object> fieldsMap = (Map<String, Object>) result.get("fields");
if(fieldsMap!=null && fieldsMap.get(apiIdField) != null) {
apiIdValue = (String)fieldsMap.get(apiIdField);
}else {
throw new IllegalArgumentException("Identification field " + apiIdField + " not provided");
}
}
}else {
SpecObject spec = parseSpec(profile, file);
apiIdField = "title";
apiIdValue = spec.getTitle();
}


logger.info("Getting API doc for "+ apiIdValue);
HttpRequest restRequest = REQUEST_FACTORY
.buildGetRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/node/apidoc?filter[title]=" + spec.getTitle()));
.buildGetRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/node/apidoc?filter[" + apiIdField + "]=" + apiIdValue));
HttpHeaders headers = restRequest.getHeaders();
headers.setAccept("application/vnd.api+json");
headers.setBasicAuthentication(profile.getPortalUserName(), profile.getPortalPassword());
logger.info("Retrieving " + spec.getTitle() + " doc.");
logger.info("Retrieving " + apiIdValue + " doc.");
restRequest.setReadTimeout(0);
response = restRequest.execute();
Gson gson = new Gson();
Expand All @@ -521,7 +546,7 @@ public static APIDocResponseObject getAPIDoc(ServerProfile profile, File file) t
logger.info("API Doc uuid:" + model.data.get(0).id);
return model;
} else {
logger.info("API Doc: "+ spec.getTitle()+" does not exist");
logger.info("API Doc: "+ apiIdValue+" does not exist");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ServerProfile {

private String options;
private String configFile;

// Portal Parameters
private String portalUserName; // Developer Portal Username
private String portalPassword; // Developer Portal Password
Expand All @@ -33,6 +33,7 @@ public class ServerProfile {
private String portalModelVocabulary; // Model Vocabulary
private String portalCronKey; // Dev portal Cron Key
private String portalModelNameConfig; // OPTIONAL configuration for Model Name
private String apiIdField; // OPTIONAL configuration for the API Identification field
private String portalAPIDocFormat; // Dev Portal API Doc Format
private Map<String, PortalField> portalModelFields; // OpenAPI spec format

Expand Down Expand Up @@ -172,6 +173,17 @@ public String getConfigFile() {
public void setConfigFile(String configFile) {
this.configFile = configFile;
}

/**
* @param configuration for the API Identification field
*/
public String getApiIdField() {
return apiIdField;
}

public void setApiIdField(String apiIdField) {
this.apiIdField = apiIdField;
}

/**
* @param options the options to set
Expand Down

0 comments on commit a1fba50

Please sign in to comment.