Skip to content

Commit

Permalink
Fix for #16
Browse files Browse the repository at this point in the history
  • Loading branch information
ssvaidyanathan committed Jun 19, 2020
1 parent c15ac7a commit d8a7a24
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 49 deletions.
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@
</licenses>

<developers>
<developer>
<name>WILLIAM O'CONNOR</name>
<email>[email protected]</email>
<organization>Achieve Internet</organization>
<organizationUrl>http://www.apigee.com</organizationUrl>
</developer>
<developer>
<name>Sai Saran Vaidyanathan</name>
<email>[email protected]</email>
Expand All @@ -44,6 +38,8 @@
</scm>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.20.RELEASE</spring.version>
<log4j.version>1.2.17</log4j.version>
Expand Down Expand Up @@ -182,6 +178,11 @@
<artifactId>commons-text</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.4</version>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 8 additions & 0 deletions samples/Drupal8/DevPortal/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"field_business_unit": "Petstore",
"field_drop_down": "one",
"field_multi_value": [
"value1",
"value2"
]
}
2 changes: 1 addition & 1 deletion samples/Drupal8/DevPortal/shared-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<plugin>
<groupId>com.apigee.smartdocs.config</groupId>
<artifactId>apigee-smartdocs-maven-plugin</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
<executions>
<execution>
<id>smartdocs-deploy</id>
Expand Down
17 changes: 17 additions & 0 deletions samples/Drupal8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ mvn install -Pdev -Dapigee.smartdocs.config.options=create
- This sample is for **Drupal 8 version of Developer portal**. The version of the plugin used in the pom **should be 2.x**
- If you are using Drupal 7 version of Developer portal, please follow the instructions [here](https://github.com/apigee/apigee-smartdocs-maven-plugin/tree/master/samples/Drupal7)
- To utilize this example, you will need a working developer portal instance with the [API Docs](https://www.drupal.org/docs/8/modules/apigee-api-catalog/expose-rest-apis-to-interact-with-api-docs#s-prerequisites) installed and enabled. That module will expose endpoints for use by the SmartDocs Maven Plugin.
- For JSON API 1.x module, use version 2.1 (JSON API 1.x will be deprecated)
- For JSON API 2.x module, use version 2.2 or later


## DevPortal

Expand Down Expand Up @@ -56,6 +59,20 @@ To run, jump to the sample project `cd /samples/DevPortal` and run

`mvn install -Pdev -Dapigee.smartdocs.config.options=create`

If you want to configure and manage fields, you can create a json file and pass that as `apigee.smartdocs.config.file` argument. A simple example
```
{
"field_foo": "bar",
"field_list:[
"item1",
"item2"
]
}
```
NOTE: The fields should be pre-configured in the portal.
`mvn install -Pdev -Dapigee.smartdocs.config.options=create -Dapigee.smartdocs.config.file=./config.json`


### Troubleshooting

#### 404 Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public void init() throws MojoExecutionException, MojoFailureException {
throw new MojoExecutionException(
"Developer portal URL not found in profile");
}
if (serverProfile.getPortalURL() != null && serverProfile.getPortalURL().endsWith("/")) {
throw new MojoExecutionException(
"Please provide the url of the developer portal without the trailing \"/\"");
}
if (serverProfile.getPortalUserName() == null) {
throw new MojoExecutionException(
"Developer portal username not found in profile");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public abstract class GatewayAbstractMojo extends AbstractMojo {
private String options;

/**
* Config dir
* Config File
*
* @parameter property="apigee.smartdocs.config.dir"
* @parameter property="apigee.smartdocs.config.file"
*/
private String configDir;
private String configFile;

/**
* Portal User Name
Expand Down Expand Up @@ -180,6 +180,7 @@ public GatewayAbstractMojo() {
public ServerProfile getProfile() {
this.buildProfile = new ServerProfile();
this.buildProfile.setOptions(this.options);
this.buildProfile.setConfigFile(this.configFile);
this.buildProfile.setPortalUserName(this.portalUserName);
this.buildProfile.setPortalPassword(this.portalPassword);
this.buildProfile.setPortalDirectory(this.portalDirectory);
Expand Down Expand Up @@ -221,11 +222,19 @@ public void setBuildOption(String buildOption) {
public String getOptions() {
return options;
}

public void setOptions(String options) {
this.options = options;
}

public void setConfigFile(String configFile) {
this.configFile = configFile;
}

public String getConfigFile() {
return configFile;
}

/**
* @return the id
*/
Expand Down
93 changes: 56 additions & 37 deletions src/main/java/com/apigee/smartdocs/config/rest/PortalRestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.yaml.snakeyaml.Yaml;

import com.apigee.smartdocs.config.utils.ServerProfile;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
Expand Down Expand Up @@ -244,21 +245,21 @@ public static class Data {

public static class Attributes {
public boolean status;
public String name;
public Description description;
public String spec_file_source;
public String title;
public Body body;
public String field_apidoc_spec_file_source;
public Object relationships;
public FileLink file_link;
public Object links;
}

public static class Description{
public static class Body{
public String value;
public String format;
}

public static class Relationships{
public Relationships_Spec spec;
public Relationships_Spec field_apidoc_spec;
}

public static class Relationships_Data{
Expand Down Expand Up @@ -568,38 +569,56 @@ private static ByteArrayContent getAPIModelContent(ServerProfile profile,
* Helper function to build the body for API Doc creations and updates.
*/
private static ByteArrayContent getAPIDocContent(ServerProfile profile, SpecObject spec, String uuid) throws IOException {
APIDocObject doc = new APIDocObject();
Data data = new Data();
Attributes attributes = new Attributes();
Description description = new Description();
Relationships relationships = new Relationships();
Relationships_Data relationships_data = new Relationships_Data();
Relationships_Spec relationships_spec = new Relationships_Spec();
data.type = "apidoc--apidoc";

attributes.status = true;
attributes.name = spec.getTitle();
Gson gson = new Gson();
JsonObject body = new JsonObject();
if (spec.getDescription() != null) {
description.value = StringEscapeUtils.escapeJava(spec.getDescription());
body.addProperty("value", StringEscapeUtils.escapeJava(spec.getDescription()));
}
description.format = profile.getPortalAPIDocFormat();
attributes.description = description;
attributes.spec_file_source = "file";
data.attributes = attributes;

relationships_data.id = uuid;
relationships_data.type = "file--file";
relationships_spec.data = relationships_data;
relationships.spec = relationships_spec;
data.relationships = relationships;
body.addProperty("format", profile.getPortalAPIDocFormat());

JsonObject attributes = new JsonObject();
attributes.addProperty("status", true);
attributes.addProperty("title", spec.getTitle());
attributes.add("body", body);
attributes.addProperty("field_apidoc_spec_file_source", "file");

doc.data = data;
//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) {
for (String key : result.keySet()) {
if(result.get(key) instanceof List){
attributes.add(key, gson.toJsonTree(result.get(key)));
}else
attributes.addProperty(key, (String)result.get(key));
}
}
}
JsonObject field_apidoc_spec_data = new JsonObject();
field_apidoc_spec_data.addProperty("type", "file--file");
field_apidoc_spec_data.addProperty("id", uuid);

JsonObject field_apidoc_spec = new JsonObject();
field_apidoc_spec.add("data", field_apidoc_spec_data);

JsonObject relationships = new JsonObject();
relationships.add("field_apidoc_spec", field_apidoc_spec);

JsonObject data = new JsonObject();
data.addProperty("type", "node--apidoc");
data.add("attributes", attributes);
data.add("relationships", relationships);

JsonObject payloadData = new JsonObject();
payloadData.add("data", data);

String payload = gson.toJson(payloadData);

Gson gson = new Gson();
String payload = gson.toJson(doc);
logger.debug("Request payload: \n" + payload);
ByteArrayContent content = new ByteArrayContent("application/vnd.api+json", payload.getBytes());
return content;
return content;
}

/**
Expand Down Expand Up @@ -672,7 +691,7 @@ public static void postAPIModel(ServerProfile profile, File file) throws IOExcep
throw e;
}
}

/**
* Posts the OpenAPI Spec to a APIDoc in Developer Portal.
*/
Expand Down Expand Up @@ -704,7 +723,7 @@ public static APIDocResponseObject getAPIDoc(ServerProfile profile, File file) t
SpecObject spec = parseSpec(profile, file);
logger.info("Getting API doc for "+ spec.getTitle());
HttpRequest restRequest = REQUEST_FACTORY
.buildGetRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/apidoc/apidoc?filter[name]=" + spec.getTitle()));
.buildGetRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/node/apidoc?filter[title]=" + spec.getTitle()));
HttpHeaders headers = restRequest.getHeaders();
headers.setAccept("application/vnd.api+json");
headers.setBasicAuthentication(profile.getPortalUserName(), profile.getPortalPassword());
Expand Down Expand Up @@ -739,7 +758,7 @@ public static APIDocObject importAPIDoc(ServerProfile profile, File file) throws
byte[] fileBytes = Files.readAllBytes(file.toPath());
ByteArrayContent fileContent = new ByteArrayContent("application/octet-stream", fileBytes);
HttpRequest restRequest = REQUEST_FACTORY
.buildPostRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/apidoc/apidoc/spec"), fileContent);
.buildPostRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/node/apidoc/field_apidoc_spec"), fileContent);
HttpHeaders headers = restRequest.getHeaders();
headers.setAccept("application/vnd.api+json");
headers.set("Content-Disposition", "file; filename=\""+spec.getTitle()+"."+profile.getPortalFormat()+"\"");
Expand Down Expand Up @@ -772,7 +791,7 @@ public static void updateAPIDoc(ServerProfile profile, File file, APIDocResponse
byte[] fileBytes = Files.readAllBytes(file.toPath());
ByteArrayContent fileContent = new ByteArrayContent("application/octet-stream", fileBytes);
HttpRequest restRequest = REQUEST_FACTORY
.buildPostRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/apidoc/apidoc/"+doc.data.get(0).id+"/spec"), fileContent);
.buildPostRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/node/apidoc/"+doc.data.get(0).id+"/field_apidoc_spec"), fileContent);
HttpHeaders headers = restRequest.getHeaders();
headers.setAccept("application/vnd.api+json");
headers.set("Content-Disposition", "file; filename=\""+spec.getTitle()+"."+profile.getPortalFormat()+"\"");
Expand All @@ -796,7 +815,7 @@ public static String createAPIDoc(ServerProfile profile, File file, APIDocObject
logger.info("Creating spec.." + doc.data.id);
ByteArrayContent content = getAPIDocContent(profile, spec, doc.data.id);
HttpRequest restRequest = REQUEST_FACTORY
.buildPostRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/apidoc/apidoc"), content);
.buildPostRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/node/apidoc"), content);
HttpHeaders headers = restRequest.getHeaders();
headers.setAccept("application/vnd.api+json");
headers.setBasicAuthentication(profile.getPortalUserName(), profile.getPortalPassword());
Expand Down Expand Up @@ -826,7 +845,7 @@ public static void deleteAPIDoc(ServerProfile profile, File file) throws IOExcep
if (respObj != null) {
logger.info("Delete API Doc..");
HttpRequest restRequest = REQUEST_FACTORY
.buildDeleteRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/apidoc/apidoc/"+respObj.data.get(0).id));
.buildDeleteRequest(new GenericUrl(profile.getPortalURL() + "/jsonapi/node/apidoc/"+respObj.data.get(0).id));
HttpHeaders headers = restRequest.getHeaders();
headers.setAccept("application/vnd.api+json");
headers.setBasicAuthentication(profile.getPortalUserName(), profile.getPortalPassword());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
public class ServerProfile {

private String options;

private String configFile;

// Portal Parameters
private String portalUserName; // Developer Portal Username
private String portalPassword; // Developer Portal Password
Expand Down Expand Up @@ -161,6 +162,17 @@ public void setPortalModelFields(Map<String, PortalField> portalModelFields) {
this.portalModelFields = portalModelFields;
}

/**
* @param configFile the options to set
*/
public String getConfigFile() {
return configFile;
}

public void setConfigFile(String configFile) {
this.configFile = configFile;
}

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

0 comments on commit d8a7a24

Please sign in to comment.