Skip to content

Commit 8794b82

Browse files
thc202psiinon
authored andcommitted
Add build target to generate the APIs (beta) (#1885)
Add target generate-apis to build.xml file. Move ApiGenerator to api package to access core class. Change ApiGenerator to make use of the new core method (>=2.8.0) which allows to pass a ResourceBundle, to include the descriptions of the API endpoints being generated. Add dummy Messages.properties to be able to work with ZAP 2.7.0. For zaproxy/zaproxy#5044 - NodeJS API Upgrade
1 parent 0672968 commit 8794b82

File tree

3 files changed

+71
-16
lines changed

3 files changed

+71
-16
lines changed

build/build.xml

+11
Original file line numberDiff line numberDiff line change
@@ -799,4 +799,15 @@
799799
</copy>
800800
</target>
801801

802+
<target name="generate-apis" depends="compile" description="Generates the API clients.">
803+
<path id="classpath">
804+
<pathelement location="${build}" />
805+
<pathelement location="${src}" />
806+
<fileset dir="${dist.lib.dir}" includes="**/*.jar" />
807+
</path>
808+
<java classname="org.zaproxy.zap.extension.api.ApiGenerator" dir=".." fork="yes">
809+
<classpath refid="classpath" />
810+
</java>
811+
</target>
812+
802813
</project>

src/lang/Messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dummy file for org.zaproxy.zap.extension.api.ApiGenerator, required for ZAP <= 2.7.0.

src/org/zaproxy/zap/extension/ApiGenerator.java src/org/zaproxy/zap/extension/api/ApiGenerator.java

+59-16
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
* See the License for the specific language governing permissions and
1818
* limitations under the License.
1919
*/
20-
package org.zaproxy.zap.extension;
20+
package org.zaproxy.zap.extension.api;
2121

2222
import java.io.IOException;
23+
import java.io.UncheckedIOException;
2324
import java.util.ArrayList;
25+
import java.util.Arrays;
2426
import java.util.List;
27+
import java.util.Locale;
28+
import java.util.ResourceBundle;
2529

30+
import org.zaproxy.zap.extension.api.AbstractAPIGenerator;
2631
import org.zaproxy.zap.extension.api.ApiImplementor;
2732
import org.zaproxy.zap.extension.api.JavaAPIGenerator;
2833
import org.zaproxy.zap.extension.api.NodeJSAPIGenerator;
@@ -36,6 +41,8 @@ public class ApiGenerator {
3641

3742
private static final String JAVA_OUTPUT_DIR = "../zap-api-java/subprojects/zap-clientapi/src/main/java/org/zaproxy/clientapi/gen";
3843

44+
private static final String PHP_OUTPUT_DIR = "../zaproxy/php/api/zapv2/src/Zap";
45+
3946
private static final String PYTHON_OUTPUT_DIR = "../zap-api-python/src/zapv2/";
4047

4148
private static final String NODE_OUTPUT_DIR = "../zap-api-nodejs/src/";
@@ -63,26 +70,62 @@ public static List<ApiImplementor> getApiImplementors() {
6370
* @param args
6471
*/
6572
public static void main(String[] args) {
66-
try {
67-
JavaAPIGenerator japi = new JavaAPIGenerator(JAVA_OUTPUT_DIR, true);
68-
japi.generateAPIFiles(getApiImplementors());
73+
List<ApiGeneratorWrapper> generators = Arrays.asList(
74+
wrapper(JavaAPIGenerator.class, JAVA_OUTPUT_DIR),
75+
wrapper(NodeJSAPIGenerator.class, NODE_OUTPUT_DIR),
76+
wrapper(PhpAPIGenerator.class, PHP_OUTPUT_DIR),
77+
wrapper(PythonAPIGenerator.class, PYTHON_OUTPUT_DIR)
78+
// wrapper(WikiAPIGenerator.class, "../zaproxy-wiki")
79+
);
80+
getApiImplementors().forEach(api -> {
81+
ResourceBundle bundle = ResourceBundle.getBundle(
82+
api.getClass().getPackage().getName() + ".resources.Messages",
83+
Locale.ENGLISH,
84+
api.getClass().getClassLoader(),
85+
ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_PROPERTIES));
6986

70-
NodeJSAPIGenerator napi = new NodeJSAPIGenerator(NODE_OUTPUT_DIR, true);
71-
napi.generateAPIFiles(getApiImplementors());
72-
73-
PhpAPIGenerator phapi = new PhpAPIGenerator("../zaproxy/php/api/zapv2/src/Zap", true);
74-
phapi.generateAPIFiles(getApiImplementors());
87+
generators.forEach(generator -> generator.generate(api, bundle));
88+
});
89+
}
90+
91+
private static ApiGeneratorWrapper wrapper(Class<? extends AbstractAPIGenerator> clazz, String outputDir) {
92+
return new ApiGeneratorWrapper(clazz, outputDir);
93+
}
7594

76-
PythonAPIGenerator pyapi = new PythonAPIGenerator(PYTHON_OUTPUT_DIR, true);
77-
pyapi.generateAPIFiles(getApiImplementors());
95+
private static class ApiGeneratorWrapper {
7896

79-
//WikiAPIGenerator wapi = new WikiAPIGenerator("../zaproxy-wiki", true);
80-
//wapi.generateAPIFiles(getApiImplementors());
81-
82-
} catch (IOException e) {
83-
e.printStackTrace();
97+
private final Class<? extends AbstractAPIGenerator> clazz;
98+
private final String outputDir;
99+
100+
public ApiGeneratorWrapper(Class<? extends AbstractAPIGenerator> clazz, String outputDir) {
101+
this.clazz = clazz;
102+
this.outputDir = outputDir;
84103
}
85104

105+
public void generate(ApiImplementor api, ResourceBundle bundle) {
106+
AbstractAPIGenerator generator;
107+
try {
108+
generator = createInstance(bundle);
109+
} catch (Exception e) {
110+
throw new RuntimeException(e);
111+
}
112+
113+
try {
114+
generator.generateAPIFiles(Arrays.asList(api));
115+
} catch (IOException e) {
116+
throw new UncheckedIOException(e);
117+
}
118+
}
119+
120+
private AbstractAPIGenerator createInstance(ResourceBundle bundle) throws Exception {
121+
try {
122+
return clazz.getDeclaredConstructor(String.class, boolean.class, ResourceBundle.class)
123+
.newInstance(outputDir, true, bundle);
124+
} catch (NoSuchMethodException e) {
125+
System.out.println("Defaulting to generator without ResourceBundle, no descriptions will be included.");
126+
return clazz.getDeclaredConstructor(String.class, boolean.class).newInstance(outputDir, true);
127+
}
128+
}
86129
}
87130

88131
}

0 commit comments

Comments
 (0)