Plugin Development - groupon/odo GitHub Wiki
An override is simply a static function with an annotation specifying the type of override it is. Currently available override types are:
- @RequestOverride (com.groupon.odo.plugin.RequestOverride) - Not currently defined - Only custom overrides are supported so far (query replacement)
- @ResponseOverride (com.groupon.odo.plugin.v2.ResponseOverride) - Overrides response data from an HTTP request. This v2 implementation allows direct access to the response for editing, as well as context by providing the original request information.
- @ResponseOverride (com.groupon.odo.plugin.ResponseOverride) - (Deprecated) Overrides response data from an HTTP request. Only passes string data to the function.
-
Create a new maven project
-
Add the following dependency (you will have it if you compiled everything in the TestProxy directory with
mvn clean install
):<dependency> <groupId>com.groupon.odo</groupId> <artifactId>proxyplugin</artifactId> <version>1.0.0-beta.5</version> </dependency>
-
Your plugin project can depend on any other maven jars that you need. Building a jar with all dependencies is described later on.
-
Add an entry to the maven-assembly-plugin configuration section of pom.xml as follows. Replace com.groupon.odo.sample with the appropriate package.
<archive> <manifestEntries> <Plugin-Package>com.groupon.odo.sample</Plugin-Package> </manifestEntries> </archive>
-
Add a new class to your project to hold your override methods. Generally you will want to group methods into classes by functional area
-
Import the appropriate override type (ex: import com.groupon.odo.plugin.v2.ResponseOverride)
-
Add a static method that returns a string with a single String parameter and the following annotation. Name your function something that is meaningful:
@ResponseOverride(description="Replace a with b") public static void replaceLetter(PluginArguments args) throws Exception { HttpServletResponse response = args.getResponse(); String responseContent = PluginHelper.readResponseContent(response); responseContent.replace('a', 'b'); PluginHelper.writeResponseContent(response, responseContent); }
-
Notice that the annotation allows for plugin method description, parameter names, and option to block the request. These should be set as necessary.
- description - A text description of the plugin method. This is displayed in the Odo UI.
- parameters - Plugin methods with parameters should set this string array value in order to see the parameter names in the Odo UI.
- blockRequest - A path with an override containing blockRequest=true enabled will not send the original request to the server. An example usage is when you are simulating http error codes such as 404. Without blockRequest enabled, the response will return as expected with a 404 response code but the request will have been successfully processed by the server.
- description - A text description of the plugin method. This is displayed in the Odo UI.
- parameters - Plugin methods with parameters should set this string array value in order to see the parameter names in the Odo UI.
- httpCode - The HTTP response code to set on the response.
- description - A text description of the plugin method. This is displayed in the Odo UI.
An override method can take multiple arguments. Currently support argument types are:
- Integer
- String
- Boolean
An example method is:
@ResponseOverride(description="Replace string with integer",
parameters={"letter", "number", "replace"})
public static void replaceLetter(PluginArguments args,
String letter,
Integer number,
Boolean replace) throws Exception {
HttpServletResponse response = args.getResponse();
String responseContent = PluginHelper.readResponseContent(response);
if (replace) {
responseContent = responseContent.replace(letter, number.toString());
}
PluginHelper.writeResponseContent(response, responseContent);
}
To configure the arguments through the Proxy UI you simply select the enabled override in the enabled overrides list for a path. A UI will appear allowing you to set parameters.
- Add build plugins to your maven project
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifestEntries>
<Plugin-Package>com.groupon.odo.sample</Plugin-Package>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- Build project:
mvn clean compile
- Build single assembly:
mvn assembly:single
- Grab assembly and put it in your plugin path directory:
cp target/plugins-jar-with-dependencies.jar destination_path