Skip to content

Commit

Permalink
feat: Add 'includeMethodParameter' driver config
Browse files Browse the repository at this point in the history
  • Loading branch information
peacekeeper committed Aug 4, 2024
1 parent fe2a6fb commit f585250
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
22 changes: 22 additions & 0 deletions driver-http/src/main/java/uniregistrar/driver/http/HttpDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ public class HttpDriver implements Driver {
public static final URI DEFAULT_UPDATE_URI = null;
public static final URI DEFAULT_DEACTIVATE_URI = null;
public static final URI DEFAULT_PROPERTIES_URI = null;
public static final Boolean DEFAULT_INCLUDE_METHOD_PARAMETER = false;

private HttpClient httpClient = DEFAULT_HTTP_CLIENT;
private URI createUri = DEFAULT_CREATE_URI;
private URI updateUri = DEFAULT_UPDATE_URI;
private URI deactivateUri = DEFAULT_DEACTIVATE_URI;
private URI propertiesUri = DEFAULT_PROPERTIES_URI;
private Boolean includeMethodParameter = DEFAULT_INCLUDE_METHOD_PARAMETER;

private String method;

private Consumer<Map<String, Object>> beforeWriteCreateConsumer;
private Consumer<Map<String, Object>> beforeReadCreateConsumer;
Expand All @@ -61,6 +65,7 @@ public CreateState create(CreateRequest createRequest) throws RegistrationExcept
// prepare HTTP request

String uriString = this.getCreateUri().toString();
if (Boolean.TRUE == this.getIncludeMethodParameter()) uriString += "?method=" + this.getMethod();

Map<String, Object> requestMap = HttpBindingUtil.toMapRequest(createRequest);
this.getBeforeWriteCreateConsumer().accept(requestMap);
Expand Down Expand Up @@ -332,6 +337,7 @@ private Map<String, Object> getHttpProperties() {
if (this.getUpdateUri() != null) httpProperties.put("updateUri", this.getUpdateUri().toString());
if (this.getDeactivateUri() != null) httpProperties.put("deactivateUri", this.getDeactivateUri().toString());
if (this.getPropertiesUri() != null) httpProperties.put("propertiesUri", this.getPropertiesUri().toString());
if (this.getIncludeMethodParameter() != null) httpProperties.put("propertiesUri", Boolean.toString(this.getIncludeMethodParameter()));
return httpProperties;
}

Expand Down Expand Up @@ -398,6 +404,14 @@ public static boolean isStateHttpContent(Map<String, Object> httpContentJson) {
* Getters and setters
*/

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public HttpClient getHttpClient() {
return this.httpClient;
}
Expand Down Expand Up @@ -455,6 +469,14 @@ public void setPropertiesUri(String propertiesUri) {
this.propertiesUri = URI.create(propertiesUri);
}

public Boolean getIncludeMethodParameter() {
return includeMethodParameter;
}

public void setIncludeMethodParameter(Boolean includeMethodParameter) {
this.includeMethodParameter = includeMethodParameter;
}

public Consumer<Map<String, Object>> getBeforeWriteCreateConsumer() {
return beforeWriteCreateConsumer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,27 @@ public static void configureLocalUniRegistrar(String filePath, LocalUniRegistrar
String method = jsonObjectDriver.has("method") ? jsonObjectDriver.get("method").getAsString() : null;
String url = jsonObjectDriver.has("url") ? jsonObjectDriver.get("url").getAsString() : null;
String propertiesEndpoint = jsonObjectDriver.has("propertiesEndpoint") ? jsonObjectDriver.get("propertiesEndpoint").getAsString() : null;
String includeMethodParameter = jsonObjectDriver.has("includeMethodParameter") ? jsonObjectDriver.get("includeMethodParameter").getAsString() : null;

if (method == null) throw new IllegalArgumentException("Missing 'method' entry in driver configuration.");
if (url == null) throw new IllegalArgumentException("Missing 'url' entry in driver configuration.");

// construct HTTP driver

HttpDriver driver = new HttpDriver();
driver.setMethod(method);

if (! url.endsWith("/")) url = url + "/";

driver.setCreateUri(url + "1.0/create");
driver.setUpdateUri(url + "1.0/update");
driver.setDeactivateUri(url + "1.0/deactivate");
if ("true".equals(propertiesEndpoint)) driver.setPropertiesUri(url + "1.0/properties");
if ("true".equals(includeMethodParameter)) driver.setIncludeMethodParameter(Boolean.valueOf(includeMethodParameter));

// done

drivers.put(method, driver);
if (log.isInfoEnabled()) log.info("Added driver for method '" + method + "' at " + driver.getCreateUri() + " and " + driver.getUpdateUri() + " and " + driver.getDeactivateUri() + " (" + driver.getPropertiesUri() + ")");
if (log.isInfoEnabled()) log.info("Added driver for method '" + method + "' at " + driver.getCreateUri() + " and " + driver.getUpdateUri() + " and " + driver.getDeactivateUri() + " (" + driver.getPropertiesUri() + ")" + " (" + driver.getIncludeMethodParameter() + ")");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static class DriverConfig {
private String method;
private String url;
private String propertiesEndpoint;
private String includeMethodParameter;

public String getMethod() {
return method;
Expand All @@ -50,12 +51,21 @@ public void setPropertiesEndpoint(String value) {
this.propertiesEndpoint = value;
}

public String getIncludeMethodParameter() {
return includeMethodParameter;
}

public void setIncludeMethodParameter(String includeMethodParameter) {
this.includeMethodParameter = includeMethodParameter;
}

@Override
public String toString() {
return new StringJoiner(", ", DriverConfig.class.getSimpleName() + "[", "]")
.add("method='" + method + "'")
.add("url='" + url + "'")
.add("propertiesEndpoint='" + propertiesEndpoint + "'")
.add("includeMethodParameter='" + includeMethodParameter + "'")
.toString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,27 @@ public void configureLocalUniRegistrar(DriverConfigs driverConfigs, LocalUniRegi
String method = dc.getMethod();
String url = dc.getUrl();
String propertiesEndpoint = dc.getPropertiesEndpoint();
String includeMethodParameter = dc.getIncludeMethodParameter();

if (method == null) throw new IllegalArgumentException("Missing 'method' entry in driver configuration.");
if (url == null) throw new IllegalArgumentException("Missing 'url' entry in driver configuration.");

// construct HTTP driver

HttpDriver driver = new HttpDriver();
driver.setMethod(method);

if (! url.endsWith("/")) url = url + "/";
driver.setCreateUri(normalizeUri((url + servletMappings.getCreate()), false));
driver.setUpdateUri(normalizeUri((url + servletMappings.getUpdate()), false));
driver.setDeactivateUri(normalizeUri((url + servletMappings.getDeactivate()), false));
if ("true".equals(propertiesEndpoint)) driver.setPropertiesUri(normalizeUri((url + servletMappings.getProperties()), false));
if ("true".equals(includeMethodParameter)) driver.setIncludeMethodParameter(Boolean.valueOf(includeMethodParameter));

// done

drivers.put(method, driver);
if (log.isInfoEnabled()) log.info("Added driver for method '" + method + "' at " + driver.getCreateUri() + " and " + driver.getUpdateUri() + " and " + driver.getDeactivateUri() + " (" + driver.getPropertiesUri() + ")");
if (log.isInfoEnabled()) log.info("Added driver for method '" + method + "' at " + driver.getCreateUri() + " and " + driver.getUpdateUri() + " and " + driver.getDeactivateUri() + " (" + driver.getPropertiesUri() + ")" + " (" + driver.getIncludeMethodParameter() + ")");
}

uniRegistrar.setDrivers(drivers);
Expand Down

0 comments on commit f585250

Please sign in to comment.